!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r v4d_setobfr - Allocate WA files and VMM space for OBSERVATIONS and FORCINGS 
*
#include "model_macros_f.h"
*

      subroutine v4d_setobfr 1,1
*
#include "impnone.cdk"
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_31 - Tanguay M.        - adapt for tracers in tr3d 
* v3_01 - Morneau J.        - adapt for cost function on scalar grid
* v3_02 - Laroche S.        - add option V4dj_locob = 101
*
*object
*      1) Set WA files for OBSERVATIONS and FORCINGS 
*      2) Allocate VMM space for OBSERVATIONS locations
*      3) Initialize OBSERVATIONS locations 
*      4) Allocate VMM space for REFERENCE model var. needed for OBS. 
*      5) Allocate VMM space for CURRENT   model var. needed for FORC. 
*
*arguments
*     none
*
*implicits
#include "glb_ld.cdk"
#include "geomg.cdk"
#include "lun.cdk"
#include "v4dg.cdk"
#include "tr3d.cdk"
#include "v4dj.cdk"
#include "v4d_vmm.cdk"
*
*modules
      external fnom,vmmcre,vmmlod,vmmget,vmmuld
      integer  fnom,vmmcre,vmmlod,vmmget,vmmuld
*
      integer pnerr,pnlkey1(4),i,j,k,j0,jn,npole,npole_n,npole_s
      character*80 attrib_S
*     ______________________________________________________
*
      if ( V4dj_locob.ne.100.and.V4dj_locob.ne.101 ) call gefstop('v4d_setobfr')
*     ______________________________________________________
*
*C    -------------------------------------------
*C    Set WA files for OBSERVATIONS and FORCINGS
*C    -------------------------------------------
*
         if(Lun_out.gt.0) write(Lun_out,1000)
*
*        Initialize Units
*        ----------------
         V4dg_iunob = 0
         pnerr = fnom(V4dg_iunob,'OBSERVATIONS','RND',0)
*
         V4dg_iunfr = 0
         pnerr = fnom(V4dg_iunfr,'FORCINGS','RND',0)
*
*        Open WA files 
*        -------------
         call waopen(V4dg_iunob)
         call waopen(V4dg_iunfr)
*
*        Set starting addresses
*        ----------------------
         V4dg_addob = 1
         V4dg_addfr = 1
*
*C    ---------------------------------------------
*C    Allocate VMM space for OBSERVATIONS locations
*C    ---------------------------------------------
*
         if(Lun_out.gt.0) write(Lun_out,1001)
*
*        Names and keys commons initialization
*        -------------------------------------
         COMMON_INIT(V4d_cst_loc,-100)
*
*        Assign the names of the variables
*        ---------------------------------
         VMM_NAM(locu)= 'FLDLOCU'
         VMM_NAM(locv)= 'FLDLOCV'
         VMM_NAM(locg)= 'FLDLOCG' 
         VMM_NAM(locs)= 'FLDLOCS' 
*
*        Set the attributes of the variables
*        -----------------------------------
         attrib_S='SAVE=Y,CL=1,W=5,INIT=R,MUSTEXIST'
*
*        Create the virtual memory for the variables
*        -------------------------------------------
         VMM_CREATE(locu , LARRAY3D, 1, attrib_S)
         VMM_CREATE(locv , LARRAY3D, 1, attrib_S)
         VMM_CREATE(locg , LARRAY3D, 1, attrib_S)
         VMM_CREATE(locs , LARRAY2D, 1, attrib_S)
*
*C    -------------------------------------
*C    Initialize OBSERVATIONS locations
*C    according to V4d_locob and V4d_mask_L
*C    -------------------------------------
* 
*        Get fields in memory
*        --------------------
         pnlkey1(1) = VMM_KEY(locu)
         pnlkey1(2) = VMM_KEY(locv)
         pnlkey1(3) = VMM_KEY(locg)
         pnlkey1(4) = VMM_KEY(locs)
*
         pnerr = vmmlod(pnlkey1,4)
*
         pnerr = VMM_GET_VAR(locu) 
         pnerr = VMM_GET_VAR(locv)
         pnerr = VMM_GET_VAR(locg)
         pnerr = VMM_GET_VAR(locs)
*
*        Zero variables
*        --------------
         do k=1,l_nk
         do j=l_miny,l_maxy
         do i=l_minx,l_maxx
            locu(i,j,k) = 0. 
            locv(i,j,k) = 0. 
            locg(i,j,k) = 0. 
         end do
         end do
         end do
         do j=l_miny,l_maxy
         do i=l_minx,l_maxx
            locs(i,j) = 0. 
         end do
         end do
*
*        -------------
*        FULL LOCATION
*        -------------
         if( V4dj_locob.eq.100 ) then
*
*           Set each level for 3D variables
*           -------------------------------
            do k=1,l_nk
*
            do j = 1,l_nj
            do i = 1,l_ni
               locu(i,j,k) = 1.
               locv(i,j,k) = 1.
               locg(i,j,k) = 1.
            end do
            end do
*
            end do
*
*           Set each level for 2D variables
*           -------------------------------
            do j = 1,l_nj
            do i = 1,l_ni
               locs(i,j) = 1.
            end do
            end do
*
         endif
*
*
*        -----------------------------------------------
*        FULL LOCATION but excluding first npole circles
*        -----------------------------------------------
         if( V4dj_locob.eq.101 ) then

           npole   = 2

           npole_s = 0
           npole_n = 0
           if (l_south) npole_s = npole
           if (l_north) npole_n = npole
*
*           Set each level for 3D variables
*           -------------------------------
            do k=1,l_nk
*
            do j = 1+npole_n,l_nj-npole_s
            do i = 1,l_ni
               locu(i,j,k) = 1.
               locv(i,j,k) = 1.
               locg(i,j,k) = 1.
            end do
            end do
*
            end do
*
*           Set each level for 2D variables
*           -------------------------------
            do j = 1+npole_n,l_nj-npole_s
            do i = 1,l_ni
               locs(i,j) = 1.
            end do
            end do
*
         endif

         pnerr = vmmuld(-1,0)
*
*C    -------------------------------------------------------------------
*C    Allocate VMM space for REFERENCE model var. needed for OBSERVATIONS
*C    -------------------------------------------------------------------
* 
         if(Lun_out.gt.0) write(Lun_out,1002)
*
*        Names and keys commons initialization
*        -------------------------------------
         COMMON_INIT(V4d_cst_ref,-100)

*        Assign the names of the variables
*        ---------------------------------
         VMM_NAM(ut1r )= '4UUT1R' 
         VMM_NAM(vt1r )= '4VVT1R' 
         VMM_NAM(tpt1r)= '4TPLT1R' 
         VMM_NAM(st1r )= '4SST1R' 
*
*        Set the attributes of the variables
*        -----------------------------------
         attrib_S='SAVE=Y,CL=1,W=5,INIT=R,MUSTEXIST'
*
*        Create the virtual memory for the variables
*        -------------------------------------------
         VMM_CREATE(ut1r,  LARRAY3D, 1, attrib_S)
         VMM_CREATE(vt1r,  LARRAY3D, 1, attrib_S)
         VMM_CREATE(tpt1r, LARRAY3D, 1, attrib_S)
         VMM_CREATE(st1r,  LARRAY2D, 1, attrib_S)
*
*C    -------------------------------------------------------------
*C    Allocate VMM space for CURRENT model var. needed for FORCINGS  
*C    -------------------------------------------------------------
*
         if(Lun_out.gt.0) write(Lun_out,1003)
*
*        Names and keys commons initialization
*        -------------------------------------
         COMMON_INIT(V4d_cst_cur,-100)

*        Assign the names of the variables
*        ---------------------------------
         VMM_NAM(ut1c )= '4UUT1C'
         VMM_NAM(vt1c )= '4VVT1C'
         VMM_NAM(tpt1c)= '4TPLT1C'
         VMM_NAM(st1c )= '4SST1C'
*
*        Set the attributes of the variables
*        -----------------------------------
         attrib_S='SAVE=Y,CL=1,W=5,INIT=R,MUSTEXIST'
*
*        Create the virtual memory for the variables
*        -------------------------------------------
         VMM_CREATE(ut1c,  LARRAY3D, 1, attrib_S)
         VMM_CREATE(vt1c,  LARRAY3D, 1, attrib_S)
         VMM_CREATE(tpt1c, LARRAY3D, 1, attrib_S)
         VMM_CREATE(st1c,  LARRAY2D, 1, attrib_S)
*
 1000 format(
     +//,'OPEN WA FILES for OBSERVATIONS and FORCINGS (S/R V4D_SETVMM)',
     + /,'============================================================',
     +//)
*
 1001 format(
     +//,'ALLOCATE AND INITIALIZE VMM SPACE FOR OBS. LOCATIONS (S/R V4D_SETVMM)',
     + /,'=====================================================================',
     +//)
*
 1002 format(
     +//,'ALLOCATE VMM SPACE FOR REFERENCE VAR. NEEDED FOR OBS. (S/R V4D_SETVMM)',
     + /,'======================================================================',
     +//)
*
 1003 format(
     +//,'ALLOCATE VMM SPACE FOR CURRENT VAR. NEEDED FOR FORC. (S/R V4D_SETVMM)',
     + /,'=====================================================================',
     +//)
*
      return
      end