!-------------------------------------- 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 newbilinad 2,30
*
#if defined (DOC)
*
***s/r NEWBILINAD  - Adjoint of 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 - 8 Oct 2009 - gd2mvogenad is more general than gd2mvoad 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 gd2mvoad_la.ftn is renamed gd2mvogenad.ftn
*  Revision: Luc Fillion - ARMA/EC - 11 may 2010 - Limit printout to processor 0.
*  Revision: Luc Fillion - ARMA/EC - 16 Jun 2010 - Remove useless local array z3d(ni,nflev,nj)
*  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 "comsim.cdk"
#include "comgd0.cdk"
#include "comcva.cdk"
      integer idum1,idum2,idum3,idum4
      real*8 zmin,zmax
*
* Local variables
*
      integer ilev
*
*     3D fields
*
      IF(myid == 0) THEN
        write(nulout,*)'NEWBILINAD- active '
      endif
!
      if(grd_typ.eq.'GU'.and.(.not.grd_roule)) then  ! i.e. old initial global Gaussian approach
!
        if(l1obs) then
          if(NGEXIST(nguu).eq.1)  call gd2mvoad_1obs('UU',nflev)
          if(NGEXIST(ngvv).eq.1)  call gd2mvoad_1obs('VV',nflev)
          if(NGEXIST(ngq).eq.1)   call gd2mvoad_1obs('Q0',nflev)
          if(NGEXIST(ngtt).eq.1)  call gd2mvoad_1obs('TT',nflev)
          if(NGEXIST(ngoz).eq.1)  call gd2mvoad_1obs('OZ',nflev)
          if(NGEXIST(ngtr).eq.1)  call gd2mvoad_1obs('TR',nflev)
          if(NGEXIST(nggz).eq.1)  call gd2mvoad_1obs('GZ',nflev)
        else
          if(NGEXIST(nguu).eq.1)  call gd2mvoad('UU',nflev)
          if(NGEXIST(ngvv).eq.1)  call gd2mvoad('VV',nflev)
          if(NGEXIST(ngq).eq.1)   call gd2mvoad('Q0',nflev)
          if(NGEXIST(ngtt).eq.1)  call gd2mvoad('TT',nflev)
          if(NGEXIST(ngoz).eq.1)  call gd2mvoad('OZ',nflev)
          if(NGEXIST(ngtr).eq.1)  call gd2mvoad('TR',nflev)
          if(NGEXIST(nggz).eq.1)  call gd2mvoad('GZ',nflev)
        endif
*
*         2D fields
*
        ilev = 1
        if(l1obs) then
          if(NGEXIST(ngps).eq.1)  call gd2mvoad_1obs('PS',ilev)
          if(NGEXIST(ngtg).eq.1)  call gd2mvoad_1obs('TG',ilev)
        else
          if(NGEXIST(ngps).eq.1)  call gd2mvoad('PS',ilev)
          if(NGEXIST(ngtg).eq.1)  call gd2mvoad('TG',ilev)
        endif
!
      else
!
!       Adjoint of: 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 auvrot2uv
          endif
        endif
!
        if(NGEXIST(nguu).eq.1)  call gd2mvogenad('UU',nflev)
        if(NGEXIST(ngvv).eq.1)  call gd2mvogenad('VV',nflev)
        if(NGEXIST(ngq).eq.1)   call gd2mvogenad('Q0',nflev)
        if(NGEXIST(ngtt).eq.1)  call gd2mvogenad('TT',nflev)
        if(NGEXIST(ngoz).eq.1)  call gd2mvogenad('OZ',nflev)
        if(NGEXIST(ngtr).eq.1)  call gd2mvogenad('TR',nflev)
        if(NGEXIST(nggz).eq.1)  call gd2mvogenad('GZ',nflev)
*
*         2D fields
*
        ilev = 1
        if(NGEXIST(ngps).eq.1)  call gd2mvogenad('PS',ilev)
        if(NGEXIST(ngtg).eq.1)  call gd2mvogenad('TG',ilev)
      endif
*
*     Conversion of wind images to physical winds
*
!      write(nulout,*) 'newbilinad: mlonobs,mk,mlatobs=',
!     &    mlonobs,mk,mlatobs
!      write(nulout,*) 'newbilinad: ut0(mlonobs,mk,mlatobs)=',
!     & ut0(mlonobs,mk,mlatobs)
!      write(nulout,*) 'newbilinad: vt0(mlonobs,mk,mlatobs)=',
!     & vt0(mlonobs,mk,mlatobs)
!      write(nulout,*) 'newbilinad: tt0(mlonobs,mk,mlatobs)=',
!     & tt0(mlonobs,mk,mlatobs)
!
      call uvwi2uv
*
      return
      end subroutine newbilinad