SUBROUTINE PPSTAAD(CDATM,KPROMA,KSTART,KPROF,KLEVP,KLOLEV,&
 & PRPRES,PLNPRES,PSTT,PSTFI,&
 & PRPRES5)  

!**** *PPSTAAD*  - Integrate the STANDARD ATMOSPHERE - ADJOINT

!     Purpose.
!     --------
!           Integrate STANDARD ATMOSPHERE for geopotental.

!**   Interface.
!     ----------
!        *CALL* *PPSTAAD(KPROMA,KSTART,KPROF,KLEVP,KLOLEV
!                       ,PRPRES,PLNPRES,PSTT,PSTFI
!                       ,PRPRES5)

!        Explicit arguments :
!        --------------------
!        KPROMA                    - HORIZONTAL DIMENSION.             (INPUT-C)
!        KSTART                    - START OF WORK.                    (INPUT-C)
!        KPROF                     - DEPTH OF WORK.                    (INPUT-C)
!        KLEVP                     - NUMBER OF INPUT PRESSURE LEVELS   (INPUT-C)
!        KLOLEV                    - BEGINING FOR THE INTERPOLATION    (INPUT-C)
!        PRPRES(KPROMA,KLEVP)      - LIST OF PRESSURES                 (INPUT)
!        PLNPRES(KPROMA,KLEVP)     - LOG(PRPRES)                      (INPUT)
!        PSTT(KPROMA,KLEVP)        - ICAO TEMPERATURE                  (OUTPUT)
!        PSTFI(KPROMA,KLEVP)       - ICAO GEOPOTENTIAL                 (OUTPUT)

!        PRPRES5(KPROMA,KLEVP)     - LIST OF PRESSURES                 (INPUT-T)

!        Implicit arguments :
!        --------------------
!        Common YOMSTA

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

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

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

!     Author.
!     -------
!        Erik Andersson ECMWF

!     Modifications.
!     --------------
!        Original : 82-04-10
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RG       ,RD
USE YOMGEM   , ONLY : VP00
USE YOMSTA   , ONLY : RZTROP   ,RZSTRA   ,RZSTR2   ,RZSTPO   ,&
 & RZMESO   ,RZMES2   ,RZMEPO   ,RZABOV   ,RTSUR    ,&
 & RTTROP   ,RTSTRA   ,RTSTR2   ,RTSTPO   ,RTMESO   ,&
 & RTMES2   ,RTMEPO   ,RTABOV   ,RPTROP   ,RPSTRA   ,&
 & RPSTR2   ,RPSTPO   ,RPMESO   ,RPMES2   ,RPMEPO   ,&
 & RPABOV   ,RDTDZ1   ,RDTDZ2   ,RDTDZ3   ,RDTDZ4   ,&
 & RDTDZ5   ,RDTDZ6   ,RDTDZ7   ,RDTDZ8   ,RDTDZ9   ,&
 & VZTROP   ,VZSTRA   ,VZSTR2   ,VZSTPO   ,VZMESO   ,&
 & VZMES2   ,VZMEPO   ,VZABOV   ,VTSUR    ,VTTROP   ,&
 & VTSTRA   ,VTSTR2   ,VTSTPO   ,VTMESO   ,VTMES2   ,&
 & VTMEPO   ,VTABOV   ,VPTROP   ,VPSTRA   ,VPSTR2   ,&
 & VPSTPO   ,VPMESO   ,VPMES2   ,VPMEPO   ,VPABOV   ,&
 & VDTDZ1   ,VDTDZ2   ,VDTDZ3   ,VDTDZ4   ,VDTDZ5   ,&
 & VDTDZ6   ,VDTDZ7   ,VDTDZ8   ,VDTDZ9  

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVP 
CHARACTER(LEN=*)  ,INTENT(IN)    :: CDATM
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLOLEV 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRPRES(KPROMA,KLEVP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLNPRES(KPROMA,KLEVP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTT(KPROMA,KLEVP) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSTFI(KPROMA,KLEVP) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRPRES5(KPROMA,KLEVP) 
!     ------------------------------------------------------------------
INTEGER(KIND=JPIM) :: IPRO(KPROMA,KLEVP)

INTEGER(KIND=JPIM) :: JPPRO

PARAMETER (JPPRO=9)
REAL(KIND=JPRB) :: ZZ(JPPRO),ZDTDZ(JPPRO),ZT(JPPRO),ZP(JPPRO)
REAL(KIND=JPRB) :: ZIP(JPPRO),ZIDTDZ(JPPRO)

INTEGER(KIND=JPIM) :: IPR, JJPR, JL, JLEV

LOGICAL :: LLPREF

REAL(KIND=JPRB) :: ZROG, ZSTZ
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!*       1.    Initialize YOMSTA.
!              ------------------

!      ZZ : Altitude du changement de gradient vertical de temperarure
!      ZP : Pression a cette altitude
!      ZT : Temperature a cette meme altitude
!      ZDTDZ : Gradient vertical de temperature au-dessus de cette altitude

!*    1.0 INITIALISATION DE ZZ ET DE ZDTDZ

IF (LHOOK) CALL DR_HOOK('PPSTAAD',0,ZHOOK_HANDLE)
LLPREF=CDATM == 'PPREF'
DO JJPR=1,JPPRO
  IF(.NOT.LLPREF) THEN
    IF(JJPR == 1) THEN
      ZZ(1)=0.0_JPRB
      ZT(1)=RTSUR
      ZP(1)=VP00
      ZDTDZ(1)=RDTDZ1
    ELSEIF(JJPR == 2) THEN
      ZZ(2)=RZTROP
      ZT(2)=RTTROP
      ZP(2)=RPTROP
      ZDTDZ(2)=RDTDZ2
    ELSEIF(JJPR == 3) THEN
      ZZ(3)=RZSTRA
      ZT(3)=RTSTRA
      ZP(3)=RPSTRA
      ZDTDZ(3)=RDTDZ3
    ELSEIF(JJPR == 4) THEN
      ZZ(4)=RZSTR2
      ZT(4)=RTSTR2
      ZP(4)=RPSTR2
      ZDTDZ(4)=RDTDZ4
    ELSEIF(JJPR == 5) THEN
      ZZ(5)=RZSTPO
      ZT(5)=RTSTPO
      ZP(5)=RPSTPO
      ZDTDZ(5)=RDTDZ5
    ELSEIF(JJPR == 6) THEN
      ZZ(6)=RZMESO
      ZT(6)=RTMESO
      ZP(6)=RPMESO
      ZDTDZ(6)=RDTDZ6
    ELSEIF(JJPR == 7) THEN
      ZZ(7)=RZMES2
      ZT(7)=RTMES2
      ZP(7)=RPMES2
      ZDTDZ(7)=RDTDZ7
    ELSEIF(JJPR == 8) THEN
      ZZ(8)=RZMEPO
      ZT(8)=RTMEPO
      ZP(8)=RPMEPO
      ZDTDZ(8)=RDTDZ8
    ELSEIF(JJPR == 9) THEN
      ZZ(9)=RZABOV
      ZT(9)=RTABOV
      ZP(9)=RPABOV
      ZDTDZ(9)=RDTDZ9
    ENDIF
  ELSE
    IF(JJPR == 1) THEN
      ZZ(1)=0.0_JPRB
      ZT(1)=VTSUR
      ZP(1)=VP00
      ZDTDZ(1)=VDTDZ1
    ELSEIF(JJPR == 2) THEN
      ZZ(2)=VZTROP
      ZT(2)=VTTROP
      ZP(2)=VPTROP
      ZDTDZ(2)=VDTDZ2
    ELSEIF(JJPR == 3) THEN
      ZZ(3)=VZSTRA
      ZT(3)=VTSTRA
      ZP(3)=VPSTRA
      ZDTDZ(3)=VDTDZ3
    ELSEIF(JJPR == 4) THEN
      ZZ(4)=VZSTR2
      ZT(4)=VTSTR2
      ZP(4)=VPSTR2
      ZDTDZ(4)=VDTDZ4
    ELSEIF(JJPR == 5) THEN
      ZZ(5)=VZSTPO
      ZT(5)=VTSTPO
      ZP(5)=VPSTPO
      ZDTDZ(5)=VDTDZ5
    ELSEIF(JJPR == 6) THEN
      ZZ(6)=VZMESO
      ZT(6)=VTMESO
      ZP(6)=VPMESO
      ZDTDZ(6)=VDTDZ6
    ELSEIF(JJPR == 7) THEN
      ZZ(7)=VZMES2
      ZT(7)=VTMES2
      ZP(7)=VPMES2
      ZDTDZ(7)=VDTDZ7
    ELSEIF(JJPR == 8) THEN
      ZZ(8)=VZMEPO
      ZT(8)=VTMEPO
      ZP(8)=VPMEPO
      ZDTDZ(8)=VDTDZ8
    ELSEIF(JJPR == 9) THEN
      ZZ(9)=VZABOV
      ZT(9)=VTABOV
      ZP(9)=VPABOV
      ZDTDZ(9)=VDTDZ9
    ENDIF
  ENDIF
ENDDO

DO JJPR=1,JPPRO
  ZIP(JJPR)=1.0_JPRB/ZP(JJPR)
  IF (ZDTDZ(JJPR) == 0.0_JPRB) THEN
    ZIDTDZ(JJPR)=0.0_JPRB
  ELSE
    ZIDTDZ(JJPR)=1.0_JPRB/ZDTDZ(JJPR)
  ENDIF
ENDDO

!*    1.2 Calcul de l'Altitude (ZSTZ), la Temperature (PSTT),
!         le Geopotentiel (PSTFI).

DO JLEV=KLOLEV,KLEVP
  DO JL=KSTART,KPROF
    IF(PRPRES5(JL,JLEV) >= ZP(2)) THEN
      IPRO(JL,JLEV)=1
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(3)) THEN
      IPRO(JL,JLEV)=2
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(4)) THEN
      IPRO(JL,JLEV)=3
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(5)) THEN
      IPRO(JL,JLEV)=4
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(6)) THEN
      IPRO(JL,JLEV)=5
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(7)) THEN
      IPRO(JL,JLEV)=6
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(8)) THEN
      IPRO(JL,JLEV)=7
    ELSEIF(PRPRES5(JL,JLEV) >= ZP(9)) THEN
      IPRO(JL,JLEV)=8
    ELSE
      IPRO(JL,JLEV)=9
    ENDIF
  ENDDO
ENDDO

ZROG=RD/RG
DO JLEV=KLOLEV,KLEVP
  DO JL=KSTART,KPROF
    IPR=IPRO(JL,JLEV)
    ZSTZ=0.0_JPRB
    ZSTZ=ZSTZ+RG*PSTFI(JL,JLEV)
    PSTFI(JL,JLEV)=0.0_JPRB
    IF(ZDTDZ(IPR) /= 0.0_JPRB) THEN
      ZSTZ=ZSTZ+ZDTDZ(IPR)*PSTT(JL,JLEV)
      PSTT(JL,JLEV)=0.0_JPRB
      PRPRES(JL,JLEV)=PRPRES(JL,JLEV)+ZT(IPR)*ZIDTDZ(IPR)&
       & *ZIP(IPR)*(-ZDTDZ(IPR)*ZROG)&
       & *(PRPRES5(JL,JLEV)*ZIP(IPR))**(-ZDTDZ(IPR)*ZROG-1.0_JPRB)&
       & *ZSTZ  
    ELSE
      PLNPRES(JL,JLEV)=PLNPRES(JL,JLEV)-ZT(IPR)*ZROG*ZSTZ
      PSTT(JL,JLEV)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO

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

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