From cd6feb9cae09479217c1d434c2ecfea58bfba189 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 8 Jul 2021 10:39:34 +0200
Subject: [PATCH] Philippe 08/07/2021: budgets: bugfix for LFI files if masks
 are used (data was overwritten)

---
 src/MNH/mode_les_diachro.f90 | 42 +++++++++++++++++++-------
 src/MNH/write_diachro.f90    | 20 ++++++++++---
 src/MNH/write_seriesn.f90    | 58 ++++++++++++++++++++++++++----------
 3 files changed, 90 insertions(+), 30 deletions(-)

diff --git a/src/MNH/mode_les_diachro.f90 b/src/MNH/mode_les_diachro.f90
index c4a3ea4cf..b3c518177 100644
--- a/src/MNH/mode_les_diachro.f90
+++ b/src/MNH/mode_les_diachro.f90
@@ -1022,6 +1022,7 @@ real,            dimension(:,:,:,:),     allocatable :: zfield  ! Normalized fie
 real,            dimension(:,:,:,:,:,:), allocatable :: zwork6  ! Contains physical field
 type(tbudiachrometadata)                             :: tzbudiachro
 type(date_time), dimension(:),           allocatable :: tzdates
+type(tfiledata)                                      :: tzfile
 
 !Reallocate each time necessary because can be reallocated to an other size in Les_time_avg
 Allocate( zfield(Size( pfield, 1 ), Size( pfield, 2 ), Size( pfield, 3 ), Size( pfield, 4 )) )
@@ -1148,20 +1149,41 @@ if ( iresp == 0 .and. any( zfield /= XUNDEF ) ) then
   tzbudiachro%nkh        = ikh
 
   if ( tzfields(1)%ndimlist(6) == NMNHDIM_BUDGET_LES_MASK ) then
-    tzfields(:)%ndimlist(6) = NMNHDIM_UNUSED
+    tzfile = tpdiafile
 
-    ! Loop on the different masks
-    ! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
-    do jp = 1, Size( hmasks )
-      tzfields(jp)%clongname = Trim( ytitle(jp) ) // ' (' // Trim( hmasks(jp) ) // ')'
-      tzfields(jp)%ndims     = tzfields(jp)%ndims - 1
+    if ( Trim( tpdiafile%cformat ) == 'LFI' .or. Trim( tpdiafile%cformat ) == 'LFICDF4' ) then
+      !For LFI files, it is necessary to write all the 'processes' (source terms) of the different masks in one pass
+      !to ensure that they are grouped together and not overwritten
+      tzfile%cformat = 'LFI'
 
-      tzbudiachro%clevels(NLVL_MASK) = hmasks(jp)
+      do jp = 1, Size( hmasks )
+        tzfields(jp)%cmnhname  = Trim( ytitle(jp) ) // ' (' // Trim( hmasks(jp) ) // ')'
+        tzfields(jp)%clongname = Trim( ytitle(jp) ) // ' (' // Trim( hmasks(jp) ) // ')'
+      end do
+
+      call Write_diachro( tzfile, tzbudiachro, tzfields, tzdates, zwork6 )
+    end if
+
+    if ( Trim( tpdiafile%cformat ) /= 'LFI' ) then
+      tzfile%cformat = 'NETCDF4'
+
+      tzfields(:)%ndimlist(6) = NMNHDIM_UNUSED
+
+      ! Loop on the different masks
+      ! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
+      do jp = 1, Size( hmasks )
+        !Keep the following line (about cmnhname, necessary especially if LFI files before (cmnhname was modified previously)
+        tzfields(jp)%cmnhname  = Trim( ytitle(jp) )
+        tzfields(jp)%clongname = Trim( ytitle(jp) ) // ' (' // Trim( hmasks(jp) ) // ')'
+        tzfields(jp)%ndims     = tzfields(jp)%ndims - 1
+
+        tzbudiachro%clevels(NLVL_MASK) = hmasks(jp)
 !PW:TODO? necessite le transfert d'info depuis les routines appelantes ou via des structures dans les modd
-      tzbudiachro%ccomments(NLVL_MASK) = ''
+        tzbudiachro%ccomments(NLVL_MASK) = ''
 
-      call Write_diachro( tpdiafile, tzbudiachro, [ tzfields(jp) ], tzdates, zwork6(:,:,:,:,:,jp:jp) )
-    end do
+        call Write_diachro( tzfile, tzbudiachro, [ tzfields(jp) ], tzdates, zwork6(:,:,:,:,:,jp:jp) )
+      end do
+    end if
   else
     !Set to the same value ('cart') than for the fields with no mask in Write_les_n
     !to put the fields in the same position of the netCDF file
diff --git a/src/MNH/write_diachro.f90 b/src/MNH/write_diachro.f90
index 0dab801a3..a1170be6f 100644
--- a/src/MNH/write_diachro.f90
+++ b/src/MNH/write_diachro.f90
@@ -143,8 +143,9 @@ subroutine Write_diachro_lfi( tpdiafile, tpbudiachro, tpfields, tpdates, pvar, t
 
 use modd_aircraft_balloon, only: flyer
 use modd_budget,         only: NLVL_CATEGORY, NLVL_GROUP, NLVL_SHAPE, nbumask, nbutshift, nbusubwrite, tbudiachrometadata
-use modd_field,          only: NMNHDIM_ONE, NMNHDIM_UNKNOWN, NMNHDIM_FLYER_TIME, NMNHDIM_NOTLISTED, NMNHDIM_UNUSED, &
-                               TYPECHAR, TYPEINT, TYPEREAL,                                                         &
+use modd_field,          only: NMNHDIM_ONE, NMNHDIM_UNKNOWN, NMNHDIM_BUDGET_LES_MASK, &
+                               NMNHDIM_FLYER_TIME, NMNHDIM_NOTLISTED, NMNHDIM_UNUSED, &
+                               TYPECHAR, TYPEINT, TYPEREAL,                           &
                                tfield_metadata_base, tfielddata
 use modd_io,             only: tfiledata
 use modd_les,            only: nles_current_iinf, nles_current_isup, nles_current_jinf, nles_current_jsup, &
@@ -182,6 +183,7 @@ character(len=LFIUNITLGT),    dimension(:), allocatable :: yunits    !Used to re
 character(len=LFICOMMENTLGT), dimension(:), allocatable :: ycomments !Used to respect LFI fileformat
 INTEGER   ::   ILENG, ILENTITRE, ILENUNITE, ILENCOMMENT
 integer   :: iil, iih, ijl, ijh, ikl, ikh
+integer   :: idx
 INTEGER   ::   II, IJ, IK, IT, IN, IP, J, JJ
 INTEGER   ::   INTRAJT, IKTRAJX, IKTRAJY, IKTRAJZ
 INTEGER   ::   ITTRAJX, ITTRAJY, ITTRAJZ
@@ -256,7 +258,17 @@ 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 )
+  if ( tpfields(1)%ndimlist(6) == NMNHDIM_BUDGET_LES_MASK ) then
+    !Remove the name of the mask (different for each 'process') to get the common name for the group
+    idx = Index( tpfields(1)%cmnhname, ' ' )
+    if ( idx > 0 ) then
+      ygroup = tpfields(1)%cmnhname(1:idx- 1)
+    else
+      ygroup = Trim( tpfields(1)%cmnhname )
+    end if
+  else
+    ygroup = Trim( tpfields(1)%cmnhname )
+  end if
 else
   ygroup = Trim( tpbudiachro%clevels(NLVL_GROUP) )
 end if
@@ -316,7 +328,7 @@ else if ( ycategory == 'Flyers' ) then
   end if
 else if ( ycategory == 'Profilers' .or. ycategory == 'Stations' ) then
   ytype = 'CART'
-else if ( ycategory == 'Time series'  ) then
+else if ( ycategory == 'Time_series'  ) then
   if ( tpbudiachro%licompress ) then
     ytype = 'CART'
   else
diff --git a/src/MNH/write_seriesn.f90 b/src/MNH/write_seriesn.f90
index a1e425f39..54ed6c7c8 100644
--- a/src/MNH/write_seriesn.f90
+++ b/src/MNH/write_seriesn.f90
@@ -119,6 +119,7 @@ REAL :: ZSIZEHB
 CHARACTER(LEN=100) :: YMSG
 type(tbudiachrometadata)                              :: tzbudiachro
 type(tfield_metadata_base), dimension(:), allocatable :: tzfields
+type(tfiledata)                                       :: tzfile
 !----------------------------------------------------------------------------
 !
 !*    1.     INITIALIZATION
@@ -303,15 +304,28 @@ tzbudiachro%njh        = njboxh
 tzbudiachro%nkl        = 1
 tzbudiachro%nkh        = ikmax
 
-! Loop on the different masks
-! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
-do jp = 1 , nstemp_serie1
-  tzbudiachro%clevels(NLVL_MASK) = Trim( csmask1(jp) )
-  tzbudiachro%ccomments(NLVL_MASK) = ''
+tzfile = tpdiafile
+if ( Trim( tpdiafile%cformat ) == 'LFI' .or. Trim( tpdiafile%cformat ) == 'LFICDF4' ) then
+  !For LFI files, it is necessary to write all the 'processes' (source terms) of the different masks in one pass
+  !to ensure that they are grouped together and not overwritten
+  tzfile%cformat = 'LFI'
 
-  call Write_diachro( tpdiafile, tzbudiachro, [ tzfields(jp) ], tpsdates(1:nsnbstept), &
-                      xsseries1(1:1,1:1,1:1,1:nsnbstept,1:1,jp:jp)                 )
-end do
+  call Write_diachro( tzfile, tzbudiachro, tzfields, tpsdates(1:nsnbstept), &
+                      xsseries1(1:1,1:1,1:1,1:nsnbstept,1:1,:)              )
+end if
+
+if ( Trim( tpdiafile%cformat ) /= 'LFI' ) then! Loop on the different masks
+  tzfile%cformat = 'NETCDF4'
+
+  ! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
+  do jp = 1 , nstemp_serie1
+    tzbudiachro%clevels(NLVL_MASK) = Trim( csmask1(jp) )
+    tzbudiachro%ccomments(NLVL_MASK) = ''
+
+    call Write_diachro( tzfile, tzbudiachro, [ tzfields(jp) ], tpsdates(1:nsnbstept), &
+                        xsseries1(1:1,1:1,1:1,1:nsnbstept,1:1,jp:jp)                 )
+  end do
+end if
 
 deallocate( tzfields )
 !
@@ -431,15 +445,27 @@ tzbudiachro%njh        = njboxh
 tzbudiachro%nkl        = 1
 tzbudiachro%nkh        = ikmax
 
-! Loop on the different masks
-! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
-do jp = 1 , nstemp_serie2
-  tzbudiachro%clevels(NLVL_MASK) = csmask2(jp)
-  tzbudiachro%ccomments(NLVL_MASK) = ''
+if ( Trim( tpdiafile%cformat ) == 'LFI' .or. Trim( tpdiafile%cformat ) == 'LFICDF4' ) then
+  !For LFI files, it is necessary to write all the 'processes' (source terms) of the different masks in one pass
+  !to ensure that they are grouped together and not overwritten
+  tzfile%cformat = 'LFI'
 
-  call Write_diachro( tpdiafile, tzbudiachro, [ tzfields(jp) ], tpsdates(1:nsnbstept), &
-                      xsseries2(1:1,1:1,1:ikmax,1:nsnbstept,1:1,jp:jp)                 )
-end do
+  call Write_diachro( tzfile, tzbudiachro, tzfields, tpsdates(1:nsnbstept), &
+                      xsseries2(1:1,1:1,1:ikmax,1:nsnbstept,1:1,:)          )
+end if
+
+if ( Trim( tpdiafile%cformat ) /= 'LFI' ) then! Loop on the different masks
+  tzfile%cformat = 'NETCDF4'
+
+  ! Do not provide all tzfields once because they can be stored in different HDF groups (based on masks)
+  do jp = 1 , nstemp_serie2
+    tzbudiachro%clevels(NLVL_MASK) = csmask2(jp)
+    tzbudiachro%ccomments(NLVL_MASK) = ''
+
+    call Write_diachro( tzfile, tzbudiachro, [ tzfields(jp) ], tpsdates(1:nsnbstept), &
+                        xsseries2(1:1,1:1,1:ikmax,1:nsnbstept,1:1,jp:jp)              )
+  end do
+end if
 
 deallocate( tzfields )
 !
-- 
GitLab