SUBROUTINE SUNDDH
!****
!     ------------------------------------------------------------------

!     SUNDDH * INITIALISE LES ELEMENTS DE CONTROLE
!     DIAGNOSTICS PAR DOMAINES HORIZONTAUX
!     ------------------------------------------------------------------

!       ---------------------------------------------------------
!     BUT
!     ---
!     DIMENSIONS PAR DEFAUTS ET PERMANENTES
!     LECTURE, EDITION DE LA NAMELIST NAMDDH
!     - LOGIQUES STOCKES DANS /YOMLDDH/
!     - DIMENSIONS STOCKEES DANS /DDHDIM/. UNE PARTIE DES DIMENSIONS EST
!     DEDUITE DE LA NAMELIST, ET NON LUE
!     -------

!     ARGUMENTS D ENTREE
!     ------------------

!     ARGUMENTS IMPLICITES
!     --------------------
!       UNITES NULNAM, NULOUT
!       INDICATEURS LOGIQUES, COMMON /YOMLDDH/
!       COMDECK * YOMMDDH *, STOCKAGE DES DIMENSIONS, DES MASQUES, ETC...

!     AUTRES ENTREES
!     --------------
!       NAMELIST NAMDDH

!     SORTIES
!     -------
!       VALEURS PAR DEFAUT OU LUES SUR NAMELIST NAMDDH
!       DES INDICATEURS LOGIQUES ET DES CARACTERISTIQUES BRUTES
!       DES DOMAINES HORIZONTAUX UTILISATEUR :
!         /YOMLDDH/, BDEDDH, NDHKD
!       DIMENSIONS DEDUITES : NDHNPU, NDHBPU, NDHBPX, NDHNOM, NDHDDX
!        ET POUR LES TABLEAUX DE CUMULS
!        NDHCV, NDHCS, NDHCVSU, NDHCSSU ET LES INDICATEURS DE
!        DE POSITION (POINTEURS LOGIQUES) PERMENENTS ET DE SITUATIONS

!     ORIGINAL
!     --------
!     91-03-22, Alain Joly.

!     MODIFICATIONS
!     -------------
!     91-12-11, J.M. Piriou: ajout du terme flux d'enthalpie du aux
!               precipitations, dans le cas masse variable (NDPSFI=1).
!     92-03-01, M. Janousek: diagnostic de l'entropie.
!     92-06-01, M. Janousek: diagnostic du moment cinetique.
!     92-06-01, J.M. Piriou: humidite relative et cumul du champ de masse.
!     93-04-02, R. El Khatib : Set-up defaults controled by LECMWF
!     93-04-24, J.M. Piriou: diagnostic de l'eau liquide et neigeuse.
!     96-04-12. M.Hamrud : supress namelist print (veryyyy long on Fujitsu)
!     97-06-16. P.Viterbo : new style for surface DDH
!     97-02-24. J.M. Piriou: correction bug calcul de LHDQLN.
!     98-05-27. A. Beljaars: boundary layer heigth included.
!     00-05-15. C. Jakob: new surface DDH (Tiles et al.)
!     01-01-26 R. El Khatib sucddh moved from su0yoma and called under LECMWF
!        R. El Khatib : 01-08-07 Pruning options
!     02-09-30  JJMorcrette: ECMWF PAR, UV, CAPE
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     2003-12-05, F. Vinit : new number of free-style variables and fluxes for Meteo-France.
!     05-06-25 F. Bouyssel: Lopez's prognostic scheme
!     06-03-23 T.Kovacic : AROME physics and common dynamics-physics interface 
!     06-01-25 F. Bouyssel: Change default of NHDQLNFP
!     JJMorcrette 20060721 PP of clear-sky PAR and TOA incident solar radiation
!     JJMorcrette 20060807 PP of VIMD
!     05-09-26 S. Serrar: NDHVFS and NDHFFS incremented to include GHG + SF6 diagnostics
!     20061002 JJMorcrette DDH for aerosol physics
!     07-01-15 G. Balsamo: NDHFWLS increased to include condensation diagnostics
!     07-07-17 S. Serrar: New GEMS diagnostics for methane
!     07-05-01  T.Kovacic : changed the number of phy. fluxes and variables if
!                   L3MT.OR.LSTRAPRO)
!     1-oct-08 O.Riviere Activates new data flow in Arome (LFLEXDIA set to TRUE)
!     18-dec-08: O.Riviere Possibility of having new data flow in Arpege/Aladin also
!     JJMorcrette 20100212 PP of CBASE, ODEGL and VISIH
!------------------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT   ,NULNAM   ,NULERR 
USE YOMLDDH  , ONLY : LSDDH    ,LHDGLB   ,LHDZON   ,LHDDOP   ,&
 & LHDLFA   ,LHDHKS   ,LHDMCI   ,LHDENT   ,&
 & LHDPRG   ,LHDPRZ   ,LHDPRD   ,LHDPR    ,LHDEFG   ,&
 & LHDEFZ   ,LHDEFD   ,LHDEF    ,LHDLIST  ,&
 & LHDOUFG  ,LHDOUFZ  ,LHDOUFD  ,LHDOUP   ,LHDFIL, LONLYVAR, &
 & LHDORIGP, LHDCDPI,LFLEXDIA 
USE YOMMDDH  , ONLY : JPDHNOX  ,JPDHXPU  ,NDHKD    ,NDHNPU   ,&
 & NDHBPU   ,NDHBPX   ,NDHNOM   ,NDHDDX   ,NDHIDH   ,&
 & NDHCS    ,NDHCV    ,NDHCVSU  ,NDHCSSU  ,&
 & NDHCVSUN ,NDHCVSUL ,NDHVV    ,NDHFVD   ,NDHFVP   ,&
 & NDHVS    ,NDHFSD   ,NDHFSP   ,NDHFFS   ,NDHVFS   ,&
 & NDHVTLS  ,NDHFTLS  ,NDHVTSS  ,NDHFTSS  ,NDHVTTS  ,&
 & NDHFTTS  ,NDHVTIS  ,NDHFTIS  ,NDHVSSS  ,NDHFSSS  ,&
 & NDHVIIS  ,NDHFIIS  ,NDHVWLS  ,NDHFWLS  ,&
 & NDHTHK   ,NDHVHK   ,NDHFHKD  ,NDHFHKP  ,NDHTMC   ,&
 & NDHVMC   ,NDHFMCD  ,NDHFMCP  ,NDHTEN   ,NDHVEN   ,&
 & NDHFEND  ,NDHFENP  ,NDHAVD   ,NDHBVD   ,NDHAVP   ,&
 & NDHBVP   ,NDHAHKD  ,NDHBHKD  ,NDHAHKP  ,NDHBHKP  ,&
 & NDHAMCD  ,NDHBMCD  ,NDHAMCP  ,NDHBMCP  ,NDHAEND  ,&
 & NDHBEND  ,NDHAENP  ,NDHBENP  ,NDHZPR   ,BDEDDH  
USE YOMSDDH  , ONLY : NHDQLNVA ,NHDQLNTD ,NHDQLNFD ,NHDQLNFP ,&
 & NHDQLNTP ,NHDADDVA ,NHDADDTD ,NHDADDFD ,NHDADDFP ,&
 & NHDADDTP ,NHDPASVA ,NHDPASTD ,NHDPASFD ,NHDPASFP ,&
 & NHDPASTP ,LHDQLN   ,LHDADD   ,LHDPAS  
USE YOEPHY   , ONLY : LAGPHY   ,LEPCLD
USE YOMCT0B  , ONLY : LECMWF
USE YOMDPHY  , ONLY : NCSI     ,NCSNEC   ,NTILES
USE SURFACE_FIELDS_MIX  , ONLY : YSP_SBD
USE YOMPHY   , ONLY : LCONDWT, LPROCLD, NPHY, JPHYARO,& 
 & L3MT, LSTRAPRO, NDPSFI
USE YOM_YGFL , ONLY : NGFL_EXT, LCO2SFC, NGHG, NTRAC, NACTAERO,LCH4SFC,YCH4S, &
 & LGRGSFC,NGRG,LFIRE
USE YOMARPHY , ONLY : LMPA, LBUFLUX
USE YOMDIM,ONLY: NPROMA,NFLEVG
USE DDH_MIX,ONLY:SUDDHFLEX
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM) :: IMASK, JBOX, JJ, JMASK

LOGICAL :: LLCUMUL, LLERR
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "posnam.intfb.h"
#include "sucddh.intfb.h"

#include "namddh.h"

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

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

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

WRITE (NULOUT,*) ' ---------------------------------------'
WRITE (NULOUT,*) ' DDH * DDH * DDH * DDH * DDH *'
WRITE (NULOUT,*) ' DIAGNOSTICS PAR DOMAINES HORIZONTAUX'

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

!**   1. VALEURS PAR DEFAUT

!        1.1 Set implicit default values

!        PAS DE DIAGNOSTICS PAR DEFAUT
LHDGLB = .FALSE.
LHDZON = .FALSE.
LHDDOP = .FALSE.

LHDLFA=.TRUE.
LFLEXDIA=.FALSE.
LHDHKS = .FALSE.
LHDMCI = .FALSE.
LHDENT = .FALSE.

LHDPRG = .FALSE.
LHDPRZ = .FALSE.
LHDPRD = .FALSE.
LHDEFG = .FALSE.
LHDEFZ = .FALSE.
LHDEFD = .FALSE.

LHDLIST = .FALSE.

LONLYVAR = NPHY==JPHYARO
LHDORIGP = .TRUE.
LHDCDPI = .FALSE.

DO JMASK = 1, JPDHXPU
  NDHBPU(JMASK) = 0
ENDDO
DO JBOX = 1, JPDHNOX
  DO JJ = 1, 10
    BDEDDH(JJ,JBOX) = 0.0_JPRB
  ENDDO
ENDDO
!     BANDE ZONALE IMPRIMEE PAR DEFAUT
NDHZPR = 1

!     LHDFIL = IMPRESS. DE LA LISTE DES ARTICLES DES FICHIERS DE SORTIE.
LHDFIL = .FALSE.

!     INITIALISATION DE SECURITE DES INDICATEURS DE SORTIES EFFECTIVES
!     (GERES AU NIVEAU DE CNT4 )
LHDOUFG=.FALSE.
LHDOUFZ=.FALSE.
LHDOUFD=.FALSE.
LHDOUP = .FALSE.

!     INITIALISATION PAR DEFAUT PROVISOIRE DES DIMENSIONS
NDHKD = 0
NDHNPU = 0
NDHBPX = 0
NDHNOM = 0
NDHDDX = 0
NDHIDH = 0

!        1.2 Modify default values according to LECMWF

IF (LECMWF) THEN
  LHDLFA=.FALSE.
ELSE
ENDIF

!     ------------------------------------------------------------------
!**   2. LECTURE SUR NAMELIST
CALL POSNAM(NULNAM,'NAMDDH')
READ (NULNAM,NAMDDH)

!     ------------------------------------------------------------------
!**   3. INITIALISATION DES DIMENSIONS DEDUITES

!*    INITIALISATION DES POINTEURS LOGIQUES PERMANENTS
!     ------------------------------------------------------------------
!     LES ENTIERS DEFINIS ICI DOIVENT ETRE RIGOUREUSEMENT EN
!     PHASE AVEC LE CODE SCIENTIFIQUE :
!       CPDYDDH : CALCULS DES CONTIBUTIONS DYNAMQIUES
!       CPPHDDH : CALCUL DES CONTRIBUTIONS PHYIQUES
!       POSDDH ET AUTRES SS-PRGM DE SORTIES (PPFIDH, PPEDDH)



IF(LHDHKS) THEN
  SELECT CASE (NPHY)
  CASE(JPHYARO)
    LHDQLN=.TRUE.
  CASE DEFAULT
!             - ARPEGE/ALADIN -
    IF ((LECMWF.AND.LEPCLD).OR.(.NOT.LECMWF.AND.(.NOT.LMPA))) THEN
      !        DIAGNOSTIC D'EAU LIQUIDE ET NEIGEUSE.
      LHDQLN=.TRUE.
      IF ( L3MT.OR.LSTRAPRO )THEN
        NHDQLNVA= 4  ! total number of variables for water
        NHDQLNTD= 2  ! number of dyn. tendencies
        NHDQLNFD= 2  ! number of dyn. fluxes
        ! number of physical fluxes
        IF (NDPSFI==1) THEN
          NHDQLNFP= 22 
        ELSE
          NHDQLNFP= 20 
        ENDIF
        NHDQLNTP= 0  ! number of physical tendencies
      ELSE
        NHDQLNVA=2
        NHDQLNTD=2
        NHDQLNFD=2
        IF (LCONDWT.AND.LPROCLD) THEN
          NHDQLNFP=14
        ELSE
          NHDQLNFP=10
        ENDIF
        NHDQLNTP=0
      ENDIF
    ELSE
      LHDQLN=.FALSE.
      NHDQLNVA=0
      NHDQLNTD=0
      NHDQLNFD=0
      NHDQLNFP=0
      NHDQLNTP=0
    ENDIF
  END SELECT
  IF (LHDCDPI) THEN
!     - Common Dynamics Physics Interface -
    IF (.NOT.LONLYVAR) THEN
      IF (LBUFLUX) THEN
        NHDQLNFP= NHDQLNFP+11
      ELSE
        NHDQLNTP= NHDQLNTP+11
      ENDIF
    ENDIF
  ENDIF
ENDIF

IF(LHDHKS.AND..NOT.LHDQLN) THEN
!        DIAGNOSTICS ADDITIONNELS DE PRECIPITATIONS.
  LHDADD=.TRUE..AND..NOT.LMPA
  NHDADDVA=0
  NHDADDTD=0
  NHDADDFD=0
  NHDADDFP=0
  NHDADDTP=20
ELSE
  LHDADD=.FALSE.
  NHDADDVA=0
  NHDADDTD=0
  NHDADDFD=0
  NHDADDFP=0
  NHDADDTP=0
ENDIF

IF(LHDHKS.AND..NOT.LHDQLN.AND.NGFL_EXT >= 1) THEN
!        DIAGNOSTIC DES VARIABLES PASSIVES (extra-GFL).
  LHDPAS=.TRUE.
  NHDPASVA=NGFL_EXT
  NHDPASTD=0
  NHDPASFD=0
  NHDPASFP=0
  NHDPASTP=0
ELSE
  LHDPAS=.FALSE.
  NHDPASVA=0
  NHDPASTD=0
  NHDPASFD=0
  NHDPASFP=0
  NHDPASTP=0
ENDIF

!     VARIABLES SI LHDHKS
SELECT CASE (NPHY)
CASE DEFAULT
  NDHVHK = 10+NHDQLNVA+NHDADDVA+NHDPASVA
END SELECT

!     VARIABLES SI LHDMCI
NDHVMC = 3
!     VARIABLES SI LHDENT
NDHVEN = 1

!     TENDANCES DYNAMIQUES SI LHDHKS
SELECT CASE (NPHY)
CASE DEFAULT
  NDHAHKD = 13+NHDQLNTD+NHDADDTD+NHDPASTD
END SELECT

!     TENDANCES DYNAMIQUES SI LHDMCI
NDHAMCD = 8
!     TENDANCES DYNAMIQUES SI LHDENT
NDHAEND = 2

!     FLUX DYNAMIQUES SI LHDHKS
SELECT CASE (NPHY)
CASE DEFAULT
  NDHBHKD = 10+NHDQLNFD+NHDADDFD+NHDPASFD
END SELECT
!     FLUX DYNAMIQUES SI LHDMCI
NDHBMCD = 9
!     FLUX DYNAMIQUES SI LHDENT
NDHBEND = 2

!     FLUX PHYSIQUES SI LHDHKS
IF (LMPA)  THEN
ELSE
ENDIF
SELECT CASE (NPHY)
CASE(JPHYARO)
CASE DEFAULT
  NDHAHKP = 26+NHDQLNFP+NHDADDFP+NHDPASFP
  IF ( (L3MT.OR.LSTRAPRO).AND.(NDPSFI==1) ) NDHAHKP = NDHAHKP + 2
END SELECT
!     FLUX PHYSIQUES SI LHDMCI
NDHAMCP = 9
!     FLUX PHYSIQUES SI LHDENT
NDHAENP = 0

!     TENDANCES PHYSIQUES SI LHDHKS
NDHBHKP = 3+NHDQLNTP+NHDADDTP+NHDPASTP
!     TENDANCES PHYSIQUES SI LHDMCI
NDHBMCP = 0
!     TENDANCES PHYSIQUES SI LHDENT
NDHBENP = 6

!     VARIABLES AU SOL

IF (LECMWF) THEN

!      number of variables in individual classes

  NDHVTLS = 3
  NDHVTSS = 6
  NDHVTTS = 4
  NDHVTIS = 4
  NDHVSSS = 1
  NDHVIIS = 1
  NDHVWLS = 2

!      total number of soil variables

  NDHVS =  NDHVTLS*NTILES+&
   & (NDHVTSS+NDHVSSS)*NCSNEC+&
   & (NDHVTTS+NDHVWLS)*YSP_SBD%NLEVS+&
   & NDHVTIS*NCSI+NDHVIIS  
  
!      number of free-style variables
! GHG fields included in DDH if present (if NGHG/=0)

  NDHVFS = 17 + NGHG + NACTAERO + NTRAC + NGRG
! SF6 included in flexible tracers
! SF6 included in DDH if present 
! All Tracers included
!  DO JTRAC=1,NTRAC
!    IF(YTRAC(JTRAC)%IGRBCODE==210182) THEN
!      NDHVFS=NDHVFS+1
!      EXIT
!    ENDIF
!  ENDDO

  IF(YCH4S%LACTIVE)THEN
    NDHVFS=NDHVFS+1
  ENDIF

ELSE

  NDHVS = 6
  NDHVFS = 9

ENDIF

!     FLUX/TENDANCES DYNAMIQUES AU SOL

NDHFSD = 0

!     FLUX TENDANCES PHYSIQUES AU SOL

IF (LECMWF) THEN

!      number of fluxes in individual classes

  NDHFTLS = 8
  NDHFTSS = 9
  NDHFTTS = 11
  NDHFTIS = 9
  NDHFSSS = 4
  NDHFIIS = 3
  NDHFWLS = 8

!      total number of soil fluxes

  NDHFSP =  NDHFTLS*NTILES+&
   & (NDHFTSS+NDHFSSS)*NCSNEC+&
   & (NDHFTTS+NDHFWLS)*YSP_SBD%NLEVS+&
   & NDHFTIS*NCSI+NDHFIIS  
  
!      number of free-style fluxes

!  NDHFFS = 18
!  NDHFFS = 21
! add PARCS and TINC
!  NDHFFS = 23
!-- add VIMD , TSRC and TTRC
!  NDHFFS = 26
!-- add FDIR and CDIR
!  NDHFFS = 28
!-- add CBASE, ODEGL and VISIH
   NDHFFS = 31

!  add NACTAERO*7 flux variables to get DDH of aerosol physics
  IF (NACTAERO > 0) NDHFFS = NDHFFS + NACTAERO * 7
!    add 3 CO2 surface flux variables when LCO2SFC = .T and
!     1 CO2 fire flux if LFIRE is .T as well
!        2 CH4 surface flux variables when LCH4SFC true
  IF(LCO2SFC) NDHFFS = NDHFFS + 3_JPIM
  IF(LCO2SFC .AND. LFIRE) NDHFFS = NDHFFS + 1_JPIM
  IF(LCH4SFC) NDHFFS = NDHFFS + 1_JPIM
  IF(LCH4SFC .AND. LFIRE) NDHFFS = NDHFFS + 1_JPIM
!    add SF6 surface flux if present
!  IF(ANY(YTRAC(:)%IGRBCODE==210182)) NDHFFS = NDHFFS + 1_JPIM
  IF(NTRAC > 0 ) NDHFFS = NDHFFS + NTRAC 

!    add GRG surface flux variables when LGRGSFC = .T
  IF(LGRGSFC) NDHFFS = NDHFFS + NGRG 
ELSE

  NDHFSP = 17
  NDHFFS = 2

ENDIF

!     ------------------------------------------------------------------
NDHFHKD = NDHAHKD + NDHBHKD
NDHFHKP = NDHAHKP + NDHBHKP
NDHFENP = NDHAENP + NDHBENP
NDHFMCD = NDHAMCD + NDHBMCD
NDHFMCP = NDHAMCP + NDHBMCP
NDHFEND = NDHAEND + NDHBEND
!     ------------------------------------------------------------------
NDHDDX = 1

!     BANDES DE LATITUDES
IF ( LHDZON ) THEN
  IF ( NDHKD == 0 ) THEN
    WRITE (NULERR,*)&
     & ' ERREUR DDH  CONTRADICTION BANDES ZONALES ',&
     & ' NB DE BANDES NUL. MOYENNES ZONALES SUPPRIMEES '  
    WRITE (NULERR,*) ' EXPERIENCE TERMINEE '
    CALL ABOR1('ERREUR DDH  CONTRADICTION BANDES ZONALES')
  ELSE
    NDHDDX = NDHDDX*NDHKD
  ENDIF
ENDIF

!     DOMAINES LIMITES ET POINTS
!     LES DIMENSIONS PRATIQUES SONT DEDUITES DE LA LECTURE DU
!     TABLEAU BDEDDH : NB DE MASQUES, NB DE DOMAINES PAR MASQUE, ETC...

IF ( LHDDOP ) THEN
  NDHNOM = 0
  NDHNPU = 0
  DO JBOX = 1, JPDHNOX
    IF ( BDEDDH(1,JBOX) /= 0.0_JPRB ) THEN
      NDHNOM = NDHNOM + 1
      IMASK = NINT( BDEDDH(2,JBOX) )
      NDHNPU = MAX( NDHNPU,IMASK )
    ENDIF
  ENDDO

  IF ( NDHNPU > JPDHXPU ) THEN
    WRITE (NULERR,*) ' ERREUR DDH PAS ASSEZ DE PLANS ',&
     & ' PARAMETER JPDHXPU = ',JPDHXPU,' DEMANDE NAMELIST = ',&
     & NDHNPU  
    CALL ABOR1('ERREUR DDH PAS ASSEZ DE PLANS')
  ENDIF

  NDHBPX = 0
  DO JMASK = 1, NDHNPU
    NDHBPU( JMASK ) = 0
    DO JBOX = 1, NDHNOM
      IMASK = NINT( BDEDDH(2,JBOX) )
      IF ( IMASK == JMASK ) THEN
        NDHBPU(JMASK) = NDHBPU(JMASK) + 1
      ENDIF
    ENDDO
    NDHBPX = MAX( NDHBPX,NDHBPU(JMASK) )
    IF ( NDHBPU(JMASK) == 0 ) THEN
      WRITE (NULERR,*) ' ERREUR DDH PLAN VIDE ',JMASK
      CALL ABOR1(' ERREUR DDH PLAN VIDE ')
    ENDIF
    NDHDDX = NDHDDX*( NDHBPU(JMASK)+1 )
  ENDDO

ENDIF

!     DIMENSIONS LIEES AU NOMBRE DE CHAMPS

NDHCS = 0
NDHTHK = 0
NDHTMC = 0
NDHTEN = 0
NDHFVD = 0
NDHFVP = 0
NDHCV = 0
NDHVV = 0
NDHAVD = 0
NDHBVD = 0
NDHAVP = 0
NDHBVP = 0
IF ( .NOT.LHDHKS ) THEN
  NDHVS = 0
  NDHFSD = 0
  NDHFSP = 0
ENDIF
IF ( LHDHKS ) THEN
  NDHCS = NDHVS + NDHVFS + NDHFSD + NDHFSP + NDHFFS
  NDHTHK = NDHVHK + NDHFHKD + NDHFHKP
  NDHCV = NDHCV + NDHTHK
  NDHVV = NDHVV + NDHVHK
  NDHAVD = NDHAVD + NDHAHKD
  NDHBVD = NDHBVD + NDHBHKD
  NDHAVP = NDHAVP + NDHAHKP
  NDHBVP = NDHBVP + NDHBHKP
  NDHFVD = NDHFVD + NDHFHKD
  NDHFVP = NDHFVP + NDHFHKP
ENDIF
IF ( LHDMCI ) THEN
  NDHTMC = NDHVMC + NDHFMCD + NDHFMCP
  NDHCV = NDHCV + NDHTMC
  NDHVV = NDHVV + NDHVMC
  NDHAVD = NDHAVD + NDHAMCD
  NDHBVD = NDHBVD + NDHBMCD
  NDHAVP = NDHAVP + NDHAMCP
  NDHBVP = NDHBVP + NDHBMCP
  NDHFVD = NDHFVD + NDHFMCD
  NDHFVP = NDHFVP + NDHFMCP
ENDIF
IF ( LHDENT ) THEN
  NDHTEN = NDHVEN + NDHFEND + NDHFENP
  NDHCV = NDHCV + NDHTEN
  NDHVV = NDHVV + NDHVEN
  NDHAVD = NDHAVD + NDHAEND
  NDHBVD = NDHBVD + NDHBEND
  NDHAVP = NDHAVP + NDHAENP
  NDHBVP = NDHBVP + NDHBENP
  NDHFVD = NDHFVD + NDHFEND
  NDHFVP = NDHFVP + NDHFENP
ENDIF
!     DIMENSIONS LIEES AU NOMBRE DE CHAMPS
!      INSUR = 1 - MOD(NDHCV,2)
!      NDHCVSU = NDHCV+INSUR
NDHCVSU = MAX(NDHCV,1)
NDHCSSU = MAX(NDHCS,1)
IF(LAGPHY) THEN
  NDHCVSUN= MAX(NDHVV + NDHFVD,1)
  NDHCVSUL= MAX(NDHCVSU-(NDHVV + NDHFVD),1)
ELSE
  NDHCVSUN= NDHCVSU
  NDHCVSUL = 1
ENDIF




!     ------------------------------------------------------------------
!**   4. CONTROLE DE COHERENCE DES OPTIONS LOGIQUES

!      INDICATEURS LOGIQUES GLOBAUX:
!         LSDDH: INDICATEUR DE DIAGNOSTICS DEMANDES
!         LHDEF: INDICATEUR DE SORTIES FICHIERS DEMANDEES
!         LHDPR: INDICATEUR DE SORTIES LISTING  DEMANDEES

LSDDH = LHDGLB .OR. LHDZON .OR. LHDDOP
LHDEF = LHDEFG .OR. LHDEFZ .OR. LHDEFD
LHDPR = LHDPRG .OR. LHDPRZ .OR. LHDPRD

!     CONTROLE DE COHERENCE

LLERR = .FALSE.
IF ( LSDDH ) THEN
!         DES DIAGNOSTICS SONT DEMANDES
  IF ( .NOT.LHDHKS.AND..NOT.LHDMCI.AND..NOT.LHDENT ) THEN
    WRITE(NULERR,*) ' ERREUR DDH *** '
    WRITE(NULERR,*) ' DIAGNOSTICS DEMANDES MAIS AUCUN',&
     & ' CHAMP SCIENTIFIQUE VALIDE'  
    LLERR=.TRUE.
  ENDIF
ENDIF
!      COHERENCE DOMAINE/SORTIE
LLCUMUL=            (LHDGLB.EQV.(LHDPRG.OR.LHDEFG))
LLCUMUL=LLCUMUL.AND.(LHDZON.EQV.(LHDPRZ.OR.LHDEFZ))
LLCUMUL=LLCUMUL.AND.(LHDDOP.EQV.(LHDPRD.OR.LHDEFD))
IF ( .NOT.LLCUMUL ) THEN
  WRITE(NULERR,*) ' ERREUR DDH *** '
  WRITE(NULERR,*) ' INCOHERENCE ENTRE DOMAINE(S) ET',&
   & ' SORTIE(S) ASSOCIEE(S) DEMANDES'  
  LLERR=.TRUE.
ENDIF

IF ( LLERR ) THEN
!        UNE INCOHERENCE AU MOINS DETECTEE.
  WRITE (NULERR,*) ' EXPERIENCE TERMINEE '
  CALL ABOR1('SUNDDH - UNE INCOHERENCE AU MOINS DETECTEE')
ENDIF

! activates flexible data flow for ddh in arome
IF (LSDDH .AND. NPHY==JPHYARO) THEN
  LFLEXDIA=.TRUE.
  NDHCV=180 ! hopefully it is enough 
  NDHCVSU=NDHCV
ENDIF

IF (LFLEXDIA) CALL SUDDHFLEX(NPROMA,NFLEVG)

!     ------------------------------------------------------------------
!**   5. IMPRESSIONS

!MH       WRITE (NULOUT,*) ' NAMELIST NAMDDH'
!MH       WRITE (NULOUT,*) ' ---------------'
!MH       WRITE (NULOUT,NAMDDH)
WRITE (NULOUT,*) ' DOMAINE GLOBAL = ',LHDGLB,' BANDES ZONALES =',LHDZON
WRITE (NULOUT,*) ' DOMAINES LIMITES OU POINTS = ',LHDDOP
WRITE (NULOUT,*) ' MASSES - ENERGIES - SOL = ',LHDHKS
WRITE (NULOUT,*) ' ENTROPIE = ',LHDENT
WRITE (NULOUT,*) ' MOMENT CINETIQUE = ',LHDMCI
WRITE (NULOUT,*) ' IMPRESSIONS GLOBE = ',LHDPRG
WRITE (NULOUT,*) ' IMPRESSIONS 1 BANDE ZONALE = ',LHDPRZ,&
 & ' BANDE NDHZPR = ',NDHZPR  
WRITE (NULOUT,*) ' IMPRESSIONS DOMAINES ET POINTS = ',LHDPRD
WRITE (NULOUT,*) ' ECRITURE GLOBE = ',LHDEFG,' BANDES ZONALES =',LHDEFZ
WRITE (NULOUT,*) ' ECRITURE DOMAINES LIMITES ET POINTS ',LHDEFD
WRITE (NULOUT,*) ' LHDQLN = ',LHDQLN,' LHDADD = ',LHDADD,' LHDPAS = ',LHDPAS

!      CIF LSDDH IF
!      ON N'IMPRIME LE DETAIL DES OPTIONS
!      QUE SI LES DIAGNOSTICS SONT ACTIVES.

IF ( LSDDH ) THEN

  IF ( LHDLIST )WRITE (NULOUT,*) ' IMPRESSIONS ETENDUES '
  IF ( .NOT.LHDLIST )WRITE (NULOUT,*) ' IMPRESSIONS MINIMALES '
  WRITE (NULOUT,*) ' DIMENSIONS POUR DDH '
  WRITE (NULOUT,*) ' ------------------- '
  WRITE (NULOUT,*) ' NB DE PLANS-MASQUES MAXI = ',JPDHXPU,&
   & ' DONT ',NDHNPU,' UTILISES '  
  WRITE (NULOUT,*) ' NB DE DOMAINES LIMITES MAXI = ',JPDHNOX,&
   & ' DONT ',NDHNOM,' UTILISES '  
  WRITE (NULOUT,*) ' NB DE BANDES DE LATITUDE ',NDHKD
  DO JMASK = 1, NDHNPU
    WRITE (NULOUT,*) ' NB DE DOMAINES LIMITES DANS LE PLAN ',&
     & JMASK,' = ',NDHBPU(JMASK)  
  ENDDO
  WRITE (NULOUT,*) ' NB MAXI DE DOMAINES LIMITES PAR PLAN =',NDHBPX
  WRITE (NULOUT,*) ' NB DE DOMAINES INTERNES POSSIBLES =',NDHDDX
  IF ( LHDDOP ) THEN
    WRITE (NULOUT,*) ' CARACTERISTIQUES DES DOMAINES LIMITES '
    WRITE (NULOUT,*) ' ------------------------------------- '
    DO JBOX = 1, JPDHNOX
      WRITE (NULOUT,5001) (BDEDDH(JJ,JBOX),JJ=1,10)
      5001 FORMAT ( T5,10(F8.2,1X) )
    ENDDO
  ENDIF
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) ' NOMBRES DE CHAMPS PAR CATEGORIES '
  WRITE (NULOUT,*) ' -------------------------------- '
  WRITE (NULOUT,*) '        VARIABLES  FLUX DYN  FLUX PHY'
  WRITE (NULOUT,*) ' SOL    ',NDHVS,NDHFSD,NDHFSP
  WRITE (NULOUT,*) ' LHDHKS ',NDHVHK,NDHFHKD,' DONT ',NDHAHKD,&
   & NDHBHKD,' ET ',NDHFHKP,' DONT ',NDHAHKP,NDHBHKP  
  WRITE (NULOUT,*) ' LHDMCI ',NDHVMC,NDHFMCD,' DONT ',NDHAMCD,&
   & NDHBMCD,' ET ',NDHFMCP,' DONT ',NDHAMCP,NDHBMCP  
  WRITE (NULOUT,*) ' LHDENT ',NDHVEN,NDHFEND,' DONT ',NDHAEND,&
   & NDHBEND,' ET ',NDHFENP,' DONT ',NDHAENP,NDHBENP  
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) ' NB TOTAL DE CHAMPS VERTICAUX NDHCV = ',NDHCV
  WRITE (NULOUT,*) ' DONT NDHVV = ',NDHVV,' VARIABLES '
  WRITE (NULOUT,*) '      NDHFVD = ',NDHFVD,' FLUX DYNAMIQUES ',&
   & ' DONT ',NDHAVD,' TENDANCES ET ',NDHBVD,' FLUX '  
  WRITE (NULOUT,*) '      NDHFVP = ',NDHFVP,' FLUX PHYSIQUES ',&
   & ' DONT ',NDHAVP,' FLUX ET ',NDHBVP,' TENDANCES '  
  WRITE (NULOUT,*) ' (VERIF) SOIT BIEN ',NDHVV+NDHFVD+NDHFVP,' CHAMPS'
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) ' NB TOTAL DE CHAMPS AU SOL NDHCS = ',NDHCS
  WRITE (NULOUT,*) ' DONT NDHVS = ',NDHVS,' VARIABLES '
  WRITE (NULOUT,*) '      NDHFSD = ',NDHFSD,' FLUX DYNAMIQUES'
  WRITE (NULOUT,*) '      NDHFSP = ',NDHFSP,' FLUX PHYSIQUES'
  WRITE (NULOUT,*) '      NDHFFS = ',NDHFFS,' FREE-STYLE FLUXES'
  WRITE (NULOUT,*) '      NDHVFS = ',NDHVFS,' FREE-STYLE VARIABLES'
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) '        NB TOTAUX PAR TYPE SCIENTIFIQUE'
  WRITE (NULOUT,*) ' (SOL    ',NDHCS,')'
  WRITE (NULOUT,*) ' LHDHKS ',NDHTHK
  WRITE (NULOUT,*) ' LHDMCI ',NDHTMC
  WRITE (NULOUT,*) ' LHDENT ',NDHTEN
  WRITE (NULOUT,*) ' '
  WRITE (NULOUT,*) ' DIMENSIONS POUR TABLEAUX LOCAUX'
  WRITE (NULOUT,*) ' NDHCVSU = ',NDHCVSU
  WRITE (NULOUT,*) ' NDHCSSU = ',NDHCSSU
  WRITE (NULOUT,*) ' NDHCVSUN = ',NDHCVSUN
  WRITE (NULOUT,*) ' NDHCVSUL = ',NDHCVSUL
  WRITE (NULOUT,*) ' '

!      CIF LSDDH ENDIF

ENDIF

WRITE (NULOUT,*) ' '
WRITE (NULOUT,*) ' LOGIQUE GLOBALE LSDDH = ',LSDDH
WRITE (NULOUT,*) ' LOGIQUE IMPRESSION LHDPR = ',LHDPR
WRITE (NULOUT,*) ' LOGIQUE ECRITURE LHDEF = ',LHDEF

WRITE (NULOUT,*) ' '
WRITE (NULOUT,*) ' ---------------------------------- DDH -'
WRITE (NULOUT,*) ' '

IF (LECMWF) THEN
  CALL SUCDDH
ENDIF

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

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