!-------------------------------------- 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 avtapgd(ptt,pgz,ki,kj)
#if defined (DOC)
*
*S/P AVTAPGD: Adjoint of lvtapgd.ftn
*
* AUTHOR: Luc Fillion - 2 May 2007
*
* REVISION:
#endif
      IMPLICIT NONE
      integer ki,kj
*
*implicits
#include "taglam4d.cdk"
#include "comlun.cdk"
#include "comdim.cdk"
#include "comode.cdk"
#include "comphy.cdk"
*modules
*
      REAL*8  ptt(ni,nflev,nj)
      real*8  pgz(ni,nflev,nj)
*
      INTEGER IKLEVM2, JK, IK
      REAL*8  ZAK, ZBK, ZCK, ZCON
**
      ZCON = -RGASD
      IKLEVM2 = nflev-2
!
      do jk=iklevm2,1,-1
        ik = nflev-1-jk
        zak = -2.0*zcon*vma(ik+1)
        zbk = -2.0*zcon*vmb(ik+1)
        zck = -2.0*zcon*vmc(ik+1)
        ptt(ki,ik,kj) = ptt(ki,ik,kj)+pgz(ki,ik,kj)*zak
        ptt(ki,ik+1,kj) = ptt(ki,ik+1,kj)+pgz(ki,ik,kj)*zbk
        ptt(ki,ik+2,kj) = ptt(ki,ik+2,kj)+pgz(ki,ik,kj)*zck
        pgz(ki,ik+2,kj) = pgz(ki,ik+2,kj)+pgz(ki,ik,kj)
        pgz(ki,ik,kj) = 0.0
      enddo
      ZAK = -2.0*ZCON*VMA(nflev)
      ZBK = -2.0*ZCON*VMB(nflev)
      ZCK = -2.0*ZCON*VMC(nflev)
      ptt(ki,nflev-1,kj)=ptt(ki,nflev-1,kj)
     &                 +ZAK*pgz(ki,nflev-1,kj)
      ptt(ki,nflev,kj)=ptt(ki,nflev,kj) +ZBK*pgz(ki,nflev-1,kj)
      ptt(ki,nflev-2,kj)=ptt(ki,nflev-2,kj)
     &                 +ZCK*pgz(ki,nflev-1,kj)
      pgz(ki,nflev-1,kj)=0.0
      pgz(ki,nflev,kj)=0.0
!
      return
      end