!-------------------------------------- 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 subackgrd(ptop,kobtot) 1,1
c
      IMPLICIT NONE
      integer kobtot
      real*8 ptop(kobtot)
c
#if defined (DOC)
*
***s/r SUBACKGRD
*     OBJECT: Initialise background state dependant factors
*             and vectors for use in TLM and adjoint of
*             non-linear operator
*
*     Author  : S. Pellerin *ARMA/AES Sept. 98
*
**    Purpose:
*             - Copy observation space background fields
*               in GOMOBSG.
*             - Initialisation of tangent linear vitual temperature operator
*             - Initialisation of tlm and adjoint hydrostatic operators
*               factors.
*     Revision: C. Chouinard *ARMA/AES Sept. 98
*             - add definition of TLM and ADJ operators for T-Td conversions
*     Revision: L. Filllion *ARMA/AES 24 nov 98
*             - Reorganize to support ES and HU options.
*Arguments
*
#endif
*implicits
#include "pardim.cdk"
#include "comdim.cdk"
#include "comdimo.cdk"
#include "comgem.cdk"
*
#include "commvo1.cdk"
#include "commvog.cdk"
*
      integer jlev,jobs
      real*8 zhu, ztd
*
#include "comcva.cdk"
#include "comphy.cdk"
#include "dinternv.cdk"
#include "finternv.cdk"
#include "finternva.cdk"
#include "finternvl.cdk"
*
*--------------------------------------------
*
      if(chum.eq.'ES') then
        do jlev = 1, nflev
          do jobs = 1, nobtot
            zhu=exp(gomqg(jlev,jobs))
c
c           fields for es to q tl transform
c
            ztd = gomtg(jlev,jobs) - gomesg(jlev,jobs)
            dlnesg(jlev,jobs) = fodle(ztd)
            estdg(jlev,jobs) = foew(ztd)
            if(zhu.eq.1.) then
              rqgfac(jlev,jobs) = 0.
            else
              rqgfac(jlev,jobs) = 1.
            endif
c
c           fields for Tv to GZ tl transform
c
            rtapfac(jlev,jobs) = 1./(ptop(jobs)/(gompsg(1,jobs) -
     &                           ptop(jobs)) + vlev(jlev))
            oltv(1,jlev,jobs) = (1.+delta*zhu)
            oltv(2,jlev,jobs) = delta*gomtg(jlev,jobs)
          enddo
        enddo
c
      else
c
c       initialize GOMT1 THE ADJOINT to 1.0 to get the operator via the adjoint
c
        DO JLEV=1,NFLEV
          DO JOBS=1,NOBTOT
            GOMT1(JLEV,JOBS) = 1.0
          ENDDO
        ENDDO
        do jlev = 1,nflev
          DO JOBS=1,NOBTOT
            zhu=exp(gomqg(jlev,jobs))
            oltv(1,jlev,jobs) = fottva(zhu,gomt1(jlev,jobs))
            oltv(2,jlev,jobs) = folnqva(zhu,gomtg(jlev,jobs),
     &                                  gomt1(jlev,jobs))
            rtapfac(jlev,jobs) = 1./(ptop(jobs)/(gompsg(1,jobs) -
     &                           ptop(jobs)) + vlev(jlev))
          enddo
        enddo
c
c       initialize the operator using the adjoint of the conversion lnq/t to t-td
c       initialising the adjoint of t-td=1.0
c
        DO JLEV=1,NFLEV
        DO JOBS=1,NOBTOT
          GOMES1(JLEV,JOBS)= 1.0
          GOMQ1(JLEV,JOBS) = 0.0
          GOMPS1(JLEV,JOBS)= 0.0
          GOMT1(JLEV,JOBS) = 0.0
        ENDDO
        ENDDO
c
        call amhuaesv
c
c       store the coefficients
c
        DO JLEV=1,NFLEV
          DO JOBS=1,NOBTOT
          OLLQ2ES(1,JLEV,JOBS) = GOMQ1 (JLEV,JOBS)
          OLLQ2ES(2,JLEV,JOBS) = GOMPS1(JLEV,JOBS)
          OLLQ2ES(3,JLEV,JOBS) = GOMT1 (JLEV,JOBS)
          ENDDO
        ENDDO
      endif
c
      RETURN
      END