SUBROUTINE PPPMERAD(KPROMA,KSTART,KPROF,PRPRESS,POROG,PTSTAR,PT0,&
 & PMSLPPP,&
 & PRPRESS5,PTSTAR5,PT05)  

!**** *PPPMERAD* - POST-PROCESS MSL PRESSURE.

!     PURPOSE.
!     --------
!           COMPUTES MSL PRESSURE.

!**   INTERFACE.
!     ----------
!        *CALL* *PPPMERAD(KPROMA,KSTART,KPROF,PRPRESS,POROG,PTSTAR,PT0
!                                      ,PMSLPPP
!                                      ,PRPRESS5,PTSTAR5,PT05)

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

!        KPROMA                    - HORIZONTAL DIMENSION.             (INPUT-C)
!        KSTART                    - START OF WORK.                    (INPUT)
!        KPROF                     - DEPTH OF WORK.                    (INPUT-C)
!        PRPRESS(KPROMA)           - SURFACE PRESSURE                  (INPUT)
!        POROG(KPROMA)             - MODEL OROGRAPHY.                  (INPUT-C)
!        PTSTAR(KPROMA)            - SURFACE TEMPERATURE               (INPUT)
!        PT0(KPROMA)               - STANDARD SURFACE TEMPERATURE      (INPUT)

!        PMSLPPP(KPROMA)           - POST-PROCESSED MSL PRESSURE       (OUTPUT)

!        PRPRESS5(KPROMA)            - SURFACE PRESSURE                 (INPUT-T)
!        PTSTAR5(KPROMA)            - SURFACE TEMPERATURE              (INPUT-T)
!        PT05(KPROMA)               - STANDARD SURFACE TEMPERATURE     (INPUT-T)

!        IMPLICIT ARGUMENTS :  CONSTANTS FROM YOMCST,YOMGEM,YOMSTA.
!        --------------------

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

!     EXTERNALS.  NONE
!     ----------

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

!     AUTHOR.
!     -------
!        ERIK ANDERSSON, MATS HAMRUD AND PHILIPPE COURTIER  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 91-10-08
!     E. A-son, J-F Geleyn 920409 Mod. T*, T0 and alpha below surface.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RD

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRPRESS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTSTAR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMSLPPP(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRPRESS5(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTAR5(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT05(KPROMA) 
REAL(KIND=JPRB) :: ZTSTAR(KPROMA),  ZALPHA(KPROMA)
REAL(KIND=JPRB) :: ZTSTAR5(KPROMA), ZALPHA5(KPROMA)

INTEGER(KIND=JPIM) :: JL

REAL(KIND=JPRB) :: ZOROG, ZT0, ZT05, ZTX, ZTY, ZX, ZX5, ZY, ZY2, ZY25, ZY5
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    POST-PROCESS MSL PRESSURE.
!              --------------------------

IF (LHOOK) CALL DR_HOOK('PPPMERAD',0,ZHOOK_HANDLE)
DO JL=KSTART,KPROF
  ZALPHA(JL)=0.0_JPRB
  ZTSTAR(JL)=0.0_JPRB
ENDDO

!*       1.0   COMPUTATION OF MODIFIED ALPHA AND TSTAR.
!*             FIVE ARRAYS

ZTX=290.5_JPRB
ZTY=255.0_JPRB
DO JL=KSTART,KPROF

  IF(PTSTAR5(JL) < ZTY) THEN
    ZTSTAR5(JL)=0.5_JPRB*(ZTY+PTSTAR5(JL))
  ELSEIF(PTSTAR5(JL) < ZTX) THEN
    ZTSTAR5(JL)=PTSTAR5(JL)
  ELSE
    ZTSTAR5(JL)=0.5_JPRB*(ZTX+PTSTAR5(JL))
  ENDIF

  IF(ZTX > ZTSTAR5(JL) .AND. PT05(JL) > ZTX) THEN
    ZT05=ZTX
  ELSEIF(ZTX <= ZTSTAR5(JL) .AND. PT05(JL) > ZTSTAR5(JL)) THEN
    ZT05=ZTSTAR5(JL)
  ELSE
    ZT05=PT05(JL)
  ENDIF

  ZOROG=SIGN(MAX(1.0_JPRB,ABS(POROG(JL))),POROG(JL))
  ZALPHA5(JL)=RD*(ZT05-ZTSTAR5(JL))/ZOROG
ENDDO

!*       1.2   COMPUTATION OF MSL PRESSURE.

DO JL=KSTART,KPROF
  ZX=0.0_JPRB
  ZY=0.0_JPRB
  ZY2=0.0_JPRB
  IF (ABS(POROG(JL)) >= 0.001_JPRB) THEN
    ZX5=POROG(JL)/(RD*ZTSTAR5(JL))
    ZY5=ZALPHA5(JL)*ZX5
    ZY25=ZY5*ZY5

    PRPRESS(JL)=PRPRESS(JL)+EXP(ZX5*(1.0_JPRB-0.5_JPRB*ZY5+1.0_JPRB/3._JPRB*ZY25))&
     & *PMSLPPP(JL)  
    ZX=ZX+EXP(ZX5*(1.0_JPRB-0.5_JPRB*ZY5+1.0_JPRB/3._JPRB*ZY25))&
     & *PRPRESS5(JL)*(1.0_JPRB-0.5_JPRB*ZY5+1.0_JPRB/3._JPRB*ZY25)*PMSLPPP(JL)  
    ZY=ZY+EXP(ZX5*(1.0_JPRB-0.5_JPRB*ZY5+1.0_JPRB/3._JPRB*ZY25))&
     & *PRPRESS5(JL)*(-0.5_JPRB*ZX5)*PMSLPPP(JL)  
    ZY2=ZY2+EXP(ZX5*(1.0_JPRB-0.5_JPRB*ZY5+1.0_JPRB/3._JPRB*ZY25))&
     & *PRPRESS5(JL)*(1.0_JPRB/3._JPRB*ZX5)*PMSLPPP(JL)  
    PMSLPPP(JL)=0.0_JPRB
    ZY=ZY+2.0_JPRB*ZY5*ZY2
    ZX=ZX+ZALPHA5(JL)*ZY
    ZALPHA(JL)=ZALPHA(JL)+ZX5*ZY
    ZTSTAR(JL)=ZTSTAR(JL)-POROG(JL)/(RD*ZTSTAR5(JL)*ZTSTAR5(JL))*ZX
  ELSE
    PRPRESS(JL)=PRPRESS(JL)+PMSLPPP(JL)
    PMSLPPP(JL)=0.0_JPRB
  ENDIF
ENDDO

!*    1.1   COMPUTATION OF MODIFIED ALPHA AND TSTAR.

DO JL=KSTART,KPROF
  ZT0=0.0_JPRB

  ZOROG=SIGN(MAX(1.0_JPRB,ABS(POROG(JL))),POROG(JL))
  ZT0=ZT0+(RD/ZOROG)*ZALPHA(JL)
  ZTSTAR(JL)=ZTSTAR(JL)-(RD/ZOROG)*ZALPHA(JL)

  IF(ZTX > ZTSTAR5(JL) .AND. PT05(JL) > ZTX) THEN
    ZT0=0.0_JPRB
  ELSEIF(ZTX <= ZTSTAR5(JL) .AND. PT05(JL) > ZTSTAR5(JL)) THEN
    ZTSTAR(JL)=ZTSTAR(JL)+ZT0
  ELSE
    PT0(JL)=PT0(JL)+ZT0
  ENDIF

  IF(PTSTAR5(JL) < ZTY) THEN
    PTSTAR(JL)=PTSTAR(JL)+0.5_JPRB*ZTSTAR(JL)
  ELSEIF(PTSTAR5(JL) < ZTX) THEN
    PTSTAR(JL)=PTSTAR(JL)+ZTSTAR(JL)
  ELSE
    PTSTAR(JL)=PTSTAR(JL)+0.5_JPRB*ZTSTAR(JL)
  ENDIF

ENDDO

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

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