SUBROUTINE ENDPOS(KPROMA,KST,KND,KFPDYN,CDCONF,KOPLEV,PXLEV,&
 & PUT0  ,PVT0  ,PTT0  ,PQT0  ,PLT0  ,PIT0  ,PSVT0,PAT0 ,PO3T0, &
 & PGHGT0,PGRGT0,PAEROT0, &
 & PSNT0 ,PRRT0 ,PGRT0 ,PHLT0,PTKET0,PDEP0,PWVEL0,&
 & POROG ,PSPT0 ,&
 & PTS0  ,PITS0 ,PSD0  ,PTXI  ,PTNI  ,PSW0  ,PSF0 ,PHV0 ,PSR0 ,PTCLS,PRHCLS,&
 & PTR0  ,PHUXI ,PHUNI ,&
 & PCFIS ,PCLSM ,PCVEG ,PCIVE ,PCARG,PCD2 ,PCSAB,&
 & PGM   ,&
 & PFP)  

!**** *ENDPOS* - End of post-processing

!     PURPOSE.
!     --------
!       Post-processing calculations after horizontal interpolations of 
!        model fields ; used to compute fields on surface-dependent levels
!        (height, eta levels or PBL physico-dynamic fields)

!**   INTERFACE.
!     ----------
!        *CALL* *ENDPOS(...)

!        EXPLICIT ARGUMENTS :
!        --------------------
!        KPROMA    : horizontal dimension
!        KST    : starting index of work
!        KND     : depth of work
!        KFPDYN    : Number of fields in output array
!        CDCONF    : Conf of work : 'Z' = height levels ; 'E' = eta levels
!        KOPLEV    : number of post-processing levels
!        PXLEV     : array containing the pp levels (heights or eta indexes)
!   --  horizontally interpolated model variables ---
!        PUT0      : zonal wind
!        PVT0      : meridional wind
!        PTT0      : temperature 
!        PQT0      : humidity
!        PLT0      : cloud liquid water
!        PIT0      : cloud ice
!        PSVT0     : passiv scalars
!        PSPT0     : surface pressure
!        PAT0      : cloud fraction
!        PO3T0     : ozone mixing ratio
!        PGHGT0    : Greenhouse Gases
!        PGRGT0    : GRG GEMS 
!        PAEROT0   : aerosols
!        PSNT0     : snow
!        PRRT0     : rain
!        PGRT0     : graupel
!        PHLT0     : hail
!        PTKET0    : turbulent kinetic energy
!        PDEP0     : n.-h. pressure departure
!        PWVEL0    : true vertical velocity w at half levels
!        POROG     : surface geopotential
!        PTS0      : output surface temperature
!        PITS0     : interpolated surface temperature
!        PSD0      : output snow depth
!        PTXI      : increment (Tx-Tcls)
!        PTNI      : increment (Tn-Tcls)
!        PSW0      : output surface water content
!        PSF0      : output surface frost
!        PHV0      : output resistance to evapotranspiration
!        PSR0      : output model dynamical roughness length times g
!        PTCLS     : interpolated cls temperature
!        PRHCLS    : interpolated cls relative moisture
!        PTR0      : output model thermal roughness length times g
!        PHUXI     : increment (HUx-HUcls)
!        PHUNI     : increment (HUn-HUcls)
!        PCFIS     : output orography
!        PCLSM     : output land-sea mask
!        PCVEG     : climatology percentage of vegetation
!        PCIVE     : climatology index of vegetation
!        PCARG     : climatology percentage of silt
!        PCD2      : climatology soild depth
!        PCSAB     : climatology percentage of sand
!        PGM       : map factor
!        PFP       : output fields array

!        IMPLICIT ARGUMENTS
!        --------------------

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

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

!          GPPRE   - compute full level pressures
!          CTSTAR  - compute T*
!          GPRCP   - compute R, Cp & Kappa
!          GPTET   - compute potential temperature
!          PPTHPW  - compute wet bulb potential temperature
!          GPEPT   - compute equivalent potential temperature
!          APACHE  - vertical interpolations

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

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-04-08
!        Modified : 95-11-13 R. El Khatib (pp of equivalent pot. temperature)
!        Modified : 99-05-06 D.Dent       (support for log surface pressure) 
!    R. El Khatib : 99-10-25 More flexibility for Ps/Log(Ps), LFPRH100
!    R. El Khatib : 00-08-01 Rewritten from horizontally interpolated fields
!       D. Dent   : 00-04-12 Support for computation of Montgomery geopotential
!       N. Wedi   : 01-11-08 Correct fullpos extrapolation
!    R. El Khatib : 01-01-05 Gusts+ iso t=0 + iso t'w=0 + plug-in FPCICA
!                   instead of FPCAPE
!    R. El Khatib : 01-06-14 iso t=0 + iso t'w=0 with vertical profile over
!                   interpolated surf. Ts (+ alt. correction)
!       F. Vana   : 02-07-04 Virtual potential temperature
!    R. El Khatib : 01-08-07 Pruning options
!    R. El Khatib : 02-04-07 ICAO fields moved from POS to here ; iso -10.
!    R. El Khatib : 03-08-13 gfl
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!    R. El Khatib : 03-08-27 computation of wetpoints up to 300 hpa only.
!        M.Hamrud : 01-Oct-2003 CY28 Cleaning
!      R. Engelen : 15-Mar-2005 Greenhouse gases
!        A. Untch : 15-Mar-2005 Aerosols
!      J. Flemming: 11-Apr-2005 Aerosol replaced with GEMS GRG 
!    Y. Bouteloup : 05-01-28 Introduction of precipitation (rain)
!    G. Hello     : 05-01-28 snow, graupel and tke (and rain too !)
!    G. Hello     : 06-08-31 simulated reflectivities
!    G. Hello     : 07-02 thetav
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!    Y. Seity     : 20-Dec-2007 hail
!    R. El Khatib : 05-May-2008 Bugfixes on recent developments
!    F. Vana      : 27-Aug-2008 vectorization support for NEC
!     ------------------------------------------------------------------

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

USE PARFPOS  , ONLY : JPOSVX2
USE YOMLUN   , ONLY : NULOUT
USE YOMDIM   , ONLY : NFLEVG
USE YOMFPC   , ONLY : NFPCLI   ,LFPRH100 ,FPBL     ,LFPQ     ,NFPCAPE
USE YOMFPG   , ONLY : FPVALH   ,FPVBH    ,NFPLEV
USE YOMPHY   , ONLY : LMPHYS
USE YOMGEM   , ONLY : VAH      ,VBH      , VDELB, VC
USE YOMSTA   , ONLY : RDTDZ1   ,NLEXTRAP ,STPRE
USE YOMCST   , ONLY : RG       ,RATM     ,RTT
USE YOMVPOS  , ONLY : QFPTYPE
USE YOMDYNA  , ONLY : LRWSDLR
USE YOMAFN   , ONLY : TFP_SP   ,TFP_LNSP ,TFP_U    ,TFP_V    ,TFP_T    ,&
 & TFP_Z    ,TFP_Q    ,TFP_L    ,TFP_I    ,TFP_CLF  ,TFP_O3MX ,&
 & TFP_HU   ,TFP_TH   ,TFP_THPW ,TFP_ETH  ,TFP_WND  ,TFP_P    ,&
 & TFP_PD   ,TFP_VD   ,TFP_VW   ,TFP_GFL  ,TFP_FUA  ,TFP_WWS  ,&
 & TFP_UCLS ,TFP_VCLS ,TFP_TCLS ,TFP_QCLS ,TFP_RCLS ,TFP_FCLS ,&
 & TFP_UGST ,TFP_VGST ,TFP_FGST ,TFP_PT0B ,TFP_HT0B ,TFP_HTPW ,&
 & TFP_CAPE ,TFP_CIEN ,TFP_TX   ,TFP_TN   ,TFP_HUX  ,TFP_HUN  ,&
 & TFP_UJET ,TFP_VJET ,TFP_PJET ,TFP_PCAO ,TFP_TCAO ,TFP_IET  ,&
 & TFP_PT1B ,TFP_HT1B ,TFP_DYNDS,TFP_SN   ,TFP_RR   ,TFP_GR   ,&
 & TFP_HL   ,TFP_TKE  ,TFP_AERO ,TFP_GHG, TFP_GRG, TFP_SRE, TFP_THV ,&
 & TFP_LCL  ,TFP_FCL  , TFP_EL
USE YOM_YGFL , ONLY : NGFL_EXT, NAERO, NGRG, NGHG

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFPDYN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOPLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KND 
CHARACTER(LEN=1)  ,INTENT(IN)    :: CDCONF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXLEV(KOPLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PIT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSVT0(KPROMA,NFLEVG,NGFL_EXT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PO3T0(KPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGHGT0(KPROMA,NFLEVG,NGHG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAEROT0(KPROMA,NFLEVG,NAERO)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGRGT0(KPROMA,NFLEVG,NGRG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSNT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRRT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGRT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHLT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTKET0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDEP0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWVEL0(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSPT0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PITS0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSD0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTXI(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTNI(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSW0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSF0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHV0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSR0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTCLS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRHCLS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTR0(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHUXI(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHUNI(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCFIS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLSM(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCVEG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCIVE(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCARG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCD2(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCSAB(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFP(KPROMA,KFPDYN) 

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

REAL(KIND=JPRB), PARAMETER :: PPROOF=30000._JPRB

REAL(KIND=JPRB) :: ZTSI  (KPROMA) ! interpolated surface temperature without
                                  ! altitude correction
REAL(KIND=JPRB) :: ZTSA  (KPROMA) ! interpolated surface temperature WITH
                                  ! altitude correction
REAL(KIND=JPRB) :: ZTSO  (KPROMA) ! output surface temperature
REAL(KIND=JPRB) :: ZTSEA (KPROMA) ! sea surface temperature
REAL(KIND=JPRB) :: ZOOROG(KPROMA) ! output orography
REAL(KIND=JPRB) :: Z1SGM (KPROMA) ! Reverse of map factor

REAL(KIND=JPRB) :: ZIPRESH(KPROMA,0:NFLEVG) ! half level pressures above
                                            ! interpolated orography
REAL(KIND=JPRB) :: ZIPRESF(KPROMA,NFLEVG)   ! full level pressures above
                                            ! interpolated orography
REAL(KIND=JPRB) :: ZVDIVPP(KPROMA,KOPLEV)   ! vertical divergence on output
                                            ! full levels
REAL(KIND=JPRB) :: ZTETAPP(KPROMA,KOPLEV)   ! Teta on output full levels
REAL(KIND=JPRB) :: ZEPTHPP(KPROMA,KOPLEV)   ! equivalent potential temperature
                                            ! on output full levels
REAL(KIND=JPRB) :: ZIBETPP(KPROMA,KOPLEV)   ! virtual potential temperature on
                                            ! output full levels
REAL(KIND=JPRB) :: ZTHPWPP(KPROMA,KOPLEV)   ! wet bulb potential temperature on
                                            ! output full levels
REAL(KIND=JPRB) :: ZKAPP(KPROMA,KOPLEV)     ! Kappa on output full levels
REAL(KIND=JPRB) :: ZRPP(KPROMA,KOPLEV)      ! R on output full levels
REAL(KIND=JPRB) :: ZCPPP(KPROMA,KOPLEV)     ! Cp on output full levels
REAL(KIND=JPRB) :: ZWIND(KPROMA,KOPLEV)     ! Wind velocity on output full
                                            ! levels
REAL(KIND=JPRB) :: ZSIMRPP(KPROMA,KOPLEV)   ! Simulated reflectiviries on 
                                            ! output full levels
REAL(KIND=JPRB) :: ZVIRTPP(KPROMA,KOPLEV)   ! Virtual theta on output full levels
REAL(KIND=JPRB) :: ZDUM(KPROMA,KOPLEV)      ! dummy field on output full levels

! ========== Overdimensionned arrays to prepare for interpolations ========

REAL(KIND=JPRB) :: ZUT0 (KPROMA,0:NFLEVG), ZVT0 (KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZTT0 (KPROMA,0:NFLEVG), ZQT0 (KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZLT0 (KPROMA,0:NFLEVG), ZIT0 (KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZAT0 (KPROMA,0:NFLEVG), ZO3T0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZSNT0(KPROMA,0:NFLEVG), ZRRT0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZGRT0(KPROMA,0:NFLEVG), ZTKET0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZHLT0(KPROMA,0:NFLEVG), ZGRHLT0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZDEP0(KPROMA,0:NFLEVG), ZWVEL0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZSVT0(KPROMA,0:NFLEVG,NGFL_EXT)
REAL(KIND=JPRB) :: ZGHGT0(KPROMA,0:NFLEVG,NGHG)
REAL(KIND=JPRB) :: ZAEROT0(KPROMA,0:NFLEVG,NAERO)
REAL(KIND=JPRB) :: ZGRGT0(KPROMA,0:NFLEVG,NGRG)

REAL(KIND=JPRB) :: ZXLEVG(NFLEVG) ! level indexes
REAL(KIND=JPRB) :: ZGEOP(KPROMA,KOPLEV) ! geopotentials of the height levels

REAL(KIND=JPRB) :: ZDTDPHI  ! dT/(g*dz1)
REAL(KIND=JPRB) :: ZMDTDPHI ! - dT/(g*dz1)
REAL(KIND=JPRB) :: Z1SG     ! 1/g

INTEGER(KIND=JPIM) :: JL, JI, JV
INTEGER(KIND=JPIM) :: ILOC, ILPT, I, ICAPE, IPOL, IPOL2, IMAXLEV, ITOP

LOGICAL :: LLPBL ! to activate PBL computation
LOGICAL :: LLICV ! to activate the computation of CAPE & CIEN
REAL(KIND=JPRB) :: ZQCLS(KPROMA) ! Q of pbl
REAL(KIND=JPRB) :: ZRHCLS(KPROMA)! RH of pbl
REAL(KIND=JPRB) :: ZTCLS(KPROMA) ! T of pbl
REAL(KIND=JPRB) :: ZUCLS(KPROMA) ! U of pbl
REAL(KIND=JPRB) :: ZVCLS(KPROMA) ! V of pbl
REAL(KIND=JPRB) :: ZFCLS(KPROMA) ! wind velocity of pbl
REAL(KIND=JPRB) :: ZPCLS(KPROMA) ! pressure of pbl
REAL(KIND=JPRB) :: ZCAPE(KPROMA) ! CAPE
REAL(KIND=JPRB) :: ZCIN(KPROMA)  ! CIN
REAL(KIND=JPRB) :: ZUGST(KPROMA) ! U of gusts
REAL(KIND=JPRB) :: ZVGST(KPROMA) ! V of gusts
REAL(KIND=JPRB) :: ZFGST(KPROMA) ! gusts

REAL(KIND=JPRB) :: ZPT0B(KPROMA) ! Pressure of iso-T=0 Celsius, from bottom
REAL(KIND=JPRB) :: ZHT0B(KPROMA) ! height of iso-T=0 Celsius, from bottom
REAL(KIND=JPRB) :: ZPT1B(KPROMA) ! Pressure of iso-T=-10 Celsius, from bottom
REAL(KIND=JPRB) :: ZHT1B(KPROMA) ! height of iso-T=-10 Celsius, from bottom
REAL(KIND=JPRB) :: ZHTPW(KPROMA) ! height of iso-T'w=0 Celsius, from bottom
REAL(KIND=JPRB) :: ZUJET(KPROMA) ! ICAO jet - U momentum
REAL(KIND=JPRB) :: ZVJET(KPROMA) ! ICAO jet - V momentum
REAL(KIND=JPRB) :: ZPJET(KPROMA) ! ICAO jet - pressure
REAL(KIND=JPRB) :: ZPICAO(KPROMA)! ICAO tropopause - pressure
REAL(KIND=JPRB) :: ZTICAO(KPROMA)! ICAO tropopause - temperature
REAL(KIND=JPRB) :: ZTMIN(KPROMA) ! Tcls min
REAL(KIND=JPRB) :: ZTMAX(KPROMA) ! Tcls max
REAL(KIND=JPRB) :: ZHUMIN(KPROMA)! Hu cls min
REAL(KIND=JPRB) :: ZHUMAX(KPROMA)! Hu cls max
INTEGER(KIND=JPIM) :: ILCL(KPROMA) ! Lifting condensation level
INTEGER(KIND=JPIM) :: IFCL(KPROMA) ! Free convection level
INTEGER(KIND=JPIM) :: IEL(KPROMA)  ! Equilibrium level
REAL(KIND=JPRB) :: ZLCL(KPROMA) !  Lifting condensation level (height)
REAL(KIND=JPRB) :: ZFCL(KPROMA) ! Free convection level (height)
REAL(KIND=JPRB) :: ZEL(KPROMA)  ! Equilibrium level (height)

! ========== APACHE-related local variables ========

LOGICAL :: LLNIVP  ! output pressure levels are given in input 
LOGICAL :: LLWFULL ! to compute w at full levels
LOGICAL :: LLWHALF ! to compute w at half levels
LOGICAL :: LLGEOP  ! to compute geopotential
LOGICAL :: LLNHPD  ! to compute pressure departuer (NH)
LOGICAL :: LLWAT   ! to compute liquid water
LOGICAL :: LLICE   ! to compute ice
LOGICAL :: LLCFR   ! to compute cloud fraction
LOGICAL :: LLO3    ! to compute ozone mixing ratio
LOGICAL :: LLSN    ! to compute snow
LOGICAL :: LLRR    ! to compute rain
LOGICAL :: LLGR    ! to compute graupel
LOGICAL :: LLHL    ! to compute hail
LOGICAL :: LLTKE   ! to compute turbulent kinetic energy
LOGICAL :: LLSPCH  ! to interpolate Q rather than Hu
REAL(KIND=JPRB) :: ZSPP(KPROMA)                      ! surface pressure
REAL(KIND=JPRB) :: ZLNSP(KPROMA)                     ! Ln of surface pressure
REAL(KIND=JPRB) :: ZWSPP(KPROMA)                     ! surface true vertical
                                                     ! velocity
REAL(KIND=JPRB) :: ZPREPP(KPROMA,MAX(NFLEVG,KOPLEV)) ! upper air pressure
REAL(KIND=JPRB) :: ZUPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! U
REAL(KIND=JPRB) :: ZVPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! V
REAL(KIND=JPRB) :: ZTPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! T
REAL(KIND=JPRB) :: ZQPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! Q
REAL(KIND=JPRB) :: ZLPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! Liquid water
REAL(KIND=JPRB) :: ZIPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! Ice
REAL(KIND=JPRB) :: ZAPP(KPROMA,MAX(NFLEVG,KOPLEV))   ! Cloud fraction
REAL(KIND=JPRB) :: ZO3PP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Ozone mixing ratio
REAL(KIND=JPRB) :: ZSNPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Snow
REAL(KIND=JPRB) :: ZRRPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Rain
REAL(KIND=JPRB) :: ZGRPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Graupel
REAL(KIND=JPRB) :: ZHLPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Hail
REAL(KIND=JPRB) :: ZGRHLPP(KPROMA,MAX(NFLEVG,KOPLEV))! Hail+Graupel
REAL(KIND=JPRB) :: ZTKEPP(KPROMA,MAX(NFLEVG,KOPLEV)) ! Turbulent kinetic energy
REAL(KIND=JPRB) :: ZHUPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! Hu
REAL(KIND=JPRB) :: ZPHIPP(KPROMA,MAX(NFLEVG,KOPLEV)) ! geopotential
REAL(KIND=JPRB) :: ZPEORG(KPROMA,MAX(NFLEVG,KOPLEV)) ! pressures of data origin
REAL(KIND=JPRB) :: ZPDEPP(KPROMA,MAX(NFLEVG,KOPLEV)) ! NH pressure departure
REAL(KIND=JPRB) :: ZWFPP(KPROMA,MAX(NFLEVG,KOPLEV))  ! w full levels
REAL(KIND=JPRB) :: ZTPWPP(KPROMA,MAX(NFLEVG,KOPLEV)) ! T'w
REAL(KIND=JPRB) :: ZGHGPP(KPROMA,MAX(NFLEVG,KOPLEV),NGHG) ! Greenhouse Gases
REAL(KIND=JPRB) :: ZAEROPP(KPROMA,MAX(NFLEVG,KOPLEV),NAERO) ! Aerosols
REAL(KIND=JPRB) :: ZGRGPP(KPROMA,MAX(NFLEVG,KOPLEV),NGRG) ! GEMS GRG 
REAL(KIND=JPRB) :: ZSVPP(KPROMA,MAX(NFLEVG,KOPLEV),NGFL_EXT) ! passive scalars
REAL(KIND=JPRB), ALLOCATABLE :: ZWHPP2(:,:,:) ! w half levels
REAL(KIND=JPRB), ALLOCATABLE :: ZPREH2(:,:,:) ! half levels pressures
! (.,JLEV,0) : half level above full level JLEV
! (.,JLEV,1) : half level below full level JLEV
REAL(KIND=JPRB) :: ZVAH(0:KOPLEV), ZVBH(0:KOPLEV) ! Useless once LLNIVP=.T.

REAL(KIND=JPRB) :: ZDELP(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZRDELP(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZLNPR(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZALPH(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZRTGR(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZRPRES(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZRPPINV(KPROMA,NFLEVG) 
REAL(KIND=JPRB) :: ZRE0(KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZPHI(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB) :: ZPHIF(KPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZR(KPROMA,NFLEVG)


! ========== More local logical ========

LOGICAL :: LL_CLF   ! Cloud fraction
LOGICAL :: LL_ETH   ! equivalent potential temperature
LOGICAL :: LL_IET   ! isobaric equivalent temperature
LOGICAL :: LL_SRE   ! simulated reflectivities
LOGICAL :: LL_THV   ! virtual theta
LOGICAL :: LL_O3MX  ! Ozone mixing ratio
LOGICAL :: LL_SN    ! Snow
LOGICAL :: LL_RR    ! Rain
LOGICAL :: LL_GR    ! Graupel
LOGICAL :: LL_HL    ! Hail
LOGICAL :: LL_TKE   ! Turbulent kinetic energy
LOGICAL :: LL_PD    ! NH pressure departure
LOGICAL :: LL_Q     ! Specific humidity
LOGICAL :: LL_HU    ! Relative humidity
LOGICAL :: LL_I     ! Solid water (Ice)
LOGICAL :: LL_TH    ! Potential temperature
LOGICAL :: LL_THPW  ! Wet bulb potential temperature
LOGICAL :: LL_VD    ! Vertical divergence
LOGICAL :: LL_VW    ! True vertical velocity
LOGICAL :: LL_WND   ! wind speed
LOGICAL :: LL_WWS   ! surface true vertical velocity
LOGICAL :: LL_L     ! liquid water
LOGICAL :: LL_Z     ! Geopotential
LOGICAL :: LL_GHG(NGHG) ! Greenhouse Gases
LOGICAL :: LL_AERO(NAERO) ! Aerosols
LOGICAL :: LL_GRG(NGRG) ! GEMS - GRG 
LOGICAL :: LL_GFL(NGFL_EXT) ! Passive scalars
LOGICAL :: LL_PT0B  ! Pressure of iso-T=0 Celsius, from bottom
LOGICAL :: LL_HT0B  ! height of iso-T=0 Celsius, from bottom
LOGICAL :: LL_PT1B  ! Pressure of iso-T=-10 Celsius, from bottom
LOGICAL :: LL_HT1B  ! height of iso-T=-10 Celsius, from bottom
LOGICAL :: LL_HTPW  ! height of iso-T'w=0 Celsius, from bottom
LOGICAL :: LL_UJET  ! ICAO U component of the jet
LOGICAL :: LL_VJET  ! ICAO V component of the jet
LOGICAL :: LL_PJET  ! ICAO pressure at jet level
LOGICAL :: LL_PCAO  ! ICAO tropopause pressure
LOGICAL :: LL_TCAO  ! ICAO tropopause temperature

LOGICAL :: LLTOP, LLAERO, LLUNIT
REAL(KIND=JPRB)  :: ZXTEMP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "apache.intfb.h"
#include "ctstar.intfb.h"
#include "fpachmt.intfb.h"
#include "fpcica.intfb.h"
#include "fpps.intfb.h"
#include "gpept.intfb.h"
#include "gpiet.intfb.h"
#include "gppre.intfb.h"
#include "gprcp.intfb.h"
#include "gptet.intfb.h"
#include "poaero.intfb.h"
#include "ppleta.intfb.h"
#include "ppltemp.intfb.h"
#include "ppthpw.intfb.h"
#include "ppwetpoint.intfb.h"
#include "gpprs0d.intfb.h"
#include "ppcvirt.intfb.h"
#include "gppreh.intfb.h"
#include "gpxyb.intfb.h"
#include "gpgeo.intfb.h"

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

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

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

!*       0.    PREPARATIONS 
!              ------------

IF (LRWSDLR) THEN
  WRITE (NULOUT,*) ' ENDPOS: not properly adapted for LRWSDLR=T!!'
ENDIF

LL_CLF  = QFPTYPE%LL(TFP_CLF%ICOD)
LL_ETH  = QFPTYPE%LL(TFP_ETH%ICOD)
LL_IET  = QFPTYPE%LL(TFP_IET%ICOD)
LL_SRE  = QFPTYPE%LL(TFP_SRE%ICOD)
LL_THV  = QFPTYPE%LL(TFP_THV%ICOD)
LL_O3MX = QFPTYPE%LL(TFP_O3MX%ICOD)
LL_PD   = QFPTYPE%LL(TFP_PD%ICOD)
LL_Q    = QFPTYPE%LL(TFP_Q%ICOD)
LL_HU   = QFPTYPE%LL(TFP_HU%ICOD)
LL_I    = QFPTYPE%LL(TFP_I%ICOD)
LL_TH   = QFPTYPE%LL(TFP_TH%ICOD)
LL_THPW = QFPTYPE%LL(TFP_THPW%ICOD)
LL_VD   = QFPTYPE%LL(TFP_VD%ICOD)
LL_VW   = QFPTYPE%LL(TFP_VW%ICOD)
LL_WND  = QFPTYPE%LL(TFP_WND%ICOD)
LL_WWS  = QFPTYPE%LL(TFP_WWS%ICOD)
LL_L    = QFPTYPE%LL(TFP_L%ICOD)
LL_Z    = QFPTYPE%LL(TFP_Z%ICOD)
DO JV=1,NGHG
  LL_GHG(JV)=QFPTYPE%LL(TFP_GHG(JV)%ICOD)
ENDDO
DO JV=1,NAERO
  LL_AERO(JV)=QFPTYPE%LL(TFP_AERO(JV)%ICOD)
ENDDO
DO JV=1,NGRG
  LL_GRG(JV)=QFPTYPE%LL(TFP_GRG(JV)%ICOD)
ENDDO
LL_SN   = QFPTYPE%LL(TFP_SN%ICOD)
LL_RR   = QFPTYPE%LL(TFP_RR%ICOD)
LL_GR   = QFPTYPE%LL(TFP_GR%ICOD)
LL_HL   = QFPTYPE%LL(TFP_HL%ICOD)
LL_TKE  = QFPTYPE%LL(TFP_TKE%ICOD)
DO JV=1,NGFL_EXT
  LL_GFL(JV)=QFPTYPE%LL(TFP_GFL(JV)%ICOD)
ENDDO

LL_PT0B = QFPTYPE%LL(TFP_PT0B%ICOD)
LL_HT0B = QFPTYPE%LL(TFP_HT0B%ICOD)
LL_PT1B = QFPTYPE%LL(TFP_PT1B%ICOD)
LL_HT1B = QFPTYPE%LL(TFP_HT1B%ICOD)
LL_HTPW = QFPTYPE%LL(TFP_HTPW%ICOD)
LL_UJET = QFPTYPE%LL(TFP_UJET%ICOD)
LL_VJET = QFPTYPE%LL(TFP_VJET%ICOD)
LL_PJET = QFPTYPE%LL(TFP_PJET%ICOD)
LL_PCAO = QFPTYPE%LL(TFP_PCAO%ICOD)
LL_TCAO = QFPTYPE%LL(TFP_TCAO%ICOD)

LLICV = QFPTYPE%LL(TFP_CAPE%ICOD) .OR. QFPTYPE%LL(TFP_CIEN%ICOD)

LLPBL = QFPTYPE%LL(TFP_UCLS%ICOD) .OR. QFPTYPE%LL(TFP_VCLS%ICOD)&
 & .OR. QFPTYPE%LL(TFP_TCLS%ICOD) .OR. QFPTYPE%LL(TFP_QCLS%ICOD)&
 & .OR. QFPTYPE%LL(TFP_RCLS%ICOD) .OR. QFPTYPE%LL(TFP_FCLS%ICOD)&
 & .OR. QFPTYPE%LL(TFP_TX%ICOD)   .OR. QFPTYPE%LL(TFP_TN%ICOD)&
 & .OR. QFPTYPE%LL(TFP_HUX%ICOD)  .OR. QFPTYPE%LL(TFP_HUN%ICOD)&
 & .OR. QFPTYPE%LL(TFP_UGST%ICOD) .OR. QFPTYPE%LL(TFP_VGST%ICOD)&
 & .OR. QFPTYPE%LL(TFP_FGST%ICOD) .OR. (LLICV .AND. NFPCAPE >= 3)  

LLAERO = LL_UJET .OR. LL_VJET .OR. LL_PJET .OR. LL_PCAO .OR. LL_TCAO

Z1SG=1.0_JPRB/RG
ZDTDPHI=RDTDZ1/RG
ZMDTDPHI=-ZDTDPHI

IMAXLEV=MAX(NFLEVG,KOPLEV)

DO JI=KST,KND
  Z1SGM(JI)=1.0_JPRB/PGM(JI)
ENDDO

!*       1.    SET OUTPUT OROGRAPHY AND SURFACE TEMPERATURES 
!              ---------------------------------------------

SELECT CASE (NFPCLI)
CASE (0)
  DO JI=KST, KND
    ZOOROG(JI)=POROG(JI)
  ENDDO
CASE DEFAULT
  DO JI=KST, KND
    ZOOROG(JI)=PCFIS(JI)
  ENDDO
END SELECT

SELECT CASE (LMPHYS)
CASE (.TRUE.)
  DO JI=KST, KND
    ZTSI(JI)=PITS0(JI)
    ZTSO(JI)=PTS0(JI)
  ENDDO
CASE DEFAULT
!   Compute T* :
  DO JI=KST, KND
    ZIPRESH(JI,NFLEVG)=PSPT0(JI)
  ENDDO
  CALL GPPRE(KPROMA,KST,KND,NFLEVG,VAH,VBH,ZIPRESH,ZIPRESF)
  CALL CTSTAR(KPROMA,KST,KND,PTT0(1,NLEXTRAP),ZIPRESH(1,NFLEVG), &
   & ZIPRESF(1,NLEXTRAP),POROG,ZTSI,ZTSEA)  
  DO JI=KST, KND
    ZTSO(JI)=ZTSI(JI)+ZDTDPHI*(ZOOROG(JI)-POROG(JI))
  ENDDO
END SELECT

DO JI=KST, KND
  ZTSA(JI)=ZTSI(JI)+ZDTDPHI*(ZOOROG(JI)-POROG(JI))
ENDDO

!*       2.    PREPARE FOR VERTICAL EXTRAPOLATIONS AT TOP + PREINITIALIZATIONS 
!              ---------------------------------------------------------------

DO JL=1, NFLEVG
  DO JI=KST, KND
    ZUT0(JI,JL)=PUT0(JI,JL)*PGM(JI)
    ZVT0(JI,JL)=PVT0(JI,JL)*PGM(JI)
    ZTT0(JI,JL)=PTT0(JI,JL)
    SELECT CASE (TFP_Q%ILMOD)
    CASE (1:)
      ZQT0(JI,JL)=PQT0(JI,JL)
    CASE DEFAULT
      ZQT0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_L%ILMOD)
    CASE (1:)
      ZLT0(JI,JL)=PLT0(JI,JL)
    CASE DEFAULT
      ZLT0(JI,JL)=0.0_JPRB
    END SELECT 
    SELECT CASE (TFP_I%ILMOD)
    CASE (1:)
      ZIT0(JI,JL)=PIT0(JI,JL)
    CASE DEFAULT
      ZIT0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_CLF%ILMOD)
    CASE (1:)
      ZAT0(JI,JL)=PAT0(JI,JL)        
    CASE DEFAULT
      ZAT0(JI,JL)=0.0_JPRB 
    END SELECT
    SELECT CASE (TFP_O3MX%ILMOD)
    CASE (1:)
      ZO3T0(JI,JL)=PO3T0(JI,JL)
    CASE DEFAULT
      ZO3T0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_SN%ILMOD)
    CASE (1:)
      ZSNT0(JI,JL)=PSNT0(JI,JL)
    CASE DEFAULT
      ZSNT0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_RR%ILMOD)
    CASE (1:)
      ZRRT0(JI,JL)=PRRT0(JI,JL)
    CASE DEFAULT
      ZRRT0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_GR%ILMOD)
    CASE (1:)
      ZGRT0(JI,JL)=PGRT0(JI,JL)
    CASE DEFAULT
      ZGRT0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_HL%ILMOD)
    CASE (1:)
      ZHLT0(JI,JL)=PHLT0(JI,JL)
    CASE DEFAULT
      ZHLT0(JI,JL)=0.0_JPRB
    END SELECT
    ZGRHLT0(JI,JL)=ZGRT0(JI,JL)+ZHLT0(JI,JL)
    SELECT CASE (TFP_TKE%ILMOD)
    CASE (1:)
      ZTKET0(JI,JL)=PTKET0(JI,JL)
    CASE DEFAULT
      ZTKET0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_PD%ILMOD)
    CASE (1:)
      ZDEP0(JI,JL)=PDEP0(JI,JL) 
    CASE DEFAULT
      ZDEP0(JI,JL)=0.0_JPRB
    END SELECT
    SELECT CASE (TFP_VW%ILMOD)
    CASE (1:) 
      ZWVEL0(JI,JL)=PWVEL0(JI,JL)
    CASE DEFAULT
      ZWVEL0(JI,JL)=0.0_JPRB
    END SELECT
  ENDDO
ENDDO
 
DO JI=KST, KND
  SELECT CASE (TFP_WWS%ILMOD)
  CASE (1:) 
    ZWVEL0(JI,0)=PWVEL0(JI,0)
  CASE DEFAULT
    ZWVEL0(JI,0)=0.0_JPRB
  END SELECT
ENDDO

DO JV=1,NGHG
  DO JL=1, NFLEVG
    DO JI=KST, KND
      ZGHGT0(JI,JL,JV)=PGHGT0(JI,JL,JV)
    ENDDO
  ENDDO
ENDDO

DO JV=1,NAERO
  DO JL=1, NFLEVG
    DO JI=KST, KND
      ZAEROT0(JI,JL,JV)=PAEROT0(JI,JL,JV)
    ENDDO
  ENDDO
ENDDO

DO JV=1,NGRG
  DO JL=1, NFLEVG
    DO JI=KST, KND
      ZGRGT0(JI,JL,JV)=PGRGT0(JI,JL,JV)
    ENDDO
  ENDDO
ENDDO

DO JV=1,NGFL_EXT
  DO JL=1, NFLEVG
    DO JI=KST, KND
      ZSVT0(JI,JL,JV)=PSVT0(JI,JL,JV)
    ENDDO
  ENDDO
ENDDO

!*       3.    SURFACE & PBL POST-PROCESSING 
!              -----------------------------

CALL FPPS(KPROMA,KST,KND,1,POROG,ZOOROG,ZTT0(1,1),ZQT0(1,1),ZIT0(1,1), &
 & ZLT0(1,1),ZRRT0(1,1),ZSNT0(1,1),ZGRHLT0(1,1),PSPT0,ZTSI,ZSPP)  

DO JI=KST,KND
  ZLNSP(JI)=LOG(ZSPP(JI))
ENDDO

IF (LL_WWS.OR.LLPBL.OR.LLICV) THEN

  LLNIVP=.TRUE.
  LLSPCH=LFPQ

  LLWHALF=LL_WWS
  LLWFULL=.FALSE.
  LLGEOP=.FALSE.
  LLNHPD=.FALSE.
  LLWAT=LLPBL.OR.LLICV
  LLICE=LLPBL.OR.LLICV
  LLCFR=.FALSE.
  LLO3=.FALSE.
  LLSN=LLPBL.OR.LLICV
  LLRR=LLPBL.OR.LLICV
  LLGR=LLPBL.OR.LLICV
  LLHL=LLPBL.OR.LLICV
  LLTKE=.FALSE.

  ALLOCATE(ZWHPP2(KPROMA,NFLEVG,0:1))
  ALLOCATE(ZPREH2(KPROMA,NFLEVG,0:1))
  DO JL=1,NFLEVG
    ZXLEVG(JL)=REAL(JL,JPRB)
  ENDDO
  CALL PPLETA(KPROMA,KST,KND,NFLEVG,NFLEVG,ZXLEVG,VAH,VBH,ZSPP,ZPREPP, &
   & ZPREH2(1,1,0),ZPREH2(1,1,1))  

  CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,NFLEVG,ZTT0,ZQT0,ZUT0,ZVT0,PSPT0, &
   & POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZDEP0,ZWVEL0,ZSNT0,ZRRT0,ZGRT0,ZHLT0, &
   & ZTKET0,LLNHPD,LLWHALF,LLWFULL,LLWAT,LLICE,LLCFR,LLO3,      &
   & LLSN,LLRR,LLGR,LLHL,LLTKE,ZTPP,ZQPP,ZUPP,ZVPP,ZSPP,ZOOROG,VAH,VBH,     &
   & ZLPP,ZIPP,ZAPP,ZO3PP,ZSNPP,ZRRPP,ZGRPP,ZHLPP,ZTKEPP,                     &
   & ZPDEPP,ZWHPP2,ZWFPP,ZPHIPP,ZHUPP,ZPEORG,LLGEOP,    &
   & ZPREPP,ZPREH2,ZTSI,ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)  

  IF (LLGR) THEN
    ZGRHLPP(KST:KND,1:NFLEVG)=ZGRPP(KST:KND,1:NFLEVG)
  ELSE
    ZGRHLPP(KST:KND,1:NFLEVG)=0._JPRB
  ENDIF
  IF (LLHL) THEN
    ZGRHLPP(KST:KND,1:NFLEVG)=ZGRHLPP(KST:KND,1:NFLEVG)+ZHLPP(KST:KND,1:NFLEVG)
  ENDIF

  SELECT CASE (LL_WWS)
  CASE (.TRUE.)
    DO JI=KST, KND
      ZWSPP(JI)=ZWHPP2(JI,NFLEVG,1)
    ENDDO
  END SELECT

  DEALLOCATE(ZWHPP2)
  DEALLOCATE(ZPREH2)

  IF (LLPBL) THEN
    CALL FPACHMT(KPROMA,KST,KND,NFLEVG,ZOOROG,ZTPP,ZQPP,ZIPP,ZLPP,ZRRPP,      &
    & ZSNPP,ZGRHLPP,ZUPP,ZVPP,ZSPP,PCLSM,PTS0,PSR0,PTR0,PCVEG,PCARG,PCD2,PCIVE,&
    & PCSAB,PSD0,PSW0,PSF0,PHV0,ZQCLS,ZRHCLS,ZTCLS,ZUCLS,ZVCLS,ZFCLS,ZUGST,  &
    & ZVGST,ZFGST,ZPCLS)  
  ENDIF
  IF (LLICV) THEN
    SELECT CASE (NFPCAPE)
    CASE (4)
      ICAPE=3
      CALL FPCICA(KST,KND,KPROMA,NFLEVG,ICAPE,PTCLS,ZPCLS,PRHCLS,ZTPP, &
       & ZPREPP,ZQPP,ZCAPE,ZCIN,ILCL,IFCL,IEL)  
    CASE DEFAULT 
      ICAPE=NFPCAPE
      CALL FPCICA(KST,KND,KPROMA,NFLEVG,ICAPE,ZTCLS,ZPCLS,ZRHCLS,ZTPP, &
       & ZPREPP,ZQPP,ZCAPE,ZCIN,ILCL,IFCL,IEL)  
    END SELECT
!   convert model levels to height levels
    ZRE0=0._JPRB
    ZRE0(:,NFLEVG)=PSPT0
    CALL GPPREH(KPROMA,KST,KND,NFLEVG,VAH,VBH,ZRE0)
    CALL GPXYB(KPROMA,KST,KND,NFLEVG,VDELB,VC,ZRE0,ZDELP,ZRDELP,ZLNPR,ZALPH,&
     & ZRTGR,ZRPRES,ZRPPINV) 
    CALL GPRCP(KPROMA,KST,KND,NFLEVG,PQ=PQT0,PR=ZR)
    CALL GPGEO(KPROMA,KST,KND,NFLEVG,ZPHI,ZPHIF,PTT0,ZR,ZLNPR,ZALPH)
    DO JI=KST, KND
      IF (ILCL(JI) > 0) THEN
        ZLCL(JI)=ZPHIF(JI,ILCL(JI))
      ELSE
        ZLCL(JI)=0._JPRB
      ENDIF
      IF (IFCL(JI) > 0) THEN
        ZFCL(JI)=ZPHIF(JI,IFCL(JI))
      ELSE
        ZFCL(JI)=0._JPRB
      ENDIF
      IF (IEL(JI) > 0) THEN
        ZEL(JI)=ZPHIF(JI,IEL(JI))
      ELSE
        ZEL(JI)=0._JPRB
      ENDIF
    ENDDO
  ENDIF
ENDIF

IF (LL_PT0B.OR.LL_HT0B.OR.LL_HTPW.OR.LLAERO.OR.LL_PT1B.OR.LL_HT1B) THEN

  LLNIVP=.TRUE.
  LLSPCH=LFPQ

  LLWHALF=.FALSE.
  LLWFULL=.FALSE.
  LLGEOP=.TRUE.
  LLNHPD=.FALSE.
  LLWAT=LL_HTPW
  LLICE=LL_HTPW
  LLCFR=.FALSE.
  LLO3=.FALSE.
  LLSN=.FALSE.
  LLRR=.FALSE.
  LLGR=.FALSE.
  LLHL=.FALSE.
  LLTKE=.FALSE.

  ALLOCATE(ZWHPP2(KPROMA,NFLEVG,0:1))
  ALLOCATE(ZPREH2(KPROMA,NFLEVG,0:1))
  DO JL=1,NFLEVG
    ZXLEVG(JL)=REAL(JL,JPRB)
  ENDDO
  CALL PPLETA(KPROMA,KST,KND,NFLEVG,NFLEVG,ZXLEVG,VAH,VBH,ZSPP,ZPREPP, &
   & ZPREH2(1,1,0),ZPREH2(1,1,1))  

  CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,NFLEVG,ZTT0,ZQT0,ZUT0,ZVT0,PSPT0, &
   & POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZDEP0,ZWVEL0,ZSNT0,ZRRT0,ZGRT0,ZHLT0, &
   & ZTKET0,LLNHPD,LLWHALF,LLWFULL,LLWAT,LLICE,LLCFR,LLO3,      &
   & LLSN,LLRR,LLGR,LLHL,LLTKE,ZTPP,ZQPP,ZUPP,ZVPP,ZSPP,ZOOROG,VAH,VBH,    &
   & ZLPP,ZIPP,ZAPP,ZO3PP,ZSNPP,ZRRPP,ZGRPP,ZHLPP,ZTKEPP,                     &
   & ZPDEPP,ZWHPP2,ZWFPP,ZPHIPP,ZHUPP,ZPEORG,LLGEOP,                         &
   & ZPREPP,ZPREH2,ZTSI,ZTSA,FPBL,RATM,LLSPCH,ZMDTDPHI)  

  DEALLOCATE(ZWHPP2)
  DEALLOCATE(ZPREH2)

  IF (LL_PT0B .OR. LL_HT0B) THEN
    LLTOP=.FALSE.
    ZXTEMP=RTT
    CALL PPLTEMP(KPROMA,KST,KND,NFLEVG,ZPHIPP,ZTPP,ZXTEMP,LLTOP,ZHT0B)
    IF (LL_PT0B) THEN
      CALL FPPS(KPROMA,KST,KND,1,POROG,ZHT0B,ZTT0(1,1),ZQT0(1,1),ZIT0(1,1), &
       & ZLT0(1,1),ZRRT0(1,1),ZSNT0(1,1),ZGRHLT0(1,1),PSPT0,ZTSI,ZPT0B)  
    ENDIF
    IF (LL_HT0B) THEN
!     Convert from geopotentials to meters
      DO JI=KST, KND
        ZHT0B(JI)=ZHT0B(JI)*Z1SG
      ENDDO
    ENDIF
  ENDIF
  IF (LL_PT1B .OR. LL_HT1B) THEN
    LLTOP=.FALSE.
    ZXTEMP=RTT-10._JPRB
    CALL PPLTEMP(KPROMA,KST,KND,NFLEVG,ZPHIPP,ZTPP,ZXTEMP,LLTOP,ZHT1B)
    IF (LL_PT1B) THEN
      CALL FPPS(KPROMA,KST,KND,1,POROG,ZHT1B,ZTT0(1,1),ZQT0(1,1),ZIT0(1,1), &
       & ZLT0(1,1),ZRRT0(1,1),ZSNT0(1,1),ZGRHLT0(1,1),PSPT0,ZTSI,ZPT1B)  
    ENDIF
    IF (LL_HT1B) THEN
!     Convert from geopotentials to meters
      DO JI=KST, KND
        ZHT1B(JI)=ZHT1B(JI)*Z1SG
      ENDDO
    ENDIF
  ENDIF

  IF (LL_HTPW) THEN
    ITOP=1
    DO JL=1,NFLEVG
      IF (STPRE(JL) >= PPROOF) THEN
        ITOP=JL
        EXIT
      ENDIF
    ENDDO
    DO JL=ITOP,NFLEVG
      CALL PPWETPOINT(KST,KND,KPROMA,ZPREPP(1,JL),ZTPP(1,JL),ZQPP(1,JL), &
       & ZLPP(1,JL),ZIPP(1,JL),ZTPWPP(1,JL))  
    ENDDO
    DO JL=1,ITOP-1
      DO JI=KST, KND
        ZTPWPP(JI,JL)=ZTPWPP(JI,ITOP)
      ENDDO
    ENDDO
    LLTOP=.FALSE.
    ZXTEMP=RTT
    CALL PPLTEMP(KPROMA,KST,KND,NFLEVG,ZPHIPP,ZTPWPP,ZXTEMP,LLTOP,ZHTPW)
!   Convert from geopotentials to meters
    DO JI=KST, KND
      ZHTPW(JI)=ZHTPW(JI)*Z1SG
    ENDDO
  ENDIF

  IF (LLAERO) THEN
    IPOL=6
    IPOL2=4
    CALL POAERO(ZPHIPP,ZTPP,ZUPP,ZVPP,ZPICAO,ZTICAO,ZUJET,ZVJET,ZPJET,ZPREPP, &
     & KPROMA,KST,KND,NFLEVG,IPOL2)  
  ENDIF

ENDIF

IF (QFPTYPE%LL(TFP_TX%ICOD)) THEN
  DO JI=KST,KND
    ZTMAX(JI) = ZTCLS(JI)+PTXI(JI)
  ENDDO
ENDIF

IF (QFPTYPE%LL(TFP_TN%ICOD)) THEN
  DO JI=KST,KND
    ZTMIN(JI) = ZTCLS(JI)+PTNI(JI)
  ENDDO
ENDIF

IF (QFPTYPE%LL(TFP_HUX%ICOD)) THEN
  DO JI=KST,KND
    ZHUMAX(JI) = MAX(0.0_JPRB,MIN(1.0_JPRB,ZRHCLS(JI)+PHUXI(JI)))
  ENDDO
ENDIF

IF (QFPTYPE%LL(TFP_HUN%ICOD)) THEN
  DO JI=KST,KND
    ZHUMIN(JI) = MAX(0.0_JPRB,MIN(1.0_JPRB,ZRHCLS(JI)+PHUNI(JI)))
  ENDDO
ENDIF

!*       4.    UPPER AIR POST-PROCESSING
!              -------------------------

IF (KOPLEV > 0) THEN

!        3.1   Compute pressure on post-processing levels
!              ------------------------------------------

  ALLOCATE(ZWHPP2(KPROMA,KOPLEV,0:1))
  ALLOCATE(ZPREH2(KPROMA,KOPLEV,0:1))
  SELECT CASE (CDCONF)
  CASE ('E')
    CALL PPLETA(KPROMA,KST,KND,NFPLEV,KOPLEV,PXLEV,FPVALH,FPVBH,ZSPP, &
     & ZPREPP,ZPREH2(1,1,0),ZPREH2(1,1,1))  
  CASE ('Z')
    DO JL=1,KOPLEV
      DO JI=KST,KND
        ZGEOP(JI,JL)=RG*PXLEV(JL)+ZOOROG(JI)
      ENDDO
    ENDDO
    CALL FPPS(KPROMA,KST,KND,KOPLEV,POROG,ZGEOP,ZTT0(1,1),ZQT0(1,1), &
     & ZIT0(1,1),ZLT0(1,1),ZRRT0(1,1),ZSNT0(1,1),ZGRHLT0(1,1),PSPT0, &
     & ZTSI,ZPREPP)  
  CASE DEFAULT 
    CALL ABOR1('ENDPOS : INTERNAL ERROR CDCONF')
  END SELECT

!        4.2   Vertical interpolations on basic fields
!              ---------------------------------------

  LLNIVP=.TRUE.

  LLGEOP =LL_Z  .AND..NOT.LFPQ
  LLWHALF=LL_VD
  LLWFULL=LL_VW
  LLNHPD =LL_PD
  LLWAT=LL_L .OR. LL_TH .OR. LL_ETH .OR. LL_THPW .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLICE=LL_I .OR. LL_TH .OR. LL_ETH .OR. LL_THPW .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLCFR=LL_CLF
  LLO3=LL_O3MX
  LLSN=LL_SN .OR. LL_TH .OR. LL_ETH .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLRR=LL_RR .OR. LL_TH .OR. LL_ETH .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLGR=LL_GR .OR. LL_TH .OR. LL_ETH .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLHL=LL_GR .OR. LL_TH .OR. LL_ETH .OR. LL_IET .OR. LL_SRE .OR. LL_THV
  LLTKE=LL_TKE
  LLSPCH=LFPQ
  CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0,PSPT0, &
   & POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZDEP0,ZWVEL0,ZSNT0,ZRRT0,ZGRT0,ZHLT0, &
   & ZTKET0,LLNHPD,LLWHALF,LLWFULL,LLWAT,LLICE,LLCFR,LLO3,      &
   & LLSN,LLRR,LLGR,LLHL,LLTKE,ZTPP,ZQPP,ZUPP,ZVPP,ZSPP,ZOOROG,ZVAH,ZVBH,    &
   & ZLPP,ZIPP,ZAPP,ZO3PP,ZSNPP,ZRRPP,ZGRPP,ZHLPP,ZTKEPP,                     &
   & ZPDEPP,ZWHPP2,ZWFPP,ZPHIPP,ZHUPP,ZPEORG,LLGEOP,                         &
   & ZPREPP,ZPREH2,ZTSI,ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)  

  IF (LLGR) THEN
    ZGRHLPP(KST:KND,1:KOPLEV)=ZGRPP(KST:KND,1:KOPLEV)
  ELSE
    ZGRHLPP(KST:KND,1:KOPLEV)=0._JPRB
  ENDIF
  IF (LLHL) THEN
    ZGRHLPP(KST:KND,1:KOPLEV)=ZGRHLPP(KST:KND,1:KOPLEV)+ZHLPP(KST:KND,1:KOPLEV)
  ENDIF

  DO JL=1,KOPLEV
    DO JI=KST,KND
      ZHUPP(JI,JL)=MAX(0.0_JPRB,MIN(1.0_JPRB,ZHUPP(JI,JL)))
    ENDDO
  ENDDO

  IF (LL_Z .AND. LFPQ) THEN
    LLGEOP=.TRUE.
    LLWHALF=.FALSE.
    LLWFULL=.FALSE.
    LLNHPD=.FALSE.
    LLWAT=.FALSE.
    LLICE=.FALSE.
    LLCFR=.FALSE.
    LLO3=.FALSE.
    LLSPCH=.TRUE.
    LLSN=.FALSE.
    LLRR=.FALSE.
    LLGR=.FALSE.
    LLHL=.FALSE.
    LLTKE=.FALSE.

    CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0,     &
     & PSPT0,POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZDEP0,ZWVEL0,ZSNT0,     &
     & ZRRT0,ZGRT0,ZHLT0,ZTKET0,LLNHPD,LLWHALF,LLWFULL,LLWAT,LLICE,   &
     & LLCFR,LLO3,LLSN,LLRR,LLGR,LLHL,LLTKE,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZOOROG,  &
     & ZVAH,ZVBH,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZWHPP2,   &
     & ZDUM,ZPHIPP,ZDUM,ZDUM,                                                &
     & LLGEOP,ZDUM,ZPREH2,ZTSI,ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)  
  ENDIF
! Greenhouse Gases (work around as for passive scalers)
  DO JV=1,NGHG
    LLGEOP=.FALSE.
    LLWHALF=.FALSE.
    LLWFULL=.FALSE.
    LLWAT=.FALSE.
    LLICE=.FALSE.
    LLCFR=.FALSE.
    LLO3=.FALSE.
    LLSPCH=.TRUE.
    LLSN=.FALSE.
    LLRR=.FALSE.
    LLGR=.FALSE.
    LLHL=.FALSE.
    LLTKE=.FALSE.
    IF (LL_GHG(JV)) THEN
     CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0, &
    & PSPT0,POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZGHGT0(1,0,JV),ZWVEL0, &
    & ZSNT0,ZRRT0,ZGRT0,ZHLT0,ZTKET0, &
    & LL_GHG(JV),LLWHALF,LLWFULL,LLWAT,LLICE, &
    & LLCFR,LLO3,LLSN,LLRR,LLGR,LLHL,LLTKE,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZOOROG, &
    & ZVAH,ZVBH,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM, &
    & ZGHGPP(1,1,JV),ZWHPP2,ZDUM,ZDUM,ZDUM,ZDUM,LLGEOP,ZDUM,ZPREH2,ZTSI, &
    & ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)
    ENDIF 
  ENDDO
! Aerosols (work around as for passive scalers)
  DO JV=1,NAERO
    LLGEOP=.FALSE.
    LLWHALF=.FALSE.
    LLWFULL=.FALSE.
    LLWAT=.FALSE.
    LLICE=.FALSE.
    LLCFR=.FALSE.
    LLO3=.FALSE.
    LLSPCH=.TRUE.
    LLSN=.FALSE.
    LLRR=.FALSE.
    LLGR=.FALSE.
    LLHL=.FALSE.
    LLTKE=.FALSE.
    IF (LL_AERO(JV)) THEN
      CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0, &
       & PSPT0,POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZAEROT0(1,0,JV),ZWVEL0, &
       & ZSNT0,ZRRT0,ZGRT0,ZHLT0,ZTKET0, &
       & LL_AERO(JV),LLWHALF,LLWFULL,LLWAT,LLICE, &
       & LLCFR,LLO3,LLSN,LLRR,LLGR,LLHL,LLTKE,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZOOROG, &
       & ZVAH,ZVBH,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM, &
       & ZAEROPP(1,1,JV),ZWHPP2,ZDUM,ZDUM,ZDUM,ZDUM,LLGEOP,ZDUM,ZPREH2,ZTSI, &
       & ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)
    ENDIF
  ENDDO

! GRG (work around as for passive scalers)
  DO JV=1,NGRG
    LLGEOP=.FALSE.
    LLWHALF=.FALSE.
    LLWFULL=.FALSE.
    LLWAT=.FALSE.
    LLICE=.FALSE.
    LLCFR=.FALSE.
    LLO3=.FALSE.
    LLSPCH=.TRUE.
    LLSN=.FALSE.
    LLRR=.FALSE.
    LLGR=.FALSE.
    LLHL=.FALSE.
    LLTKE=.FALSE.
    IF (LL_GRG(JV)) THEN
      CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0, &
       & PSPT0,POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZGRGT0(1,0,JV),ZWVEL0, &
       & ZSNT0,ZRRT0,ZGRT0,ZHLT0,ZTKET0, &
       & LL_GRG(JV),LLWHALF,LLWFULL,LLWAT,LLICE, &
       & LLCFR,LLO3,LLSN,LLRR,LLGR,LLHL,LLTKE,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZOOROG, &
       & ZVAH,ZVBH,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM, &
       & ZGRGPP(1,1,JV),ZWHPP2,ZDUM,ZDUM,ZDUM,ZDUM,LLGEOP,ZDUM,ZPREH2,ZTSI, &
       & ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)
    ENDIF
  ENDDO
! Passive scalars (en attendant de faire mieux)
  DO JV=1,NGFL_EXT
    LLGEOP=.FALSE.
    LLWHALF=.FALSE.
    LLWFULL=.FALSE.
    LLWAT=.FALSE.
    LLICE=.FALSE.
    LLCFR=.FALSE.
    LLO3=.FALSE.
    LLSPCH=.TRUE.
    LLSN=.FALSE.
    LLRR=.FALSE.
    LLGR=.FALSE.
    LLHL=.FALSE.
    LLTKE=.FALSE.
    IF (LL_GFL(JV)) THEN
      CALL APACHE(LLNIVP,KPROMA,KST,KND,NFLEVG,KOPLEV,ZTT0,ZQT0,ZUT0,ZVT0, &
       & PSPT0,POROG,VAH,VBH,ZLT0,ZIT0,ZAT0,ZO3T0,ZSVT0(1,0,JV),      &
       & ZWVEL0,ZSNT0,ZRRT0,ZGRT0,ZHLT0,ZTKET0,LL_GFL(JV),LLWHALF,LLWFULL, &
       & LLWAT,LLICE,LLCFR,LLO3,LLSN,LLRR,LLGR,LLHL,LLTKE,ZDUM,ZDUM,ZDUM,&
       & ZDUM,ZDUM,ZOOROG,ZVAH,ZVBH,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,ZDUM,    &
       & ZDUM,ZDUM,ZSVPP(1,1,JV),ZWHPP2,ZDUM,ZDUM,ZDUM,ZDUM,LLGEOP,ZDUM,   &
       & ZPREH2,ZTSI,ZTSO,FPBL,RATM,LLSPCH,ZMDTDPHI)  
    ENDIF 
  ENDDO

!        4.3   Computation of elaborated fields from basic ones
!              ------------------------------------------------

!        4.3.1 Vertical divergence

  IF (LL_VD) THEN
    DO JL=1,KOPLEV
      DO JI=KST,KND
        ZVDIVPP(JI,JL)=RG*(ZWHPP2(JI,JL,1)-ZWHPP2(JI,JL,0))
      ENDDO
    ENDDO
  ENDIF

!        4.3.2 Potential and equivalent potential temperatures
!              and isobaric equivalent temperature.

  IF (LL_TH .OR. LL_ETH .OR. LL_IET) THEN
    CALL GPRCP(KPROMA,KST,KND,KOPLEV,PQ=ZQPP,PQI=ZIPP,PQL=ZLPP,&
     & PQR=ZRRPP,PQS=ZSNPP,PQG=ZGRHLPP,PCP=ZCPPP,PR=ZRPP,PKAP=ZKAPP)
    IF (LL_TH .OR. LL_ETH) THEN
      CALL GPTET(KPROMA,KST,KND,KOPLEV,ZPREPP,ZTPP,ZKAPP,ZTETAPP)
      IF (LL_ETH) THEN
        CALL GPEPT(KPROMA,KST,KND,KOPLEV,ZTETAPP,ZTPP,ZPREPP,ZEPTHPP)
      ENDIF
    ENDIF
    IF (LL_IET) THEN
      CALL GPIET(KPROMA,KST,KND,KOPLEV,ZQPP,ZTPP,ZCPPP,ZPREPP,ZIBETPP)
    ENDIF
  ENDIF

!        4.3.3 Relative moisture in %

  IF (LL_HU .AND. LFPRH100) THEN
    DO JL=1,KOPLEV
      DO JI=KST,KND
        ZHUPP(JI,JL)=ZHUPP(JI,JL)*100.0_JPRB
      ENDDO
    ENDDO
  ENDIF

!        4.3.4 Wet bulb potential temperature

  IF (LL_THPW) THEN
    DO JL=1,KOPLEV
      CALL PPTHPW(KST,KND,KPROMA,ZPREPP(1,JL),ZTPP(1,JL),ZQPP(1,JL), &
       & ZLPP(1,JL),ZIPP(1,JL),ZTHPWPP(1,JL))  
    ENDDO
  ENDIF

!        4.3.5 Wind velocity

  IF (LL_WND) THEN
    DO JL=1,KOPLEV
      DO JI=KST,KND
        ZWIND(JI,JL)=SQRT(ZUPP(JI,JL)*ZUPP(JI,JL)+ZVPP(JI,JL)*ZVPP(JI,JL))
      ENDDO
    ENDDO
  ENDIF

!       4.3.6. Simulated Reflectivities

  IF (LL_SRE) THEN
    LLUNIT=.TRUE.
    CALL GPPRS0D(KPROMA,KST,KND,KOPLEV,ZTPP,ZQPP,ZLPP,ZRRPP, &
     & ZIPP,ZSNPP,ZGRHLPP,LLUNIT,ZSIMRPP)
  ENDIF

!       4.3.7. Virtual theat

  IF (LL_THV) THEN
    CALL PPCVIRT(KPROMA,KST,KND,KOPLEV,ZTETAPP,ZQPP,ZLPP,ZRRPP, &
     & ZSNPP,ZGRHLPP,ZIPP,ZVIRTPP)
  ENDIF

  DEALLOCATE(ZWHPP2)
  DEALLOCATE(ZPREH2)

ENDIF ! (KOPLEV > 0)

!*       5.     SELECT AND TRANSFER POST-PROCESSED FIELDS TO OUTPUT ARRAY
!               ---------------------------------------------------------

! Store data according to the derivative order and the shape of array :

CALL STORE_DATA(TFP_SP%ICOD  ,PDATA1=ZSPP   )
CALL STORE_DATA(TFP_LNSP%ICOD,PDATA1=ZLNSP  )
CALL STORE_DATA(TFP_WWS%ICOD ,PDATA1=ZWSPP  )
CALL STORE_DATA(TFP_UCLS%ICOD,PDATA1=ZUCLS  )
CALL STORE_DATA(TFP_VCLS%ICOD,PDATA1=ZVCLS  )
CALL STORE_DATA(TFP_TCLS%ICOD,PDATA1=ZTCLS  )
CALL STORE_DATA(TFP_QCLS%ICOD,PDATA1=ZQCLS  )
CALL STORE_DATA(TFP_RCLS%ICOD,PDATA1=ZRHCLS )
CALL STORE_DATA(TFP_FCLS%ICOD,PDATA1=ZFCLS  )
CALL STORE_DATA(TFP_TN%ICOD  ,PDATA1=ZTMIN  )
CALL STORE_DATA(TFP_TX%ICOD  ,PDATA1=ZTMAX  )
CALL STORE_DATA(TFP_HUX%ICOD ,PDATA1=ZHUMAX )
CALL STORE_DATA(TFP_HUN%ICOD ,PDATA1=ZHUMIN )
CALL STORE_DATA(TFP_UGST%ICOD,PDATA1=ZUGST  )
CALL STORE_DATA(TFP_VGST%ICOD,PDATA1=ZVGST  )
CALL STORE_DATA(TFP_FGST%ICOD,PDATA1=ZFGST  )
CALL STORE_DATA(TFP_CAPE%ICOD,PDATA1=ZCAPE  )
CALL STORE_DATA(TFP_CIEN%ICOD,PDATA1=ZCIN   )
CALL STORE_DATA(TFP_PT0B%ICOD,PDATA1=ZPT0B  )
CALL STORE_DATA(TFP_HT0B%ICOD,PDATA1=ZHT0B  )
CALL STORE_DATA(TFP_PT1B%ICOD,PDATA1=ZPT1B  )
CALL STORE_DATA(TFP_HT1B%ICOD,PDATA1=ZHT1B  )
CALL STORE_DATA(TFP_HTPW%ICOD,PDATA1=ZHTPW  )
CALL STORE_DATA(TFP_UJET%ICOD,PDATA1=ZUJET  )
CALL STORE_DATA(TFP_VJET%ICOD,PDATA1=ZVJET  )
CALL STORE_DATA(TFP_PJET%ICOD,PDATA1=ZPJET  )
CALL STORE_DATA(TFP_PCAO%ICOD,PDATA1=ZPICAO )
CALL STORE_DATA(TFP_TCAO%ICOD,PDATA1=ZTICAO )

CALL STORE_DATA(TFP_U%ICOD   ,PDATA2=ZUPP   )
CALL STORE_DATA(TFP_V%ICOD   ,PDATA2=ZVPP   )
CALL STORE_DATA(TFP_T%ICOD   ,PDATA2=ZTPP   )
CALL STORE_DATA(TFP_Z%ICOD   ,PDATA2=ZPHIPP )
CALL STORE_DATA(TFP_Q%ICOD   ,PDATA2=ZQPP   )
CALL STORE_DATA(TFP_L%ICOD   ,PDATA2=ZLPP   )
CALL STORE_DATA(TFP_I%ICOD   ,PDATA2=ZIPP   )
CALL STORE_DATA(TFP_CLF%ICOD ,PDATA2=ZAPP   )
CALL STORE_DATA(TFP_O3MX%ICOD,PDATA2=ZO3PP  )
CALL STORE_DATA(TFP_SN%ICOD  ,PDATA2=ZSNPP  )
CALL STORE_DATA(TFP_RR%ICOD  ,PDATA2=ZRRPP  )
CALL STORE_DATA(TFP_GR%ICOD  ,PDATA2=ZGRPP  )
CALL STORE_DATA(TFP_HL%ICOD  ,PDATA2=ZHLPP  )
CALL STORE_DATA(TFP_TKE%ICOD ,PDATA2=ZTKEPP )
CALL STORE_DATA(TFP_HU%ICOD  ,PDATA2=ZHUPP  )
CALL STORE_DATA(TFP_TH%ICOD  ,PDATA2=ZTETAPP)
CALL STORE_DATA(TFP_THPW%ICOD,PDATA2=ZTHPWPP)
CALL STORE_DATA(TFP_ETH%ICOD ,PDATA2=ZEPTHPP)
CALL STORE_DATA(TFP_IET%ICOD ,PDATA2=ZIBETPP)
CALL STORE_DATA(TFP_WND%ICOD ,PDATA2=ZWIND  )
CALL STORE_DATA(TFP_P%ICOD   ,PDATA2=ZPREPP )
CALL STORE_DATA(TFP_PD%ICOD  ,PDATA2=ZPDEPP )
CALL STORE_DATA(TFP_VD%ICOD  ,PDATA2=ZVDIVPP)
CALL STORE_DATA(TFP_VW%ICOD  ,PDATA2=ZWFPP  )
CALL STORE_DATA(TFP_SRE%ICOD ,PDATA2=ZSIMRPP)
CALL STORE_DATA(TFP_THV%ICOD ,PDATA2=ZVIRTPP)
CALL STORE_DATA(TFP_LCL%ICOD ,PDATA1=ZLCL)
CALL STORE_DATA(TFP_FCL%ICOD ,PDATA1=ZFCL)
CALL STORE_DATA(TFP_EL%ICOD ,PDATA1=ZEL)


DO JV=1,NGHG
  CALL STORE_DATA(TFP_GHG(JV)%ICOD,PDATA2=ZGHGPP(1:KPROMA,1:IMAXLEV,JV))
ENDDO

DO JV=1,NAERO
  CALL STORE_DATA(TFP_AERO(JV)%ICOD,PDATA2=ZAEROPP(1:KPROMA,1:IMAXLEV,JV))
ENDDO

DO JV=1,NGRG
  CALL STORE_DATA(TFP_GRG(JV)%ICOD,PDATA2=ZGRGPP(1:KPROMA,1:IMAXLEV,JV))
ENDDO

DO JV=1,NGFL_EXT
  CALL STORE_DATA(TFP_GFL(JV)%ICOD,PDATA2=ZSVPP(1:KPROMA,1:IMAXLEV,JV))
ENDDO

DO JV=1, JPOSVX2
  I=TFP_FUA(JV)%ICOD
  IF (QFPTYPE%LL(I)) THEN
    DO JL=1,QFPTYPE%ILEV(I)
      ILOC=QFPTYPE%IGFP(I)+JL-1
      ILPT=QFPTYPE%ILVP(JL,I)
      DO JI=KST, KND 
        PFP(JI,ILOC) = 0.0_JPRB
      ENDDO
    ENDDO
  ENDIF
ENDDO

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

IF (LHOOK) CALL DR_HOOK('ENDPOS',1,ZHOOK_HANDLE)

!------------------------------------------------------------------
CONTAINS

SUBROUTINE STORE_DATA(K,PDATA1,PDATA2)

!------------------------------------------------------------------
INTEGER(KIND=JPIM), INTENT(IN) :: K
REAL(KIND=JPRB)   , INTENT(IN), OPTIONAL :: PDATA1(:)
REAL(KIND=JPRB)   , INTENT(IN), OPTIONAL :: PDATA2(:,:)
!------------------------------------------------------------------
REAL(KIND=JPRB) :: ZHOOK_HANDLE
!------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('ENDPOS:STORE_DATA',0,ZHOOK_HANDLE)
!------------------------------------------------------------------

IF (QFPTYPE%LL(K)) THEN
  IF (PRESENT(PDATA1)) THEN
    ILOC=QFPTYPE%IGFP(K)
    SELECT CASE (ABS(TFP_DYNDS(K)%IORDR))
    CASE (0)
      DO JI=KST, KND
        PFP(JI,ILOC) = PDATA1(JI)
      ENDDO
    CASE (1)
      DO JI=KST, KND
        PFP(JI,ILOC) = PDATA1(JI)*Z1SGM(JI)
      ENDDO
    CASE (2)
      DO JI=KST, KND
        PFP(JI,ILOC) = PDATA1(JI)*(Z1SGM(JI)*Z1SGM(JI))
      ENDDO
    END SELECT
  ELSEIF (PRESENT(PDATA2)) THEN
    DO JL=1,QFPTYPE%ILEV(K)
      ILOC=QFPTYPE%IGFP(K)+JL-1
      ILPT=QFPTYPE%ILVP(JL,K)
      SELECT CASE (ABS(TFP_DYNDS(K)%IORDR))
      CASE (0)
        DO JI=KST, KND
          PFP(JI,ILOC) = PDATA2(JI,ILPT)
        ENDDO
      CASE (1)
        DO JI=KST, KND
          PFP(JI,ILOC) = PDATA2(JI,ILPT)*Z1SGM(JI)
        ENDDO
      CASE (2)
        DO JI=KST, KND
          PFP(JI,ILOC) = PDATA2(JI,ILPT)*(Z1SGM(JI)*Z1SGM(JI))
        ENDDO
      END SELECT
    ENDDO
  ELSE
    CALL ABOR1('ENPOS.STORE_DATA : INTERFACE ERROR')
  ENDIF 
ENDIF    
!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('ENDPOS:STORE_DATA',1,ZHOOK_HANDLE)
END SUBROUTINE STORE_DATA

!     ------------------------------------------------------------------
END SUBROUTINE ENDPOS
