SUBROUTINE CPCHET(KPROMA, KSTART, KPROF, KFLEV, KTSNO,&
 & PFHSCL , PFHSCN , PFHSSL , PFHSSN ,&
 & PFHPCL , PFHPCN , PFHPSL , PFHPSN ,&
 & PDIFCQ , PDIFCQI, PDIFCQL, PDIFCS ,&
 & PDIFTQ , PDIFTQI, PDIFTQL, PDIFTS ,&
 & PFCCQL , PFCCQN , PFCSQL , PFCSQN ,&
 & PFPLCL , PFPLCN , PFPLSL , PFPLSN ,&
 & PFPFPSL, PFPFPSN, PFPFPCL, PFPFPCN,&
 & PFPEVPSL,PFPEVPSN,PFPEVPCL,PFPEVPCN,&
 & PFRMH  , PFRSO  , PFRTH  ,         &
 & PSTRCU , PSTRCV , PSTRDU , PSTRDV ,&
 & PSTRTU , PSTRTV , PSTRMU , PSTRMV ,&
 & PRDELP , PCP    ,&
 & PT     , PQ     , PQI    , PQL    ,&
 & PCPS   , PTS    , PQS    ,         &
 & PTENDU , PTENDV , PTENDH , PTENDQ ,&
 & PTENDQI, PTENDQL, PTENDQR, PTENDQS,&
 & PGEMU  , PGELAM , PHI    , PHIF )

USE PARKIND1  ,ONLY : JPIM     ,JPRB 
USE YOMCHET   ,ONLY : GCHET ,GCHETN
USE YOMCST    ,ONLY : RG
USE YOMCT0    ,ONLY : CNMEXP
USE YOMDYN    ,ONLY : TSTEP
USE YOMLUN    ,ONLY : NULOUT
USE YOMPHY    ,ONLY : NDPSFI
USE YHLOPTION ,ONLY : LHLTEND
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

IMPLICIT NONE

#ifdef DOC
!**** *CPCHET

!     Purpose.
!     --------
!       Frequency distributions for absolute values of physical tendencies
!       Finding and output of coordinates of points with absolute values of
!       of logarithms of physical tendencies exceeding the thresholds

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

!        Explicit arguments :
!        --------------------

!    INPUT:
!     ------
!        KPROMA    : DIMENSION HORIZONTALE - horizontal dimension
!        KSTART    : DEBUT DE LA BOUCLE HORIZONTALE - first element of work
!        KPROF     : BORNE HORIZONTALE DES CALCULS -last element of work
!        KFLEV     : DIMENSION ET BORNE VERTICALE - nb of levels in gpt space
!        KTSNO     : time-step number
!        PTENDU    : "U"-wind physical tendency
!        PTENDV    : "V"-wind physical tendency
!        PTENDH    : Enthalpy physical tendency
!        PTENDQ    : Moisture physical tendency
!        PTENDQI   : Cloud Ice physical tendency
!        PTENDQL   : Cloud Liquid water physical tendency
!        PTENDQR   : Liquid Precipitation contains physical tendency
!        PTENDQS   : Solid Precipitation contains physical tendency
!        PGEMU     : sine of geographical latitude
!        PGELAM    : geographical longitude
!        PHI       : geopotential height on interlayers at time t
!        PHIF      : geopotential height on layers at time t

!        Implicit arguments :
!        --------------------

!   Method
!   -------

!   Externals
!   ---------

!   References
!   ----------

!   Author
!   ------
!   2004-03-01: T.Kovacic, J.M.Piriou, F.Bouyssel

!   Modifications
!   -------------

!   2006-01-25 : F.Bouyssel : Introduction of PFPEVPL, PFPEVPN
!   2006-06-01 : B. Sass : Modified call to CPTEND
!----------------------------------------------------------------
#endif
  
! * INPUT:
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTSNO 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHSCL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHSCN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHSSL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHSSN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHPCL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHPCN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHPSL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFHPSN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCQ (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCQI(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCQL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFCS (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQ (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQI(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTS (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCCQL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCCQN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCSQL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFCSQN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLCN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSL (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLSN (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRMH  (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRSO  (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTH  (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRCU (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRCV (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRDU (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRDV (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTU (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTV (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRMU (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRMV (KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPFPSL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPFPSN(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPFPCL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPFPCN(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPEVPSL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPEVPSN(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPEVPCL(KPROMA,0:KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPEVPCN(KPROMA,0:KFLEV)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP    (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT     (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ     (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQI    (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQL    (KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCPS   (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS    (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS    (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDU (KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDV (KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDH (KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQ (KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQI(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQL(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQR(KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENDQS(KPROMA,KFLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEMU  (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAM (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHI    (KPROMA,0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHIF   (KPROMA,KFLEV) 

!  LOCAL VARIABLES
INTEGER(KIND=JPIM) :: J, JLEV, JROF, JVAR, JCLASS, JWIDTH, JIND
INTEGER(KIND=JPIM), DIMENSION(GCHET%NWIDTH+1) :: JQDIST
INTEGER(KIND=JPIM), DIMENSION(KPROMA*KFLEV)   :: JJROF
REAL(KIND=JPRB) :: ZEPS, ZPREC, ZDPSFI, ZCPI, ZTI
REAL(KIND=JPRB) :: ZDIFCQ (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFCQI(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFCQL(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFCS (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFTQ (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFTQI(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFTQL(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZDIFTS (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFCCQL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFCCQN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFCSQL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFCSQN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPLCL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPLCN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPLSL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPLSN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFRMH  (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFRSO  (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFRTH  (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFTKE  (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFHP   (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFP    (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRCU (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRCV (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRDU (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRDV (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRTU (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRTV (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRMU (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZSTRMV (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPFPSL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPFPSN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPFPCL (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPFPCN (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPEVPSL(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPEVPSN(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPEVPCL(KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZFPEVPCN(KPROMA,0:KFLEV)

REAL(KIND=JPRB) :: ZFEPFP (KPROMA,0:KFLEV)
REAL(KIND=JPRB) :: ZTENDU (KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDV (KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDH (KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDQ (KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDQI(KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDQL(KPROMA,KFLEV) 
REAL(KIND=JPRB) :: ZTENDQR(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTENDQS(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTENDTKE(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTENDT(KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZTEND  (GCHET%NVPHT,KPROMA,KFLEV)
REAL(KIND=JPRB) :: ZABSTD (GCHET%NVPHT,KPROMA,KFLEV)
REAL(KIND=JPRB), DIMENSION(GCHET%NVPHT)    :: ZSTCL
REAL(KIND=JPRB), DIMENSION(KPROMA*KFLEV,6) :: ZREEL

REAL(KIND=JPRB) :: ZHOOK_HANDLE
  
#include "cptend.intfb.h"

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

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

!*       1.    Initialization of physical tendencies to be checked
!              ---------------------------------------------------

IF (GCHETN%CPTSEP == 'ALL ') THEN

  ZTEND(1,:,:) = PTENDU (:,:)
  ZTEND(2,:,:) = PTENDV (:,:)
  ZTEND(3,:,:) = PTENDH (:,:)
  ZTEND(4,:,:) = PTENDQ (:,:)
  ZTEND(5,:,:) = PTENDQI(:,:)
  ZTEND(6,:,:) = PTENDQL(:,:)
  ZTEND(7,:,:) = PTENDQR(:,:)
  ZTEND(8,:,:) = PTENDQS(:,:)

ELSE

  ZDPSFI = REAL( NDPSFI ,JPRB)
  ZDIFCQ  = 0._JPRB
  ZDIFCQI = 0._JPRB
  ZDIFCQL = 0._JPRB
  ZDIFCS  = 0._JPRB
  ZDIFTQ  = 0._JPRB
  ZDIFTQI = 0._JPRB
  ZDIFTQL = 0._JPRB
  ZDIFTS  = 0._JPRB
  ZFCCQL  = 0._JPRB
  ZFCCQN  = 0._JPRB
  ZFCSQL  = 0._JPRB
  ZFCSQN  = 0._JPRB
  ZFPLCL  = 0._JPRB
  ZFPLCN  = 0._JPRB
  ZFPLSL  = 0._JPRB
  ZFPLSN  = 0._JPRB
  ZFRMH   = 0._JPRB
  ZFRSO   = 0._JPRB
  ZFRTH   = 0._JPRB
  ZFTKE   = 0._JPRB
  ZFHP    = 0._JPRB
  ZFP     = 0._JPRB
  ZSTRCU  = 0._JPRB
  ZSTRCV  = 0._JPRB
  ZSTRDU  = 0._JPRB
  ZSTRDV  = 0._JPRB
  ZSTRTU  = 0._JPRB
  ZSTRTV  = 0._JPRB
  ZSTRMU  = 0._JPRB
  ZSTRMV  = 0._JPRB
  ZFPFPSL  = 0._JPRB
  ZFPFPSN  = 0._JPRB  
  ZFPFPCL  = 0._JPRB
  ZFPFPCN  = 0._JPRB  
  ZFPEVPSL = 0._JPRB
  ZFPEVPSN = 0._JPRB
  ZFPEVPCL = 0._JPRB
  ZFPEVPCN = 0._JPRB

  IF     (GCHETN%CPTSEP == 'RAYT') THEN
    ZFRSO   = PFRSO
    ZFRTH   = PFRTH
  ELSEIF (GCHETN%CPTSEP == 'VDIF') THEN
    ZDIFTQ  = PDIFTQ
    ZDIFTQL = PDIFTQL
    ZDIFTQI = PDIFTQI
    ZDIFTS  = PDIFTS
    ZSTRTU  = PSTRTU
    ZSTRTV  = PSTRTV
  ELSEIF (GCHETN%CPTSEP == 'DRAG') THEN
    ZFRMH   = PFRMH
    ZSTRMU  = PSTRMU
    ZSTRMV  = PSTRMV
    ZSTRDU  = PSTRDU
    ZSTRDV  = PSTRDV
  ELSEIF (GCHETN%CPTSEP == 'STRA') THEN
    ZFPLSL  = PFPLSL
    ZFPLSN  = PFPLSN
    ZFCSQL  = PFCSQL
    ZFCSQN  = PFCSQN
    ZFP     = PFPLSL + PFPLSN
    ZFPFPSL  = PFPFPSL 
    ZFPFPSN  = PFPFPSN  
    ZFPEVPSL = PFPEVPSL
    ZFPEVPSN = PFPEVPSN

!   Initialize temperature tendency with zero values 

    DO JLEV = 1 , KFLEV
      DO JROF=KSTART,KPROF
        ZTENDT(JROF,JLEV)=0._JPRB
      ENDDO 
    ENDDO 

    DO JROF=KSTART,KPROF
      ZFEPFP(JROF,0)= - ZDPSFI * PCP(JROF,1) * PT(JROF,1) *ZFP(JROF,0)
      DO JLEV = 1 , KFLEV-1
        ZTI = 0.5_JPRB * ( PT (JROF,JLEV) + PT (JROF,JLEV+1) )
        ZCPI= 0.5_JPRB * ( PCP(JROF,JLEV) + PCP(JROF,JLEV+1) )
        ZFEPFP(JROF,JLEV)= - ZDPSFI * ZCPI * ZTI * ZFP(JROF,JLEV)
      ENDDO
      ZFEPFP(JROF,KFLEV)= - ZDPSFI * PCPS(JROF) * PTS(JROF) *ZFP(JROF,KFLEV)
    ENDDO
    ZFHP    = PFHSSL + PFHSSN + PFHPSL + PFHPSN + ZFEPFP
  ELSEIF (GCHETN%CPTSEP == 'CONV') THEN
    ZDIFCQ  = PDIFCQ
    ZDIFCQL = PDIFCQL
    ZDIFCQI = PDIFCQI
    ZDIFCS  = PDIFCS
    ZSTRCU  = PSTRCU
    ZSTRCV  = PSTRCV
    ZFPLCL  = PFPLCL
    ZFPLCN  = PFPLCN
    ZFCCQL  = PFCCQL
    ZFCCQN  = PFCCQN
    ZFPFPCL  = PFPFPCL 
    ZFPFPCN  = PFPFPCN  
    ZFPEVPCL = PFPEVPCL
    ZFPEVPCN = PFPEVPCN
    ZFP     = PFPLCL + PFPLCN
    DO JROF=KSTART,KPROF
      ZFEPFP(JROF,0)= - ZDPSFI * PCP(JROF,1) * PT(JROF,1) *ZFP(JROF,0)
      DO JLEV = 1 , KFLEV-1
        ZTI = 0.5_JPRB * ( PT (JROF,JLEV) + PT (JROF,JLEV+1) )
        ZCPI= 0.5_JPRB * ( PCP(JROF,JLEV) + PCP(JROF,JLEV+1) )
        ZFEPFP(JROF,JLEV)= - ZDPSFI * ZCPI * ZTI * ZFP(JROF,JLEV)
      ENDDO
      ZFEPFP(JROF,KFLEV)= - ZDPSFI * PCPS(JROF) * PTS(JROF) *ZFP(JROF,KFLEV)
    ENDDO
    ZFHP    = PFHSCL + PFHSCN + PFHPCL + PFHPCN + ZFEPFP
  ENDIF

! Diagnostic tendency computation works only if special tendency accumulation
! has not been performed (special HIRLAM option LHLTEND =true )  

IF( .NOT. LHLTEND ) THEN

  CALL CPTEND( KPROMA, KSTART, KPROF, KFLEV,&
   & ZDIFCQ , ZDIFCQI, ZDIFCQL, ZDIFCS ,&
   & ZDIFTQ , ZDIFTQI, ZDIFTQL, ZDIFTS ,&
   & ZFCCQL , ZFCCQN , ZFCSQL , ZFCSQN ,&
   & ZFPLCL , ZFPLCN , ZFPLSL , ZFPLSN ,&
   & ZFPFPSL, ZFPFPSN, ZFPFPCL, ZFPFPCN,&
   & ZFPEVPSL,ZFPEVPSN,ZFPEVPCL,ZFPEVPCN,&
   & ZFRMH  , ZFRSO  , ZFRTH  , ZFTKE  ,&
   & ZFHP   , ZFP    ,&
   & ZSTRCU , ZSTRCV , ZSTRDU , ZSTRDV ,&
   & ZSTRTU , ZSTRTV , ZSTRMU , ZSTRMV ,&
   & PRDELP , & 
   & PQ     , PQI    , PQL    ,&
   & PQS    , &
   & ZTENDU , ZTENDV , ZTENDH , ZTENDQ ,&
   & ZTENDQI, ZTENDQL, ZTENDQR, ZTENDQS,&
   & ZTENDTKE, ZTENDT )

ENDIF 

  ZTEND(1,:,:) = ZTENDU (:,:)
  ZTEND(2,:,:) = ZTENDV (:,:)
  ZTEND(3,:,:) = ZTENDH (:,:)
  ZTEND(4,:,:) = ZTENDQ (:,:)
  ZTEND(5,:,:) = ZTENDQI(:,:)
  ZTEND(6,:,:) = ZTENDQL(:,:)
  ZTEND(7,:,:) = ZTENDQR(:,:)
  ZTEND(8,:,:) = ZTENDQS(:,:)

ENDIF

ZABSTD(:,:,:) = ABS(ZTEND(:,:,:))

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

!*       2.    Computation of frequency distributions
!              --------------------------------------

JWIDTH   = GCHET%NWIDTH
ZEPS     = 1.0E-20_JPRB
ZPREC    = LOG(GCHETN%APREC)
ZSTCL(:) = LOG(GCHETN%STARTCLASS(:))

IF (GCHETN%LFREQD) THEN
  DO JVAR = 1, GCHET%NVPHT
    IF (GCHETN%CPHYST(JVAR:JVAR) == '1') THEN
      JQDIST(:) = 0
      DO JLEV = GCHETN%NLEV1, GCHETN%NLEV2
        DO JROF = KSTART, KPROF
          JCLASS=MAX(0,MIN(JWIDTH,INT((LOG(MAX(ZEPS,ZABSTD(JVAR,JROF,JLEV))) &
            & - ZSTCL(JVAR))/ZPREC))) + 1
          JQDIST(JCLASS) = JQDIST(JCLASS) + 1
        ENDDO
      ENDDO
      GCHET%NQDIST(JVAR,:) = GCHET%NQDIST(JVAR,:) + JQDIST(:)
    ENDIF
  ENDDO
ENDIF

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

!*       3.    Geographical points exceeding thresholds
!              ----------------------------------------

IF ((GCHETN%LCOORD).OR.(GCHETN%LPROFV)) THEN
  DO JVAR = 1, GCHET%NVPHT
    IF (GCHETN%CPHYST(JVAR:JVAR) == '1') THEN

      JIND=0
      DO JLEV = GCHETN%NLEV1, GCHETN%NLEV2
        DO JROF = KSTART, KPROF
          IF (ZABSTD(JVAR,JROF,JLEV) >= GCHETN%TENDTHRESH(JVAR)) THEN
            JIND = JIND + 1
            ZREEL(JIND,1) = PGELAM(JROF)
            ZREEL(JIND,2) = ASIN(PGEMU(JROF))
            ZREEL(JIND,3) = PHIF(JROF,JLEV)/RG
            ZREEL(JIND,4) = (PHIF(JROF,JLEV) - PHI(JROF,KFLEV))/RG
            ZREEL(JIND,5) = KTSNO
            ZREEL(JIND,6) = ZTEND(JVAR,JROF,JLEV)
            JJROF(JIND)   = JROF
          ENDIF    
        ENDDO
      ENDDO

      IF ((GCHET%NPHTEXTH+JIND) > (GCHETN%NCOORMAX)) THEN
        WRITE(NULOUT,*) 'WARNING : MAX NUMBER OF POINTS REACHED IN CPCHET!'
        JIND = GCHETN%NCOORMAX - GCHET%NPHTEXTH
      ENDIF

      IF (GCHETN%LCOORD) THEN
        CALL LFAOUV  (GCHET%NULCOOR, GCHET%CFICCOOR,'A')
        CALL LFAPRECI(GCHET%NULCOOR, 4)
        CALL LFAPRECR(GCHET%NULCOOR, 8) 
        CALL LFAECRC (GCHET%NULCOOR, 'INDEXP', CNMEXP,1)
        CALL LFAECRR (GCHET%NULCOOR, 'TIME_STEP', TSTEP,1)
        DO J = 1, JIND
          CALL LFAECRR(GCHET%NULCOOR, GCHET%CVARNA(JVAR), ZREEL(J,1:6), 6)
        ENDDO  
        CALL LFAFER(GCHET%NULCOOR)
      ENDIF

      IF (GCHETN%LPROFV) THEN
        DO J = 1, JIND
          GCHET%APOINT(GCHET%NPHTEXTH+J)%ALAT      = ZREEL(J,1)
          GCHET%APOINT(GCHET%NPHTEXTH+J)%ALONG     = ZREEL(J,2)
          GCHET%APOINT(GCHET%NPHTEXTH+J)%HEIGHTSEA = ZREEL(J,3)
          GCHET%APOINT(GCHET%NPHTEXTH+J)%TEND_VAL  = ZREEL(J,6)
          GCHET%APOINT(GCHET%NPHTEXTH+J)%NCHETLONG = JJROF(J)
        ENDDO
        GCHET%NPHTEXTH = GCHET%NPHTEXTH + JIND
      ENDIF

    ENDIF
  ENDDO
ENDIF

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

END SUBROUTINE CPCHET
