From a8cb5e5bac7b077c7b184fc720d50cdd8d57ee1b Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 1 Jul 2021 11:25:26 +0200
Subject: [PATCH] Philippe 01/07/2021: budgets: LES: use level "group" for
 netCDF files

---
 src/MNH/mode_les_diachro.f90     | 93 ++++++++++++++++++--------------
 src/MNH/write_diachro.f90        |  4 +-
 src/MNH/write_les_budgetn.f90    | 39 ++++++++------
 src/MNH/write_les_rt_budgetn.f90 | 38 +++++++------
 src/MNH/write_les_sv_budgetn.f90 | 33 ++++++------
 src/MNH/write_lesn.f90           | 40 ++++++++++++--
 6 files changed, 153 insertions(+), 94 deletions(-)

diff --git a/src/MNH/mode_les_diachro.f90 b/src/MNH/mode_les_diachro.f90
index 1aeec63c1..3bef1bc24 100644
--- a/src/MNH/mode_les_diachro.f90
+++ b/src/MNH/mode_les_diachro.f90
@@ -654,15 +654,17 @@ end if
 
 end function Les_time_avg_1pt
 
-!#######################################################################
-subroutine Les_diachro_1D( tpdiafile, tpfield, odoavg, odonorm, pfield )
-!#######################################################################
+!##############################################################################################
+subroutine Les_diachro_1D( tpdiafile, tpfield, hgroup, hgroupcomment, odoavg, odonorm, pfield )
+!##############################################################################################
 
 use modd_field, only: NMNHDIM_BUDGET_LES_TIME, NMNHDIM_UNUSED, tfield_metadata_base
 use modd_io,    only: tfiledata
 
 type(tfiledata),                       intent(in) :: tpdiafile  ! File to write
 type(tfield_metadata_base),            intent(in) :: tpfield    ! Metadata of field
+character(len=*),                      intent(in) :: hgroup     ! Group of the field
+character(len=*),                      intent(in) :: hgroupcomment
 logical,                               intent(in) :: odoavg     ! Compute and store time average
 logical,                               intent(in) :: odonorm    ! Compute and store normalized field
 real,                    dimension(:), intent(in) :: pfield     ! Data array
@@ -687,7 +689,7 @@ if ( tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_TIME ) then
   tzfield%ndimlist(1) = NMNHDIM_UNUSED
   tzfield%ndimlist(3) = NMNHDIM_UNUSED
   tzfield%ndimlist(4) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield, reshape( pfield, [ 1, size( pfield, 1 ), 1, 1 ] ), &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment, reshape( pfield, [ 1, size( pfield, 1 ), 1, 1 ] ), &
                            odoavg, odonorm )
 else
   call Print_msg( NVERB_ERROR, 'IO', 'Les_diachro_1D', &
@@ -696,9 +698,9 @@ end if
 
 end subroutine Les_diachro_1D
 
-!#######################################################################
-subroutine Les_diachro_2D( tpdiafile, tpfield, odoavg, odonorm, pfield )
-!#######################################################################
+!##############################################################################################
+subroutine Les_diachro_2D( tpdiafile, tpfield, hgroup, hgroupcomment, odoavg, odonorm, pfield )
+!##############################################################################################
 
 use modd_field, only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_SV, NMNHDIM_BUDGET_LES_TIME, NMNHDIM_UNUSED, &
                       tfield_metadata_base
@@ -706,6 +708,8 @@ use modd_io,    only: tfiledata
 
 type(tfiledata),                         intent(in) :: tpdiafile  ! File to write
 type(tfield_metadata_base),              intent(in) :: tpfield    ! Metadata of field
+character(len=*),                        intent(in) :: hgroup     ! Group of the field
+character(len=*),                        intent(in) :: hgroupcomment
 logical,                                 intent(in) :: odoavg     ! Compute and store time average
 logical,                                 intent(in) :: odonorm    ! Compute and store normalized field
 real,                    dimension(:,:), intent(in) :: pfield     ! Data array
@@ -729,7 +733,7 @@ if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
      .and. tzfield%ndimlist(2) == NMNHDIM_BUDGET_LES_TIME  ) then
   tzfield%ndimlist(3) = NMNHDIM_UNUSED
   tzfield%ndimlist(4) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield,                                                &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment,                         &
                            reshape( pfield, [ size( pfield, 1 ), size( pfield, 2 ), 1, 1 ] ), &
                            odoavg, odonorm )
 else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_TIME &
@@ -738,7 +742,7 @@ else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_TIME &
   tzfield%ndimlist(2) = tzfield%ndimlist(1)
   tzfield%ndimlist(1) = NMNHDIM_UNUSED
   tzfield%ndimlist(3) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield,                                                &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment,                         &
                            reshape( pfield, [ 1, size( pfield, 1 ), 1, size( pfield, 2 ) ] ), &
                            odoavg, odonorm )
 else
@@ -749,9 +753,9 @@ end if
 
 end subroutine Les_diachro_2D
 
-!##########################################################################################
-subroutine Les_diachro_3D( tpdiafile, tpfield, odoavg, odonorm, pfield, hsuffixes, hmasks )
-!##########################################################################################
+!#################################################################################################################
+subroutine Les_diachro_3D( tpdiafile, tpfield, hgroup, hgroupcomment, odoavg, odonorm, pfield, hsuffixes, hmasks )
+!#################################################################################################################
 
 use modd_field, only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_MASK, NMNHDIM_BUDGET_LES_SV, &
                       NMNHDIM_BUDGET_LES_TIME,  NMNHDIM_BUDGET_TERM,     NMNHDIM_UNUSED,        &
@@ -760,6 +764,8 @@ use modd_io,    only: tfiledata
 
 type(tfiledata),                           intent(in) :: tpdiafile  ! File to write
 type(tfield_metadata_base),                intent(in) :: tpfield    ! Metadata of field
+character(len=*),                          intent(in) :: hgroup     ! Group of the field
+character(len=*),                          intent(in) :: hgroupcomment
 logical,                                   intent(in) :: odoavg     ! Compute and store time average
 logical,                                   intent(in) :: odonorm    ! Compute and store normalized field
 real,                    dimension(:,:,:), intent(in) :: pfield     ! Data array
@@ -792,7 +798,7 @@ if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_3D', 'wrong size for hmasks (' // Trim( tzfield%cmnhname ) // ')' )
 
   tzfield%ndimlist(4) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield,                                                                &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment,                                         &
                            reshape( pfield, [ size( pfield, 1 ), size( pfield, 2 ), size( pfield, 3 ), 1 ] ), &
                            odoavg, odonorm, hmasks = hmasks )
 else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
@@ -806,7 +812,7 @@ else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_3D', 'wrong size for hsuffixes (' // Trim( tzfield%cmnhname ) // ')' )
 
   tzfield%ndimlist(4) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield,                                                                &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment,                                         &
                            reshape( pfield, [ size( pfield, 1 ), size( pfield, 2 ), size( pfield, 3 ), 1 ] ), &
                            odoavg, odonorm, hsuffixes = hsuffixes )
 else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
@@ -822,7 +828,7 @@ else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
 
   tzfield%ndimlist(4) = tzfield%ndimlist(3)
   tzfield%ndimlist(3) = NMNHDIM_UNUSED
-  call Les_diachro_common( tpdiafile, tzfield,                                                                &
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment,                                         &
                            reshape( pfield, [ size( pfield, 1 ), size( pfield, 2 ), 1, size( pfield, 3 ) ] ), &
                            odoavg, odonorm )
 else
@@ -832,9 +838,9 @@ end if
 
 end subroutine Les_diachro_3D
 
-!##########################################################################################
-subroutine Les_diachro_4D( tpdiafile, tpfield, odoavg, odonorm, pfield, hsuffixes, hmasks )
-!##########################################################################################
+!#################################################################################################################
+subroutine Les_diachro_4D( tpdiafile, tpfield, hgroup, hgroupcomment, odoavg, odonorm, pfield, hsuffixes, hmasks )
+!#################################################################################################################
 
 use modd_field, only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_MASK, NMNHDIM_BUDGET_LES_SV, &
                       NMNHDIM_BUDGET_LES_TIME,  NMNHDIM_BUDGET_TERM,     NMNHDIM_UNUSED,        &
@@ -842,7 +848,9 @@ use modd_field, only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_MASK, NMNHDIM
 use modd_io,    only: tfiledata
 
 type(tfiledata),                             intent(in) :: tpdiafile  ! File to write
-type(tfield_metadata_base), intent(in) :: tpfield ! Metadata of field
+type(tfield_metadata_base),                  intent(in) :: tpfield ! Metadata of field
+character(len=*),                            intent(in) :: hgroup     ! Group of the field
+character(len=*),                            intent(in) :: hgroupcomment
 logical,                                     intent(in) :: odoavg     ! Compute and store time average
 logical,                                     intent(in) :: odonorm    ! Compute and store normalized field
 real,                    dimension(:,:,:,:), intent(in) :: pfield     ! Data array
@@ -875,7 +883,7 @@ if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL&
   if ( Size( hmasks ) /= Size( pfield, 3) ) &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_4D', 'wrong size for hmasks (' // Trim( tzfield%cmnhname ) // ')' )
 
-  call Les_diachro_common( tpdiafile, tzfield, pfield, odoavg, odonorm, hmasks = hmasks )
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment, pfield, odoavg, odonorm, hmasks = hmasks )
 else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
           .and. tzfield%ndimlist(2) == NMNHDIM_BUDGET_LES_TIME  &
           .and. tzfield%ndimlist(3) == NMNHDIM_BUDGET_TERM      &
@@ -887,7 +895,7 @@ else if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
   if ( Size( hsuffixes ) /= Size( pfield, 3) ) &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_4D', 'wrong size for hsuffixes (' // Trim( tzfield%cmnhname ) // ')' )
 
-  call Les_diachro_common( tpdiafile, tzfield, pfield, odoavg, odonorm, hsuffixes= hsuffixes )
+  call Les_diachro_common( tpdiafile, tzfield, hgroup, hgroupcomment, pfield, odoavg, odonorm, hsuffixes= hsuffixes )
 else
   call Print_msg( NVERB_ERROR, 'IO', 'Les_diachro_4D', &
                   'ndimlist configuration not yet implemented for ' // Trim( tzfield%cmnhname ) )
@@ -895,9 +903,9 @@ end if
 
 end subroutine Les_diachro_4D
 
-!##############################################################################################
-subroutine Les_diachro_common( tpdiafile, tpfield, pfield, odoavg, odonorm, hsuffixes, hmasks )
-!##############################################################################################
+!#####################################################################################################################
+subroutine Les_diachro_common( tpdiafile, tpfield, hgroup, hgroupcomment, pfield, odoavg, odonorm, hsuffixes, hmasks )
+!#####################################################################################################################
 
 use modd_field,         only: tfield_metadata_base
 use modd_io,            only: tfiledata
@@ -910,6 +918,8 @@ implicit none
 
 type(tfiledata),                                          intent(in) :: tpdiafile ! File to write
 type(tfield_metadata_base),                               intent(in) :: tpfield
+character(len=*),                                         intent(in) :: hgroup    ! Group of the field
+character(len=*),                                         intent(in) :: hgroupcomment
 real,                       dimension(:,:,:,:),           intent(in) :: pfield    ! Data array
 logical,                                                  intent(in) :: odoavg    ! Compute and store time average
 logical,                                                  intent(in) :: odonorm   ! Compute and store normalized field
@@ -947,7 +957,7 @@ if ( Present( hsuffixes ) ) then
                     'at the same time (' // Trim( tpfield%cmnhname ) // ')' )
   if ( Size( hsuffixes ) /= Size( pfield, 3) ) &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_common', 'wrong size for hsuffixes (' // Trim( tpfield%cmnhname ) // ')' )
-  ycomment(:) = Trim( tpfield%ccomment(:) ) // ' ' // hsuffixes(:)
+  ycomment(:) = Trim( tpfield%ccomment(:) ) // ': ' // hsuffixes(:)
 else if ( Present( hmasks ) ) then
   if ( Size( hmasks ) /= Size( pfield, 3) ) &
     call Print_msg( NVERB_FATAL, 'IO', 'Les_diachro_common', 'wrong size for hmasks (' // Trim( tpfield%cmnhname ) // ')' )
@@ -1018,7 +1028,8 @@ iresp = 0
 if ( oavg ) call Les_time_avg_4d( zfield, tzdates, iresp )
 
 if ( Present( hsuffixes ) ) then
-  ytitle(:) = Trim( tpfield%cmnhname ) // '_' // hsuffixes(:)
+  !ytitle(:) = Trim( tpfield%cmnhname ) // '_' // hsuffixes(:)
+  ytitle(:) = hsuffixes(:)
 else
   ytitle(:) = tpfield%cmnhname
 endif
@@ -1057,9 +1068,9 @@ if ( iresp == 0 .and. any( zfield /= XUNDEF ) ) then
   tzbudiachro%clevels  (NLVL_SUBCATEGORY) = ''
   tzbudiachro%ccomments(NLVL_SUBCATEGORY) = ''
 
-  tzbudiachro%lleveluse(NLVL_GROUP)       = .false.
-  tzbudiachro%clevels  (NLVL_GROUP)       = ''
-  tzbudiachro%ccomments(NLVL_GROUP)       = ''
+  tzbudiachro%lleveluse(NLVL_GROUP)       = .true.
+  tzbudiachro%clevels  (NLVL_GROUP)       = Trim( hgroup )
+  tzbudiachro%ccomments(NLVL_GROUP)       = Trim( hgroupcomment )
 
   tzbudiachro%lleveluse(NLVL_SHAPE)       = .true.
   tzbudiachro%clevels  (NLVL_SHAPE)       = 'Cartesian'
@@ -1296,12 +1307,12 @@ tzbudiachro%lleveluse(NLVL_SUBCATEGORY) = .false.
 tzbudiachro%clevels  (NLVL_SUBCATEGORY) = ''
 tzbudiachro%ccomments(NLVL_SUBCATEGORY) = ''
 
-tzbudiachro%lleveluse(NLVL_GROUP)       = .false.
-tzbudiachro%clevels  (NLVL_GROUP)       = ''
+tzbudiachro%lleveluse(NLVL_GROUP)       = .true.
+tzbudiachro%clevels  (NLVL_GROUP)       = 'Two_point_correlation'
 tzbudiachro%ccomments(NLVL_GROUP)       = ''
 
-tzbudiachro%lleveluse(NLVL_SHAPE)       = .true.
-tzbudiachro%clevels  (NLVL_SHAPE)       = 'Two_point_correlation'
+tzbudiachro%lleveluse(NLVL_SHAPE)       = .false.
+tzbudiachro%clevels  (NLVL_SHAPE)       = ''
 tzbudiachro%ccomments(NLVL_SHAPE)       = ''
 
 tzbudiachro%lleveluse(NLVL_TIMEAVG)     = .true.
@@ -1487,12 +1498,12 @@ tzbudiachro%lleveluse(NLVL_SUBCATEGORY) = .false.
 tzbudiachro%clevels  (NLVL_SUBCATEGORY) = ''
 tzbudiachro%ccomments(NLVL_SUBCATEGORY) = ''
 
-tzbudiachro%lleveluse(NLVL_GROUP)       = .false.
-tzbudiachro%clevels  (NLVL_GROUP)       = ''
+tzbudiachro%lleveluse(NLVL_GROUP)       = .true.
+tzbudiachro%clevels  (NLVL_GROUP)       = 'Spectrum'
 tzbudiachro%ccomments(NLVL_GROUP)       = ''
 
-tzbudiachro%lleveluse(NLVL_SHAPE)       = .true.
-tzbudiachro%clevels  (NLVL_SHAPE)       = 'Spectrum'
+tzbudiachro%lleveluse(NLVL_SHAPE)       = .false.
+tzbudiachro%clevels  (NLVL_SHAPE)       = ''
 tzbudiachro%ccomments(NLVL_SHAPE)       = ''
 
 tzbudiachro%lleveluse(NLVL_TIMEAVG)     = .true.
@@ -1549,12 +1560,12 @@ tzbudiachro%lleveluse(NLVL_SUBCATEGORY) = .false.
 tzbudiachro%clevels  (NLVL_SUBCATEGORY) = ''
 tzbudiachro%ccomments(NLVL_SUBCATEGORY) = ''
 
-tzbudiachro%lleveluse(NLVL_GROUP)       = .false.
-tzbudiachro%clevels  (NLVL_GROUP)       = ''
+tzbudiachro%lleveluse(NLVL_GROUP)       = .true.
+tzbudiachro%clevels  (NLVL_GROUP)       = 'Spectrum'
 tzbudiachro%ccomments(NLVL_GROUP)       = ''
 
-tzbudiachro%lleveluse(NLVL_SHAPE)       = .true.
-tzbudiachro%clevels  (NLVL_SHAPE)       = 'Spectrum'
+tzbudiachro%lleveluse(NLVL_SHAPE)       = .false.
+tzbudiachro%clevels  (NLVL_SHAPE)       = ''
 tzbudiachro%ccomments(NLVL_SHAPE)       = ''
 
 tzbudiachro%lleveluse(NLVL_TIMEAVG)     = .true.
diff --git a/src/MNH/write_diachro.f90 b/src/MNH/write_diachro.f90
index cfb648886..d78c07c1d 100644
--- a/src/MNH/write_diachro.f90
+++ b/src/MNH/write_diachro.f90
@@ -255,6 +255,8 @@ else if ( tpbudiachro%clevels(NLVL_GROUP) == 'RhodJ' ) then
 else if ( tpbudiachro%nsv > 0 ) then
   Allocate( character(len=9) :: ygroup )
   Write( ygroup, '( "SV", i3.3, i4.4 )' ) tpbudiachro%nsv, nbutshift
+else if ( tpbudiachro%clevels(NLVL_CATEGORY) == 'LES_budgets' .and. tpbudiachro%clevels(NLVL_GROUP)(1:3)/='BU_' ) then
+  ygroup = Trim( tpfields(1)%cmnhname )
 else
   ygroup = Trim( tpbudiachro%clevels(NLVL_GROUP) )
 end if
@@ -285,7 +287,7 @@ if (       Trim( tpbudiachro%clevels(NLVL_CATEGORY) ) == 'LES_budgets' &
 end if
 
 if (       Trim( tpbudiachro%clevels(NLVL_CATEGORY) ) == 'LES_budgets' &
-     .and. Trim( tpbudiachro%clevels(NLVL_SHAPE) )    == 'Spectrum'    ) then
+     .and. Trim( tpbudiachro%clevels(NLVL_GROUP) )    == 'Spectrum'    ) then
   if ( tpbudiachro%ltcompress ) then
     ygroup = 'T_' // Trim( ygroup )
     !Limit to 10 characters (backward compatibility again...)
diff --git a/src/MNH/write_les_budgetn.f90 b/src/MNH/write_les_budgetn.f90
index fec25b5ea..c056ca3da 100644
--- a/src/MNH/write_les_budgetn.f90
+++ b/src/MNH/write_les_budgetn.f90
@@ -99,7 +99,8 @@ INTEGER :: JK ! vertical loop counter
 INTEGER :: JT ! temporal loop counter
 !
 CHARACTER(len=9), DIMENSION(NMAX_ILES) :: YSUBTITLE
-CHARACTER(len=8)                       :: YGROUP
+character(len=:), allocatable          :: ygroup
+character(len=:), allocatable          :: ygroupcomment
 !
 REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZLES_BUDGET
 !
@@ -131,7 +132,8 @@ gdonorm = Trim( cles_norm_type ) /= 'NONE'
 !*      1.  total (resolved+subgrid) kinetic energy budget
 !            ------------------------------------
 !
-YGROUP= 'BU_KE'
+ygroup = 'BU_KE'
+ygroupcomment = 'Total (resolved+subgrid) kinetic energy budget'
 ILES=0
 ILES_STA=ILES
 !
@@ -432,13 +434,14 @@ END DO
 !       -------
 !
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'resolved KE budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'resolved KE budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'm2 s-3'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
@@ -446,7 +449,8 @@ call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles),
 !*      2.  temperature variance budget
 !           ---------------------------
 !
-YGROUP= 'BU_THL2'
+ygroup = 'BU_THL2'
+ygroupcomment = 'Temperature variance budget'
 ILES=0
 !
 ILES_STA=ILES
@@ -684,20 +688,22 @@ ZLES_BUDGET(NLES_K,:,ILES) = ZLES_BUDGET(NLES_K-1,:,ILES)
 !       -------
 !
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'thetal variance budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'thetal variance budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'K2 s-1'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
 !*      3.  temperature flux budget
 !            ---------------------
 !
-YGROUP= 'BU_WTHL'
+ygroup = 'BU_WTHL'
+ygroupcomment = 'Temperature flux budget'
 ILES=0
 !
 ILES_STA=ILES
@@ -1002,13 +1008,14 @@ ZLES_BUDGET(:,:,ILES)=-XLES_RES_ddxa_Thl_SBG_UaW(:,:,1)       &
 !       -------
 !
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'thetal flux budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'thetal flux budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'm K s-2'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
diff --git a/src/MNH/write_les_rt_budgetn.f90 b/src/MNH/write_les_rt_budgetn.f90
index e2cb00961..f51f69059 100644
--- a/src/MNH/write_les_rt_budgetn.f90
+++ b/src/MNH/write_les_rt_budgetn.f90
@@ -92,7 +92,8 @@ INTEGER :: JK ! vertical loop counter
 INTEGER :: JT ! temporal loop counter
 !
 CHARACTER(len=9), DIMENSION(NMAX_ILES) :: YSUBTITLE
-CHARACTER(len=8)                       :: YGROUP
+character(len=:), allocatable          :: ygroup
+character(len=:), allocatable          :: ygroupcomment
 !
 REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZLES_BUDGET
 !
@@ -125,7 +126,8 @@ gdonorm = Trim( cles_norm_type ) /= 'NONE'
 !           ---------------------------
 !
 !
-YGROUP= 'BU_RT2'
+ygroup = 'BU_RT2'
+ygroupcomment = 'Total water variance budget'
 ILES=0
 ILES_STA=ILES
 !
@@ -357,13 +359,14 @@ ZLES_BUDGET(NLES_K,:,ILES) = ZLES_BUDGET(NLES_K-1,:,ILES)
 !* 2.16 writing
 !       -------
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'Rt variance budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'Rt variance budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'kg2 kg-2 s-1'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
@@ -371,7 +374,8 @@ call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles),
 !           -----------------------
 !
 !
-YGROUP= 'BU_WRT'
+ygroup = 'BU_WRT'
+ygroupcomment = 'Total water flux budget'
 ILES=0
 ILES_STA=ILES
 !
@@ -668,13 +672,14 @@ ZLES_BUDGET(:,:,ILES)=-XLES_RES_ddxa_Rt_SBG_UaW(:,:,1)       &
 !* 3.22 writing
 !       -------
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'Rt flux budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'Rt flux budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'm kg kg-1 s-2'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
@@ -682,6 +687,8 @@ call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles),
 !           ------------------------------------------------------------
 !
 !
+ygroup = 'BU_THLR'
+ygroupcomment = 'Liquid potential temperature - total water covariance budget'
 YGROUP= 'BU_THLR'
 ILES=0
 ILES_STA=ILES
@@ -918,13 +925,14 @@ ZLES_BUDGET(NLES_K,:,ILES) = ZLES_BUDGET(NLES_K-1,:,ILES)
 !* 2.16 writing
 !       -------
 !
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = 'Thl-Rt covariance budget'
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'Thl-Rt covariance budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'K kg kg-1 s-1'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
diff --git a/src/MNH/write_les_sv_budgetn.f90 b/src/MNH/write_les_sv_budgetn.f90
index 4bed642c4..fcc924690 100644
--- a/src/MNH/write_les_sv_budgetn.f90
+++ b/src/MNH/write_les_sv_budgetn.f90
@@ -93,8 +93,8 @@ INTEGER :: JSV! scalar loop counter
 INTEGER :: JP ! process loop counter
 !
 CHARACTER(len=9), DIMENSION(NMAX_ILES)      :: YSUBTITLE
-CHARACTER(len=8)                            :: YGROUP
-CHARACTER(len=20)                           :: YTITLE
+character(len=:), allocatable               :: ygroup
+character(len=:), allocatable               :: ygroupcomment
 !
 REAL, DIMENSION(:,:,:,:), ALLOCATABLE :: ZLES_BUDGET
 !
@@ -115,7 +115,8 @@ ZLES_BUDGET(:,:,:,:) = XUNDEF
 !           ----------------------------
 !
 !
-YGROUP='BU_SV2'
+ygroup = 'BU_SV2'
+ygroupcomment = 'Total scalar variance budget'
 !
 ILES=0
 ILES_STA=ILES
@@ -378,15 +379,13 @@ END DO
 !* 2.16 writing
 !       -------
 !
-YTITLE = "Sv variance budget  "
-
 tzfield%ngrid = 0 !Not on the Arakawa grid
 tzfield%ntype = TYPEREAL
 
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = ytitle
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'Sv variance budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'kg2 kg-2 s-1'
 
 tzfield%ndims = 4
@@ -399,7 +398,8 @@ tzfield%ndimlist(5:) = NMNHDIM_UNUSED
 gdoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
 gdonorm = trim(cles_norm_type) /= 'NONE'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles, :), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles, :), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
@@ -407,7 +407,8 @@ call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles,
 !           -----------------------
 !
 !
-YGROUP = 'BU_WSV'
+ygroup = 'BU_WSV'
+ygroupcomment = 'Total water flux budget'
 !
 !
 ILES=0
@@ -494,6 +495,7 @@ END IF
 !      -----------
 !
 ILES=ILES+1
+!PW: not in documentation. Always set to 0
 YSUBTITLE(ILES) = 'SBG_DISS'
 !
 DO JSV=1,NSV
@@ -760,15 +762,13 @@ END DO
 !* 3.22 writing
 !       -------
 !
-YTITLE = "Sv flux budget      "
-
 tzfield%ngrid = 0 !Not on the Arakawa grid
 tzfield%ntype = TYPEREAL
 
-tzfield%cmnhname  = ygroup
+tzfield%cmnhname  = ygroup !cmnhname will be overwritten by ysubtitle(:) in Les_diachro
 tzfield%cstdname  = ''
-tzfield%clongname = ygroup
-tzfield%ccomment  = ytitle
+tzfield%clongname = ygroup !clongname will be overwritten by ysubtitle(:) in Les_diachro
+tzfield%ccomment  = 'Sv flux budget' !ccomment will be completed with ysubtitle(:) in Les_diachro
 tzfield%cunits    = 'm kg kg-1 s-2'
 
 tzfield%ndims = 4
@@ -781,7 +781,8 @@ tzfield%ndimlist(5:) = NMNHDIM_UNUSED
 gdoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
 gdonorm = trim(cles_norm_type) /= 'NONE'
 
-call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles, :), hsuffixes = ysubtitle(:iles) )
+call Les_diachro( tpdiafile, tzfield, ygroup, ygroupcomment, gdoavg, gdonorm, &
+                  zles_budget(:, :, :iles, :), hsuffixes = ysubtitle(:iles) )
 
 !-------------------------------------------------------------------------------
 !
diff --git a/src/MNH/write_lesn.f90 b/src/MNH/write_lesn.f90
index e971d432c..3ddc83537 100644
--- a/src/MNH/write_lesn.f90
+++ b/src/MNH/write_lesn.f90
@@ -16,6 +16,9 @@ private
 public :: Write_les_n
 
 
+character(len=:), allocatable :: cgroup
+character(len=:), allocatable :: cgroupcomment
+
 logical :: ldoavg    ! Compute and store time average
 logical :: ldonorm   ! Compute and store normalized field
 
@@ -283,6 +286,9 @@ tfield%ndimlist(4:) = NMNHDIM_UNUSED
 ldoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
 ldonorm = .false.
 
+cgroup        = 'Miscellaneous'
+cgroupcomment = 'Miscellaneous terms (geometry, various unclassified averaged terms...)'
+
 call Les_diachro_write( tpdiafile, zavg_pts_ll,                'AVG_PTS',  'number of points used for averaging',   '1', ymasks )
 call Les_diachro_write( tpdiafile, zavg_pts_ll / zcart_pts_ll, 'AVG_PTSF', 'fraction of points used for averaging', '1', ymasks )
 call Les_diachro_write( tpdiafile, zund_pts_ll,                'UND_PTS',  'number of points below orography',      '1', ymasks )
@@ -294,6 +300,9 @@ DEALLOCATE(ZUND_PTS_ll)
 !*      2.1  mean quantities
 !            ---------------
 !
+cgroup = 'Mean'
+cgroupcomment = 'Mean vertical profiles of the model variables'
+
 tfield%ndims = 3
 tfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_LEVEL
 tfield%ndimlist(2)  = NMNHDIM_BUDGET_LES_TIME
@@ -374,6 +383,9 @@ call Les_diachro_write( tpdiafile, XLES_MEAN_WIND, 'MEANWIND',       'Profile of
 call Les_diachro_write( tpdiafile, XLES_RESOLVED_MASSFX, 'MEANMSFX', 'Total updraft mass flux',         'kg m-2 s-1', ymasks )
 
 if ( lles_pdf ) then
+  cgroup = 'PDF'
+  cgroupcomment = ''
+
   call Les_diachro_write( tpdiafile,   XLES_PDF_TH,  'PDF_TH',  'Pdf potential temperature Profiles', '1', ymasks )
   call Les_diachro_write( tpdiafile,   XLES_PDF_W,   'PDF_W',   'Pdf vertical velocity Profiles',     '1', ymasks )
   call Les_diachro_write( tpdiafile,   XLES_PDF_THV, 'PDF_THV', 'Pdf virtual pot. temp. Profiles',    '1', ymasks )
@@ -402,6 +414,9 @@ if ( lles_resolved ) then
   ldoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
   ldonorm = trim(cles_norm_type) /= 'NONE'
 
+  cgroup = 'Resolved'
+  cgroupcomment = 'Mean vertical profiles of the resolved fluxes, variances and covariances'
+
   tfield%ndims = 3
   tfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_LEVEL
   tfield%ndimlist(2)  = NMNHDIM_BUDGET_LES_TIME
@@ -606,6 +621,9 @@ if ( lles_subgrid ) then
   ldoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
   ldonorm = trim(cles_norm_type) /= 'NONE'
 
+  cgroup = 'Subgrid'
+  cgroupcomment = 'Mean vertical profiles of the subgrid fluxes, variances and covariances'
+
   tfield%ndims = 3
   tfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_LEVEL
   tfield%ndimlist(2)  = NMNHDIM_BUDGET_LES_TIME
@@ -724,6 +742,9 @@ ldonorm = trim(cles_norm_type) /= 'NONE'
 !            ------------------
 !
 if ( lles_updraft ) then
+  cgroup = 'Updraft'
+  cgroupcomment = 'Updraft vertical profiles of some resolved and subgrid fluxes, variances and covariances'
+
   call Les_diachro_write( tpdiafile, XLES_UPDRAFT,     'UP_FRAC', 'Updraft fraction',                                 '1' )
   call Les_diachro_write( tpdiafile, XLES_UPDRAFT_W,   'UP_W',    'Updraft W mean value',                             'm s-1' )
   call Les_diachro_write( tpdiafile, XLES_UPDRAFT_Th,  'UP_TH',   'Updraft potential temperature mean value',         'K' )
@@ -831,6 +852,9 @@ end if
 !            --------------------
 !
 if ( lles_downdraft ) then
+  cgroup = 'Downdraft'
+  cgroupcomment = 'Downdraft vertical profiles of some resolved and subgrid fluxes, variances and covariances'
+
   call Les_diachro_write( tpdiafile, XLES_DOWNDRAFT,     'DW_FRAC', 'Downdraft fraction',                                 '1' )
   call Les_diachro_write( tpdiafile, XLES_DOWNDRAFT_W,   'DW_W',    'Downdraft W mean value',                             'm s-1' )
   call Les_diachro_write( tpdiafile, XLES_DOWNDRAFT_Th,  'DW_TH',   'Downdraft potential temperature mean value',         'K' )
@@ -946,7 +970,10 @@ end if
 !*      3.   surface normalization parameters
 !            --------------------------------
 !
-!Prepare metadate (used in Les_diachro_write calls)
+cgroup = 'Miscellaneous'
+cgroupcomment = 'Miscellaneous terms (geometry, various unclassified averaged terms...)'
+
+!Prepare metadata (used in Les_diachro_write calls)
 tfield%ndims = 2
 tfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_LEVEL
 tfield%ndimlist(2)  = NMNHDIM_BUDGET_LES_TIME
@@ -965,6 +992,9 @@ call Les_diachro_write( tpdiafile, XLES_DTHRADLW, 'DTHRADLW', 'LW radiative temp
 call Les_diachro_write( tpdiafile, XLES_RADEFF,   'RADEFF',   'Mean effective radius',             'micron' )
 
 
+cgroup = 'Surface'
+cgroupcomment = 'Averaged surface fields'
+
 ! !Prepare metadate (used in Les_diachro_write calls)
 tfield%ndims = 1
 tfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_TIME
@@ -1162,7 +1192,7 @@ tfield%clongname = hmnhname
 tfield%ccomment  = hcomment
 tfield%cunits    = hunits
 
-call Les_diachro( tpdiafile, tfield, ldoavg, ldonorm, pdata )
+call Les_diachro( tpdiafile, tfield, cgroup, cgroupcomment, ldoavg, ldonorm, pdata )
 
 end subroutine Les_diachro_write_1D
 
@@ -1185,7 +1215,7 @@ tfield%clongname = hmnhname
 tfield%ccomment  = hcomment
 tfield%cunits    = hunits
 
-call Les_diachro( tpdiafile, tfield, ldoavg, ldonorm, pdata )
+call Les_diachro( tpdiafile, tfield, cgroup, cgroupcomment, ldoavg, ldonorm, pdata )
 
 end subroutine Les_diachro_write_2D
 
@@ -1209,7 +1239,7 @@ tfield%clongname = hmnhname
 tfield%ccomment  = hcomment
 tfield%cunits    = hunits
 
-call Les_diachro( tpdiafile, tfield, ldoavg, ldonorm, pdata, hmasks = hmasks )
+call Les_diachro( tpdiafile, tfield, cgroup, cgroupcomment, ldoavg, ldonorm, pdata, hmasks = hmasks )
 
 end subroutine Les_diachro_write_3D
 
@@ -1233,7 +1263,7 @@ tfield%clongname = hmnhname
 tfield%ccomment  = hcomment
 tfield%cunits    = hunits
 
-call Les_diachro( tpdiafile, tfield, ldoavg, ldonorm, pdata, hmasks = hmasks )
+call Les_diachro( tpdiafile, tfield, cgroup, cgroupcomment, ldoavg, ldonorm, pdata, hmasks = hmasks )
 
 end subroutine Les_diachro_write_4D
 
-- 
GitLab