!-------------------------------------- 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 newbilin 6,30
*
#if defined (DOC)
*
***s/r NEWBILIN  - Horizontal bilinear interpolation of the model variables
*               in grid-point space to observation points.
*
*     Author  : P. GAUTHIER *ARMA/MSC July 2002
*     .         -------------------------------
*     Purpose:  Build GOMU, GOMV, GOMT, GOMGZ, GOMQ and GOMPS  with the model variables at the
*               observation points from the model variables in grid-points
*               space using bilinear interpolation.
*
*  Revision: Luc Fillion - ARMA/MSC -  Dec 2003 - Limited-Area option added.
*  Revision: Luc Fillion - ARMA/EC - 14 Aug 2007 - Update to v_10_0_3.
*  Revision: Luc Fillion - ARMA/EC - 7 Oct 2009 - gd2mvogen is more general than gd2mvo in terms of allowed grid.
*                                      The use of gd2mvo is kept for historical reasons in grd_typ = 'GU' mode
*                                      Gaussian grid with non-rotated poles. Old gd2mvo_la.ftn is renamed gd2mvogen.ftn
*                                      and used for GENeral cases.
*  Revision: Luc Fillion - ARMA/EC - 11 May 2010 - Limit printout toroot.
*  Revision: Luc Fillion - ARMA/EC - 16 Jun 2010 - Introduce 1obs interpolators to avoid treating MPI.
*
#endif
      USE procs_topo
      IMPLICIT NONE
*implicits
#include "pardim.cdk"
#include "comdim.cdk"
#include "comlun.cdk"
#include "comstate.cdk"
#include "comgrd_param.cdk"
#include "comcva.cdk"
*
*     Local variables
*
      integer ilev
!
!!
      IF(myid == 0) THEN
        write(nulout,*)'NEWBILIN- Horizontal interpolation GD --> MVO'
      endif
!
!     Conversion of wind images to physical winds
!
      call uvwi2uv
!
!     3D fields interpolation
!
      if(grd_typ.eq.'GU'.and.(.not.grd_roule)) then      ! i.e. original global Gaussian grid approach
        if(l1obs) then
          if(NGEXIST(nguu).eq.1)  call gd2mvo('UU',nflev)
          if(NGEXIST(ngvv).eq.1)  call gd2mvo('VV',nflev)
          if(NGEXIST(ngq).eq.1)   call gd2mvo('Q0',nflev)
          if(NGEXIST(ngtt).eq.1)  call gd2mvo('TT',nflev)
          if(NGEXIST(ngoz).eq.1)  call gd2mvo('OZ',nflev)
          if(NGEXIST(ngtr).eq.1)  call gd2mvo('TR',nflev)
          if(NGEXIST(nggz).eq.1)  call gd2mvo('GZ',nflev)
        else
          if(NGEXIST(nguu).eq.1)  call gd2mvo('UU',nflev)
          if(NGEXIST(ngvv).eq.1)  call gd2mvo('VV',nflev)
          if(NGEXIST(ngq).eq.1)   call gd2mvo('Q0',nflev)
          if(NGEXIST(ngtt).eq.1)  call gd2mvo('TT',nflev)
          if(NGEXIST(ngoz).eq.1)  call gd2mvo('OZ',nflev)
          if(NGEXIST(ngtr).eq.1)  call gd2mvo('TR',nflev)
          if(NGEXIST(nggz).eq.1)  call gd2mvo('GZ',nflev)
        endif
*
*       2D fields interpolation
*
        ilev = 1
        if(l1obs) then
          if(NGEXIST(ngps).eq.1)  call gd2mvo_1obs('PS',ilev)
          if(NGEXIST(ngtg).eq.1)  call gd2mvo_1obs('TG',ilev)
        else
          if(NGEXIST(ngps).eq.1)  call gd2mvo('PS',ilev)
          if(NGEXIST(ngtg).eq.1)  call gd2mvo('TG',ilev)
        endif
      else
        if(NGEXIST(nguu).eq.1)  call gd2mvogen('UU',nflev)
        if(NGEXIST(ngvv).eq.1)  call gd2mvogen('VV',nflev)
        if(NGEXIST(ngq).eq.1)   call gd2mvogen('Q0',nflev)
        if(NGEXIST(ngtt).eq.1)  call gd2mvogen('TT',nflev)
        if(NGEXIST(ngoz).eq.1)  call gd2mvogen('OZ',nflev)
        if(NGEXIST(ngtr).eq.1)  call gd2mvogen('TR',nflev)
        if(NGEXIST(nggz).eq.1)  call gd2mvogen('GZ',nflev)
!
!       Rotate tangential wind components to real sphere tangential components
!
        if(grd_roule) then
          if((NGEXIST(nguu).eq.1).and.(NGEXIST(ngvv).eq.1)) then
            call uvrot2uv
          endif
        endif
*
*       2D fields
*
        ilev = 1
        if(NGEXIST(ngps).eq.1)  call gd2mvogen('PS',ilev)
        if(NGEXIST(ngtg).eq.1)  call gd2mvogen('TG',ilev)
      endif
*
      return
      end subroutine newbilin