SUBROUTINE LAPINEATL(KST,KPROF,&
 & PCOLON,PSILON,PGM,PGEMU,PGSQM2,PGECLO,PGESLO,&
 & PGNORDL,PGNORDM,PINDX,PINDY,&
! -----------------------------------------------------------------------
 & PB1,PB2,PB15,&
! -----------------------------------------------------------------------
 & PLON,PLAT,PR,PS,&
 & PLON5,PLAT5,PR5,PS5,&
! -----------------------------------------------------------------------
 & KL0,KLH0,PDLO,PCLO,PCLOSLD,PDLAT,PCLA,PCLASLD,PDVER,PVINTW,PVINTWSLD,& 
 & PDLO5,PCLO5,PCLOSLD5,&
 & PDLAT5,PCLA5,PCLASLD5,PDVER5,PVINTW5,PVINTWSLD5,&
! -----------------------------------------------------------------------
 & PCOSCO,PSINCO,PCOPHI,PSINLA,PLEV,&
 & PCOSCO5,PSINCO5,PCOPHI5,PSINLA5,PLEV5,&
! -----------------------------------------------------------------------
 & PUF5,PVF5,POUT5)

!**** *LAPINEATL* - semi-LAgrangian scheme:(Trajectory)
!                                         (tangent-linear version)
!                Interface subroutine for interpolations and lagrangian
!                trends. (Programme INterface d'Ensemble).

!     Purpose.
!     --------
!           Grid point calculations in dynamics.

!           Computes the semi-Lagrangian trajectory.
!           Computes the weights ready for interpolations at origin point.

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

!        Explicit arguments :
!        --------------------
!        INPUT:
!          KST     - first element of arrays where computations are performed.
!          KPROF   - depth of work.
!          PCOLON  - cos(computational sphere longitude).
!          PSILON  - sin(computational sphere longitude).
!          PGM     - mapping factor.
!          PGEMU   - sin(geographical latitude).
!          PGSQM2  - cos(geographical latitude).
!          PGECLO  - cos(geographical longitude).
!          PGESLO  - sin(geographical longitude).
!          PGNORDL - zonal component of the vector pointed towards the
!                    true North pole.
!          PGNORDM - meridian component of the vector pointed towards the
!                    true North pole.
!          PINDX   - x coordinate on the plane.
!          PINDY   - y coordinate on the plane.
!          PB1     - SLBUF1-buffer for interpolations.
!          PB2     - SLBUF2-buf to communicate info from non lag. to lag. dyn.
!          PB15    - SLBUF1-buf traj for interpolations.

!        OUTPUT AND TRAJECTORY-OUTPUT:
!          PLON    - computational sphere longitude of interpolation point.
!          PLAT    - computational sphere latitude of interpolation point.
!          PR      - first element of the wind displacement matrix for O.
!          PS      - second element of the wind displacement matrix for O.
!          PLON5   - cf. PLON (trajectory)
!          PLAT5   - cf. PLAT (trajectory)
!          PR5     - cf. PR (trajectory)
!          PS5     - cf. PS (trajectory)
!          KL0     - index of the four western points
!                    of the 16 points interpolation grid.
!          KLH0    - second value of index of the four western points
!                    of the 16 points interpolation grid if needed.
!          PDLO    - distances for horizontal linear interpolations
!                    in longitude (latitude rows 0, 1, 2, 3)
!          PCLO    - weights for horizontal cubic interpolations in
!                    longitude (latitude rows 1, 2)
!          PCLOSLD - weights for horizontal cubic interpolations in
!                    longitude, SLHD case (latitude rows 1, 2)
!          PDLAT   - distance for horizontal linear interpolations
!                    in latitude
!          PCLA    - weights for horizontal cubic interpolations
!                    in latitude
!          PCLASLD - weights for horizontal cubic interpolations
!                    in latitude, SLHD case
!          PDVER   - distances for vertical linear interpolation
!                    on a same vertical.
!          PVINTW  - Vertical cubic interpolation weights.
!          PVINTWSLD - vertical cubic interpolation weights, SLHD case
!          PDLO5   - cf. PDLO (trajectory)
!          PCLO5   - cf. PCLO (trajectory)
!          PCLOSLD5- cf. PCLOSLD (trajectory)
!          PDLAT5  - cf. PDLAT (trajectory)
!          PCLA5   - cf. PCLA (trajectory)
!          PCLASLD5- cf. PCLASLD (trajectory)
!          PDVER5  - cf. PDVER (trajectory)
!          PVINTW5 - cf. PVINTW (trajectory)
!          PVINTWSLD5 - cf. PVINTWSLD (trajectory)
!          PCOSCO  - cos(Longitude-Longitude(grid-point))*cos(Latitude)
!                    of the interpolation point (geographical longitude
!                    and latitude).
!                    ! if LDPLANE: x - coordinate (fractional system).
!          PSINCO  - sin(Longitude-Longitude(grid-point))*cos(Latitude)
!                    of the interpolation point (geographical longitude
!                    and latitude).
!                    ! if LDPLANE: y - coordinate (fractional system).
!          PCOPHI  - cosine of the geographical angle between the
!                    interpolation point and the grid-point.
!          PSINLA  - sine of the interpolation point geographical latitude.
!          PLEV    - vertical coordinate of the interpolation point.
!          PCOSCO5 - cf. PCOSCO (trajectory)
!          PSINCO5 - cf. PSINCO (trajectory)
!          PCOPHI5 - cf. PCOPHI (trajectory)
!          PSINLA5 - cf. PSINLA (trajectory)
!          PLEV5   - cf. PLEV (trajectory)

!        TRAJECTORY-INPUT:
!          PUF5    - interpolated U-wind.                             (in)
!          PVF5    - interpolated V-wind.                             (in)
!          POUT5   - indicator of O point of the SL trajectory.       (in)
!                    If O lays inside the domain, the value is zero.

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

!     Method.
!     -------

!     Externals.
!     ----------
!       See includes below.
!       Called by CALL_SL_TL.

!     Reference.
!     ----------
!        ARPEGE documentation about semi-Lagrangian scheme.

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

!     Modifications.
!     --------------
!        Original : 99/07/13
!        Modified 00-02-07 by C. Temperton: removed redundant use of PDP0
!                                           when NVLAG=3 (contained zero!)
!        Modified August 2000 by Deborah Salmond to split into _traj and _int
!        Modified 00-11-09 by C. Temperton: streamlining.
!        Modified 00-11-23 by C. Temperton: renamed _TRAJ and _INT routines.
!        Modified 01-08-30 by K. YESSAD: pruning and some other cleanings.
!        R. El Khatib : 01-08-07 Pruning options
!        Modified 03-04-09 by C. Temperton: use SLADJTR to repeat    
!                                           trajectory calculations.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        D.Salmond     22-Dec-2003 Karim's Larcin cleaning
!        Modified 05-01-11 by C. Temperton: streamlining.
!        Modified 05-01-14 by C. Temperton: transfer computations from
!                                start of LAIDEP to end of LARMES.
!        D.Salmond     08-Feb-2005 SLADJTR replaced by LARMES5
!        K. Yessad     07-Sep-2005 Now calls directly LARCINATL.
!        F. Vana       01-Jun-2006 LRPLANE=.T.
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!        F. Vana       27-Jun-2008 Weights driven interpolation
!        K. Yessad Nov 2008: rationalisation of dummy argument interfaces
!        F. Vana 13-Jan-2009: TL/AD of SLHD
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NDGSAG   ,NDGSAL   ,NDGENG   ,NDGENL   ,NDGUNG   ,&
 & NDGUXG   ,NDLUNG   ,NDLUXG   ,NFLEVG   ,NDGSAH   ,&
 & NDGENH   ,NRLEVX   ,NPROMA   ,NFLSA    ,NFLEN
USE YOMCT0   , ONLY : LRPLANE
USE YOMDYNA  , ONLY : LSLHD    ,LSLHDQUAD
USE YOMDYN   , ONLY : NITMP    ,VETAON   ,VETAOX   ,LSETTLST ,LELTRA   ,LADVF
USE YOMCST   , ONLY : RPI
USE YOMGEM   , ONLY : NLOENG
USE YEMGEO   , ONLY : EDELX    ,EDELY
USE YOMRIP   , ONLY : RTDT
USE YOMSC2   , ONLY : NSLWIDE  ,NFLDSLB1 ,NFLDSLB2 ,NFLDSLB15
USE YOMMP    , ONLY : NSLOFF   ,NFRSTLOFF,NASLB1
USE PTRSLB2  , ONLY : MSLB2KAPPA ,MSLB2KAPPA5

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCOLON(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSILON(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEMU(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGSQM2(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGECLO(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGESLO(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDL(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGNORDM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PINDX(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PINDY(NPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PB1(NASLB1,NFLDSLB1)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PB2(NPROMA,NFLDSLB2)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PB15(NASLB1,NFLDSLB15)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLON(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLAT(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PR(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PS(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLON5(NPROMA,NFLEVG,NITMP) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLAT5(NPROMA,NFLEVG,NITMP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PR5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PS5(NPROMA,NFLEVG,NITMP)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KL0(NPROMA,NFLEVG,0:3,NITMP) 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KLH0(NPROMA,NFLEVG,0:3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLO(NPROMA,NFLEVG,0:3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLO(NPROMA,NFLEVG,3,2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLOSLD(NPROMA,NFLEVG,3,2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLAT(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLA(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLASLD(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDVER(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVINTW(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVINTWSLD(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLO5(NPROMA,NFLEVG,0:3,NITMP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLO5(NPROMA,NFLEVG,3,2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLOSLD5(NPROMA,NFLEVG,3,2)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLAT5(NPROMA,NFLEVG,NITMP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLA5(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLASLD5(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDVER5(NPROMA,NFLEVG,NITMP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVINTW5(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVINTWSLD5(NPROMA,NFLEVG,3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCOSCO(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSINCO(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCOPHI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSINLA(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLEV(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCOSCO5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSINCO5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCOPHI5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSINLA5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLEV5(NPROMA,NFLEVG,NITMP)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUF5(NPROMA,NFLEVG,NITMP-1)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVF5(NPROMA,NFLEVG,NITMP-1)
REAL(KIND=JPRB)   ,INTENT(IN)    :: POUT5(NPROMA,NFLEVG,NITMP)

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

REAL(KIND=JPRB) :: ZGMDTX(NPROMA)
REAL(KIND=JPRB) :: ZGMDTY(NPROMA)
REAL(KIND=JPRB) :: ZLSDEPI(NDGSAH:NDGENH)

INTEGER(KIND=JPIM) :: ISTALAT(NDGSAH:NDGENH)
INTEGER(KIND=JPIM) :: ILEV(NPROMA,NFLEVG)

INTEGER(KIND=JPIM) :: IGLGLO, JGL
INTEGER(KIND=JPIM) :: ITIP
INTEGER(KIND=JPIM) :: IROT

LOGICAL :: LLSETTLST

REAL(KIND=JPRB) :: ZUF0(1), ZVF0(1), ZWF0(1), ZWRL0(1), &
 & ZWRL05(1), ZURL(1),ZURL5(1),ZVRL(1),ZVRL5(1)

REAL(KIND=JPRB) :: ZDEPI

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "larcinatl.intfb.h"
#include "elarmestl.intfb.h"
#include "larmestl.intfb.h"

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

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

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

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

ZDEPI=2.0_JPRB*RPI
DO JGL=MAX(NDGSAG,NDGSAL+NFRSTLOFF-NSLWIDE)-NFRSTLOFF,&
   & MIN(NDGENG,NDGENL+NFRSTLOFF+NSLWIDE)-NFRSTLOFF  
  IGLGLO=JGL+NFRSTLOFF
  ZLSDEPI(JGL)=REAL(NLOENG(IGLGLO),JPRB)/ZDEPI
ENDDO

IF(LRPLANE) THEN
  ZGMDTX (KST:KPROF)=0.5_JPRB*PGM(KST:KPROF)*RTDT/EDELX
  ZGMDTY (KST:KPROF)=0.5_JPRB*PGM(KST:KPROF)*RTDT/EDELY
ENDIF

DO JGL=MAX(NDGSAH,LBOUND(NSLOFF,1)),MIN(NDGENH,UBOUND(NSLOFF,1))
  ISTALAT(JGL)=NSLOFF(JGL)
ENDDO

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

!*       2.    COMPUTATION OF THE SL TRAJECTORY.
!              ---------------------------------

LLSETTLST=LSETTLST.AND.(.NOT.LELTRA)

!              (FIRST, REPEAT TRAJECTORY CALCULATIONS)
!              ---------------------------------------

IF (LRPLANE) THEN

  CALL ELARMESTL(KST,KPROF,ISTALAT,LLSETTLST,PINDX,PINDY,ZGMDTX,ZGMDTY,&
   & PB1,PB15,PB2,&
   & ZLSDEPI,PCOLON,PSILON,PGEMU,PGSQM2,PGECLO,PGESLO,&
   & PLON,PLAT,PCOSCO,PSINCO,PSINLA,PCOPHI,PLEV,PR,PS,&
   & KL0,KLH0,PDLO,PDLAT,PCLA,ILEV,PDVER,PVINTW,&
   & PLON5,PLAT5,PCOSCO5,PSINCO5,PSINLA5,PCOPHI5,PLEV5,PR5,PS5,&
   & PDLO5,PDLAT5,PCLA5,PDVER5,PVINTW5,POUT5)  

ELSE

  CALL LARMESTL(KST,KPROF,ISTALAT,LLSETTLST,PB1,PB15,PB2,&
   & ZLSDEPI,PCOLON,PSILON,PGEMU,PGSQM2,PGECLO,PGESLO,PGNORDL,PGNORDM,&
   & PLON,PLAT,PCOSCO,PSINCO,PSINLA,PCOPHI,PLEV,PR,PS,&
   & KL0,KLH0,PDLO,PDLAT,PCLA,ILEV,PDVER,PVINTW,&
   & PLON5,PLAT5,PCOSCO5,PSINCO5,PSINLA5,PCOPHI5,PLEV5,PR5,PS5,PUF5,PVF5,&
   & PDLO5,PDLAT5,PCLA5,PDVER5,PVINTW5)  

ENDIF

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

!*       3.    COMPUTATION OF WEIGHTS AND INDICES ARRAYS FOR INTERPOLATIONS.
!              -------------------------------------------------------------

ITIP=3
IROT=1

! Remark: ZURL,ZVRL,ZWRL0,ZURL5,ZVRL5,ZWRL05,ZUF0,ZVF0,ZWF0
! are not used in this call of LARCINATL.

CALL LARCINATL(KST,KPROF,ISTALAT,LSLHD,LSLHDQUAD,ITIP,IROT,LRPLANE,&
 & ZLSDEPI,&
 & PCOLON,PSILON,PGEMU,PGSQM2,PGECLO,PGESLO,&
 & PCOSCO,PSINCO,PSINLA,PCOPHI,PLEV,&
 & PCOSCO5(1,1,NITMP),PSINCO5(1,1,NITMP),&
 & PSINLA5(1,1,NITMP),PCOPHI5(1,1,NITMP),PLEV5(1,1,NITMP),&
 & PB2(1,MSLB2KAPPA),PB2(1,MSLB2KAPPA5),&
 & ZURL,ZVRL,ZWRL0,ZURL5,ZVRL5,ZWRL05,&
 & PLON,PLAT,PR,PS,&
 & ZUF0,ZVF0,ZWF0,&
 & PLON5(1,1,NITMP),PLAT5(1,1,NITMP),PR5(1,1,NITMP),PS5(1,1,NITMP),&
 & KL0(1,1,0,NITMP),KLH0(1,1,0),PDLO,PCLO,PCLOSLD,&
 & PDLAT,PCLA,PCLASLD,ILEV,PDVER,PVINTW,PVINTWSLD,&
 & PDLO5(1,1,0,NITMP),PCLO5,PCLOSLD5,&
 & PDLAT5(1,1,NITMP),PCLA5,PCLASLD5,PDVER5(1,1,NITMP),PVINTW5,PVINTWSLD5)

IF (LRPLANE) THEN
! * Use PSINLA and PCOPHI to transfer origin point coordinates to LAPINEB
!   for "2 Omega vectorial a k" recalculation.
  IF(LADVF) THEN
    PSINLA5(KST:KPROF,1:NFLEVG,NITMP)=PLON5(KST:KPROF,1:NFLEVG,NITMP)
    PCOPHI5(KST:KPROF,1:NFLEVG,NITMP)=PLAT5(KST:KPROF,1:NFLEVG,NITMP)
    PSINLA(KST:KPROF,1:NFLEVG)=PLON(KST:KPROF,1:NFLEVG)
    PCOPHI(KST:KPROF,1:NFLEVG)=PLAT(KST:KPROF,1:NFLEVG)
  ENDIF
ENDIF

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

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