SUBROUTINE LACDYNAD(CDCONF,KST,KPROF,PDT,&
 & PSLHDA,PSLHDD0,&
 & PGFL,&
 & PRE0,PKAP0,PDELP0,PRDELP0,PEVEL0,PVVEL0,PSGRTL,PSGRTM,&
 & PGFL5,&
 & PRE5,PKAP5,PDELP5,PRDELP5,PEVEL5,PVVEL5,&
 & PGM,PRCORI,PGOMVRL,PGOMVRM,&
 & POROG,POROGL,POROGM,&
 & PGMV,PGMVS,PB1,PB2,PGMVT1,PGMVT1S, &
 & PGMV5,PGMV5S)  

!**** *LACDYNAD* Semi-Lagrangian scheme.  (adjoint version)
!                Computation of the t and t-dt useful quantities
!                 at grid-points.

!     Purpose.
!     --------

!          Dynamic non-linear computations in grid-point space
!          for hydrostatic and NH primitive equations and SL scheme.
!          (adjoint code of NH currently not yet coded).

!          Additional remarks:
!          - notation "prehyds" is for hydrostatic surface pressure.
!          - for input and output upper air variables, values are at full levels
!            if no other information is provided.

!          This subroutine fills the semi-Lagrangian buffers to be
!          interpolated.

!**   Interface.
!     ----------
!        *CALL* *LACDYNAD(..)

!        Explicit arguments :
!        --------------------

!        INPUT:      
!          CDCONF  - configuration.
!          KST     - first element of work.
!          KPROF   - depth of work.
!          PDT     - For a leap-frog scheme (three time level scheme):
!                     'dt' at the first time-step, '2 dt' otherwise.
!                    For a 2TL SL scheme: timestep 'dt'.
!          PSLHDA  - Scaling factor of the deformation in f(d) function
!                    (including the model resolution correction)
!          PSLHDD0 - Treshold for deformation tensor enhancement

!        INPUT in TL:   ('5' for corresponding trajectory variables)
!          PGFL    - GFL variables at full levels.
!          PRE0    - hydrostatic pressure "prehyd" at half levels at t.
!          PKAP0   - "R/cp" at full levels at t.
!          PDELP0  - pressure depth of layers at t.
!          PRDELP0 - 1/PDELP0 at t.
!          PEVEL0  - "(etadot d prehyd/d eta)" at half levels at t.
!          PVVEL0  - "omega/prehyd" at full levels at t.
!          PSGRTL  - zonal component of the pressure force term
!                    (grad(phi)+RT grad(p)).
!          PSGRTM  - meridian component of the pressure force term.
!          PGM     - mapping factor.
!          PRCORI  - Coriolis parameter "f = 2 Omega sin(theta)".
!          PGOMVRL - zonal component of "2 Omega vec a k".
!          PGOMVRM - meridian component of "2 Omega vec a k".
!          POROG   - surface orography.
!          POROGL  - zonal component of the orography gradient.
!          POROGM  - meridian component of the orography gradient.

!        INPUT-OUTPUT in TL:   ('5' for corresponding trajectory variables)
!          PGMV    - GMV variables at t-dt and t.
!          PGMVS   - GMVS variables at t-dt and t.
!          PB1     - "SLBUF1" buffer for interpolations.
!          PB2     - "SLBUF2" buffer.
!          PGMVT1  - GMV variables at t+dt.
!          PGMVT1S - GMVS variables at t+dt.
!          PEDOTT95 to PSPNLT95: trajectory variables.
!          PGMV5,PGMV5S: trajectory for PGMV,PGMVS.

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!           Called by CPG_DYN_AD.

!     Reference.
!     ----------
!             Arpege documentation about semi-lagrangian scheme.

!     Author.
!     -------
!        C. Temperton (ECMWF)

!     Modifications.
!     --------------
!   Original : 99/10/13.
!   Modified 00-11-06 by C. Temperton: bugfix for ozone advection.
!   Modified 2000-12  by A. Untch: call to LASSIEAD changed
!   Modified 01-08-30 by K. YESSAD: pruning and some other cleanings.
!   R. El Khatib : 01-08-07 Pruning options
!   Modified 02-01-22 by C. Temperton: case LSPRT=TRUE.
!   O.Spaniel    : 03-04-15 cleaning-a same named entity from modules
!   M.Hamrud      01-Oct-2003 CY28 Cleaning
!   K.Yessad      19-Aug-2004 Correct some intent attributes (PDT,PGFL).
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad Aug 2008: rationalisation of dummy argument interfaces
!   F. Vana  13-Jan-2009: computation of KAPPA for SLHD
!  ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMDYNA  , ONLY : LSLHD    ,LSLHD_STATIC
USE YOMDIM   , ONLY : NPROMA   ,NFLEVG
USE YOMSC2   , ONLY : NFLDSLB2 ,NFLDSLB1
USE YOMGMV   , ONLY : NDIMGMV  ,NDIMGMVS ,YT5      ,YT1
USE YOM_YGFL , ONLY : YGFL

!  ------------------------------------------------------------------

IMPLICIT NONE

CHARACTER(LEN=1)  ,INTENT(IN)    :: CDCONF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLHDA(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLHDD0(NPROMA)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRE0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PKAP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDELP0(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRDELP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PEVEL0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVVEL0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSGRTL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSGRTM(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGFL5(NPROMA,NFLEVG,YGFL%NDIM5) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRE5(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKAP5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP5(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL5(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVVEL5(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRCORI(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGOMVRL(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGOMVRM(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROGM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVS(NPROMA,NDIMGMVS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB1(NPROMA,NFLDSLB1)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB2(NPROMA,NFLDSLB2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1(NPROMA,NFLEVG,YT1%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVT1S(NPROMA,YT1%NDIMS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV5(NPROMA,NFLEVG,YT5%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV5S(NPROMA,YT5%NDIMS)

!     ------------------------------------------------------------------

! - computed in LASURE:
REAL(KIND=JPRB) :: ZREDIV(NPROMA)
! - computed in LASSIE or LANHSI:
REAL(KIND=JPRB) :: ZTOD0  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZGAGT0L(NPROMA,NFLEVG),ZGAGT0M(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTOD5  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZBDT(NPROMA)
REAL(KIND=JPRB) :: ZSDIV0(NPROMA)
REAL(KIND=JPRB) :: ZSDIV5(NPROMA)

LOGICAL :: LL2TLFF1

REAL(KIND=JPRB) :: ZBETA, ZBT, ZDTS2, ZESGM, ZESGP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!  ------------------------------------------------------------------

#include "lassiead.intfb.h"
#include "lasure.intfb.h"
#include "lattesad.intfb.h"
#include "latte_kappaad.intfb.h"
#include "lattexad.intfb.h"
#include "laventad.intfb.h"

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('LACDYNAD',0,ZHOOK_HANDLE)

!     ------------------------------------------------------------------

!*       1.    PRELIMINARY INITIALISATIONS:
!              ----------------------------

! Remark: T/L of LASURE is not needed

CALL LASURE(CDCONF,KST,KPROF,PDT,PGM,&
 & ZDTS2,ZBETA,ZBT,LL2TLFF1,ZBDT,ZREDIV,ZESGP,ZESGM)  

ZTOD0(:,:)=0.0_JPRB
ZGAGT0L(:,:)=0.0_JPRB
ZGAGT0M(:,:)=0.0_JPRB
ZTOD5(:,:)=0.0_JPRB
ZSDIV0(:)=0.0_JPRB
ZSDIV5(:)=0.0_JPRB

!     ------------------------------------------------------------------

!*       6.    COMPUTATION OF "KAPPA" 
!              ----------------------

IF (LSLHD.AND.(.NOT.LSLHD_STATIC)) THEN
  CALL LATTE_KAPPAAD(KST,KPROF,ZDTS2,PSLHDA,PSLHDD0,PGMV,PGMV5,PB2)
ENDIF

!     ------------------------------------------------------------------

!*       5.    COMPUTATION OF THE 2D-EQUATIONS RIGHT-HAND SIDE TERMS.
!              ------------------------------------------------------

CALL LATTESAD(KST,KPROF,ZDTS2,ZBDT,ZESGP,ZESGM,&
 & POROGL,POROGM,ZSDIV0,PEVEL0,PDELP0,PRE0,PGMVS,&
 & PGMV,PGMVT1S,PB1,PB2,&
 & PEVEL5,PDELP5,PRE5,PGMV5,PGMV5S)

!     ------------------------------------------------------------------

!*       4.    COMPUTATION OF THE 3D-EQUATIONS RIGHT-HAND SIDE TERMS.
!              ------------------------------------------------------

CALL LATTEXAD(KST,KPROF,ZDTS2,ZBT,ZBDT,ZESGP,ZESGM,&
 & PRCORI,POROG,POROGL,POROGM,&
 & ZGAGT0L,ZGAGT0M,ZTOD0,PSGRTL,PSGRTM,PKAP0,PVVEL0,PRDELP0,PEVEL0,&
 & PGFL,PGMV,PGMVT1,PB1,PB2,&
 & PKAP5,PVVEL5,PRDELP5,PEVEL5,PGMV5)

!     ------------------------------------------------------------------

!*       3.    COMPUTATION OF THE WIND COMPONENTS NECESSARY FOR SL TRAJECTORY.
!              ---------------------------------------------------------------

CALL LAVENTAD(KST,KPROF,LL2TLFF1,PSGRTL,PSGRTM,ZDTS2,PRCORI,&
 & PRDELP0,PEVEL0,PGMV,PB1,PB2,PRDELP5,PEVEL5)

!     ------------------------------------------------------------------

!*       2.    COMPUTATION OF THE LINEAR TERMS FOR SEMI-IMPLICIT SCHEME.
!              ---------------------------------------------------------

CALL LASSIEAD(KST,KPROF,PRCORI,PGMV,PGMVS,PGFL,&
 & ZSDIV0,ZTOD0,ZGAGT0L,ZGAGT0M,PGMV5,PGFL5,ZSDIV5,ZTOD5)

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('LACDYNAD',1,ZHOOK_HANDLE)
END SUBROUTINE LACDYNAD
