SUBROUTINE PREGPRH (KNUMB,PT,PQ,PSP,PQP,PRSH,PRSF,PES,PQRH,KLTROP)

!      PREGPRH - Calculation of relative humidity and full level
!                pressure, as well as tropopause level

!     Arguments :
!     -----------
!     Input:
!       KNUMB  - number of elements for which computations
!                are performed (used in message passing version)
!       PT     - temperature
!       PQ     - specific humidity
!       PSP    - ln surface pressure
!     Output:
!       PQP    - positive specific humidity
!       PRSH   - pressure on half model levels
!       PRSF   - pressure on full model levels
!       PES    - quasi saturation water vapour pressure
!       PQRH   - quasi relative humidity
!       KLTROP - tropopause level

!     Author:  ELIAS HOLM    *ECMWF* (modified from SUJBWAVGEN)
!     -------

!     Modifications:
!     --------------
!     ORIGINAL     06-07-10   Used by JBCHVAR* change of control variable.
!     E. Holm      07-12-14   Include tropopause calculation and
!                             modify relative humidity in stratosphere
!     ------------------------------------------------------------------

USE PARKIND1   , ONLY : JPIM     ,JPRB
USE YOMHOOK    , ONLY : LHOOK    ,DR_HOOK
USE YOMDIM     , ONLY : NFLEVG   ,NPROMA
USE YOMGEM     , ONLY : VBH      ,VAH
USE YOMJG      , ONLY : LJB_SUPSAT
USE YOMCST     , ONLY : RETV
USE YOMJBCHVAR , ONLY : RQS, RQT, RQTBAL, RQLMIN, RQLMAX,&
 & RQLSTD, RQHMIN, RQHMAX, RQHSTD, RO3MIN, RO3STD
IMPLICIT NONE
INTEGER(KIND=JPIM),INTENT(IN) :: KNUMB
INTEGER(KIND=JPIM),INTENT(OUT):: KLTROP(NPROMA)
REAL(KIND=JPRB), INTENT(IN)   :: PT(NPROMA,NFLEVG), PQ(NPROMA,NFLEVG), PSP(NPROMA)
REAL(KIND=JPRB), INTENT(OUT)  :: PQP(NPROMA,NFLEVG), PES(NPROMA,NFLEVG),&
                               & PQRH(NPROMA,NFLEVG), PRSH(NPROMA,0:NFLEVG),&
                               & PRSF(NPROMA,NFLEVG)

INTEGER(KIND=JPIM) :: IST, IEND, JROF, JLEV, JLEVM

REAL(KIND=JPRB) :: ZHOOK_HANDLE, ZQSSTR, ZQSTRO, ZQSMIX, ZRHMAX

#include "gppre.intfb.h"
#include "troplev.intfb.h"
#include "qneglim.intfb.h"
#include "fjbchvar.h"
#include "gprh.intfb.h"
!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('PREGPRH',0,ZHOOK_HANDLE)
IST=1
IEND=KNUMB

!              PS

DO JROF=1,IEND
  PRSH(JROF,NFLEVG)= EXP(PSP(JROF))
ENDDO

!              P(ETA)

CALL GPPRE(NPROMA,IST,IEND,NFLEVG,VAH,VBH,PRSH,PRSF)

!              TROPOPAUSE

CALL TROPLEV(NPROMA,IST,IEND,NFLEVG,PT,PQ,PRSF,KLTROP) 

!              RELATIVE HUMIDITY

IF (LJB_SUPSAT) THEN
  ZRHMAX = 2.0_JPRB
ELSE
  ZRHMAX = 1.0_JPRB
ENDIF
PQP(:,:)=PQ(:,:)
CALL QNEGLIM (PQP,1,NPROMA,1,NFLEVG,IST,IEND,1,NFLEVG)
CALL GPRH(.FALSE.,NPROMA,IST,IEND,NFLEVG,ZRHMAX,0.0_JPRB,&
 & PQP,PT,PRSF,PES,PQRH)

!              MODIFY RH IN STRATOSPHERE ( --> q/q_sref, q_sref=4.0E-6 )

IF (LJB_SUPSAT) THEN
  ZQSSTR=4.0E-6_JPRB
  DO JROF=1,IEND
    DO JLEV=1,NFLEVG
      JLEVM = MAX(JLEV,KLTROP(JROF))
      ZQSTRO = PES(JROF,JLEVM)/(PRSF(JROF,JLEVM)*(1.0_JPRB+RETV)-PES(JROF,JLEVM)*RETV)
      ZQSMIX= ZQSTRO+FQT(KLTROP(JROF)-JLEV)*(ZQSSTR-ZQSTRO)
      PQRH(JROF,JLEV) = PQP(JROF,JLEV)/ZQSMIX
      PES(JROF,JLEV) = ZQSMIX*PRSF(JROF,JLEV)*(1.0_JPRB+RETV)/(1.0_JPRB+RETV*ZQSMIX)
    ENDDO
  ENDDO
ENDIF

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

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