SUBROUTINE LATTE_BBC( &
 ! --- INPUT --------------------------------------------------
 & KST,KPROF,PDTS2,PESGP,PESGM,PDBBC,PRDPHI,&
 ! --- INPUT/OUTPUT -------------------------------------------
 & PGWS,PGMVS,PGMV,&
 ! --- OUTPUT -------------------------------------------------
 & PB1,PB2)

!------------------------------------------------------------------------------
!**** *LATTE_BBC*   Semi-Lagrangian scheme for NH model.
!                   Computation of the t and t-dt useful quantities
!                   at grid-points.
!                   Additional variables required in the NH
!                   vertical divergence variable when (LRDBBC,LGWADV)=(T,F).

!                   Remark: this routine must not be called for LGWADV=T
!                   because the additional quantities to be interpolated
!                   computed in this routine are useless if LGWADV=T.
!                   When LGWADV=T, [gw]_surf(t+dt) is computed by a diagnostic
!                   relationship in routine LAPINEB, using some provisional
!                   "(U,V)(t+dt)" data available after the interpolations.

! Purpose
! -------

! Interface
! ---------
!   INPUT:
!     KST         - first element of work.
!     KPROF       - depth of work.
!     PDTS2       - 0.5*time step for the first time-integration step of
!                   a leap-frog scheme or all time-integration steps of
!                   a two-time level scheme; time step for the following
!                   time-integration steps of a leap-frog scheme.
!     PESGP       - (1 + uncentering factor).
!     PESGM       - (1 - uncentering factor).
!     PDBBC       - (dgw_s/dt) at t.
!     PRDPHI      - (RT prehyd [Delta log(prehyd)])/pre at t.

!   INPUT/OUTPUT:
!     PGWS        - (gw_s) at t (for predictor).
!     PGMVS       - GMVS variables at t-dt and t.
!     PGMV        - GMV variables at t-dt and t.

!   OUTPUT:
!     PB1         - "SLBUF1" buffer for interpolations.
!     PB2         - "SLBUF2" buffer.

! Externals
! ---------
!   none
!   Called by LACDYN.

! Method
! ------

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

! Author
! ------
!      Aug-2003 P. SMOLIKOVA, K. YESSAD

! Modifications
! -------------
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   09-Jun-2004 J. Masek   NH cleaning (LFULLIMP)
!   01-Jul-2004 K. Yessad  Make clearer the tests for PC scheme.
!   12-Jan-2005 K. Yessad
!    - memory transfer PGWS <-> PGWST9 moved from LACDYN to LATTE_BBC.
!    - test (.NOT.LGWADV) moved in LACDYN.
!    - some other cleanings.
!   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 : LTWOTL   ,LPC_FULL ,LPC_NESC ,LPC_CHEAP
USE YOMCT3   , ONLY : NSTEP
USE YOMDIM   , ONLY : NPROMA   ,NPROMNH  ,NFLEVG   ,NFLSA
USE YOMDYN   , ONLY : LSETTLS  ,NCURRENT_ITER
USE YOMSC2   , ONLY : NFLDSLB1 ,NFLDSLB2
USE YOMGMV   , ONLY : NDIMGMV,NDIMGMVS,YT9
USE PTRSLB1  , ONLY : MSLB1DBBC9,MSLB1DPHI9,MSLB1GWS9
USE PTRSLB2  , ONLY : MSLB2DBBC1,MSLB2DPHI1

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDTS2 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PESGM 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDBBC(NPROMNH) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDPHI(NPROMNH,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGWS(NPROMNH) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMVS(NPROMA,NDIMGMVS)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PB1(NPROMA,NFLDSLB1)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PB2(NPROMA,NFLDSLB2)


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

INTEGER(KIND=JPIM) :: JLEV,JROF

LOGICAL :: LLCT,LLCTC

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"

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

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

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

!*       1.   Preliminary initializations.

LLCT = LPC_FULL .AND. NCURRENT_ITER > 0  ! corrector step
LLCTC= LPC_CHEAP .AND. NCURRENT_ITER > 0

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

!*       2.   2TLSL scheme.

IF (LTWOTL) THEN

  ! * SL2TL:

  IF( .NOT.LLCT )THEN

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

    IF (LPC_CHEAP.AND.(.NOT.LPC_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('LATTE_BBC: If LPC_CHEAP=T, LPC_NESC should be T')
    ENDIF

    ! * Save PGWS in PGMVS(.,YT9%MGWS):
    IF (LPC_FULL) PGMVS(KST:KPROF,YT9%MGWS)=PGWS(KST:KPROF)

    ! * Calculation of "DPHIT1" and "DBBCT1":
    IF ((NSTEP <= 0).OR.(LPC_FULL.AND.LPC_NESC).OR.LSETTLS) THEN
      DO JROF=KST,KPROF
        PB2(JROF,MSLB2DPHI1)=PESGP*PRDPHI(JROF,NFLEVG)
        PB2(JROF,MSLB2DBBC1)=PESGP*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,NFLEVG))
      ENDDO
    ELSE
      ! * remaining case: lsettls=false, lpc_nesc=false.
      DO JROF=KST,KPROF
        PB2(JROF,MSLB2DPHI1)=PESGP* &
         & (1.5_JPRB*PRDPHI(JROF,NFLEVG)-0.5_JPRB*PGMV(JROF,NFLEVG,YT9%MDPHI))
        PB2(JROF,MSLB2DBBC1)=PESGP* &
         & (1.5_JPRB*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,NFLEVG)) &
         & -0.5_JPRB*PDTS2*PGMVS(JROF,YT9%MDBBC)*PGMV(JROF,JLEV,YT9%MDPHI))
      ENDDO
    ENDIF

    ! * Calculation of PB1(.,MSLB1DPHI9) and PB1(.,MSLB1DBBC9):
    IF ((NSTEP <= 0).OR.(LPC_FULL.AND.LPC_NESC)) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1DPHI9+JLEV-NFLSA)=PESGM*PRDPHI(JROF,JLEV)
          PB1(JROF,MSLB1DBBC9+JLEV-NFLSA)= &
           & PESGM*PDTS2*PDBBC(JROF)*PRDPHI(JROF,JLEV)
        ENDDO
      ENDDO
    ELSEIF (LSETTLS) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1DPHI9+JLEV-NFLSA)=(1.0_JPRB+PESGM)*PRDPHI(JROF,JLEV)&
           & -PGMV(JROF,JLEV,YT9%MDPHI)
          PB1(JROF,MSLB1DBBC9+JLEV-NFLSA)= &
           & (1.0_JPRB+PESGM)*PDTS2*PDBBC(JROF)*PRDPHI(JROF,JLEV) &
           & -PDTS2*PGMVS(JROF,YT9%MDBBC)*PGMV(JROF,JLEV,YT9%MDPHI)
        ENDDO
      ENDDO
    ELSE
      ! * remaining case: lsettls=false, lpc_nesc=false.
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1DPHI9+JLEV-NFLSA)=PESGM* &
           & (1.5_JPRB*PRDPHI(JROF,JLEV)-0.5_JPRB*PGMV(JROF,JLEV,YT9%MDPHI))
          PB1(JROF,MSLB1DBBC9+JLEV-NFLSA)=PESGM* &
           & (1.5_JPRB*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,JLEV))&
           & -0.5_JPRB*PDTS2*PGMVS(JROF,YT9%MDBBC)*PGMV(JROF,JLEV,YT9%MDPHI))
        ENDDO
      ENDDO
    ENDIF

    ! * Calculation of PB1(.,MSLB1GWS9):
    PB1(KST:KPROF,MSLB1GWS9)=PGWS(KST:KPROF)

    ! * Update t-dt arrays for next time step and corrector
    !   (PGMVS(.,YT9%MDBBC) and PGMV(.,.,YT9%MDPHI)).
    PGMVS(KST:KPROF,YT9%MDBBC)=PDBBC(KST:KPROF)
    PGMV(KST:KPROF,1:NFLEVG,YT9%MDPHI)=PRDPHI(KST:KPROF,1:NFLEVG)

  ELSE

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

    ! * Save PGMVS(.,YT9%MGWS) in PGWS:
    PGWS(KST:KPROF)=PGMVS(KST:KPROF,YT9%MGWS)

    ! * Calculation of "DPHIT1" and "DBBCT1":
    DO JROF=KST,KPROF
      PB2(JROF,MSLB2DPHI1)=PESGP*PRDPHI(JROF,NFLEVG)
      PB2(JROF,MSLB2DBBC1)=PESGP*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,NFLEVG))
    ENDDO

    ! * Calculation of PB1(.,MSLB1DPHI9) and PB1(.,MSLB1DBBC9):
    IF (.NOT.LLCTC) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KPROF
          PB1(JROF,MSLB1DPHI9+JLEV-NFLSA)=PESGM*PGMV(JROF,JLEV,YT9%MDPHI)
          PB1(JROF,MSLB1DBBC9+JLEV-NFLSA)= &
           & PESGM*PDTS2*PGMVS(JROF,YT9%MDBBC)*PGMV(JROF,JLEV,YT9%MDPHI)
        ENDDO
      ENDDO
    ENDIF

    ! * Calculation of PB1(.,MSLB1GWS9):
    IF (.NOT.LLCTC) PB1(KST:KPROF,MSLB1GWS9)=PGMVS(KST:KPROF,YT9%MGWS)

  ENDIF

ENDIF

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

!*       3.   3TLSL scheme.

IF (.NOT.LTWOTL) THEN

  ! * SL3TL:
  ! * Remark: the case LLCT is not yet coded.

  ! * Calculation of "DPHIT1" and "DBBCT1":
  DO JROF=KST,KPROF
    PB2(JROF,MSLB2DPHI1)=PESGP*PRDPHI(JROF,NFLEVG)
    PB2(JROF,MSLB2DBBC1)=PESGP*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,NFLEVG))
  ENDDO

  ! * Calculation of PB1(.,MSLB1DPHI9) and PB1(.,MSLB1DBBC9):
  IF (.NOT.LLCT) THEN
    DO JLEV=1,NFLEVG
      DO JROF=KST,KPROF
        PB1(JROF,MSLB1DPHI9+JLEV-NFLSA)=PESGM*PRDPHI(JROF,JLEV)
        PB1(JROF,MSLB1DBBC9+JLEV-NFLSA)= &
         & PESGM*(PDTS2*PDBBC(JROF)*PRDPHI(JROF,JLEV))
      ENDDO
    ENDDO
  ENDIF

  ! * Calculation of PB1(.,MSLB1GWS9):
  IF (.NOT.LLCT) PB1(KST:KPROF,MSLB1GWS9)=PGWS(KST:KPROF)

ENDIF

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

!*       4.   Final calculations.

! * Fill the jlev=kflev+1 and jlev=0 parts of PB1(.,MSLB1DPHI9)
!   and PB1(.,MSLB1DBBC9) with zeros
!   (as it is done for the other variables of "PB1" in routine LAVABO).
IF (.NOT.LLCTC) THEN
  PB1(KST:KPROF,MSLB1DPHI9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1DPHI9+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1DBBC9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1DBBC9+NFLEVG+1)=0.0_JPRB
ENDIF

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

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

