SUBROUTINE LASCAW_CLA_TL(KFLEV,KPROM,KGLN,KGLX,KST,KPROF,LDT_SLHD,&
 & KILA,PWH,PDLAT,PKAPPA,PSLD1_,PSLD2_,PSLD3_,PSLDW_,&
 & PWH5,PDLAT5,PKAPPA5,&
 & PCLA,PCLASLD,PCLA5,PCLASLD5)

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

!**** *LASCAW_CLA_TL  -  Weights for semi-LAgrangian interpolator:
!                        Computes PCLA and PCLASLD for one layer
!                        (high-order zonal weights)
!                        TL + trajectory code
!      Spherical geometry only.

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

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

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

!        INPUT:
!          KFLEV    - Vertical dimension
!          KPROM    - horizontal dimension.
!          KGLN     - lower dimension for latitude.
!          KGLX     - upper dimension for latitude.
!          KST      - first element of arrays where computations are performed.
!          KPROF    - depth of work.
!          LDT_SLHD - keys for SLHD.
!          KILA     - cf. ILA in LASCAW.
!          PWH      - cf. ZZWH in LASCAW.
!          PDLAT    - distance for horizontal linear interpolations in latitude
!          PKAPPA   - kappa function ("coefficient of SLHD").
!          PSLD1_   - auxiliary quantity for SLHD interpolation in latitude
!          PSLD2_   - auxiliary quantity for SLHD interpolation in latitude
!          PSLD3_   - auxiliary quantity for SLHD interpolation in latitude
!          PSLDW_   - weights for SLHD Laplacian smoother in latitude
!          PWH5     - cf. PWH (trajectory).
!          PDLAT5   - cf. PDLAT (trajectory).
!          PKAPPA5  - cf. PKAPPA (trajectory).

!        OUTPUT:
!          PCLA    - weights for horizontal cubic interpolations in latitude.
!          PCLASLD - cf. PCLA, SLHD case.
!          PCLA5   - cf. PCLA (trajectory).
!          PCLASLD5- cf. PCLASLD (trajectory).

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

!     Method.
!     -------
!        See documentation about semi-Lagrangian scheme.

!     Externals.
!     ----------

!        No external.
!        Called by LASCAWTL.

!     Reference.
!     ----------

!     Author.
!     -------
!        K. YESSAD, after former LASCAWTL code (JAN 2009).
!        METEO-FRANCE, CNRM/GMAP.

!     Modifications.
!     --------------
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK 
USE YOMDYNA  , ONLY : SLHDKMIN ,SLHDKMAX
USE YOMLASCAW, ONLY : KPDUP

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

IMPLICIT NONE

INTEGER(KIND=JPIM), INTENT(IN)  :: KFLEV
INTEGER(KIND=JPIM), INTENT(IN)  :: KPROM
INTEGER(KIND=JPIM), INTENT(IN)  :: KGLN
INTEGER(KIND=JPIM), INTENT(IN)  :: KGLX
INTEGER(KIND=JPIM), INTENT(IN)  :: KST
INTEGER(KIND=JPIM), INTENT(IN)  :: KPROF
LOGICAL           , INTENT(IN)  :: LDT_SLHD(3)
INTEGER(KIND=JPIM), INTENT(IN)  :: KILA(KPROM,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PWH(KPROM,3,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PDLAT(KPROM,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PKAPPA(KPROM,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PSLD1_(KPDUP,KGLN:KGLX)
REAL(KIND=JPRB)   , INTENT(IN)  :: PSLD2_(KPDUP,KGLN:KGLX)
REAL(KIND=JPRB)   , INTENT(IN)  :: PSLD3_(KPDUP,KGLN:KGLX)
REAL(KIND=JPRB)   , INTENT(IN)  :: PSLDW_(KPDUP,3,3,KGLN:KGLX)
REAL(KIND=JPRB)   , INTENT(IN)  :: PWH5(KPROM,3,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PDLAT5(KPROM,KFLEV)
REAL(KIND=JPRB)   , INTENT(IN)  :: PKAPPA5(KPROM,KFLEV)
REAL(KIND=JPRB)   , INTENT(OUT) :: PCLA(KPROM,KFLEV,3)
REAL(KIND=JPRB)   , INTENT(OUT) :: PCLASLD(KPROM,KFLEV,3)
REAL(KIND=JPRB)   , INTENT(OUT) :: PCLA5(KPROM,KFLEV,3)
REAL(KIND=JPRB)   , INTENT(OUT) :: PCLASLD5(KPROM,KFLEV,3)

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

INTEGER(KIND=JPIM) :: JROF,IJ_,ILA,JLEV
REAL(KIND=JPRB) :: ZWA1,ZWA2,ZWA3,ZWD1,ZWD2,ZWD3,ZWH1,ZWH2,ZWH3
REAL(KIND=JPRB) :: ZWDS1,ZWDS2,ZWDS3,ZWL1,ZWL2,ZWL3
REAL(KIND=JPRB) :: ZWA15,ZWA25,ZWA35,ZWD15,ZWD25,ZWD35,ZWH15,ZWH25,ZWH35
REAL(KIND=JPRB) :: ZWDS15,ZWDS25,ZWDS35,ZWL15,ZWL25,ZWL35
LOGICAL :: LLSLHD,LLSLHDQUAD,LLSLHD_OLD
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('LASCAW_CLA_TL',0,ZHOOK_HANDLE)
!     ------------------------------------------------------------------

LLSLHD=LDT_SLHD(1)
LLSLHDQUAD=LDT_SLHD(2)
LLSLHD_OLD=LDT_SLHD(3)

! * Calculation of PCLA and PCLASLD, PCLA5 and PCLASLD5:
DO JLEV=1,KFLEV
!CDIR NODEP
DO JROF=KST,KPROF

  ! a/ Preliminary calculations:

  IJ_ = MOD(JROF+1-KST,KPDUP)+1
  ILA=KILA(JROF,JLEV)

  ! b/ Trajectory code (in particuliar computes PCLA5 and PCLASLD5):

#if defined(NECSX)
  ! scalar variable initialization (to meet vectorization condition)
  ZWA15=0._JPRB
  ZWA25=0._JPRB
  ZWA35=0._JPRB
  ZWD15=0._JPRB
  ZWD25=0._JPRB
  ZWD35=0._JPRB
  ZWDS15=0._JPRB
  ZWDS25=0._JPRB
  ZWDS35=0._JPRB
#endif

  ZWH15=PWH5(JROF,1,JLEV)
  ZWH25=PWH5(JROF,2,JLEV)
  ZWH35=PWH5(JROF,3,JLEV)
  IF (LLSLHDQU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JLEV)*(ZWDS35-ZWA35)
  ELSEIF (LLSLHDQUAD) THEN
    ZWA15=ZWH15+SLHDKMIN*(ZWL15-ZWH15)
    ZWA25=ZWH25+SLHDKMIN*(ZWL25-ZWH25)
    ZWA35=ZWH35+SLHDKMIN*(ZWL35-ZWH35)
    PCLA5(JROF,JLEV,1)=ZWA15
    PCLA5(JROF,JLEV,2)=ZWA25
    PCLA5(JROF,JLEV,3)=ZWA35
  ELSE
    PCLA5(JROF,JLEV,1)=ZWH15
    PCLA5(JROF,JLEV,2)=ZWH25
    PCLA5(JROF,JLEV,3)=ZWH35
  ENDIF

  ! c/ TL (in particuliar computes PCLA and PCLASLD):

#if defined(NECSX)
  ! scalar variable initialization (to meet vectorization condition)
  ZWA1=0._JPRB
  ZWA2=0._JPRB
  ZWA3=0._JPRB
  ZWD1=0._JPRB
  ZWD2=0._JPRB
  ZWD3=0._JPRB
  ZWDS1=0._JPRB
  ZWDS2=0._JPRB
  ZWDS3=0._JPRB
#endif

  ZWH1=PWH(JROF,1,JLEV)
  ZWH2=PWH(JROF,2,JLEV)
  ZWH3=PWH(JROF,3,JLEV)
  IF (LLSLHDQUAD) THEN
    ZWL2=PDLAT(JROF,JLEV)
    ZWL1=-ZWL2
    ZWL3=PSLD3_(IJ_,ILA+1)*((ZWL1-ZWL2)*PDLAT5(JROF,JLEV) + ZWL2)
    ZWL1=ZWL1+PSLD1_(IJ_,ILA+1)*ZWL3
    ZWL2=ZWL2+PSLD2_(IJ_,ILA+1)*ZWL3
  ELSEIF (LLSLHD_OLD) THEN
    ZWL2=PDLAT(JROF,JLEV)
    ZWL1=-ZWL2
    ZWL3=0.0_JPRB
  ENDIF

  IF (LLSLHD) THEN
    ZWA1=ZWH1+SLHDKMIN*(ZWL1-ZWH1)
    ZWA2=ZWH2+SLHDKMIN*(ZWL2-ZWH2)
    ZWA3=ZWH3+SLHDKMIN*(ZWL3-ZWH3)
    ZWD1=ZWH1+SLHDKMAX*(ZWL1-ZWH1)
    ZWD2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     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('LASCAW_CLA_TL',1,ZHOOK_HANDLE)
END SUBROUTINE LASCAW_CLA_TL
