!-------------------------------------- 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 vspng_drv_tr - Equivalent to vspng_drv for TRAJECTORY 
*                    (F_pip, F_qp (No Hyd) ONLY)
*
#include "model_macros_f.h"
*

      subroutine vspng_drv_tr (F_pip,F_qp,DIST_DIM,Nk) 1,4
*
      implicit none
*
      integer DIST_DIM, Nk
      real   F_pip(DIST_SHAPE,Nk), F_qp(DIST_SHAPE,Nk)
*
*author
*     M.Tanguay
*
*revision
* v2_21 - Tanguay M.        - initial MPI version
* v2_31 - Tanguay M.        - TRAJ of control for sponge on momentum and
*                             T', T'lin & hence T on Vspng_nk levels +
*                             sponge on top level only on all other variables
* v3_00 - Tanguay M.        - correction as in vspng_drv 
* v3_01 - Laroche/Tanguay   - correction nj as in vspng_drv
* v3_03 - Tanguay M.        - Adjoint NoHyd configuration
* v3_11 - Tanguay M.        - AIXport+Opti+OpenMP for TLM-ADJ
* v3_20 - Tanguay M.        - Adjoint of variable higher order diffusion operator
*
*object
*     see id section
*
*arguments
* 
*implicits
#include "glb_ld.cdk"
#include "dcst.cdk"
#include "cstv.cdk"
#include "schm.cdk"
#include "geomg.cdk"
#include "trp.cdk"
#include "vspng.cdk"
#include "hzd.cdk"
#include "opr.cdk"
*
      integer i, j, k, nkspng
      real*8 HALF_8,TWO_8,c_8
      parameter( HALF_8  = 0.5 )
      parameter(  TWO_8  = 2.0 )
*
      real work(LDIST_SHAPE,Nk)
      real*8, dimension (trp_12emax*G_ni*Vspng_nk) :: aix_8,bix_8,cix_8,dix_8
      real*8, dimension (trp_22emax*G_nj*Vspng_nk) :: aiy_8,biy_8,ciy_8
      real*8 cy_8(l_nj+1), xp0_8(G_ni), yp0_8(G_nj)
*     __________________________________________________________________
*
      if ( Cstv_phidf_8 .gt. 0.0) then
*
         c_8 = Cstv_phidf_8*Cstv_dt_8/(Dcst_rayt_8*Dcst_rayt_8)
         do j = 1, l_nj+1
            cy_8(j) = G_yg_8(l_j0+j-1)
         end do
*
*     Mass related fields
*     ~~~~~~~~~~~~~~~~~~~
*
         if (Vspng_uvwdt_L) then
             nkspng   = Vspng_nk
             Vspng_nk = 1
         endif
*
         if (Hzd_difva_L) then
           call vspng_abc (aix_8,bix_8,cix_8,dix_8,aiy_8,biy_8,ciy_8,c_8,
     $                     cy_8,Opr_opsxp0_8,Opr_opsxp2_8,Opr_opsyp0_8,
     $                     Hzd_yp2su_8,G_ni,G_nj,G_nj)
         else
           call vspng_abc (aix_8,bix_8,cix_8,dix_8,aiy_8,biy_8,ciy_8,c_8,
     $                     cy_8,Opr_opsxp0_8,Opr_opsxp2_8,Opr_opsyp0_8,
     $                     Opr_opsyp2_8,G_ni,G_nj,G_nj)
         endif
*
*     Save pi' for the diffusion of q'
*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!$omp parallel
         if ( (.not.Schm_hydro_L) .and. Schm_difqp_L ) then
!$omp do
            do k = 1, Vspng_nk
            do j = 1, l_nj
            do i = 1, l_ni
               work(i,j,k) = F_pip(i,j,k)
            end do
            end do
            end do
!$omp enddo
         endif
!$omp end parallel
*
*     The hydrostatic pressure: pi'  
*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         call vspng_del2 (F_pip,
     $                  Opr_opsxp0_8(G_ni+1),Opr_opsyp0_8(G_nj+1),
     $                  aix_8,bix_8,cix_8,dix_8,aiy_8,biy_8,ciy_8,
     $                  LDIST_DIM,Vspng_nk,trp_12emax,trp_22emax,G_nj)
*
*****************************
*  3. Nonyhydrostatic model *
*****************************
*
         if ( .not. Schm_hydro_L ) then
*
         if ( Schm_difqp_L ) then ! q' & related variables
*
*        Indirect diffusion of q'
*        ~~~~~~~~~~~~~~~~~~~~~~~~
!$omp parallel do
         do k=1, Vspng_nk
         do j=1, l_nj
         do i=1, l_ni
            work(i,j,k) = ( Geomg_z_8(k) + dble(work(i,j,k)) )
     %                       *exp( dble(F_qp(i,j,k)) )
         end do
         end do
         end do
!$omp end parallel do
*
         call vspng_del2 (work,
     $                  Opr_opsxp0_8(G_ni+1),Opr_opsyp0_8(G_nj+1),
     $                  aix_8,bix_8,cix_8,dix_8,aiy_8,biy_8,ciy_8,
     $                  LDIST_DIM,Vspng_nk,trp_12emax,trp_22emax,G_nj)
*
!$omp parallel do
         do k=1, Vspng_nk
         do j=1, l_nj
         do i=1, l_ni
            F_qp(i,j,k) = log(             dble(work(i,j,k))
     %                       /( Geomg_z_8(k) + dble(F_pip(i,j,k)) )  )
         end do
         end do
         end do
!$omp end parallel do
*
         endif
         endif
*
         if (Vspng_uvwdt_L) Vspng_nk = nkspng
*
      endif
*
*     __________________________________________________________________
*
      return
      end