SUBROUTINE PHYS_TL (KPRINT, KLEV, KLON, &       
                 &  PT5               , & 
                 &  PQ5               , &
                 &  PP5               , &
                 &  PPH5              , &
                 &  PTENT5            , &
                 &  PTENQ5            , & 
                 &  PS5               , &
                 &  PDIFTS5           , &
                 &  PDIFTQ5           , &
                 &  PSTRTU5           , &
                 &  PSTRTV5           , &
                 &  PRAIN3D5          , &
                 &  PSNOW3D5          , &
                 &  PDQL5             , &
                 &  PDQI5             , & 
                 &  PDCC5             , &
                 &  PFLS5             , &
                 &  PFLC5             , &
                 &  PT                , &
                 &  PQ                , &
                 &  PP                , &
                 &  PPH               , &
                 &  PTENT             , &
                 &  PTENQ             , &
                 &  PS                , &
                 &  PDIFTS            , &
                 &  PDIFTQ            , &
                 &  PSTRTU            , &
                 &  PSTRTV            , &
                 &  PRAIN3D           , &
                 &  PSNOW3D           , &
                 &  PDQL              , &
                 &  PDQI              , &
                 &  PDCC              , &
                 &  PFLS              , &
                 &  PFLC)
   

!* TL of physics part of the observation operator: Mass-flux convection & large scale precip: 
!*     Original (06/11/97) : J.-F. MAHFOUF
!*     Modified (06/03/98) : New physical parameterisations, P. LOPEZ
!*     Modified (31/03/04) : New Interface w/ 1D-Var TB-Obs. operator, clean-up. P.BAUER
!*     Modified (01/10/08) : LLRAIN1D name change to reflect usage. AGEER

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

IMPLICIT NONE

INTEGER (KIND=JPIM), INTENT   ( IN)                          :: KPRINT, KLEV, KLON
REAL    (KIND=JPRB), INTENT   ( IN), DIMENSION (KLON,KLEV+1) :: PPH5
REAL    (KIND=JPRB), INTENT   ( IN), DIMENSION (KLON,KLEV)   :: PP5, PT5, PQ5
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PTENQ5, PTENT5
REAL    (KIND=JPRB), INTENT   ( IN), DIMENSION (KLON)        :: PS5, PDIFTS5, PDIFTQ5, PSTRTU5, PSTRTV5
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PRAIN3D5, PSNOW3D5
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PDQL5, PDQI5, PDCC5
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON)        :: PFLS5, PFLC5
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV+1) :: PPH
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PP, PT, PQ
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PTENQ, PTENT
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON)        :: PS, PDIFTS, PDIFTQ, PSTRTU, PSTRTV
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PRAIN3D, PSNOW3D
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON,KLEV)   :: PDQL, PDQI, PDCC
REAL    (KIND=JPRB), INTENT (INOUT), DIMENSION (KLON)        :: PFLS, PFLC

!* Local 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZU5, ZV5, ZW5, ZTENU5 , ZTENV5, ZQSAT5
!LOP REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZGEOM15, ZARPRC5
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZGEOM15
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZLU5, ZLUDE5, ZMFU5, ZMFD5 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZGEOMH5, ZFPLCL5, ZFPLCN5, ZFHPCL5, ZFHPCN5 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZFHPSL5, ZFHPSN5, ZFPLSL5, ZFPLSN5
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZDIFTQ5, ZDIFTS5, ZDIFCQ5, ZDIFCS5
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZSTRCU5, ZSTRCV5, ZFCCQL5, ZFCCQN5
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZCOVPTOT5

REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZU , ZV , ZW , ZTENU , ZTENV , ZQSAT 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZGEOM1
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZLU , ZLUDE , ZMFU , ZMFD  
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZGEOMH , ZFPLCL , ZFPLCN , ZFHPCL , ZFHPCN  
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZFHPSL , ZFHPSN , ZFPLSL , ZFPLSN 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZDIFTQ , ZDIFTS , ZDIFCQ , ZDIFCS 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV+1) :: ZSTRCU , ZSTRCV , ZFCCQL , ZFCCQN 
REAL (KIND=JPRB), DIMENSION (KLON,KLEV)   :: ZCOVPTOT 

REAL (KIND=JPRB), DIMENSION (KLON)        :: ZARPRC5

REAL (KIND=JPRB) :: ZTSPHY 

REAL (KIND=JPRB), DIMENSION (KLON)      :: ZFACT5, ZFACT25, ZDPH5, ZFACT35
REAL (KIND=JPRB), DIMENSION (:,:,:), ALLOCATABLE :: ZCEN5, ZTENC5
REAL (KIND=JPRB), DIMENSION (:),     ALLOCATABLE :: ZSCAV5

INTEGER (KIND=JPIM), DIMENSION (KLON) :: IBASC, ITOPC, JTOPC, JBASEC, JBOTSC, JTYPE
INTEGER (KIND=JPIM) :: ISTEP, ISTART, IIDIA, IFDIA, ITDIA
INTEGER (KIND=JPIM) :: JL, JK, ITRAC
     
LOGICAL, DIMENSION (KLON) :: LLLAND, LLCUM, LLSC 
LOGICAL :: LLSLPHY, LLRAIN1D

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "saturtl.intfb.h"
#include "cucalln2tl.intfb.h"
#include "cloudsttl.intfb.h"

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

IIDIA  =    1
IFDIA  = KLON
ITDIA  =    1
ISTEP  =    1
ISTART =    1
ITOPC  = KLEV
IBASC  =    1

LLRAIN1D = .TRUE.
LLSLPHY = .FALSE.
LLLAND  = .FALSE. ! Force the estimation of precipitation over oceans

!   TIME STEP
ZTSPHY = TSTEP
         
ZU5    (:,:) = 0.0_JPRB
ZU     (:,:) = 0.0_JPRB
ZV5    (:,:) = 0.0_JPRB 
ZV     (:,:) = 0.0_JPRB 
ZW5    (:,:) = 0.0_JPRB 
ZW     (:,:) = 0.0_JPRB 
ZTENU5 (:,:) = 0.0_JPRB
ZTENU  (:,:) = 0.0_JPRB
ZTENV5 (:,:) = 0.0_JPRB
ZTENV  (:,:) = 0.0_JPRB

ZGEOMH5 (:,KLEV+1) = 0.0_JPRB
ZGEOMH  (:,KLEV+1) = 0.0_JPRB      

DO JK = KLEV, 1, -1
  ZFACT5  (:) = 287.0_JPRB * (1.0_JPRB + 0.608_JPRB * PQ5 (:,JK)) / PP5 (:,JK)
  ZFACT25 (:) = ZFACT5 (:) * PT5 (:,JK)
  ZDPH5   (:) = PPH5   (:,JK+1) - PPH5 (:,JK)
  ZFACT35 (:) = ZDPH5  (:) / PP5 (:,JK)

  ZGEOMH5 (:,JK) = ZGEOMH5 (:,JK+1) &
   & +  287.0_JPRB * PT5 (:,JK) * (1.0_JPRB + 0.608_JPRB * PQ5(:,JK)) / PP5 (:,JK) &
   & * (PPH5 (:,JK+1) - PPH5 (:,JK))

  ZGEOMH  (:,JK) = ZGEOMH (:,JK+1) &
   & + PT (:,JK) * ZFACT5 (:) * ZDPH5 (:) &
   & + 287.0_JPRB * PT5 (:,JK) * 0.608_JPRB * PQ (:,JK) * ZFACT35 (:) &
   & + ZFACT25 (:) * (PPH (:,JK+1) - PPH(:,JK)) &
   & - ZFACT25 (:) * ZFACT35 (:) * PP (:,JK)
ENDDO               

DO JK = 1, KLEV
  ZGEOM15 (:,JK) = 0.5_JPRB * (ZGEOMH5 (:,JK+1) + ZGEOMH5 (:,JK))
  ZGEOM1  (:,JK) = 0.5_JPRB * (ZGEOMH  (:,JK+1) + ZGEOMH  (:,JK))
ENDDO

DO JL = 1, KLON
  ZDIFTS5 (JL,KLEV+1) = PDIFTS5 (JL)
  ZDIFTS  (JL,KLEV+1) = PDIFTS  (JL)
  ZDIFTQ5 (JL,KLEV+1) = PDIFTQ5 (JL)
  ZDIFTQ  (JL,KLEV+1) = PDIFTQ  (JL)
ENDDO

CALL SATURTL(IIDIA,IFDIA,KLON,ITDIA,KLEV,PP5, PT5, ZQSAT5, &
                                      &  PP , PT , ZQSAT )
ITRAC=0
CALL CUCALLN2TL (IIDIA  , IFDIA  , KLON   , KLEV                     , &
              &  ISTEP  , LLLAND , LLRAIN1D                          , &
              &  ZTSPHY                                              , &
              &  PT5    , PQ5,     ZU5,     ZV5                      , &
              &  ZW5    , ZDIFTQ5, ZDIFTS5, PPH5                     , &
              &  PP5    , PPH5   , ZGEOM15, ZGEOMH5                  , &
              &  PTENT5 , PTENQ5 , ZTENU5 , ZTENV5 , ZARPRC5         , &
              &  ITOPC  , IBASC  , JTYPE                             , &
              &  JBASEC , JTOPC  , JBOTSC , LLCUM  , LLSC            , &
              &  ZLU5   , ZLUDE5 , ZMFU5  , ZMFD5                    , &
              &  ZDIFCQ5, ZDIFCS5, ZFHPCL5, ZFHPCN5                  , &
              &  ZFPLCL5, ZFPLCN5, ZSTRCU5, ZSTRCV5, ZFCCQL5, ZFCCQN5, &
              &  ITRAC  , ZCEN5  , ZTENC5 , ZSCAV5                   , &
              &  PT     , PQ     , ZU ,     ZV                       , &
              &  ZW     , ZDIFTQ , ZDIFTS , PPH                      , &
              &  PP     , PPH    , ZGEOM1 , ZGEOMH                   , &
              &  PTENT  , PTENQ  , ZTENU  , ZTENV                    , &
              &  ZLU    , ZLUDE  , ZMFU   , ZMFD                     , &
              &  ZDIFCQ , ZDIFCS , ZFHPCL , ZFHPCN                   , &
              &  ZFPLCL , ZFPLCN , ZSTRCU , ZSTRCV , ZFCCQL , ZFCCQN , &
              &  ZCEN5  , ZTENC5)
       
CALL CLOUDSTTL (IIDIA  , IFDIA  , KLON   , ITDIA  , KLEV, &
             &  LLRAIN1D, ZTSPHY                        , &
             &  PPH5   , PP5    , PQ5    , ZQSAT5 , PT5 , &
             &  ZLUDE5 , ZLU5                           , &
             &  PTENT5 , PTENQ5                         , &
             &  PDCC5  , PDQI5  , PDQL5                 , &
             &  ZFPLSL5, ZFPLSN5, ZFHPSL5, ZFHPSN5      , &
             &  ZCOVPTOT5                               , &
             &  PPH    , PP     , PQ     , ZQSAT  , PT  , &
             &  ZLUDE  , ZLU                            , &
             &  PTENT  , PTENQ                          , &
             &  PDCC   , PDQI   , PDQL                  , &
             &  ZFPLSL , ZFPLSN , ZFHPSL , ZFHPSN       , &
             &  ZCOVPTOT )

DO JL = 1, KLON
!   DO JK = 1, KLEV + 1
!      ZFPLCL5 (JL,JK) = MAX (0.0_JPRB, ZFPLCL5 (JL,JK))
!      ZFPLCN5 (JL,JK) = MAX (0.0_JPRB, ZFPLCN5 (JL,JK))
!      ZFPLSL5 (JL,JK) = MAX (0.0_JPRB, ZFPLSL5 (JL,JK))
!      ZFPLSN5 (JL,JK) = MAX (0.0_JPRB, ZFPLSN5 (JL,JK))
!   ENDDO
  DO JK = 1, KLEV
     PRAIN3D5(JL,JK) = (ZFPLSL5(JL,JK  ) + ZFPLCL5(JL,JK  ) &
                   & +  ZFPLSL5(JL,JK+1) + ZFPLCL5(JL,JK+1))*0.5_JPRB
     PRAIN3D (JL,JK) = (ZFPLSL (JL,JK  ) + ZFPLCL (JL,JK  ) &
                   & +  ZFPLSL (JL,JK+1) + ZFPLCL (JL,JK+1))*0.5_JPRB
      
     PSNOW3D5(JL,JK) = (ZFPLSN5(JL,JK  ) + ZFPLCN5(JL,JK  ) &
                   & +  ZFPLSN5(JL,JK+1) + ZFPLCN5(JL,JK+1))*0.5_JPRB
     PSNOW3D (JL,JK) = (ZFPLSN (JL,JK  ) + ZFPLCN (JL,JK  ) &
                   & +  ZFPLSN (JL,JK+1) + ZFPLCN (JL,JK+1))*0.5_JPRB
  ENDDO
  
  PFLC5 (JL) =  ZFPLCL5 (JL,KLEV+1) + ZFPLCN5 (JL,KLEV+1)
  PFLC  (JL) =  ZFPLCL  (JL,KLEV+1) + ZFPLCN  (JL,KLEV+1)
  
  PFLS5 (JL) =  ZFPLSL5 (JL,KLEV+1) + ZFPLSN5 (JL,KLEV+1)
  PFLS  (JL) =  ZFPLSL  (JL,KLEV+1) + ZFPLSN  (JL,KLEV+1)

ENDDO

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