SUBROUTINE CPEDIA(KPROMA,KIDIA,KFDIA,KSTGLO,LDFSTEP,LDMLPP,LDRS6,LDRSACC,&
 & PFPLCL ,PFPLCN ,PFPLSL ,PFPLSN,&
 & PFRSO  ,PFRSOD ,PFRTH  ,PFRTHD ,PUVDF  , PPARF ,&
 & PCAPE , PPARCF, PTINCF ,PFDIR  ,PCDIR  , PCBASE,P0DEGL ,PVISIH,&
 & PDIFTS ,PDIFTQ ,PSTRTU ,PSTRTV,&
 & PFCLL  ,PFCLN  ,PTCLS  ,PISUND,&
 & PVDIS  ,PVDISG ,PUSTRG,&
 & PVSTRG ,PRUISS ,PRUISP,&
 & PRESH  ,PRESF  ,PDELP,&
 & PT     ,PQ     ,PO3    ,PGHG   ,PTRAC ,PGRG   ,PAERO,&
 & PL     ,PR     ,PI     ,PS     ,PTENQ, POROG,&
 & PCH4S  ,PDLSP  ,PDCP   ,PDSF   ,PDBLD  ,PSUND,&
 & PITTRC ,PITSRC ,PISTRC ,PISSRC ,PIES, PISMLT, PI10FG, PILSPF, PIVIMD,&
 & PVTTRC ,PVTSRC ,PVSTRC ,PVSSRC ,PVES, PVSMLT, PV10FG, PV10FG6,PVLSPF,PVVIMD,&
 & PDSSHF ,PDSLHF ,PDSP   ,PDMSL  ,&
 & PDSSR  ,PDSTR  ,PDSSRD ,PDSTRD ,PDTSR  ,PDTTR,&
 & PDUVDF ,PDPARF ,PDCAPE ,PDPARCF,PDTINCF,PDFDIR,PDCDIR,PDCBASE,PD0DEGL,PDVISIH,&
 & PDEWSS ,PDNSSS ,PDE    ,PDLGWS,&
 & PDMGWS ,PDGWD  ,PDMX2T ,PDMN2T ,PDMX2T6,PDMN2T6,PDRO,PDSSRO,PDSRO,&
 & PDCSF  ,PDLSSF,&
 & PDIEWSS,PDINSSS,PDISSHF,PDIE   ,&
 & PDTCW  ,PDTCWV ,PDTCLW ,PDTCIW, PDTCRW, PDTCSW, PDTCO3, &
 & PDTCGHG,PDTCTRAC,PDTCGRG,PDTCCH4S,PDTCAERO)  

!**** *CPEDIA* - DIAGNOSTIC FIELDS FROM ECMWF PHYSICS

!     Purpose.
!     --------

!           COMPUTE THE DIAGNOSTIC FIELDS FOR FROM ECMWF PHYSICS.

!**   Interface.
!     ----------
!        *CALL* *CPEDIA(...)*

!        Explicit arguments :
!        --------------------
!        KPROMA   : dimension
!        KIDIA    : start of horizontal loop
!        KFDIA    : end of horizontal loop
!        LDFSTEP  : .true. if first step
!        LDMLPP   : .true. if first step after a write-out
!        LDRS6    : .true. to reset 6 hourly min/max bins
!        LDRSACC  : .true. to reset accumulated fluxes at PP time
! Inputs for CPEDIA = outputs from APLPAR (same names as in APLPAR):

!        PFPLCL   : convective precipitation as rain.
!        PFPLCN   : convective precipitation as snow.
!        PFPLSL   : stratiform precipitation as rain.
!        PFPLSN   : stratiform precipitation as snow.
!        PFRSO    : shortwave radiative flux.
!        PFRTH    : longwave radiative flux.
!        PFRSOD   : downward shortwave radiative flux at surface.
!        PFRTHD   : downward longwave radiative flux at surface.
!        PUVDF    : downward UV flux at surface
!        PPARF    : downward PAR flux at surface
!        PCAPE    : CAPE
!        PPARCF   : clear-sky downward PAR flux at surface
!        PTINCF   : TOA inclident solar radiation
!        PFDIR    : total sky surface direct SW radiation
!        PCDIR    : clear-sky surface direct SW radiation
!        PCBASE   : cloud base height
!        P0DEGL   : zero deg level
!        PVISIH   : horizontal visibility
!        PDIFTS   : sensible heat flux from vertical diffusion
!        PDIFTQ   : moisture flux from vertical diffusion
!        PSTRTU   : flux of u momentum from vertical diffusion
!        PSTRTV   : flux of v momentum from vertical diffusion
!        PFCLL    : latent heat flux corresponding to evaporation   
!        PFCLN    : latent heat flux corresponding to sublimation    
!        PTCLS    : temperature at 2 meters (diagnostic).
!        PRESH    : half level pressure
!        PRESF    : full level pressure
!        PDELP    : pressure difference across layers
!        PT       : temperature
!        PQ       : Q
!        PO3      : ozone mixing ratio
!        PGHG(NGHG): GHG mixing ratio
!        PCH4S     : CH4 atmospheric sink (kg/kg)
!        PTRAC(NTRAC): TRAC mixing ratio
!        PGRG(NGRG): GRG mixing ratio
!        PAERO(NACTAERO): AERO mixing ratio
!        PL       : Cloud liquid water
!        PR       : Rain liquid water
!        PI       : Cloud ice
!        PS       : Cloud snow
!        POROG    : orography
!        PITTRC   : top thermal radiation clear sky
!        PITSRC   : top solar radiation clear sky
!        PISTRC   : surface thermal radiation clear sky
!        PISSRC   : surface solar radiation clear sky 
!        PIES     : snow evaporation
!        PISMLT   : snow melt
!        PI10FG   : gust a 10 m
!        PILSPF   : large scale precipitation fraction
!        PIVIMD   : vertically-integrated mass divergence

!        PDLSP    : large scale precipitation             (output)
!        PDCP     : convective precipitation              (output)
!        PDSF     : snow fall                             (output)
!        PDBLD    : boundary layer dissipitaion           (output)
!        PDSSHF   : surface sensible heat flux            (output)
!        PDSLHF   : surface latent heat flux              (output)
!        PDSP     : surface pressure                      (output)
!        PDMSL    : mean sea level pressure               (output)
!        PDSSR    : surface solar radiation               (output)
!        PDSTR    : surface thermal radiation             (output)
!        PDSSRD   : surface solar radiation downwards     (output)
!        PDSTRD   : surface thermal radiation downwards   (output)
!        PDTSR    : top solar radiation                   (output)
!        PDTTR    : top thermal radiation                 (output)
!        PDUVDF   : surface UV                            (output)
!        PDPARF   : surface PAR                           (output)
!        PDCAPE   : CAPE                                  (output)
!        PDPARCF  : surface clear-sky PAR                 (output)
!        PDTINCF  : TOA incident solar radiation          (output)
!        PDCBASE  : cloud base height                     (output)
!        P0DEGL   : zero deg level                        (output)
!        PVISIH   : horizontal visibility                 (output)
!        PDFDIR   : total sky surface direct SW radiation (output)
!        PDCDIR   : clear-sky surface direct SW radiation (output) 
!        PDEWSS   : U-sterss                              (output)
!        PDNSSS   : V-stress                              (output)
!        PDE      : evaporation                           (output)
!        PDLGWS   : lat. comp. of gravity wave stress     (output)
!        PDMGWS   : mer. comp. of gravity wave stress     (output)
!        PDGWD    : gravity wave dissipation              (output)
!        PDMX2T   : max temp. at 2 m since prev. p.p.     (output)
!        PDMN2T   : min temp. at 2 m since prev. p.p.     (output)
!        PDMX2T6  : max temp. at 2 m since last 6 hours   (output)
!        PDMN2T6  : min temp. at 2 m since last 6 hours   (output)
!        PDRO     : runoff                                (output)
!        PDSRO    : Surface runoff                        (output)
!        PDSSRO   : Sub-surface runoff                    (output)
!        PDCSF    ; convective snow fall                  (output)
!        PDLSSF   : large scale snow fall                 (output)
!        PDIE     : instantaneous water vapour flux over  (output)
!                   liquid water (or wet soil)         
!        PDISSHF  : instantaneous sensible heat flux at   (output)
!                   surface level.
!        PDIEWSS  : instantaneous surface u-stress        (output) 
!        PDINSSS  : instantaneous surface v-stress        (output)
!        PDTCW    : total column water (all phases)       (output)
!        PDTCWV   : total column water vapour             (output)
!        PDTCLW   : total column liquid water             (output)
!        PDTCIW   : total column ice water                (output)
!        PDTCRW   : total column rain water               (output)
!        PDTCSW   : total column snow water               (output)
!        PDTCO3   : total column ozone                    (output)
!        PDTCGHG  : total column GHG fields               (output)
!        PDTCTRAC : total column TRAC fields              (output)
!        PDTCGRG  : total column GRG fields               (output)
!        PDTCCH4S : total column of CH4 atmospheric sink  (output)
!        PDTCAERO : total column AERO fields              (output)
!        PVTTRC   : top thermal radiation clear sky       (output)
!        PVTSRC   : top solar radiation clear sky         (output)
!        PVSTRC   : surface thermal radiation clear sky   (output)
!        PVSSRC   : surface solar radiation clear sky     (output)
!        PVES     : snow evaporation                      (output)
!        PVSMLT   : snow melt                             (output)
!        PV10FG   : gust a 10 m since prev. p.p.          (output)
!        PV10FG6  : gust a 10 m since last 6 hours        (output)
!        PVLSPF   : large scale precipitation fraction    (output)
!        PVVIMD   : vertically-integrated mass divergence (output)      

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

!     Method.
!     -------

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

!     Reference.
!     ----------

!     Author.
!     -------
!        M. Hamrud    ECMWF

!     Modifications.
!     --------------
!      Original     : 93-02-15
!      Modification : 93-06-01 M.Hamrud (Runoff fields)
!      Modification : 93-09-15 M.Hamrud/P.Viterbo (more fields)
!      Modification : 94-04-26 M.Hamrud (max and min temp. mod.)
!      Modification : 96-06-04 D.Dent   (reset accumulated fluxes)
!      Modification : 99-03-26 J.Teixeira  (surface tiling)
!      Modified     : 00-03-06 by A.Beljaars: PP of TTRC,TSRC,STRC,SSRC,ES,SMLT,10FG,LSPF
!      Modified     : 00-11-27 S. Abdalla: Force MSL computations at first step (line~350)
!      Modified     : 15-10-01 D.Salmond FULLIMP mods
!      Modified     : 02-09-01 JJMorcrette UV, PAR, CAPE
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!      Modified     : 04-02-12 A.Tompkins TCLW, TCIW
!      Modified     : 15-Dec-05 A.Dethof Add total column GRG fields
!      Modified     : 26-Sep-05 S.Serrar Add total column CO2, SF6
!      Modified     : 02-Jun-06 R. Engelen CO2 replaced by generic GHG
!      Modified     : 07-Sep-06 S. Serrar tracers for diagnostics
!      JJMorcrette 20060721 PP of clear-sky PAR and TOA incident solar radiation
!      JJMorcrette 20060807 PP of VIMD
!      Modified     : 17-Jul-07 S.Serrar variable for methane atmospheric sink
!      Modified     : 20061002  JJMorcrette DDH for aerosol physics
!      JJMorcrette    20100212  PP for CBASE, 0DEGL, VISIH
!      JJMorcrette 20091201 Total and clear-sky direct SW radiation at the surface
!      Modified     : 01-Mar-10 R. Forbes TCRW, TCSW
!      Modified     : 25-Mar-10 T. Wilhelmsson Add 6 hourly min/max fields
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NFLEVG 
USE YOMDYN   , ONLY : TSTEP
USE YOMSTA   , ONLY : NLEXTRAP
USE YOMPPC   , ONLY : M2DGGP ,NO2DGG
USE YOMFPC   , ONLY : MFPPHY ,NFPPHY
USE YOMCT0   , ONLY : LFPOS
USE YOM_YGFL, ONLY :  YO3, YL, YI, YR, YS, &
                 &    YGHG, NGHG , YTRAC, NTRAC ,YGRG, NGRG, YAERO, NACTAERO , YCH4S  

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTGLO 
LOGICAL           ,INTENT(IN)    :: LDFSTEP 
LOGICAL           ,INTENT(IN)    :: LDMLPP 
LOGICAL           ,INTENT(IN)    :: LDRSACC 
LOGICAL           ,INTENT(IN)    :: LDRS6 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCL(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCN(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSL(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSN(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRSO(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRSOD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTH(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTHD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUVDF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPARF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCAPE(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPARCF(KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTINCF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCBASE(KPROMA), P0DEGL(KPROMA), PVISIH(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFDIR(KPROMA), PCDIR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTS(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQ(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTU(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTV(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCLL(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCLN(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTCLS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PISUND(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVDIS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVDISG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUSTRG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVSTRG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRUISS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRUISP(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRESH(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRESF(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KPROMA,0:NFLEVG+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PO3(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGHG(KPROMA,NFLEVG,NGHG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCH4S(KPROMA,NFLEVG)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTRAC(KPROMA,NFLEVG,NTRAC)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGRG(KPROMA,NFLEVG,NGRG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAERO(KPROMA,NFLEVG,NACTAERO)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENQ(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PL(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PR(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PI(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PS(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLSP(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDCP(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDBLD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSUND(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PITTRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PITSRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PISTRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PISSRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PIES(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PISMLT(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PI10FG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PILSPF(KPROMA)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PIVIMD(KPROMA) 

REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVTTRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVTSRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVSTRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVSSRC(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVES(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVSMLT(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV10FG(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV10FG6(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVLSPF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSSHF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSLHF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVVIMD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDSP(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDMSL(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSSR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSTR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSSRD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSTRD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDTSR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDTTR(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDUVDF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDPARF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDCAPE(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDPARCF(KPROMA)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDTINCF(KPROMA)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDCBASE(KPROMA), PD0DEGL(KPROMA), PDVISIH(KPROMA)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDFDIR(KPROMA), PDCDIR(KPROMA)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDEWSS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDNSSS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDE(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLGWS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMGWS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDGWD(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMX2T(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMN2T(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMX2T6(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDMN2T6(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDRO(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSRO(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDSSRO(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDCSF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDLSSF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIEWSS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDINSSS(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDISSHF(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIE(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCW(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCWV(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCLW(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCIW(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCRW(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCSW(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCO3(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCGHG(KPROMA,NGHG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCTRAC(KPROMA,NTRAC)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCGRG(KPROMA,NGRG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCCH4S(KPROMA)
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDTCAERO(KPROMA,NACTAERO) 

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

INTEGER(KIND=JPIM), EXTERNAL :: ISRCHEQ

REAL(KIND=JPRB) :: ZTB    (KPROMA),ZPRESBH(KPROMA),ZPRESBF(KPROMA)
REAL(KIND=JPRB) :: ZTSTAR (KPROMA),ZT0    (KPROMA)
REAL(KIND=JPRB) :: ZWCH   (KPROMA,0:NFLEVG)
REAL(KIND=JPRB) :: ZWVLI  (KPROMA,NFLEVG)
REAL(KIND=JPRB) :: ZCLD   (KPROMA,NFLEVG)
INTEGER(KIND=JPIM) :: IMN2T, IMX2T, I10FG
INTEGER(KIND=JPIM) :: JK, JROF, JGHG, JTRAC ,JGRG, JAERO

LOGICAL :: LLMN2TPP, LLMX2TPP, LL10FGPP

REAL(KIND=JPRB) :: ZTSTEP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "ctstar.intfb.h"
#include "gppwc.intfb.h"
#include "gptco3.intfb.h"
#include "pppmer.intfb.h"

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

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

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

!*       1.    COMPUTE DIAGNOSTIC QUANTITIES.
!              ------------------------------

!*       1.1  ACCUMULATED FIELDS

ZTSTEP=TSTEP

! Vertically-integrated mass divergence

CALL GPTCO3(KPROMA,KIDIA,KFDIA,NFLEVG,PDELP,PTENQ,PIVIMD)

IF(LDRSACC.AND.(LDFSTEP.OR.LDMLPP)) THEN
!        reset accumulated fluxes
  DO JROF=KIDIA,KFDIA
    PDCP  (JROF)=(PFPLCL(JROF,NFLEVG)+PFPLCN(JROF,NFLEVG))*ZTSTEP
    PDLSP (JROF)=(PFPLSL(JROF,NFLEVG)+PFPLSN(JROF,NFLEVG))*ZTSTEP
    PDSF  (JROF)=(PFPLCN(JROF,NFLEVG)+PFPLSN(JROF,NFLEVG))*ZTSTEP
    PDCSF (JROF)= PFPLCN(JROF,NFLEVG)*ZTSTEP
    PDLSSF(JROF)= PFPLSN(JROF,NFLEVG)*ZTSTEP
    PDSLHF(JROF)=(PFCLL (JROF)+PFCLN(JROF))*ZTSTEP
    PDSSHF(JROF)= PDIFTS(JROF,NFLEVG)*ZTSTEP
    PDE   (JROF)= PDIFTQ(JROF,NFLEVG)*ZTSTEP
    PDBLD (JROF)= PVDIS (JROF)       *ZTSTEP
    PDEWSS(JROF)= PSTRTU(JROF,NFLEVG)*ZTSTEP
    PDNSSS(JROF)= PSTRTV(JROF,NFLEVG)*ZTSTEP
    PDGWD (JROF)= PVDISG(JROF)       *ZTSTEP
    PDLGWS(JROF)= PUSTRG(JROF)       *ZTSTEP
    PDMGWS(JROF)= PVSTRG(JROF)       *ZTSTEP
    PDRO  (JROF)=(PRUISS(JROF) + PRUISP(JROF)      )*ZTSTEP
    PDSSRO(JROF)=(PRUISP(JROF)       )*ZTSTEP
    PDSRO (JROF)=(PRUISS(JROF)       )*ZTSTEP
    PDSSR (JROF)= PFRSO (JROF,NFLEVG)*ZTSTEP
    PDSSRD(JROF)= PFRSOD(JROF)       *ZTSTEP
    PDSTR (JROF)= PFRTH (JROF,NFLEVG)*ZTSTEP
    PDSTRD(JROF)= PFRTHD(JROF)       *ZTSTEP
    PDTSR (JROF)= PFRSO (JROF,0     )*ZTSTEP
    PDTTR (JROF)= PFRTH (JROF,0     )*ZTSTEP
    PSUND (JROF)= PISUND(JROF)       *ZTSTEP
    PDUVDF(JROF)= PUVDF (JROF)       *ZTSTEP
    PDPARF(JROF)= PPARF (JROF)       *ZTSTEP
!    PDCAPE(JROF)= PCAPE (JROF)       *ZTSTEP
    PDPARCF(JROF)=PPARCF(JROF)       *ZTSTEP
    PDTINCF(JROF)=PTINCF(JROF)       *ZTSTEP
    PDFDIR(JROF)= PFDIR(JROF)        *ZTSTEP
    PDCDIR(JROF)= PCDIR(JROF)        *ZTSTEP

    PVTTRC(JROF)= PITTRC(JROF)       *ZTSTEP
    PVTSRC(JROF)= PITSRC(JROF)       *ZTSTEP
    PVSTRC(JROF)= PISTRC(JROF)       *ZTSTEP
    PVSSRC(JROF)= PISSRC(JROF)       *ZTSTEP
    PVES  (JROF)= PIES  (JROF)       *ZTSTEP
    PVSMLT(JROF)= PISMLT(JROF)       *ZTSTEP
    PVLSPF(JROF)= PILSPF(JROF)       *ZTSTEP
    PVVIMD(JROF)= PIVIMD(JROF)       *ZTSTEP

  ENDDO
ELSE

  DO JROF=KIDIA,KFDIA
    PDCP  (JROF)=PDCP  (JROF)+(PFPLCL(JROF,NFLEVG)+PFPLCN(JROF,NFLEVG))*ZTSTEP
    PDLSP (JROF)=PDLSP (JROF)+(PFPLSL(JROF,NFLEVG)+PFPLSN(JROF,NFLEVG))*ZTSTEP
    PDSF  (JROF)=PDSF  (JROF)+(PFPLCN(JROF,NFLEVG)+PFPLSN(JROF,NFLEVG))*ZTSTEP
    PDCSF (JROF)=PDCSF (JROF)+ PFPLCN(JROF,NFLEVG)*ZTSTEP
    PDLSSF(JROF)=PDLSSF(JROF)+ PFPLSN(JROF,NFLEVG)*ZTSTEP
    PDSLHF(JROF)=PDSLHF(JROF)+(PFCLL (JROF)+PFCLN(JROF))*ZTSTEP
    PDSSHF(JROF)=PDSSHF(JROF)+ PDIFTS(JROF,NFLEVG)*ZTSTEP
    PDE   (JROF)=PDE   (JROF)+ PDIFTQ(JROF,NFLEVG)*ZTSTEP
    PDBLD (JROF)=PDBLD (JROF)+ PVDIS (JROF)       *ZTSTEP
    PDEWSS(JROF)=PDEWSS(JROF)+ PSTRTU(JROF,NFLEVG)*ZTSTEP
    PDNSSS(JROF)=PDNSSS(JROF)+ PSTRTV(JROF,NFLEVG)*ZTSTEP
    PDGWD (JROF)=PDGWD (JROF)+ PVDISG(JROF)       *ZTSTEP
    PDLGWS(JROF)=PDLGWS(JROF)+ PUSTRG(JROF)       *ZTSTEP
    PDMGWS(JROF)=PDMGWS(JROF)+ PVSTRG(JROF)       *ZTSTEP
    PDRO  (JROF)=PDRO  (JROF)+(PRUISS(JROF)+ PRUISP(JROF)      )*ZTSTEP
    PDSSRO(JROF)=PDSSRO(JROF)+(PRUISP(JROF)      )*ZTSTEP
    PDSRO (JROF)=PDSRO (JROF)+(PRUISS(JROF)      )*ZTSTEP
    PDSSR (JROF)=PDSSR (JROF)+ PFRSO (JROF,NFLEVG)*ZTSTEP
    PDSSRD(JROF)=PDSSRD(JROF)+ PFRSOD(JROF)       *ZTSTEP
    PDSTR (JROF)=PDSTR (JROF)+ PFRTH (JROF,NFLEVG)*ZTSTEP
    PDSTRD(JROF)=PDSTRD(JROF)+ PFRTHD(JROF)       *ZTSTEP
    PDTSR (JROF)=PDTSR (JROF)+ PFRSO (JROF,0     )*ZTSTEP
    PDTTR (JROF)=PDTTR (JROF)+ PFRTH (JROF,0     )*ZTSTEP
    PSUND (JROF)=PSUND (JROF)+ PISUND(JROF)       *ZTSTEP
    PDUVDF(JROF)=PDUVDF(JROF)+ PUVDF (JROF)       *ZTSTEP
    PDPARF(JROF)=PDPARF(JROF)+ PPARF (JROF)       *ZTSTEP
!    PDCAPE(JROF)=PDCAPE(JROF)+ PCAPE (JROF)       *ZTSTEP
    PDPARCF(JROF)=PDPARCF(JROF)+PPARCF(JROF)      *ZTSTEP
    PDTINCF(JROF)=PDTINCF(JROF)+PTINCF(JROF)      *ZTSTEP
    PDFDIR(JROF)=PDFDIR(JROF)+ PFDIR(JROF)        *ZTSTEP
    PDCDIR(JROF)=PDCDIR(JROF)+ PCDIR(JROF)        *ZTSTEP

    PVTTRC(JROF)=PVTTRC(JROF)+ PITTRC(JROF)       *ZTSTEP
    PVTSRC(JROF)=PVTSRC(JROF)+ PITSRC(JROF)       *ZTSTEP
    PVSTRC(JROF)=PVSTRC(JROF)+ PISTRC(JROF)       *ZTSTEP
    PVSSRC(JROF)=PVSSRC(JROF)+ PISSRC(JROF)       *ZTSTEP
    PVES  (JROF)=PVES  (JROF)+ PIES  (JROF)       *ZTSTEP
    PVSMLT(JROF)=PVSMLT(JROF)+ PISMLT(JROF)       *ZTSTEP
    PVLSPF(JROF)=PVLSPF(JROF)+ PILSPF(JROF)       *ZTSTEP
    PVVIMD(JROF)=PVVIMD(JROF)+ PIVIMD(JROF)       *ZTSTEP

  ENDDO

ENDIF

!*       1.2  INSTANTANEOUS FIELDS

LLMX2TPP =.FALSE.
LLMN2TPP =.FALSE.
LL10FGPP =.FALSE.
IF(LDMLPP ) THEN
  IF(LFPOS) THEN
    IMX2T = ISRCHEQ(NFPPHY,MFPPHY,1,201)
    IF(IMX2T <= NFPPHY) LLMX2TPP=.TRUE.
    IMN2T = ISRCHEQ(NFPPHY,MFPPHY,1,202)
    IF(IMN2T <= NFPPHY) LLMN2TPP=.TRUE.
    I10FG = ISRCHEQ(NFPPHY,MFPPHY,1,49)
    IF(I10FG <= NFPPHY) LL10FGPP=.TRUE.
  ELSE
    IMX2T = ISRCHEQ(NO2DGG,M2DGGP,1,201)
    IF(IMX2T <= NO2DGG) LLMX2TPP=.TRUE.
    IMN2T = ISRCHEQ(NO2DGG,M2DGGP,1,202)
    IF(IMN2T <= NO2DGG) LLMN2TPP=.TRUE.
    I10FG = ISRCHEQ(NO2DGG,M2DGGP,1,49)
    IF(I10FG <= NO2DGG) LL10FGPP=.TRUE.
  ENDIF
ENDIF

DO JROF=KIDIA,KFDIA

  IF(LLMX2TPP.OR.LDFSTEP) THEN
    PDMX2T(JROF) = PTCLS(JROF)
  ELSE
    PDMX2T(JROF) = MAX(PTCLS(JROF),PDMX2T(JROF))
  ENDIF
  IF(LLMN2TPP.OR.LDFSTEP) THEN
    PDMN2T(JROF) = PTCLS(JROF)
  ELSE
    PDMN2T(JROF) = MIN(PTCLS(JROF),PDMN2T(JROF))
  ENDIF
  IF(LL10FGPP.OR.LDFSTEP) THEN
    PV10FG(JROF) = PI10FG(JROF)
  ELSE
    PV10FG(JROF) = MAX(PI10FG(JROF),PV10FG(JROF))
  ENDIF

  IF(LDRS6.OR.LDFSTEP) THEN
    PDMX2T6(JROF) = PTCLS(JROF)
    PDMN2T6(JROF) = PTCLS(JROF)
    PV10FG6(JROF) = PI10FG(JROF)
  ELSE
    PDMX2T6(JROF) = MAX(PTCLS(JROF),PDMX2T6(JROF))
    PDMN2T6(JROF) = MIN(PTCLS(JROF),PDMN2T6(JROF))
    PV10FG6(JROF) = MAX(PI10FG(JROF),PV10FG6(JROF))
  ENDIF

  PDIE(JROF)    = PDIFTQ(JROF,NFLEVG)
  PDISSHF(JROF) = PDIFTS(JROF,NFLEVG)
  PDIEWSS(JROF) = PSTRTU(JROF,NFLEVG)
  PDINSSS(JROF) = PSTRTV(JROF,NFLEVG)
  PDSP(JROF)    =  PRESH(JROF,NFLEVG)

ENDDO

!*       1.3  MEAN SEA LEVEL PRESSURE

DO JROF=KIDIA,KFDIA
  ZTB    (JROF) = PT   (JROF,NLEXTRAP)
  ZPRESBH(JROF) = PRESH(JROF,NFLEVG)
  ZPRESBF(JROF) = PRESF(JROF,NLEXTRAP)
ENDDO
CALL CTSTAR(KPROMA,KIDIA,KFDIA,ZTB,ZPRESBH,ZPRESBF,POROG,ZTSTAR,ZT0)
CALL PPPMER(KPROMA,KIDIA,KFDIA,ZPRESBH,POROG,ZTSTAR,ZT0,PDMSL)

!*       1.4  TOTAL COLUMN: WATER VAPOUR, CAPE, CLOUD BASE, 0DEGL,  WATER, ICE, LIQUID

IF(LDMLPP ) THEN
  CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,PQ,PRESH)
  DO JROF=KIDIA,KFDIA
    PDTCWV (JROF)=ZWCH(JROF,NFLEVG)
    PDCAPE (JROF)=PCAPE(JROF)
    PDCBASE(JROF)=PCBASE(JROF)
    PD0DEGL(JROF)=P0DEGL(JROF)
    PDVISIH(JROF)=PVISIH(JROF)
  ENDDO

  ! Total column liquid water content
  IF (YL%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZCLD(JROF,JK)=PL(JROF,JK)
        IF (ZCLD(JROF,JK)<1.E-12_JPRB) ZCLD(JROF,JK)=0.0_JPRB 
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZCLD,PRESH)
    DO JROF=KIDIA,KFDIA
      PDTCLW(JROF)=ZWCH(JROF,NFLEVG)
    ENDDO
  ENDIF 

  ! Total column rain water content
  IF (YR%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZCLD(JROF,JK)=PR(JROF,JK)
        IF (ZCLD(JROF,JK)<1.E-12_JPRB) ZCLD(JROF,JK)=0.0_JPRB 
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZCLD,PRESH)
    DO JROF=KIDIA,KFDIA
      PDTCRW(JROF)=ZWCH(JROF,NFLEVG)
    ENDDO
  ENDIF 

  ! Total column ice water content
  IF (YI%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZCLD(JROF,JK)=PI(JROF,JK)
        IF (ZCLD(JROF,JK)<1.E-12_JPRB) ZCLD(JROF,JK)=0.0_JPRB 
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZCLD,PRESH)
    DO JROF=KIDIA,KFDIA
      PDTCIW(JROF)=ZWCH(JROF,NFLEVG)
    ENDDO
  ENDIF 

  ! Total column snow water content
  IF (YS%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZCLD(JROF,JK)=PS(JROF,JK)
        IF (ZCLD(JROF,JK)<1.E-12_JPRB) ZCLD(JROF,JK)=0.0_JPRB 
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZCLD,PRESH)
    DO JROF=KIDIA,KFDIA
      PDTCSW(JROF)=ZWCH(JROF,NFLEVG)
    ENDDO
  ENDIF 

  ! Total column water content (vapour+liquid+ice+rain+snow)
  IF (YL%LACTIVE .AND. YI%LACTIVE .AND. .NOT. YS%LACTIVE .AND. &
   & .NOT. YR%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZWVLI(JROF,JK)=((PI(JROF,JK))+PL(JROF,JK))+PQ(JROF,JK)
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZWVLI,PRESH)
  ENDIF

  IF (YL%LACTIVE .AND. YI%LACTIVE .AND. YS%LACTIVE .AND. .NOT. YR%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZWVLI(JROF,JK)=(((PI(JROF,JK))+PS(JROF,JK))+PL(JROF,JK))+PQ(JROF,JK)
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZWVLI,PRESH)
  ENDIF

  IF (YL%LACTIVE .AND. YI%LACTIVE .AND. YS%LACTIVE .AND. YR%LACTIVE) THEN
    DO JK=1,NFLEVG
      DO JROF=KIDIA,KFDIA
        ZWVLI(JROF,JK)=((((PI(JROF,JK))+PS(JROF,JK))+PL(JROF,JK))+&
        &                  PR(JROF,JK))+PQ(JROF,JK)
      ENDDO
    ENDDO
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,ZWVLI,PRESH)
  ENDIF

  DO JROF=KIDIA,KFDIA
    PDTCW(JROF)=ZWCH(JROF,NFLEVG)
  ENDDO

ENDIF

!*       1.5  TOTAL COLUMN OZONE

IF(LDMLPP .AND. YO3%LACTIVE) THEN
  CALL GPTCO3(KPROMA,KIDIA,KFDIA,NFLEVG,PDELP,PO3,PDTCO3)
ENDIF

!*       1.6  TOTAL COLUMN GHG fields

DO JGHG=1,NGHG
  IF(YGHG(JGHG)%LACTIVE) THEN
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,PGHG(:,:,JGHG),PRESH)
  ENDIF
  DO JROF=KIDIA,KFDIA
    PDTCGHG(JROF,JGHG)=ZWCH(JROF,NFLEVG)
  ENDDO
ENDDO

IF(YCH4S%LACTIVE) THEN
  CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,PCH4S,PRESH)
  DO JROF=KIDIA,KFDIA
    PDTCCH4S(JROF)=ZWCH(JROF,NFLEVG)
  ENDDO
ENDIF


!*       1.6b  TOTAL COLUMN Diagnostic Tracers fields

DO JTRAC=1,NTRAC
  IF(YTRAC(JTRAC)%LACTIVE) THEN
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,PTRAC(:,:,JTRAC),PRESH)
  ENDIF
  DO JROF=KIDIA,KFDIA
    PDTCTRAC(JROF,JTRAC)=ZWCH(JROF,NFLEVG)
  ENDDO
ENDDO

!*       1.7  TOTAL COLUMN GRG fields

DO JGRG=1,NGRG
  IF(YGRG(JGRG)%LACTIVE) THEN
    CALL GPPWC(KPROMA,KIDIA,KFDIA,NFLEVG,ZWCH,PGRG(:,:,JGRG),PRESH)
  ENDIF
  DO JROF=KIDIA,KFDIA
    PDTCGRG(JROF,JGRG)=ZWCH(JROF,NFLEVG)
  ENDDO
ENDDO

!*       1.8  TOTAL COLUMN AERO fields

DO JAERO=1,NACTAERO
  IF(YAERO(JAERO)%LACTIVE) THEN
    CALL GPTCO3(KPROMA,KIDIA,KFDIA,NFLEVG,PDELP,PAERO(:,:,JAERO),PDTCAERO(:,JAERO))
  ENDIF
ENDDO

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

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