SUBROUTINE GPGEO(KPROMA,KSTART,KPROF,KFLEV,PHI,PHIF,PT,PR,PLNPR,PALPH)

!**** *GPGEO* - Computes half and full level geopotential height "gz".

!     Purpose.
!     --------

!      Computes half and full level geopotential height "gz".

!      Laplace relation writes:

!       d (gz)/d prehyd = - RT/pre = - (RT/prehyd) * (prehyd/pre)

!      where:
!       - "gz" is the geopotential height.
!       - "prehyd" is the hydrostatic pressure.
!       - "pre" is the total pressure including non-hydrostatic effects.
!       - "R" is the air constant (including moisture effects).
!       - "T" is the temperature.

!      It is important to note that when relaxing the thin layer hypothesis
!      the geopotential height "gz" is different from the total geopotential
!      "Phi" (used in the RHS of the horizontal wind equation), except
!      at the surface where Phi_s can still be defined by Phi_s = g z[surf].

!      Integrating the Laplace equations yields the following discretisation
!      for "gz".

!      * "gz" at interlayer "lbar":

!        g z[lbar] = g z[surf]
!        + sum[k=L to l+1] (prehyd/pre)[k] R[k] T[k] delta[k]

!      * "gz" at layer "l":

!        g z[l] = g z[lbar] + (prehyd/pre)[l] R[l] T[l] alpha[l]

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

!        Explicit arguments :
!        --------------------
!          KPROMA : horizontal dimensioning                          (input)
!          KSTART : start of work                                    (input)
!          KPROF  : depth of work                                    (input)
!          KFLEV  : number of levels                                 (input)
!          PHI    : geopotential height "gz" at interlayers          (output)
!          PHIF   : geopotential height "gz" at layers               (output)
!          PT     : temperature at layers                            (input)
!          PR     : "R" at layers for hydrostatic model              (input)
!                    "(prehyd/pre) R" at layers for NH model
!          PLNPR  : term "delta" on layers                           (input)
!                   (= logarithm of ratio of pressure if "ndlnpr=0")
!          PALPH  : term "alpha" on layers                           (input)

!        Implicit arguments :    None.
!        --------------------

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

!     Externals.   None.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

!     Modifications.
!     --------------
!        Original : 88-02-04
!        K. YESSAD (AUGUST 2000):
!         - correct false comments (especially for NH model and option ndlnpr=1)
!         - relaxation of thin layer hypothesis: no active code changed but
!           correct ambiguous comments and terminology (replace "geopotential"
!           by "geopotential height").
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMGEM   , ONLY : VFE_RDETAH
USE YOMCVER  , ONLY : LVERTFE

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PHI(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PHIF(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PR(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLNPR(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALPH(KPROMA,KFLEV) 

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

REAL(KIND=JPRB) :: ZOROG(KPROMA), ZPHI(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZOUT(KPROMA,0:KFLEV)

INTEGER(KIND=JPIM) :: JLEV, JLON
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "verint.intfb.h"

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

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

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

!*       1.    COMPUTES HALF AND FULL LEVEL GEOPOTENTIAL HEIGHT.
!              -------------------------------------------------

IF(LVERTFE) THEN
  DO JLON=KSTART,KPROF
    ZOROG(JLON)=PHI(JLON,KFLEV)
  ENDDO
  DO JLEV=1,KFLEV
    DO JLON=KSTART,KPROF
      ZPHI(JLON,JLEV)=-PR(JLON,JLEV)*PT(JLON,JLEV)*&
       & PLNPR(JLON,JLEV)*VFE_RDETAH(JLEV)  
    ENDDO
  ENDDO
  CALL VERINT(KPROMA,KSTART,KPROF,KFLEV,ZPHI,ZOUT,1)
  DO JLEV=1,KFLEV
    DO JLON=KSTART,KPROF
      PHIF(JLON,JLEV) =ZOUT(JLON,JLEV-1)+ZOROG(JLON)
    ENDDO
  ENDDO
  DO JLEV=KFLEV,1,-1
    DO JLON=KSTART,KPROF
      PHI(JLON,JLEV-1)=PHI(JLON,JLEV)&
       & +PR(JLON,JLEV)*PT(JLON,JLEV)*PLNPR(JLON,JLEV)  
    ENDDO
  ENDDO
ELSE
  DO JLEV=KFLEV,1,-1
    DO JLON=KSTART,KPROF
      PHI(JLON,JLEV-1)=PHI(JLON,JLEV)&
       & +PR(JLON,JLEV)*PT(JLON,JLEV)*PLNPR(JLON,JLEV)  

      PHIF(JLON,JLEV) =PHI(JLON,JLEV)&
       & +PALPH(JLON,JLEV)*PR(JLON,JLEV)*PT(JLON,JLEV)  
    ENDDO
  ENDDO
ENDIF

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

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

