SUBROUTINE LAVABO(KST,KPROF,LD2TLFF1,PB1)

!**** *LAVABO*   Semi-Lagrangian scheme.
!                VAlues at the BOundaries: Upper and lower extrapolations.

!     Purpose.
!     --------
!          This subroutine do upper and lower extrapolations of
!          semi-lagrangian quantities for extra-levels JLEV=0
!          and JLEV=NFLEVG+1.
!          Abbreviation "vwv" stands for "vertical wind variable".

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

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

!        INPUT:
!          KST      : first element of work.
!          KPROF    : depth of work.
!          LD2TLFF1 : .T./.F.: Refined treatement of (2*Omega Vec r) at
!                    the origin point when there is t-dt (or t in SL2TL)
!                    physics / Other cases.

!        INPUT/OUTPUT:
!          PB1      : "SLBUF1" buffer for interpolations.

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

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

!     Externals.
!     ----------
!         None
!         Called by LACDYN.

!     Reference.
!     ----------
!         Arpege documentation Part I  Chapter 3
!                              Part II Chapter 5 and 6
!         Documentation about semi-Lagrangian scheme.

!     Author.
!     -------
!        K. YESSAD (METEO FRANCE/CNRM/GMAP) after routine
!          CPLGDY1 coded by Maurice IMBARD.

! Modifications
! -------------
!   Original : APRIL 1992.
!   Modified by K. YESSAD: NOVEMBER 1992.
!   Modified by K. YESSAD: DECEMBER 1992
!            (non interpolating vertical scheme).
!   Modified 93-12-14 by K. YESSAD: interpolations at medium point for
!                                 semi-lagrangian temperature equation.
!   Modified 93-12-21 by C. Temperton: interpolations at medium point for
!                                 semi-lagrangian moisture equation.
!   Modified 94-03-23 by M. Hortal: interpolations at medium point for
!                                 semi-lagrangian momentum equations.
!   Modified in AUGUST 1995 by K. YESSAD:
!      - Accurate formulation for two-time level SL scheme.
!      - Second order uncentering for two-time level SL scheme.
!      - DO loops recoded according to F90 norms.
!   Modified 96-05-24 by R. Bubnova & M. Janousek: non-hydrostatic
!   Modified 96-03 by A. Untch : grid-point prognostic ozone (EC)
!   K. YESSAD 98-05-06: new quantities PURL9 and PVRL9 if LD2TLFF1=.T..
!   K. YESSAD (July 1998): cleaning
!    - F90 writing (for more efficiency and readability).
!    - removal of options requiring an anterior point.
!   Modified 98-05-26 by E. Holm : Order of ozone changed in call
!                                 and inside routine.
!   Modified 99-03-31 by P. SMOLIKOVA: option LRDAORG (double averaging).
!   Modified 99-08-16 by C. Temperton: removed KVINTSL (redundant) 
!   Modified 00-02-24 by M. Szczech: ozone - LDGPO3 -> LDADVO3.
!   Modified 01-06-22 by K. YESSAD:
!    - relaxation of thin layer hypothesis.
!   Modified 01-08-30 by K. YESSAD: pruning and some other cleanings.
!   R. El Khatib : 01-08-07 Pruning options
!   03 - 2002 J.Vivoda PC schemes for NH dynamics (LPC_XXXX keys)
!   Modified 08-2002 C. Smith : use "w" as prognostic variable in the
!    semi-lag advection of vertical divergence in the NH model.
!   Modified 2003-03-16 M.Hamrud/M.Hortal - Revised data flow (GFL)
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   10-Jun-2004 J. Masek   NH cleaning (LPC_NOTR)
!   Modified 2004-Nov by F. Vana: update of YGFLC%CSLINT
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   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_OLD  ,LNHDYN   ,LVERCOR
USE YOMDIM   , ONLY : NPROMA   ,NFLEVG   ,NFLSA    ,NFLEN
USE YOMDYNA  , ONLY : LRWSDLR  ,LRWSDLW  ,NVDVAR
USE YOMDYN   , ONLY : LSLINLC1 ,LSLINLC2 ,LSLINL   ,NVLAG    ,NWLAG    , &
 & NTLAG    ,NSVDLAG  ,NSPDLAG  ,LADVFW
USE YOMSC2   , ONLY : NFLDSLB1
USE YOMSLPHY , ONLY : LSLPHY
USE PTRSLB1  , ONLY : MSLB1U9  ,MSLB1V9  ,MSLB1T9  ,MSLB1GFL9 ,&
 & MSLB1PD9 ,MSLB1VD9 ,MSLB1Y9  ,MSLB1UR0 ,MSLB1VR0 ,MSLB1WR0 ,&
 & MSLB1UR9 ,MSLB1VR9 ,MSLB1U0  ,MSLB1V0  ,MSLB1T0  ,MSLB1PD0 ,MSLB1VD0 , &
 & MSLB1C9  ,MSLB1C9_SI,MSLB1UP9 ,MSLB1VP9 ,MSLB1TP9 ,MSLB1GFLP9,&
 & MSLB1RSSA9,MSLB1NHX9,&
 & MSLB1U9_SI,MSLB1V9_SI,MSLB1T9_SI,MSLB1PD9_SI,MSLB1VD9_SI
USE YOM_YGFL , ONLY : YGFL     ,YGFLC

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
LOGICAL           ,INTENT(IN)    :: LD2TLFF1 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB1(NPROMA,NFLDSLB1)

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

INTEGER(KIND=JPIM) :: JGFL, ISLB1GFL9, ISLB1GFLP9
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

!*       1.    UPPER AND LOWER LATERAL BOUNDARIES CONDITIONS SET TO 0.
!              --------------------------------------------------------

! * P[X]RL0 and P[X]RL9 for [X]=U,V,W:
PB1(KST:KPROF,MSLB1UR0)=0.0_JPRB
PB1(KST:KPROF,MSLB1UR0+NFLEVG+1)=0.0_JPRB
PB1(KST:KPROF,MSLB1VR0)=0.0_JPRB
PB1(KST:KPROF,MSLB1VR0+NFLEVG+1)=0.0_JPRB
PB1(KST:KPROF,MSLB1WR0)=0.0_JPRB
PB1(KST:KPROF,MSLB1WR0+NFLEVG+1)=0.0_JPRB
IF (LD2TLFF1) THEN
  PB1(KST:KPROF,MSLB1UR9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1UR9+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1VR9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1VR9+NFLEVG+1)=0.0_JPRB
ENDIF

! * P[X]L0 and P[X]L9 for [X]=U,V,T,advected GFL:
PB1(KST:KPROF,MSLB1U9)=0.0_JPRB
PB1(KST:KPROF,MSLB1U9+NFLEVG+1)=0.0_JPRB
PB1(KST:KPROF,MSLB1V9)=0.0_JPRB
PB1(KST:KPROF,MSLB1V9+NFLEVG+1)=0.0_JPRB
PB1(KST:KPROF,MSLB1T9)=0.0_JPRB
PB1(KST:KPROF,MSLB1T9+NFLEVG+1)=0.0_JPRB
DO JGFL=1,YGFL%NUMFLDS
  IF( (YGFLC(JGFL)%CSLINT /= 'LAITVSPCQM  ').AND.YGFLC(JGFL)%LADV ) THEN
    ISLB1GFL9=MSLB1GFL9+(YGFLC(JGFL)%MP_SL1-1)*(NFLEN-NFLSA+1)
    PB1(KST:KPROF,ISLB1GFL9)=0.0_JPRB
    PB1(KST:KPROF,ISLB1GFL9+NFLEVG+1)=0.0_JPRB
  ENDIF
ENDDO

IF (NWLAG /= 2) THEN
  PB1(KST:KPROF,MSLB1U0)=0.0_JPRB
  PB1(KST:KPROF,MSLB1U0+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1V0)=0.0_JPRB
  PB1(KST:KPROF,MSLB1V0+NFLEVG+1)=0.0_JPRB
ENDIF
IF (NTLAG /= 2) THEN
  PB1(KST:KPROF,MSLB1T0)=0.0_JPRB
  PB1(KST:KPROF,MSLB1T0+NFLEVG+1)=0.0_JPRB
ENDIF

! * P[X]L0 and P[X]L9 for [X]=SPD,VWV,NHX:
IF (LNHDYN) THEN
  PB1(KST:KPROF,MSLB1VD9) = 0.0_JPRB
  PB1(KST:KPROF,MSLB1VD9+NFLEVG+1) = 0.0_JPRB
  PB1(KST:KPROF,MSLB1PD9) = 0.0_JPRB
  PB1(KST:KPROF,MSLB1PD9+NFLEVG+1) = 0.0_JPRB
  IF (NVDVAR == 4) THEN
    PB1(KST:KPROF,MSLB1NHX9) = 0.0_JPRB
    PB1(KST:KPROF,MSLB1NHX9+NFLEVG+1) = 0.0_JPRB
  ENDIF
  IF (NSVDLAG /= 2) THEN
    PB1(KST:KPROF,MSLB1VD0) = 0.0_JPRB
    PB1(KST:KPROF,MSLB1VD0+NFLEVG+1) = 0.0_JPRB
  ENDIF
  IF (NSPDLAG /= 2) THEN
    PB1(KST:KPROF,MSLB1PD0) = 0.0_JPRB
    PB1(KST:KPROF,MSLB1PD0+NFLEVG+1) = 0.0_JPRB
  ENDIF
ENDIF

! * P[X]L0 and P[X]L9 for [X]=C (continuity equation):
IF (NVLAG == 2.OR.NVLAG == 3) THEN
  PB1(KST:KPROF,MSLB1C9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1C9+NFLEVG+1)=0.0_JPRB
  IF (LSLINLC1.OR.LSLINLC2) PB1(KST:KPROF,MSLB1C9_SI)=0.0_JPRB
  IF (LSLINLC1.OR.LSLINLC2) PB1(KST:KPROF,MSLB1C9_SI+NFLEVG+1)=0.0_JPRB
ENDIF

! * PCY9:
IF(LNHDYN.AND.LPC_OLD)THEN
  PB1(KST:KPROF,MSLB1Y9) = 0.0_JPRB
  PB1(KST:KPROF,MSLB1Y9+NFLEVG+1) = 0.0_JPRB
ENDIF

! * P[X]LP9 for [X]=U,V,T,advected GFL:
IF( LSLPHY ) THEN
  ! ky: it seems to miss the zeroing of the bottom condition;
  !     is it JGFL or YGFLC(JGFL)%MP_SL1 in ISLB1GFLP9?
  PB1(KST:KPROF,MSLB1UP9+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1VP9+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1TP9+NFLEVG+1)=0.0_JPRB
  DO JGFL=1,YGFL%NUMFLDS
    IF(YGFLC(JGFL)%LADV) THEN
      ISLB1GFLP9=MSLB1GFLP9+(JGFL-1)*(NFLEN-NFLSA+1)
      PB1(KST:KPROF,ISLB1GFLP9+NFLEVG+1)=0.0_JPRB
    ENDIF
  ENDDO
ENDIF

! * P[X]L9 for [X]=VCRSSA:
IF ((LVERCOR.OR.(LRWSDLW.AND.LRWSDLR)).AND.LADVFW) THEN
  ! * need to interpolate "rs/a" (instant t-dt or t according to "ltwotl") at O.
  PB1(KST:KPROF,MSLB1RSSA9)=0.0_JPRB
  PB1(KST:KPROF,MSLB1RSSA9+NFLEVG+1)=0.0_JPRB
ENDIF

! * P[X]L9_SI (GMV equations):
IF (LSLINL) THEN
  PB1(KST:KPROF,MSLB1U9_SI)=0.0_JPRB
  PB1(KST:KPROF,MSLB1U9_SI+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1V9_SI)=0.0_JPRB
  PB1(KST:KPROF,MSLB1V9_SI+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1T9_SI)=0.0_JPRB
  PB1(KST:KPROF,MSLB1T9_SI+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1PD9_SI)=0.0_JPRB
  PB1(KST:KPROF,MSLB1PD9_SI+NFLEVG+1)=0.0_JPRB
  PB1(KST:KPROF,MSLB1VD9_SI)=0.0_JPRB
  PB1(KST:KPROF,MSLB1VD9_SI+NFLEVG+1)=0.0_JPRB
ENDIF 

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

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

