SUBROUTINE LATTEX_DNT(KST,KPROF,LDSETTLS,KXLAG,PESGP,PESGM,PXT0,PXT9,PMOY1X,&
 & PXSI,PXNLT9,PXT1,PXL0,PXL9,PCXNLT9,LDPC_NESC)  

!------------------------------------------------------------------------------
! LATTEX_DNT - Semi-Lagrangian scheme.
!              Computation of the t and t-dt useful quantities
!              at grid-points. Equations for tri-dimensional
!              variables for 2TL scheme.

! Purpose
! -------

! Interface
! ---------
!   CALL LATTEX_DNT(..)

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

! * INPUT:
!          KST     - first element of work.
!          KPROF   - depth of work.
!          LDSETTLS- .T./.F.: Stable/Conventional extrapolations for SL2TL.
!          KXLAG   - type of SL discretisation 
!          PESGP   - (1 + uncentering factor).
!          PESGM   - (1 - uncentering factor).
!          PXT0    - prognostic variable time t (predictor or SI),
!                    preliminary t+dt (corrector)
!          PXT9    - prognostic variable time t (corrector),
!                    not used for predictor or SI
!          PMOY1X  - full nonlinear model at time t [(Delta t/2) "cursive" A]

! * INPUT/OUTPUT:
!          PXSI    - semi-implicit linear model at time t
!                    [- (Delta t/2) "cursive" B]
!          PXNLT9  - buffer used during predictor resp. SI time step
!          PXT1    - t+dt term and other final point terms 
!          PXL0    - second SL quantity to be interpolated (linearly) at O
!                    if KXLAG=3
!          PXL9    - SL quantity to be interpolated at O 
!          PCXNLT9 - buffer used during corrector

! Externals
! ---------
!   none
!   Called by LATTEX.

! Method
! ------

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

! Author
! ------
!      Mar-2002 J. VIVODA (SHMI/CHMI/LACE) - rationalization of LATTEX

! Modifications
! -------------
!   12-Oct-2002 J. Masek   PC bugfix
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   09-Jun-2004 J. Masek   NH cleaning (LPC_NOTR, LDFULLIMP)
!   01-Jul-2004 K. Yessad  Make clearer the tests for PC scheme.
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   K. Yessad (Aug 2008): simplify XIDT treatment with PC + cleanings
!   K. Yessad Aug 2008: rationalisation of dummy argument interfaces
! End Modifications
!------------------------------------------------------------------------------

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

USE YOMCT0   , ONLY : LPC_NESC ,LPC_FULL ,LPC_CHEAP
USE YOMCT3   , ONLY : NSTEP
USE YOMDIM   , ONLY : NPROMA   ,NFLEVG   ,NFLSA    ,NFLEN
USE YOMDYN   , ONLY : XIDT     ,NCURRENT_ITER

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
LOGICAL           ,INTENT(IN)    :: LDSETTLS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KXLAG 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGM 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMOY1X(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXSI(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXNLT9(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXT1(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXL0(NPROMA,NFLSA:NFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXL9(NPROMA,NFLSA:NFLEN) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCXNLT9(NPROMA,NFLEVG) 
LOGICAL,OPTIONAL  ,INTENT(IN)    :: LDPC_NESC

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

INTEGER(KIND=JPIM) :: JLEV,JROF

!     * ZXNLT0 (resp. ZXNLT1) the non linear term at t (resp. t+dt).
REAL(KIND=JPRB) :: ZXNLT1(NPROMA)
REAL(KIND=JPRB) :: ZXNLT0(NPROMA)
REAL(KIND=JPRB) :: ZXIDT0,ZXIDT9,ZXIGP

LOGICAL :: LLCT,LLPC_NESC

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"

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

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

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

!*      1. AUXILIARY VARIABLES.
!       -----------------------

IF (PRESENT(LDPC_NESC)) THEN
  LLPC_NESC=LDPC_NESC
ELSE
  LLPC_NESC=LPC_NESC
ENDIF

LLCT = (LPC_FULL .AND. NCURRENT_ITER > 0)

ZXIDT0=1.0_JPRB+XIDT
ZXIDT9=1.0_JPRB+XIDT
ZXIGP=1.0_JPRB+XIDT

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

!*      2. MAIN CALCULATIONS.
!       ---------------------

IF( .NOT.LLCT )THEN

  !############################################
  ! 2.1 Predictor for LPC_FULL,
  !     or predictor for LPC_OLD,
  !     or case NSITER=0.
  !############################################

  IF (LPC_CHEAP.AND.(.NOT.LLPC_NESC)) THEN
    ! LPC_CHEAP=T is currently coded for LPC_NESC only
    !  (for LPC_NESC=F interpolated quantities are not the same ones
    !  at the predictor and corrector steps, and the LPC_CHEAP code
    !  currently assumes that they are identical). 
    CALL ABOR1('LATTEX_DNT: If LPC_CHEAP=T, LPC_NESC should be T')
  ENDIF

  DO JLEV=1,NFLEVG

    ! nonlinear residual time t
    DO JROF=KST,KPROF
      ZXNLT0(JROF)=PMOY1X(JROF,JLEV)+ZXIDT0*PXSI(JROF,JLEV)
    ENDDO
  
    ! Fill PXL9,PXL0,PXT1.
    IF (KXLAG == 2) THEN
      IF (NSTEP <= 0 .OR. (LPC_FULL.AND.LLPC_NESC)) THEN
        DO JROF=KST,KPROF
          PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT0(JROF,JLEV) &
           & +PESGM*PMOY1X(JROF,JLEV)
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT0(JROF)
        ENDDO
      ELSEIF (LDSETTLS) THEN
        DO JROF=KST,KPROF
          PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT0(JROF,JLEV) &
           & +PESGM*PMOY1X(JROF,JLEV)+(ZXNLT0(JROF)-PXNLT9(JROF,JLEV))
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT0(JROF)
        ENDDO
      ELSE
        ! * remaining case: ldsettls=false, lpc_nesc=false.
        DO JROF=KST,KPROF
          PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT0(JROF,JLEV) &
           & +PESGM*PMOY1X(JROF,JLEV) &
           & +0.5_JPRB*PESGM*(ZXNLT0(JROF)-PXNLT9(JROF,JLEV))
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV) &
           & +PESGP*(1.5_JPRB*ZXNLT0(JROF)-0.5_JPRB*PXNLT9(JROF,JLEV))
        ENDDO
      ENDIF
    ELSEIF (KXLAG == 3) THEN
      IF (NSTEP <= 0 .OR. (LPC_FULL.AND.LLPC_NESC)) THEN
        DO JROF=KST,KPROF
          PXL0(JROF,JLEV)=PXL0(JROF,JLEV)+PESGM*PMOY1X(JROF,JLEV)
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT0(JROF)
        ENDDO
      ELSEIF (LDSETTLS) THEN
        DO JROF=KST,KPROF
          PXL0(JROF,JLEV)=PXL0(JROF,JLEV)+PESGM*PMOY1X(JROF,JLEV) &
           & +(ZXNLT0(JROF)-PXNLT9(JROF,JLEV))
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT0(JROF)
        ENDDO
      ELSE
        ! * remaining case: ldsettls=false, lpc_nesc=false.
        DO JROF=KST,KPROF
          PXL0(JROF,JLEV)=PXL0(JROF,JLEV)+PESGM*PMOY1X(JROF,JLEV) &
           & +0.5_JPRB*PESGM*(ZXNLT0(JROF)-PXNLT9(JROF,JLEV))
          PXT1(JROF,JLEV)=PXT1(JROF,JLEV) &
           & +PESGP*(1.5_JPRB*ZXNLT0(JROF)-0.5_JPRB*PXNLT9(JROF,JLEV))
        ENDDO
      ENDIF
      DO JROF=KST,KPROF
        PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT0(JROF,JLEV)
      ENDDO
    ENDIF

    ! save quantities for corrector step
    IF( LPC_FULL )THEN
      ! save nonlinear model at time t
      PCXNLT9(KST:KPROF,JLEV)=PMOY1X(KST:KPROF,JLEV)
    ENDIF
  
    IF( .NOT.(LPC_FULL.AND.LLPC_NESC) )THEN
      ! save of nonlinear residual at time t
      ! to be used as nonlinear residual at time t-dt next time step
      DO JROF=KST,KPROF
        PXNLT9(JROF,JLEV)=PMOY1X(JROF,JLEV)+ZXIDT9*PXSI(JROF,JLEV)
      ENDDO
    ENDIF

  ENDDO

ELSE

  !############################################
  ! 2.2 Corrector for LPC_FULL
  !############################################

  DO JLEV=1,NFLEVG

    ! nonlinear residual time t+dt (preliminary state from predictor)

    DO JROF=KST,KPROF
      ZXNLT1(JROF)=PMOY1X(JROF,JLEV)+ZXIDT9*PXSI(JROF,JLEV)
    ENDDO

    IF (KXLAG == 2) THEN
      IF (.NOT. LPC_CHEAP) THEN
        DO JROF=KST,KPROF
          PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT9(JROF,JLEV) &
           & +PESGM*PCXNLT9(JROF,JLEV)
        ENDDO
      ENDIF
      DO JROF=KST,KPROF
        PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT1(JROF)
      ENDDO
    ELSEIF (KXLAG == 3) THEN
      IF (.NOT. LPC_CHEAP) THEN
        DO JROF=KST,KPROF
          PXL0(JROF,JLEV)=PXL0(JROF,JLEV)+PESGM*PCXNLT9(JROF,JLEV)
          PXL9(JROF,JLEV)=PXL9(JROF,JLEV)+PXT9(JROF,JLEV)
        ENDDO
      ENDIF
      DO JROF=KST,KPROF
        PXT1(JROF,JLEV)=PXT1(JROF,JLEV)+PESGP*ZXNLT1(JROF)
      ENDDO
    ENDIF

  ENDDO

ENDIF

!########################################################
! 2.3 Addition of preliminary quantity for LAGPHY physics
!########################################################

DO JLEV=1,NFLEVG
  IF(XIDT > 0.0_JPRB)THEN
    DO JROF=KST,KPROF
      PXT1(JROF,JLEV)=PXT1(JROF,JLEV)-ZXIGP*PXSI(JROF,JLEV)
      PXSI(JROF,JLEV)=ZXIGP*PXSI(JROF,JLEV)
    ENDDO
  ELSE
    DO JROF=KST,KPROF
      PXT1(JROF,JLEV)=PXT1(JROF,JLEV)-PESGP*PXSI(JROF,JLEV)
      PXSI(JROF,JLEV)=PESGP*PXSI(JROF,JLEV)
    ENDDO
  ENDIF
ENDDO

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

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

