SUBROUTINE GNHPREH( &
 ! --- INPUT -------------------------------------------
 & KPROMA,KFLEV,KST,KEND,PSPD,PREH,PDELP,PLNPR,PNHPPI, &
 ! --- OUTPUT ------------------------------------------
 & PNHPREH,PDELNHPRE &
 & )

! GNHPREH - Computation of the total pressure "pre" at half levels,
!           from the "pressure departure" prognostic variable.

! Purpose
! -------
!   Computes:
!    * the total pressure "pre" at half levels.
!    * the total pressure depth "Delta pre" at full levels.
!   The assumptions currently done are:
!    1/ LVERTFE=F: 
!       For NVDVAR=2: the pressure departure prognostic variable at half
!        levels is the average of those at full levels.
!       For NVDVAR=3: not yet coded. 
!    2/ LVERTFE=T: 
!       the total pressure "pre" at half levels is computed from the full
!       level total pressure depth "Delta pre" (first computed).
!    3/ The pressure departure "pre - prehyd" is zero at the top of the model.

! Interface
! ---------
!   INPUT:
!    KPROMA    - horizontal dimension.
!    KFLEV     - number of levels.
!    KST       - start of work.
!    KEND      - end of work.
!    PSPD      - pressure departure prognostic variable at full levels.
!    PREH      - hydrostatic pressure "prehyd" at half levels.
!    PDELP     - "delta prehyd" at full levels.
!    PLNPR     - delta = delta(log(prehyd)) at full levels.
!    PNHPPI    - ratio pre/prehyd at full levels.

!   OUTPUT:
!    PNHPREH   - total pressure "pre" at half levels.
!    PDELNHPRE - total pressure depth "Delta pre" at full levels.

! Externals
! ---------

! Method
! ------

! Reference
! ---------

! Author
! ------
!   K. YESSAD (MF/CNRM/GMAP), Dec 2004.

! Modifications
! -------------
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
! -----------------------------------------------------------------------------

USE PARKIND1 , ONLY : JPIM     ,JPRB
USE YOMCVER  , ONLY : LVERTFE  ,LVFE_DERIB,LVFE_DELNHPRE
USE YOMDYNA  , ONLY : NPDVAR   ,LRWSDLR
USE YOMDYN   , ONLY : RC_PD1   ,RCORPDH
USE YOMGEM   , ONLY : VFE_RDETAH
USE YOMHOOK  , ONLY : LHOOK    ,DR_HOOK

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPD(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PREH(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLNPR(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNHPPI(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNHPREH(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDELNHPRE(KPROMA,KFLEV) 

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

INTEGER(KIND=JPIM) :: JLEV, JROF

REAL(KIND=JPRB) :: ZIN(KPROMA,0:KFLEV+1) 
REAL(KIND=JPRB) :: ZDX(KPROMA,0:KFLEV+1) 
REAL(KIND=JPRB) :: ZSPDH
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "verder.intfb.h"

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

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

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

!*     1. FINITE DIFFERENCES VERTICAL DISCRETISATION
!      ---------------------------------------------

! Half level pressure "pre" is computed first; full level pressure depths
!  are then computed.

! * Computation of total pressure "pre" at half levels (PNHPREH):
!   Required for the time being as input for the AROME physics, but
!    in VFE discretisation, the use of this quantity is not recommanded
!    (in this case we use the FD discretisation, which does not take
!    account of the VFE calculation of PDELNHPRE, and we should notice
!    that the sum of PDELNHPRE*(delta eta) on a whole column does not
!    give exactly PNHPREH(surf)-PNHPREH(top)).

IF ( NPDVAR == 2 ) THEN

  ! * Top (the pressure departure "pre - prehyd" is assumed to be zero):
  PNHPREH(KST:KEND,0)=PREH(KST:KEND,0)

  ! * Half levels 1 to KFLEV-1:
  IF (LRWSDLR) THEN
    DO JLEV=1,KFLEV-1
      DO JROF=KST,KEND
        ZSPDH=0.5_JPRB*(PSPD(JROF,JLEV)+PSPD(JROF,JLEV+1))
        PNHPREH(JROF,JLEV)=EXP(ZSPDH-RC_PD1*RCORPDH(JLEV))*PREH(JROF,JLEV)
      ENDDO
    ENDDO
  ELSE
    DO JLEV=1,KFLEV-1
      DO JROF=KST,KEND
        ZSPDH=0.5_JPRB*(PSPD(JROF,JLEV)+PSPD(JROF,JLEV+1))
        PNHPREH(JROF,JLEV)=EXP(ZSPDH)*PREH(JROF,JLEV)
      ENDDO
    ENDDO
  ENDIF

  ! * Surface:
  !   The pressure departure variable is assumed to be constant
  !   under the full level l=nflevg.
  IF (LRWSDLR) THEN
    DO JROF=KST,KEND
      ZSPDH=PSPD(JROF,KFLEV)
      PNHPREH(JROF,KFLEV)=EXP(ZSPDH-RC_PD1*RCORPDH(KFLEV))*PREH(JROF,KFLEV)
    ENDDO
  ELSE
    DO JROF=KST,KEND
      ZSPDH=PSPD(JROF,KFLEV)
      PNHPREH(JROF,KFLEV)=EXP(ZSPDH)*PREH(JROF,KFLEV)
    ENDDO
  ENDIF

ELSEIF ( NPDVAR == 3 .OR. NPDVAR == 4 ) THEN

  ! * Top (the pressure departure "pre - prehyd" is assumed to be zero):
  PNHPREH(KST:KEND,0)=PREH(KST:KEND,0)

  ! * Half levels 1 to KFLEV-1 (treated like for NPDVAR=2):
  DO JLEV=1,KFLEV-1
    DO JROF=KST,KEND
      ZSPDH=0.5_JPRB*(LOG(PNHPPI(JROF,JLEV))+LOG(PNHPPI(JROF,JLEV+1)))
      PNHPREH(JROF,JLEV)=EXP(ZSPDH)*PREH(JROF,JLEV)
    ENDDO
  ENDDO

  ! * Surface:
  !   log(pre/prehyd) is assumed to be constant under the full level l=nflevg.
  DO JROF=KST,KEND
    ZSPDH=LOG(PNHPPI(JROF,KFLEV))
    PNHPREH(JROF,KFLEV)=EXP(ZSPDH)*PREH(JROF,KFLEV)
  ENDDO

ENDIF

! * Computation of the total pressure depth "Delta pre" at full levels.
IF (.NOT.(LVERTFE.AND.LVFE_DELNHPRE)) THEN

  DO JLEV=1,KFLEV
    DO JROF=KST,KEND
      PDELNHPRE(JROF,JLEV)=PNHPREH(JROF,JLEV)-PNHPREH(JROF,JLEV-1)
    ENDDO
  ENDDO

ENDIF

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

!*     1. VFE VERTICAL DISCRETISATION
!      ------------------------------

! Full level pressure depths "Delta pre" are computed first;
!  half level pressure "pre" is then computed, starting from the
!  upper boundary condition "pre - prehyd=0".

IF (LVERTFE.AND.LVFE_DELNHPRE) THEN

  ! * Computation of the total pressure depth "Delta pre" at full levels.
  !   Calculation is done using the identity:
  !    d pre / d prehyd = pre/prehyd + d ((pre-prehyd)/prehyd)/ d (log(prehyd))
  !   then:
  !    Delta pre = Delta prehyd * [d pre / d prehyd]

  DO JLEV=1,KFLEV
    DO JROF=KST,KEND
      ZIN(JROF,JLEV)=PNHPPI(JROF,JLEV)-1.0_JPRB
    ENDDO
  ENDDO
  IF (LVFE_DERIB) THEN
    ! * Top: "[pre/prehyd]_top-1=0":
    ZIN(KST:KEND,0)=0.0_JPRB
    ! * Bottom: "[pre/prehyd]_surf=[pre/prehyd]_(l=L)":
    ZIN(KST:KEND,KFLEV+1)=ZIN(KST:KEND,KFLEV)
    CALL VERDER(KPROMA,KST,KEND,KFLEV+2,ZIN(1,0),ZDX(1,0),.TRUE.)
  ELSE
    CALL VERDER(KPROMA,KST,KEND,KFLEV,ZIN(1,1),ZDX(1,1))
  ENDIF
  DO JLEV=1,KFLEV
    DO JROF=KST,KEND
      PDELNHPRE(JROF,JLEV)=PDELP(JROF,JLEV)*(PNHPPI(JROF,JLEV) &
       & +ZDX(JROF,JLEV)/(PLNPR(JROF,JLEV)*VFE_RDETAH(JLEV)))
    ENDDO
  ENDDO

ENDIF

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

IF (LHOOK) CALL DR_HOOK('GNHPREH',1,ZHOOK_HANDLE)

END SUBROUTINE GNHPREH

