From f9a7d2c61d584ac87a4f9f9a45e96a3622f82336 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 15 Oct 2020 10:05:44 +0200
Subject: [PATCH] Philippe 15/10/2020: IO: Write_les_rt_budget_n: restructure
 Les_diachro calls to use tfield_metadata_base type

---
 src/MNH/mode_les_diachro.f90     |  10 +--
 src/MNH/write_les_rt_budgetn.f90 | 119 ++++++++++++++++++-------------
 src/MNH/write_lesn.f90           |   6 +-
 3 files changed, 80 insertions(+), 55 deletions(-)

diff --git a/src/MNH/mode_les_diachro.f90 b/src/MNH/mode_les_diachro.f90
index aa55d6390..815f3f073 100644
--- a/src/MNH/mode_les_diachro.f90
+++ b/src/MNH/mode_les_diachro.f90
@@ -933,7 +933,7 @@ subroutine Les_diachro_3D( tpdiafile, tpfield, odoavg, odonorm, pfield, hsuffixe
 !##################################################################################
 
 use modd_field, only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_MASK, NMNHDIM_BUDGET_LES_SV, &
-                      NMNHDIM_BUDGET_LES_TIME,  NMNHDIM_UNUSED,                                 &
+                      NMNHDIM_BUDGET_LES_TIME,  NMNHDIM_BUDGET_TERM,     NMNHDIM_UNUSED,        &
                       tfield_metadata_base
 use modd_io,    only: tfiledata
 
@@ -959,9 +959,9 @@ if ( Any( tzfield%ndimlist(4:) /= NMNHDIM_UNUSED ) ) then
   tzfield%ndimlist(4:) = NMNHDIM_UNUSED
 end if
 
-if (       tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL &
-     .and. tzfield%ndimlist(2) == NMNHDIM_BUDGET_LES_TIME  &
-     .and. tzfield%ndimlist(3) == NMNHDIM_BUDGET_LES_MASK  ) then
+if (         tzfield%ndimlist(1) == NMNHDIM_BUDGET_LES_LEVEL                                                 &
+     .and.   tzfield%ndimlist(2) == NMNHDIM_BUDGET_LES_TIME                                                  &
+     .and. ( tzfield%ndimlist(3) == NMNHDIM_BUDGET_LES_MASK .or.tzfield%ndimlist(3) == NMNHDIM_BUDGET_TERM ) ) then
   if ( .not. Present( hsuffixes ) ) &
     call Print_msg( NVERB_ERROR, 'IO', 'Les_diachro_3D', &
                     'optional dummy argument hsuffixes is needed for tpfield (' // Trim( tzfield%cmnhname ) // ')' )
@@ -1264,6 +1264,7 @@ call Les_diachro_gen( tpdiafile, hgroup, [ hcomment ], hunit,
 !-------------------------------------------------------------------------------
 END SUBROUTINE LES_DIACHRO_SV
 !-------------------------------------------------------------------------------
+
 !#####################################################################
 SUBROUTINE LES_DIACHRO_MASKS(TPDIAFILE,HGROUP,HTITLE,HCOMMENT,HUNIT,PFIELD,HAVG)
 !#####################################################################
@@ -1287,6 +1288,7 @@ call Les_diachro_gen( tpdiafile, hgroup, hcomment, hunit,
 !-------------------------------------------------------------------------------
 END SUBROUTINE LES_DIACHRO_MASKS
 !-------------------------------------------------------------------------------
+
 !########################################################################
 SUBROUTINE LES_DIACHRO_SV_MASKS(TPDIAFILE,HGROUP,HTITLE,HCOMMENT,HUNIT,PFIELD,HAVG)
 !########################################################################
diff --git a/src/MNH/write_les_rt_budgetn.f90 b/src/MNH/write_les_rt_budgetn.f90
index 92f37d1db..0d1dd441c 100644
--- a/src/MNH/write_les_rt_budgetn.f90
+++ b/src/MNH/write_les_rt_budgetn.f90
@@ -1,33 +1,26 @@
-!MNH_LIC Copyright 2002-2019 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 2002-2020 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
 !-----------------------------------------------------------------
-!######################
-MODULE MODI_WRITE_LES_RT_BUDGET_n
-!######################
-!
-INTERFACE
-!
-      SUBROUTINE  WRITE_LES_RT_BUDGET_n(TPDIAFILE,HLES_AVG)
-!
-USE MODD_IO, ONLY: TFILEDATA
-!
-TYPE(TFILEDATA),  INTENT(IN) :: TPDIAFILE ! file to write
-CHARACTER(LEN=1), INTENT(IN) :: HLES_AVG  ! flag to perform the averages
-!                                         ! or normalizations
-END SUBROUTINE WRITE_LES_RT_BUDGET_n
-!
-END INTERFACE
-!
-END MODULE MODI_WRITE_LES_RT_BUDGET_n
-!
-!     ######################
-      SUBROUTINE  WRITE_LES_RT_BUDGET_n(TPDIAFILE,HLES_AVG)
-!     ######################
+!################################
+module mode_write_les_rt_budget_n
+!################################
+
+implicit none
+
+private
+
+public :: Write_les_rt_budget_n
+
+contains
+
+!#############################################
+subroutine  Write_les_rt_budget_n( tpdiafile )
+!#############################################
 !
 !
-!!****  *WRITE_LES_n* writes the LES final diagnostics for model _n 
+!!****  *Write_les_rt_budget_n* writes the LES final diagnostics for model _n
 !!                         
 !!
 !!    PURPOSE
@@ -49,14 +42,17 @@ END MODULE MODI_WRITE_LES_RT_BUDGET_n
 !!    MODIFICATIONS
 !!    -------------
 !!      Original         06/11/02
-!!  Philippe Wautelet: 05/2016-04/2018: new data structures and calls for I/O
-!!
-!! --------------------------------------------------------------------------
+!  P. Wautelet: 05/2016-04/2018: new data structures and calls for I/O
+!  P. Wautelet 15/10/2020: restructure Les_diachro calls to use tfield_metadata_base type
+! --------------------------------------------------------------------------
 !
 !*      0. DECLARATIONS
 !          ------------
 !
 USE MODD_CST
+use modd_field,       only: NMNHDIM_BUDGET_LES_LEVEL, NMNHDIM_BUDGET_LES_TIME, &
+                            NMNHDIM_BUDGET_TERM, NMNHDIM_UNUSED,               &
+                            tfield_metadata_base, TYPEREAL
 USE MODD_IO, ONLY: TFILEDATA
 USE MODD_LES
 USE MODD_LES_n
@@ -72,12 +68,12 @@ IMPLICIT NONE
 !*      0.1  declarations of arguments
 !
 TYPE(TFILEDATA),  INTENT(IN) :: TPDIAFILE ! file to write
-CHARACTER(LEN=1), INTENT(IN) :: HLES_AVG  ! flag to perform the averages
-!                                         ! or normalizations
 !
 !
 !*      0.2  declaration of local variables
 !
+integer, parameter :: NMAX_ILES = 40
+
 INTEGER :: ILES
 INTEGER :: ILES_STA
 INTEGER :: JLES
@@ -86,20 +82,34 @@ INTEGER :: ILES_P1, ILES_P2
 INTEGER :: JK ! vertical loop counter
 INTEGER :: JT ! temporal loop counter
 !
-CHARACTER(len=9), DIMENSION(:), ALLOCATABLE :: YSUBTITLE
-CHARACTER(len=8)                            :: YGROUP
+CHARACTER(len=9), DIMENSION(NMAX_ILES) :: YSUBTITLE
+CHARACTER(len=8)                       :: YGROUP
 !
 REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZLES_BUDGET
 !
+logical                    :: gdoavg  ! Compute and store time average
+logical                    :: gdonorm ! Compute and store normalized field
+type(tfield_metadata_base) :: tzfield
 !-------------------------------------------------------------------------------
 !
 !*          Initializations
 !            ---------------
 !
-ALLOCATE(ZLES_BUDGET(NLES_K,NLES_TIMES,40))
-ALLOCATE(YSUBTITLE(40))
+ALLOCATE(ZLES_BUDGET(NLES_K,NLES_TIMES,NMAX_ILES))
 !
 ZLES_BUDGET(:,:,:) = XUNDEF
+
+tzfield%ngrid = 0 !Not on the Arakawa grid
+tzfield%ntype = TYPEREAL
+tzfield%ndims = 3
+tzfield%ndimlist(1)  = NMNHDIM_BUDGET_LES_LEVEL
+tzfield%ndimlist(2)  = NMNHDIM_BUDGET_LES_TIME
+tzfield%ndimlist(3)  = NMNHDIM_BUDGET_TERM
+tzfield%ndimlist(4:) = NMNHDIM_UNUSED
+
+gdoavg  = xles_temp_mean_start /= XUNDEF .and. xles_temp_mean_end /= XUNDEF
+gdonorm = Trim( cles_norm_type ) /= 'NONE'
+
 !-------------------------------------------------------------------------------
 !
 !*      2.  total water variance budget
@@ -338,11 +348,14 @@ ZLES_BUDGET(NLES_K,:,ILES) = ZLES_BUDGET(NLES_K-1,:,ILES)
 !* 2.16 writing
 !       -------
 !
-!
-CALL LES_DIACHRO_MASKS(TPDIAFILE,YGROUP,YSUBTITLE(:ILES),"Rt variance budget"//YSUBTITLE(:ILES),"kg2 kg-2 s-1", &
-                       ZLES_BUDGET(:,:,:ILES),HLES_AVG)
-!
-!
+tzfield%cmnhname  = ygroup
+tzfield%cstdname  = ''
+tzfield%clongname = ygroup
+tzfield%ccomment  = 'Rt variance budget'
+tzfield%cunits    = 'kg2 kg-2 s-1'
+
+call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), ysubtitle(:iles) )
+
 !-------------------------------------------------------------------------------
 !
 !*      3.  total water flux budget
@@ -645,11 +658,14 @@ ZLES_BUDGET(:,:,ILES)=-XLES_RES_ddxa_Rt_SBG_UaW(:,:,1)       &
 !* 3.22 writing
 !       -------
 !
-!
-CALL LES_DIACHRO_MASKS(TPDIAFILE,YGROUP,YSUBTITLE(:ILES),"Rt flux budget"//YSUBTITLE(:ILES),"m kg kg-1 s-2", &
-                       ZLES_BUDGET(:,:,:ILES),HLES_AVG)
-!
-!
+tzfield%cmnhname  = ygroup
+tzfield%cstdname  = ''
+tzfield%clongname = ygroup
+tzfield%ccomment  = 'Rt flux budget'
+tzfield%cunits    = 'm kg kg-1 s-2'
+
+call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), ysubtitle(:iles) )
+
 !-------------------------------------------------------------------------------
 !
 !*      4.  liquid potential temperature - total water covariance budget
@@ -892,15 +908,20 @@ ZLES_BUDGET(NLES_K,:,ILES) = ZLES_BUDGET(NLES_K-1,:,ILES)
 !* 2.16 writing
 !       -------
 !
-!
-CALL LES_DIACHRO_MASKS(TPDIAFILE,YGROUP,YSUBTITLE(:ILES),"Thl-Rt covariance budget"//YSUBTITLE(:ILES), &
-                                              "K kg kg-1 s-1",ZLES_BUDGET(:,:,:ILES),HLES_AVG)
-!
+tzfield%cmnhname  = ygroup
+tzfield%cstdname  = ''
+tzfield%clongname = ygroup
+tzfield%ccomment  = 'Thl-Rt covariance budget'
+tzfield%cunits    = 'K kg kg-1 s-1'
+
+call Les_diachro( tpdiafile, tzfield, gdoavg, gdonorm, zles_budget(:, :, :iles), ysubtitle(:iles) )
+
 !-------------------------------------------------------------------------------
 !
 DEALLOCATE(ZLES_BUDGET)
-DEALLOCATE(YSUBTITLE)
 !
 !-------------------------------------------------------------------------------
 !
-END SUBROUTINE WRITE_LES_RT_BUDGET_n 
+end subroutine Write_les_rt_budget_n
+
+end module mode_write_les_rt_budget_n
diff --git a/src/MNH/write_lesn.f90 b/src/MNH/write_lesn.f90
index 146c34483..846710566 100644
--- a/src/MNH/write_lesn.f90
+++ b/src/MNH/write_lesn.f90
@@ -83,10 +83,10 @@ USE MODE_ll
 USE MODE_LES_DIACHRO
 use mode_les_spec_n,            only: Les_spec_n
 USE MODE_MODELN_HANDLER
+use mode_write_les_rt_budget_n, only: Write_les_rt_budget_n
 use mode_write_les_sv_budget_n, only: Write_les_sv_budget_n
 !
 USE MODI_WRITE_LES_BUDGET_n
-USE MODI_WRITE_LES_RT_BUDGET_n
 !
 IMPLICIT NONE
 !
@@ -1408,9 +1408,11 @@ END IF
 !            -----------
 !
 CALL WRITE_LES_BUDGET_n(TPDIAFILE,HLES_AVG)
-IF (LUSERV) CALL WRITE_LES_RT_BUDGET_n(TPDIAFILE,HLES_AVG)
+
 end do AVG
 
+if ( luserv )  call Write_les_rt_budget_n( tpdiafile )
+
 if ( nsv > 0 ) call Write_les_sv_budget_n( tpdiafile )
 !
 !-------------------------------------------------------------------------------
-- 
GitLab