SUBROUTINE PROFILECHET &
 & (KLON, &
 & PGELAM, PGEMU, &
 & PGM, PMU0, POROG, POROGL, POROGM, PRCORI, PRATATH, PRATATX, &
 & PLSM, PARG,  PSAB, PALBF, PALBSF, PEMISF, PD2, PIVEG, PLAI, PCT, &
 & PGZ0F, PGZ0HF, PGZ0RLF, PGETRL, PVRLAN, PVRLDI, PRSMIN, PVEG0, PHV, &
 & PAESEA, PAELAN, PAESOO, PAEDES, PVO3ABC, &
 & PSPT0, PSPT0L, PSPT0M, PTT0, PTT0L, PTT0M, PQT0, PQT0L, PQT0M, &
 & PUT0, PVT0, PVORT0, PDIVT0, PCVGQ, PLCVQ, &
 & PTS0, PTP0, PWL0, PWS0, PWSI0, PWP0, PWPI0, PSNS0, &
 & PDIFCQ, PDIFCQN, PDIFCQL, PDIFCS, PDIFTQ, PDIFTQN, PDIFTQL, &
 & PDIFTS, PFCCQL, PFCCQN, PFCSQL, PFCSQN, PFCQNG, PFCQNNG, &
 & PFCQLNG, PFPLCL, PFPLCN, PFPLSL, PFPLSN, PFRSO, PFRTH, &
 & PSTRCU, PSTRCV, PSTRDU, PSTRDV, PSTRTU, PSTRTV, PSTRMU, PSTRMV, &
 & PFRMH, PFCHOZ, PNEB, PQICE, PQLI, PRH, &
 & PFCS, PFCLL, PFCLN, PFEVL, PFEVN, PFEVV, PFTR, PFLWSP, PFONTE, &
 & PFGEL, PFGELS, PFRSODS, PFRSOPS, PFRSOPT, PFRTHDS, &
 & PQS, PRUISL, PRUISP, PRUISS, &
 & PUCLS, PVCLS, PTCLS, PQCLS, PRHCLS, &
 & PCLCT, PCLCH, PCLCM, PCLCL, PCLCC, &
 & PFPLCH, PFPLSH, PTCCH, PSCCH, PBCCH, PBLH )

USE PARKIND1  ,ONLY : JPIM    ,JPRB
USE YOMHOOK   ,ONLY : LHOOK   ,DR_HOOK
USE YOMCHET   ,ONLY : GCHET   ,GCHETN
USE YOMDPHY   ,ONLY : NTSSG  
USE YOMDIM    ,ONLY : NFLEVG  ,NPROMA  ,NPROMM 
USE YOMLUN    ,ONLY : NULOUT

IMPLICIT NONE

#ifdef DOC
! **** *PROFILECHET* 

!---------------------------------------------------------------- 
!   Purpose
!   -------
!   Output profiles for SCM for points with physical tendencies exceding 
!   the threshold.

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

!   Modifications
!   -------------
!   2004-10, F. Bouyssel : cleaning
!   2007-05, F. Bouyssel : add PFCLL, PFCLN

!----------------------------------------------------------------
#endif
!-------------------------------------------------
!  Dummy variables 
!-------------------------------------------------

INTEGER(KIND=JPIM),INTENT(IN) :: KLON
REAL(KIND=JPRB),INTENT(IN)    :: PGELAM(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PGEMU(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PGM(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PMU0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: POROG(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: POROGL(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: POROGM(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PRCORI(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PRATATH(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PRATATX(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PLSM(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PARG(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PSAB(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PALBF(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PALBSF(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PEMISF(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PD2(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PIVEG(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PLAI(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PCT(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PGZ0F(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PGZ0HF(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PGZ0RLF(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PGETRL(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PVRLAN(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PVRLDI(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PRSMIN(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PVEG0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PHV(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PAESEA(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PAELAN(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PAESOO(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PAEDES(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PVO3ABC(NPROMA,3)
REAL(KIND=JPRB),INTENT(IN)    :: PSPT0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PSPT0L(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PSPT0M(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PTT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PTT0L(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PTT0M(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PQT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PQT0L(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PQT0M(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PUT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PVT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PVORT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIVT0(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PCVGQ(NPROMM,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PLCVQ(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PTS0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PTP0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PWL0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PWS0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PWSI0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PWP0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PWPI0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PSNS0(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFCQ(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFCQN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFCQL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFCS(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFTQ(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFTQN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFTQL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PDIFTS(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCCQL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCCQN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCSQL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCSQN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCQNG(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCQNNG(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCQLNG(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLCL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLCN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLSL(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLSN(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFRSO(NPROMM,0:NFLEVG,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFRTH(NPROMM,0:NFLEVG,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRCU(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRCV(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRDU(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRDV(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRTU(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRTV(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRMU(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PSTRMV(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFRMH(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCHOZ(NPROMM,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PNEB(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PQICE(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PQLI(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PRH(NPROMA,NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFCS(NPROMM,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFCLL(NPROMM,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFCLN(NPROMM,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFEVL(NPROMM,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFEVN(NPROMM,NTSSG+1)
REAL(KIND=JPRB),INTENT(IN)    :: PFEVV(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFTR(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFLWSP(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFONTE(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFGEL(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFGELS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFRSODS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFRSOPS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFRSOPT(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFRTHDS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PQS(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PRUISL(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PRUISP(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PRUISS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PUCLS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PVCLS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PTCLS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PQCLS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PRHCLS(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PCLCT(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PCLCH(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PCLCM(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PCLCL(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PCLCC(NPROMM)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLCH(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PFPLSH(NPROMA,0:NFLEVG)
REAL(KIND=JPRB),INTENT(IN)    :: PTCCH(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PSCCH(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PBCCH(NPROMA)
REAL(KIND=JPRB),INTENT(IN)    :: PBLH(NPROMA)

LOGICAL :: LLTEST
INTEGER(KIND=JPIM) :: I, J, IIND
INTEGER(KIND=JPIM), DIMENSION(GCHET%NPHTEXTH) :: JIND
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "writeprofile.intfb.h"
!-------------------------------------------------
! Check arguments.
!-------------------------------------------------

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

! ----------------------------------------------------
  
  ! deleting multiple points over the same geographical point
IIND = 1
JIND(IIND) = 1
DO I = 2, GCHET%NPHTEXTH
  LLTEST=.TRUE.
  DO J = 1, I-1
    IF(GCHET%APOINT(I)%NCHETLONG==GCHET%APOINT(J)%NCHETLONG) THEN
      LLTEST=.FALSE.
      EXIT
    ENDIF
  ENDDO
  IF (LLTEST) THEN
    IIND = IIND + 1
    JIND(IIND) = I
  ENDIF
ENDDO

GCHET%NPHTEXTH = IIND
DO I = 1, IIND
  GCHET%APOINT(I) = GCHET%APOINT(JIND(I))
ENDDO

IF (GCHET%NPHTEXTH>GCHETN%NPROFMAX) THEN
  WRITE(NULOUT,*) 'WARNING : MAX NUMBER OF PROFILES REACHED IN PROFILECHET!'
  GCHET%NPHTEXTH = GCHETN%NPROFMAX
ENDIF

DO J = 1, GCHET%NPHTEXTH
  CALL WRITEPROFILE &
   & ('ChetProfile.', &
   & GCHET%APOINT(J)%NCHETLONG, GCHET%APOINT(J)%ALAT,&
   & GCHET%APOINT(J)%ALONG,0._JPRB,PGELAM, PGEMU,&
   & PGM, PMU0, POROG, POROGL, POROGM, PRCORI, PRATATH, PRATATX, &
   & PLSM, PARG,  PSAB, PALBF, PALBSF, PEMISF, PD2, PIVEG, PLAI, PCT, &
   & PGZ0F, PGZ0HF, PGZ0RLF, PGETRL, PVRLAN, PVRLDI, PRSMIN, PVEG0, PHV, &
   & PAESEA, PAELAN, PAESOO, PAEDES, PVO3ABC, &
   & PSPT0, PSPT0L, PSPT0M, PTT0, PTT0L, PTT0M, PQT0, PQT0L, PQT0M, &
   & PUT0, PVT0, PVORT0, PDIVT0, PCVGQ, PLCVQ, &
   & PTS0, PTP0, PWL0, PWS0, PWSI0, PWP0, PWPI0, PSNS0, &
   & PDIFCQ, PDIFCQN, PDIFCQL, PDIFCS, PDIFTQ, PDIFTQN, PDIFTQL, &
   & PDIFTS, PFCCQL, PFCCQN, PFCSQL, PFCSQN, PFCQNG, PFCQNNG, &
   & PFCQLNG, PFPLCL, PFPLCN, PFPLSL, PFPLSN, PFRSO, PFRTH, &
   & PSTRCU, PSTRCV, PSTRDU, PSTRDV, PSTRTU, PSTRTV, PSTRMU, PSTRMV, &
   & PFRMH, PFCHOZ, PNEB, PQICE, PQLI, PRH, &
   & PFCS, PFCLL, PFCLN, PFEVL, PFEVN, PFEVV, PFTR, PFLWSP, PFONTE, &
   & PFGEL, PFGELS, PFRSODS, PFRSOPS, PFRSOPT, PFRTHDS, &
   & PQS, PRUISL, PRUISP, PRUISS, &
   & PUCLS, PVCLS, PTCLS, PQCLS, PRHCLS, &
   & PCLCT, PCLCH, PCLCM, PCLCL, PCLCC, &
   & PFPLCH, PFPLSH, PTCCH, PSCCH, PBCCH, PBLH )
ENDDO
GCHET%NPHTEXTH = 0

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

END SUBROUTINE PROFILECHET
