SUBROUTINE PPRHAD(KPROMA,KSTART,KPROF,KFLEV,KLEVP,KLOLEV,&
 & KLEVB,PRPRES,LDBELO,LDBLOW,PRHMAX,PRHMIN,&
 & PRXP,PRXPD,PQF,PTF,PRHPP,&
 & PRXP5,PRXPD5,PTF5,PQF5)  

!**** *PPRHAD* - POST-PROCESS RELATIVE HUMIDITY - ADJOINT.

!     PURPOSE.
!     --------
!           COMPUTES AND PERFORMS THE VERTICAL INTERPOLATION OF
!           RELATIVE HUMIDITY GIVEN MODEL CO-ORDINATE LEVEL.

!**   INTERFACE.
!     ----------
!        *CALL* *PPRHAD(KPROMA,KSTART,KPROF,KFLEV,KLEVP,KLOLEV,KLEVB,
!                     PRPRES,LDBELO,LDBLOW,PRXP,PRXPD,
!                     PQF,PTF,PRHPP,PRXP5,PRXPD5,PTF5,PQF5)

!        EXPLICIT ARGUMENTS
!        --------------------

!        KPROMA                    - HORIZONTAL DIMENSION.             (INPUT-C)
!        KSTART                    - START OF WORK.                    (INPUT-C)
!        KPROF                     - DEPTH OF WORK.                    (INPUT-C)
!        KFLEV                     - NUMBER OF INPUT PRESSURE LEVELS   (INPUT-C)
!        KLEVP                     - NUMBER OF OUTPUT PRESSURE LEVELS  (INPUT-C)
!        KLEVB(KPROMA,KLEVP,NPPM)  - INPUT LEVEL BELOW PRPRES          (INPUT-C)
!                                    (SEE PPFLEV)
!        PRPRES(KPROMA,KLEVP)      -  POST-PROCESSING LEVEL PRESSURES   (INPUT-C)
!        LDBELO(KPROMA,KLEVP)      - .TRUE. IF PRESSURE IS UNDER
!                                     LOWEST  MODEL LEVEL              (INPUT-C)
!        LDBLOW(KLEVP)             - FIRST LEVEL OF LDBELO CONTAINING
!                                    AT LEAST ONE .TRUE.               (INPUT-C)
!        PRHMAX               - MAXIMUM RELATIVE HUMIDITY              (INPUT-C)
!        PRHMIN               - MINIMUM RELATIVE HUMIDITY              (INPUT-C)
!        PRXP(KPROMA,0:KFLEV,NPPM) - HALF,FULL AND LN HALF,FULL LEVEL
!                                    PRESSURES (SEE PPINIT)            (INPUT)
!        PRXPD(KPROMA,0:KFLEV,NPPM) - 1./D(P) AND 1./D(LN(P))          (INPUT)
!        PQF(KPROMA,0:KFLEV)       - SPECIFIC HUMIDITY ON FULL INPUT
!                                    LEVELS.                           (INPUT)
!        PTF(KPROMA,0:KFLEV)       - TEMPERATURE ON FULL INPUT LEVELS  (INPUT)

!        PRHPP(KPROMA,KLEVP)       - POST-PROCESSED RELATIVE HUMIDITY  (OUTPUT)

!        IMPLICIT ARGUMENTS :  NONE.
!        --------------------

!     METHOD.
!     -------
!        SEE DOCUMENTATION

!     EXTERNALS.  GPRH - COMPUTE RELATIVE HUMIDITY ON MODEL LEVELS
!     ----------  PPINTPAD - LINEAR INTERPOLATION
!                 GPRHAD - COMPUTE RELATIVE HUMIDITY ON MODEL LEVELS

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

!     AUTHOR.
!     -------
!        MATS HAMRUD  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 91-01-07
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NPPM

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLOLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVB(KPROMA,KLEVP,NPPM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRPRES(KPROMA,KLEVP) 
LOGICAL           ,INTENT(IN)    :: LDBELO(KPROMA,KLEVP) 
LOGICAL           ,INTENT(IN)    :: LDBLOW(KLEVP) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRHMAX 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRHMIN 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRXP(KPROMA,0:KFLEV,NPPM) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRXPD(KPROMA,0:KFLEV,NPPM) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQF(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTF(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRHPP(KPROMA,KLEVP) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRXP5(KPROMA,0:KFLEV,NPPM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRXPD5(KPROMA,0:KFLEV,NPPM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTF5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQF5(KPROMA,0:KFLEV) 
REAL(KIND=JPRB) :: ZES(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZRHF(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZES5(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZRHF5(KPROMA,0:KFLEV)

INTEGER(KIND=JPIM) :: ISLCT, JL, JLEV, JLEVP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "gprh.intfb.h"
#include "gprhad.intfb.h"
#include "ppintpad.intfb.h"

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

!*       1.    POST-PROCESS RELATIVE HUMIDITY.
!              -------------------------------

IF (LHOOK) CALL DR_HOOK('PPRHAD',0,ZHOOK_HANDLE)
CALL GPRH(.TRUE.,KPROMA,KSTART,KPROF,KFLEV,PRHMAX,PRHMIN,&
 & PQF5(1,1),PTF5(1,1),&
 & PRXP5(1,1,2),ZES5(1,1),ZRHF5(1,1))  
DO JL=KSTART,KPROF
  ZRHF5(JL,0)=ZRHF5(JL,1)
ENDDO

!*       1.4   CONSTANT EXTRAPOLATION BELOW LOWEST MODEL LEVEL.

DO JLEV=0,KFLEV
  DO JL=KSTART,KPROF
    ZES(JL,JLEV)=0.0_JPRB
    ZRHF(JL,JLEV)=0.0_JPRB
  ENDDO
ENDDO

DO JLEVP=KLOLEV,KLEVP
  IF(LDBLOW(JLEVP)) THEN
    DO JL=KSTART,KPROF
      IF ( LDBELO(JL,JLEVP)) THEN
        ZRHF(JL,KFLEV)=ZRHF(JL,KFLEV)+PRHPP(JL,JLEVP)
        PRHPP(JL,JLEVP)=0.0_JPRB
      ENDIF
    ENDDO
  ENDIF
ENDDO

!*       1.3   INTERPOLATE RELATIVE HUMIDITY.

ISLCT=2
CALL PPINTPAD(KPROMA,KSTART,KPROF,KFLEV,KLEVP,KLOLEV,KLEVB,&
 & ISLCT,LDBELO,LDBLOW,PRPRES,PRXP,PRXPD,&
 & ZRHF,PRHPP,PRXP5,PRXPD5,ZRHF5)  

!*       1.2   PREPARE FOR EXTRAPOLATION ABOVE TOP

DO JL=KSTART,KPROF
  ZRHF(JL,1)=ZRHF(JL,1)+ZRHF(JL,0)
ENDDO

!*       1.1   COMPUTE RELATIVE HUMIDITY ON MODEL LEVELS.

CALL GPRHAD(.TRUE.,KPROMA,KSTART,KPROF,KFLEV,PRHMAX,PRHMIN,&
 & PQF(1,1),PTF(1,1),PRXP(1,1,2),ZES(1,1),&
 & ZRHF(1,1),ZES5(1,1),PRXP5(1,1,2),PQF5(1,1),PTF5(1,1))  

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

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

