!-------------------------------------- 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_tl - TLM of vtap 
*
#include "model_macros_f.h"
*

      subroutine vtap_tl() 3
*
#include "impnone.cdk"
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_21 - Tanguay M.        - replace xfis by topo
* 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_tl for to vtap_tl
*
*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 work1m(LDIST_SHAPE,l_nk),work2m(LDIST_SHAPE,l_nk)
*     ______________________________________________________
*
      do k=1,l_nk
      do j=l_miny,l_maxy
      do i=l_minx,l_maxx
*
*        TRAJECTORY
*        ----------
         Indm_fipm(i,j,k) = ZERO_8
         work1m   (i,j,k) = ZERO_8
         work2m   (i,j,k) = ZERO_8
*
*        TLM 
*        ---
         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
*
*     --------------------------------------------------
*     Initialize special T such as d(fit1)/dZ = -(R T/Z)
*     --------------------------------------------------
      do k=1,l_nk
      do j=1,l_nj
      do i=1,l_ni
*
*        TLM Evaluate TRAJ exp(qt1)
*        --------------------------
         work2(i,j,k) = Geomg_pib(k)*exp(Indm_sm(i,j))*Ind_s(i,j)
*
*        TRAJ Evaluate exp(qt1)
*        ----------------------
         work2m(i,j,k) = Geomg_pia(k) + Geomg_pib(k)*exp(Indm_sm(i,j))
*
*        TLM Evaluate special T
*        ----------------------
         work2(i,j,k)= (Ind_tp(i,j,k))*
     %                 (1.0+Geomg_dpib(k)*(exp(Indm_sm(i,j))-1.))*
     %                 (Geomg_z_8(k) / work2m(i,j,k)) +
*
     %                 (Cstv_tstr_8+Indm_tpm(i,j,k))*
     %                 (Geomg_dpib(k)*(exp(Indm_sm(i,j))*Ind_s(i,j)))*
     %                 (Geomg_z_8(k) / work2m(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) 


*        TRAJ Evaluate special T
*        -----------------------
         work2m(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)) -  Cstv_tstr_8
*
      end do
      end do
      end do
*
*     Initialize GEO bottom boundary condition
*     ----------------------------------------
      do j=1,l_nj
      do i=1,l_ni
*
*        TRAJECTORY
*        ----------
         work1m(i,j,l_nk) = Ind_topo(i,j)
*
*        TLM
*        --- 
         work1 (i,j,l_nk) = ZERO_8 
*
      end do
      end do
*
*     Build GEO from T based on VTAP relationship
*     -------------------------------------------
      pcon = - Dcst_rgasd_8 
*
      do jk = 0,l_nk-2
*
         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
*
*        TRAJECTORY
*        ----------
         work1m(i,j,ik) = pr1*( work2m(i,j,ik+1) + work2m(i,j,ik) ) + work1m(i,j,ik+1)
*
*        TLM 
*        ---
         work1 (i,j,ik) = pr1*( work2 (i,j,ik+1) + work2 (i,j,ik) ) + work1 (i,j,ik+1)
*
         end do
         end do
*
      end do

      do k=1,l_nk
      do j=1,l_nj
      do i=1,l_ni
*
*        TRAJECTORY
*        ----------
         Indm_fim (i,j,k) = Cstvr_fistr_8(k) + work1m(i,j,k)
         Indm_fipm(i,j,k) = Indm_fim(i,j,k) - Cstvr_fistr_8(k) - Ind_topo(i,j)
*
*        TLM
*        ---
         Ind_fi (i,j,k) = work1 (i,j,k)
         Ind_fip(i,j,k) = Ind_fi(i,j,k) 
*
      end do
      end do
      end do
*
      return
      end