SUBROUTINE LACDYNTL(CDCONF,KST,KPROF,PDT,&
 & PSLHDA,PSLHDD0,&
 & PUT95 ,PVT95,&
 & PGFL,&
 & PRE0,PKAP0,PDELP0,PRDELP0,PEVEL0,PVVEL0,PSGRTL,PSGRTM,&
 & PGFL5,&
 & PRE5,PKAP5,PDELP5,PRDELP5,PEVEL5,PVVEL5,PSGRT5L,PSGRT5M,&
 & PGM,PRCORI,PGOMVRL,PGOMVRM,&
 & POROG,POROGL,POROGM,&
 & PGMV,PGMVS,PB1,PB2,PGMVT1,PGMVT1S, &
 & PEDOTT95,PUNLT95,PVNLT95,PTNLT95,PSPNLT95,&
 & PGMV5,PGMV5S,PB15)  

!**** *LACDYNTL* Semi-Lagrangian scheme.  (tangent-linear 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.

!          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* *LACDYNTL(..)

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

!        INPUT:      ('5' for corresponding trajectory variables)
!          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
!          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:   ('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,PB15: trajectory for PGMV,PGMVS,PB1.

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

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

!     Externals.
!     ----------
!           Called by CPG_DYN_TL.

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

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

!     Modifications.
!     --------------
!   Original : 99/08/11.
!   Clive Temperton 00-11-06 : bugfix for ozone advection.
!   A. Untch 2000-12: changes to call to LASSIETL
!   Modified 01-08-30 by K. YESSAD: pruning and some other cleanings.
!   R. El Khatib : 01-08-07 Pruning options
!   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
USE YOMDIM   , ONLY : NPROMA   ,NFLEVG   ,NFLSA    ,NFLEN
USE YOMSC2   , ONLY : NFLDSLB2 ,NFLDSLB1 ,NFLDSLB15
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(IN)    :: PUT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRE0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKAP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP0(NPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVVEL0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRTM(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: 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)    :: PSGRT5L(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSGRT5M(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) :: PEDOTT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUNLT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVNLT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTNLT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSPNLT95(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV5(NPROMA,NFLEVG,YT5%NDIM)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV5S(NPROMA,YT5%NDIMS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB15(NPROMA,NFLDSLB15)

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

! * computed in LASURE:
REAL(KIND=JPRB) :: ZREDIV(NPROMA)
! * computed in LASSIETL or LANHSITL:
REAL(KIND=JPRB) :: ZTOD0  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZGAGT0L(NPROMA,NFLEVG),ZGAGT0M(NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZTOD5  (NPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZGAGT5L(NPROMA,NFLEVG),ZGAGT5M(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 "lassietl.intfb.h"
#include "lasure.intfb.h"
#include "lattestl.intfb.h"
#include "latte_kappatl.intfb.h"
#include "lattextl.intfb.h"
#include "lavabotl.intfb.h"
#include "laventtl.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('LACDYNTL',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)  

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

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

CALL LASSIETL(KST,KPROF,PRCORI,PGMV,PGMVS,PGFL, &
 & ZSDIV0,ZTOD0,ZGAGT0L,ZGAGT0M, &
 & PGMV5,PGMV5S,PGFL5,ZSDIV5,ZTOD5,ZGAGT5L,ZGAGT5M)  

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

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

CALL LAVENTTL(KST,KPROF,LL2TLFF1,PSGRTL,PSGRTM,ZDTS2,PRCORI,&
 & PRDELP0,PEVEL0,PGMV,PB1,PB2, &
 & PSGRT5L,PSGRT5M,PRDELP5,PEVEL5,PGMV5,PUT95,PVT95,PEDOTT95,PB15)

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

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

CALL LATTEXTL(KST,KPROF,ZDTS2,ZBT,ZBDT,ZESGP,ZESGM,&
 & PRCORI,PGOMVRL,PGOMVRM,POROG,POROGL,POROGM,&
 & ZGAGT0L,ZGAGT0M,ZTOD0,PSGRTL,PSGRTM,PKAP0,PVVEL0,PRDELP0,PEVEL0,&
 & PGFL,PGMV,PGMVT1,PB1,PB2,&
 & ZGAGT5L,ZGAGT5M,ZTOD5,PSGRT5L,PSGRT5M,PKAP5,PVVEL5,PRDELP5,PEVEL5,&
 & PGMV5,PGFL5,PUNLT95,PVNLT95,PTNLT95,PB15)

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

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

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

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

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

IF (LSLHD) THEN
  CALL LATTE_KAPPATL(KST,KPROF,ZDTS2,PSLHDA,PSLHDD0,PGMV,PGMV5,PB2)
ENDIF

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

!*       7.    UPPER AND LOWER LATERAL BOUNDARIES CONDITIONS.
!              ----------------------------------------------

CALL LAVABOTL(KST,KPROF,LL2TLFF1,PB15,PB1)

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

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