!-------------------------------------- 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 --------------------------------------
!
!

      subroutine gd2mvogen(CDVARNAME,klev) 19,1
!
#if defined (DOC)
!
!**s/r gd2mvogen  - Horizontal interpolation of the model variables
!                   in grid-point space to observation locations.
!                   Bilinear interpolation from the 4 nearest grid points.
!
!Author  : L. Fillion *ARMA/MSC Dec 2009 - More general version of sub. gd2mvo.
!                                          N.B.: A general LAM or Global (rotated or not, staggered or not)
!                                          grid can be used here.
!Revision: 
!
!*    Purpose:  Build GOMOBS (in COMMVO) from GD (in COMGD0) using
!     .         bilinear interpolation.
!
! Arguments
!     CDVARNAME: identificator of the variable to be treated
!     KLEV     : number of levels (typically 1 for 2D-fields and NFLEV for 3D-fields)
!
#endif
      use modstag, only: lstagwinds
!
      IMPLICIT NONE
*implicits
#include "taglam4d.cdk"
#include "pardim.cdk"
#include "comdim.cdk"
#include "comdimo.cdk"
#include "comlun.cdk"
#include "comct0.cdk"
#include "comleg.cdk"
#include "comgem.cdk"
#include "comgd0.cdk"
#include "comcst.cdk"
#include "comoahdr.cdk"
#include "comoabdy.cdk"
#include "comoba.cdk"
#include "commvo.cdk"
#include "namstag.cdk"
!
!     Arguments
!
      character*2 cdvarname
      integer klev
!
!     Local Variables
!
      integer ix,iy
      integer jlev,jobs
      real zx_4,zy_4,zone
      real*8 zx,zy
      real*8 dlw1,dlw2,dlw3,dlw4
!
      real*8 zfield(nibeg:niend,1:klev,njbeg:njend)
      real*8 zprofil(1:klev,nobtot)
!
!!
!
!     Transfer Grid point field into local array (for genericity)
!
      zfield(:,:,:) = 0.
      zprofil(:,:)  = 0.
!
      select case (cdvarname)
!
!     2D fields
!
      case('TG')
         zfield(:,1,:)= gtg0(:,1,:)
      case('PS')
         zfield(:,1,:)= gps0(:,1,:)
!
!     3D fields
!
      case('TT')
         zfield(:,1:klev,:)=  TT0(:,1:KLEV,:)
      case('Q0')
         zfield(:,1:klev,:)=   Q0(:,1:KLEV,:)
      case('O3')
         zfield(:,1:klev,:)= GOZ0(:,1:klev,:)
      case('TR')
         zfield(:,1:klev,:)= GTR0(:,1:klev,:)
      case('UU')
         zfield(:,1:klev,:)=  UT0(:,1:KLEV,:)
      case('VV')
         zfield(:,1:klev,:)=  VT0(:,1:KLEV,:)
      case ('GZ')
         zfield(:,1:klev,:)=  GZ0(:,1:klev,:)
      end select
!
      OBSERVATIONS: DO JOBS = 1, NOBTOT
!
         zx_4   = ROBHDR(NCMTLO,JOBS)
         zy_4   = ROBHDR(NCMTLA,JOBS)
!
         if(lstagwinds) then ! actif seulement en Bi-Fourier pour l'instant et non mode REG-LAM....
           if(cdvarname.eq.'UU') zx_4   = zx_4 - 0.5
           if(cdvarname.eq.'VV') zy_4   = zy_4 - 0.5
         endif
!         
         ix = int(zx_4)
         iy = int(zy_4)
         zone=1.0
         zx = mod(zx_4,zone)
         zy = mod(zy_4,zone)
!
         dlw1  = (1.-zx)*(1.-zy)
         dlw2  = zx*(1.-zy)
         dlw3  = (1.-zx)*zy
         dlw4  = zx*zy
!
         DO JLEV = 1, KLEV
           ZPROFIL(JLEV,JOBS) =   DLW1*ZFIELD(ix,jlev,iy)
     &                        + DLW2*ZFIELD(ix+1,jlev,iy)
     &                        + DLW3*ZFIELD(ix,jlev,iy+1)
     &                        + DLW4*ZFIELD(ix+1,jlev,iy+1)
         END DO

      END DO OBSERVATIONS
!
!     Transfer to Global Array GOMOBS
!
      SELECT CASE (CDVARNAME)
!
!     2D fields
!
      CASE('TG')
         GOMTGR(1:KLEV,1:NOBTOT) = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('PS')
         GOMPS(1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
!
!     3D fields
!
      CASE('TT')
         GOMT (1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('Q0')
         GOMQ (1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('O3')
         GOMOZ(1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('TR')
         GOMTR(1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('UU')
         GOMU(1:KLEV,1:NOBTOT)   = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('VV')
         GOMV(1:KLEV,1:NOBTOT)   = ZPROFIL(1:KLEV,1:NOBTOT)
      CASE('GZ')
         GOMGZ(1:KLEV,1:NOBTOT)  = ZPROFIL(1:KLEV,1:NOBTOT)
      END SELECT
!
      RETURN
      END