!-------------------------------------- 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 --------------------------------------
***s/r vtap_ad - ADJ of vtap_tl 
*
#include "model_macros_f.h"
*

      subroutine vtap_ad() 2
*
#include "impnone.cdk"
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_21 - Tanguay M.        - cancel xfis
* v2_30 - Edouard S.        - replace Schm_elast_L by Schm_cptop_L
* v2_31 - Tanguay M.        - adapt for vertical hybrid coordinate 
* v3_03 - Tanguay M.        - use Ind comdeck
* v3_22 - Lee V.            - Name change from v4d_vtap_ad for to vtap_ad
*
*object
*     see id section
*
*arguments
*     none
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "cstv.cdk"
#include "dcst.cdk"
#include "geomg.cdk"
#include "schm.cdk"
#include "ind.cdk"
#include "indm.cdk"
*
*modules
      integer i,j,k,ik,jk
*
      real pcon,pr1
*
      real*8, parameter :: ZERO_8 = 0.0
*
*     -----------------
*     Define work space
*     -----------------
      real work1 (LDIST_SHAPE,l_nk),work2 (LDIST_SHAPE,l_nk)
      real                          work2m(LDIST_SHAPE,l_nk)
*
*     ______________________________________________________
*
*     Zero local adjoint variables
*     ----------------------------
      do k=1,l_nk
      do j=l_miny,l_maxy
      do i=l_minx,l_maxx
         work1(i,j,k) = ZERO_8
         work2(i,j,k) = ZERO_8
      end do
      end do
      end do
*
      do k=1,l_nk
      do j=1,l_nj
      do i=1,l_ni
*
         Ind_fi (i,j,k) = Ind_fip(i,j,k) + Ind_fi(i,j,k)
         Ind_fip(i,j,k) = ZERO_8 
*
         work1  (i,j,k) = Ind_fi (i,j,k) + work1 (i,j,k)
         Ind_fi (i,j,k) = ZERO_8
*
      end do
      end do
      end do
*
*     ADJ of
*     Build GEO from T based on VTAP relationship
*     -------------------------------------------
      pcon = - Dcst_rgasd_8
*
      do jk = l_nk-2,0,-1
*
         ik  = l_nk-1-jk
         pr1 = - pcon*(Geomg_z_8(ik+1)-Geomg_z_8(ik))/(Geomg_z_8(ik+1)+Geomg_z_8(ik))
*
         do j = 1, l_nj
         do i = 1, l_ni
            work2(i,j,ik+1) = pr1*( work1(i,j,ik) ) + work2(i,j,ik+1)
            work2(i,j,ik  ) = pr1*( work1(i,j,ik) ) + work2(i,j,ik  )
            work1(i,j,ik+1) =       work1(i,j,ik)   + work1(i,j,ik+1)
            work1(i,j,ik)   = ZERO_8
         end do
         end do
*
      end do
*
*     ADJ of
*     Initialize GEO bottom boundary condition
*     ----------------------------------------
      do j=1,l_nj
      do i=1,l_ni
*
         work1 (i,j,l_nk) = ZERO_8 
*
      end do
      end do
*
*     ADJ of
*     --------------------------------------------------
*     Initialize special T such as d(fit1)/dZ = -(R T/Z)
*     --------------------------------------------------
*
      do k=l_nk,1,-1
      do j=1,l_nj
      do i=1,l_ni
*
*        TRAJ Evaluate exp(qt1)
*        ----------------------
         work2m(i,j,k) = Geomg_pia(k) + Geomg_pib(k)*exp(Indm_sm(i,j))
*
*
*        ADJ Evaluate special T
*        ----------------------
         Ind_tp(i,j,k) = (work2(i,j,k))*
     %                   (1.0+Geomg_dpib(k)*(exp(Indm_sm(i,j))-1.))*
     %                   (Geomg_z_8(k) / work2m(i,j,k)) + Ind_tp(i,j,k)
*
         Ind_s(i,j)    = (Cstv_tstr_8+Indm_tpm(i,j,k))*
     %                   (Geomg_dpib(k)*(exp(Indm_sm(i,j))*work2(i,j,k)))*
     %                   (Geomg_z_8(k) / work2m(i,j,k)) + Ind_s(i,j)
*
         work2(i,j,k) = -
     %                   (Cstv_tstr_8+Indm_tpm(i,j,k))*
     %                   (1.0+Geomg_dpib(k)*(exp(Indm_sm(i,j))-1.))*
     %                   (Geomg_z_8(k) / work2m(i,j,k)**2)*work2(i,j,k)
*
*
*        ADJ Evaluate TRAJ exp(qt1)
*        --------------------------
         Ind_s(i,j)   = Geomg_pib(k)*exp(Indm_sm(i,j))*work2(i,j,k) + Ind_s(i,j)
         work2(i,j,k) = ZERO_8
*
      end do
      end do
      end do
*
*     ADJ of
*     Zero variables
*     --------------
      do k=1,l_nk
      do j=l_miny,l_maxy
      do i=l_minx,l_maxx
         Ind_fi (i,j,k) = ZERO_8
         Ind_fip(i,j,k) = ZERO_8
         work1  (i,j,k) = ZERO_8
         work2  (i,j,k) = ZERO_8
      end do
      end do
      end do
*
      return
      end