From d930f58b93d69cb2d729cd3fb57a88acd1942b06 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Tue, 26 Jul 2016 17:36:41 +0200
Subject: [PATCH] Philippe 26/07/2016: * added fields NBAKID, NOUTID, CFILENAME
 and CDADFILENAME to TOUTBAK datatype + use them * building of the generic
 output name was displaced before SET_GRID in ini_modeln

---
 src/LIB/SURCOUCHE/src/mode_io.f90 | 10 ++--
 src/MNH/ini_modeln.f90            | 80 +++++++++++++++++--------------
 src/MNH/modeln.f90                | 22 ++-------
 src/MNH/set_grid.f90              | 24 +++++++---
 4 files changed, 74 insertions(+), 62 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/mode_io.f90 b/src/LIB/SURCOUCHE/src/mode_io.f90
index afcb24fa3..7f9f4e071 100644
--- a/src/LIB/SURCOUCHE/src/mode_io.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io.f90
@@ -62,9 +62,13 @@ MODULE MODE_IO_ll
 
   !Structure describing the characteristics of an output or a backup
   TYPE TOUTBAK
-    INTEGER :: NSTEP        !Timestep number
-    REAL    :: XTIME        !Time from start of the segment (in seconds and rounded to a timestep)
-    INTEGER :: NOUTDAD = -1 !Index of the corresponding dad file (file with same time)
+    INTEGER           :: NBAKID = -1  !Backup number
+    INTEGER           :: NOUTID = -1  !Output number
+    INTEGER           :: NSTEP        !Timestep number
+    REAL              :: XTIME        !Time from start of the segment (in seconds and rounded to a timestep)
+    CHARACTER(LEN=28) :: CFILENAME    !Filename
+    INTEGER           :: NOUTDAD = -1 !Index of the corresponding dad file (file with same time)
+    CHARACTER(LEN=28) :: CDADFILENAME !Filename of dad
   END TYPE TOUTBAK
   PUBLIC TOUTBAK
 
diff --git a/src/MNH/ini_modeln.f90 b/src/MNH/ini_modeln.f90
index 141b5449a..12972c657 100644
--- a/src/MNH/ini_modeln.f90
+++ b/src/MNH/ini_modeln.f90
@@ -413,6 +413,7 @@ USE MODD_LATZ_EDFLX
 USE MODD_ADVFRC_n
 USE MODD_RELFRC_n
 USE MODD_2D_FRC
+USE MODD_IO_SURF_MNH, ONLY : IO_SURF_MNH_MODEL
 !
 USE MODD_CH_PRODLOSSTOT_n
 USE MODI_CH_INIT_PRODLOSSTOT_n
@@ -513,7 +514,7 @@ REAL, DIMENSION(:,:,:),   POINTER ::  DPTR_XZZ
 REAL, DIMENSION(:,:,:), POINTER ::   DPTR_XLSUM,DPTR_XLSVM,DPTR_XLSWM,DPTR_XLSTHM,DPTR_XLSRVM
 REAL, DIMENSION(:,:,:), POINTER ::   DPTR_XLSUS,DPTR_XLSVS,DPTR_XLSWS,DPTR_XLSTHS,DPTR_XLSRVS
 !
-INTEGER                         ::  IIB,IJB,IIE,IJE,IDIMX,IDIMY
+INTEGER                         ::  IIB,IJB,IIE,IJE,IDIMX,IDIMY,IMI
 !
 !-------------------------------------------------------------------------------
 !
@@ -668,7 +669,7 @@ IF (NVERB >= 5) THEN
   WRITE (UNIT=ILUOUT,FMT='("THERE ARE ",I2," SOLID VARIABLES")') NRRI
 END IF
 !
-!*       2.3  Update NSV and floating indices for the current model
+!*       2.4  Update NSV and floating indices for the current model
 !
 ! 
 CALL UPDATE_NSV(KMI) 
@@ -1517,7 +1518,30 @@ CALL INI_BIKHARDT_n (NDXRATIO_ALL(KMI),NDYRATIO_ALL(KMI),KMI)
 !
 !-------------------------------------------------------------------------------
 !
-!*       6.    INITIALIZE GRIDS AND METRIC COEFFICIENTS
+!*       6.     BUILT THE GENERIC OUTPUT NAME
+!               ----------------------------
+!
+IF (KMI == 1) THEN
+  DO IMI = 1 , NMODEL
+    WRITE(IO_SURF_MNH_MODEL(IMI)%COUTFILE,'(A,".",I1,".",A)') CEXP,IMI,TRIM(ADJUSTL(CSEG))
+    WRITE(LUNIT_MODEL(IMI)%CFMDIAC, '(A,".",I1,".",A)') CEXP,IMI,TRIM(ADJUSTL(CSEG))//'.000'
+  END DO
+  !
+  IF (CPROGRAM=='MESONH') THEN
+    IF ( NDAD(KMI) == 1)  CDAD_NAME(KMI) = CEXP//'.1.'//CSEG
+    IF ( NDAD(KMI) == 2)  CDAD_NAME(KMI) = CEXP//'.2.'//CSEG
+    IF ( NDAD(KMI) == 3)  CDAD_NAME(KMI) = CEXP//'.3.'//CSEG
+    IF ( NDAD(KMI) == 4)  CDAD_NAME(KMI) = CEXP//'.4.'//CSEG
+    IF ( NDAD(KMI) == 5)  CDAD_NAME(KMI) = CEXP//'.5.'//CSEG
+    IF ( NDAD(KMI) == 6)  CDAD_NAME(KMI) = CEXP//'.6.'//CSEG
+    IF ( NDAD(KMI) == 7)  CDAD_NAME(KMI) = CEXP//'.7.'//CSEG
+    IF ( NDAD(KMI) == 8)  CDAD_NAME(KMI) = CEXP//'.8.'//CSEG
+  END IF
+END IF
+!
+!-------------------------------------------------------------------------------
+!
+!*       7.    INITIALIZE GRIDS AND METRIC COEFFICIENTS
 !              ----------------------------------------
 !
 CALL SET_GRID(KMI,HINIFILE,HLUOUT,IIU,IJU,IKU,NIMAX_ll,NJMAX_ll,         &
@@ -1553,7 +1577,7 @@ NDT_2_WAY(KMI)=4
 !
 !-------------------------------------------------------------------------------
 !
-!*      7.    INITIALIZE DATA FOR JVALUES AND AEROSOLS 
+!*      8.    INITIALIZE DATA FOR JVALUES AND AEROSOLS 
 !
 IF ( LUSECHEM .OR. LCHEMDIAG ) THEN
   IF ((KMI==1).AND.(CPROGRAM == "MESONH".OR.CPROGRAM == "DIAG  "))  &
@@ -1575,7 +1599,7 @@ IF (CCLOUD=='LIMA') CALL INIT_AEROSOL_PROPERTIES
 !
 !-------------------------------------------------------------------------------
 !
-!*       8.    INITIALIZE THE PROGNOSTIC FIELDS
+!*       9.    INITIALIZE THE PROGNOSTIC FIELDS
 !              --------------------------------
 !
 CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-before read_field::XUT",PRECISION)
@@ -1606,7 +1630,7 @@ CALL READ_FIELD(HINIFILE,HLUOUT,IMASDEV, IIU,IJU,IKU,XTSTEP,                  &
 !-------------------------------------------------------------------------------
 !
 !
-!*        9.   INITIALIZE REFERENCE STATE
+!*        10.  INITIALIZE REFERENCE STATE
 !              ---------------------------
 !
 !
@@ -1804,26 +1828,10 @@ END IF
 IF (LLG .AND. LINIT_LG .AND. CPROGRAM=='MESONH') &
   CALL INI_LG(XXHAT,XYHAT,XZZ,XSVT,XLBXSVM,XLBYSVM)
 
-!
-!*       16.    BUILT THE GENERIC OUTPUT NAME
-!               ----------------------------
-!
-WRITE(COUTFILE,'(A,".",I1,".",A)') CEXP,KMI,TRIM(ADJUSTL(CSEG))
-WRITE(CFMDIAC, '(A,".",I1,".",A)') CEXP,KMI,TRIM(ADJUSTL(CSEG))//'.000'
-IF (CPROGRAM=='MESONH') THEN
-  IF ( NDAD(KMI) == 1)  CDAD_NAME(KMI) = CEXP//'.1.'//CSEG
-  IF ( NDAD(KMI) == 2)  CDAD_NAME(KMI) = CEXP//'.2.'//CSEG
-  IF ( NDAD(KMI) == 3)  CDAD_NAME(KMI) = CEXP//'.3.'//CSEG
-  IF ( NDAD(KMI) == 4)  CDAD_NAME(KMI) = CEXP//'.4.'//CSEG
-  IF ( NDAD(KMI) == 5)  CDAD_NAME(KMI) = CEXP//'.5.'//CSEG
-  IF ( NDAD(KMI) == 6)  CDAD_NAME(KMI) = CEXP//'.6.'//CSEG
-  IF ( NDAD(KMI) == 7)  CDAD_NAME(KMI) = CEXP//'.7.'//CSEG
-  IF ( NDAD(KMI) == 8)  CDAD_NAME(KMI) = CEXP//'.8.'//CSEG
-END IF
 !
 !-------------------------------------------------------------------------------
 !
-!*       17.    INITIALIZE THE PARAMETERS FOR THE DYNAMICS
+!*       16.    INITIALIZE THE PARAMETERS FOR THE DYNAMICS
 !               ------------------------------------------
 !
 CALL INI_DYNAMICS(HLUOUT,XLON,XLAT,XRHODJ,XTHVREF,XMAP,XZZ,XDXHAT,XDYHAT,     &
@@ -1850,10 +1858,10 @@ CALL INI_DYNAMICS(HLUOUT,XLON,XLAT,XRHODJ,XTHVREF,XMAP,XZZ,XDXHAT,XDYHAT,     &
 !
 !-------------------------------------------------------------------------------
 !
-!*      18.    SURFACE FIELDS
+!*      17.    SURFACE FIELDS
 !              --------------
 !
-!*      18.1   Radiative setup
+!*      17.1   Radiative setup
 !              ---------------
 !
 IF (CRAD   /= 'NONE') THEN
@@ -1900,13 +1908,13 @@ END IF
 CALL INI_SW_SETUP (CRAD,NSWB_MNH,XSW_BANDS)
 !
 !
-!       18.1.1 Special initialisation for CO2 content
+!       17.1.1 Special initialisation for CO2 content
 !              CO2 (molar mass=44) horizontally and vertically homogeneous at 360 ppm
 !
 XCCO2 = 360.0E-06 * 44.0E-03 / XMD
 !
 !
-!*      18.2   Externalized surface fields
+!*      17.2   Externalized surface fields
 !              ---------------------------
 !
 ALLOCATE(ZCO2(IIU,IJU))
@@ -1996,14 +2004,14 @@ IF (CRAD   == 'ECMW' .AND. CGETRAD=='READ') THEN
 END IF
 !
 !
-!*      18.3   Mesonh fields
+!*      17.3   Mesonh fields
 !              -------------
 !
 IF (CPROGRAM/='REAL  ') CALL MNHREAD_ZS_DUMMY_n(CINIFILEPGD)
 !
 !-------------------------------------------------------------------------------
 !
-!*       19.    INITIALIZE THE PARAMETERS FOR THE PHYSICS
+!*       18.    INITIALIZE THE PARAMETERS FOR THE PHYSICS
 !               -----------------------------------------
 !
 IF (CRAD   == 'ECMW') THEN
@@ -2090,7 +2098,7 @@ END IF
 !
 !-------------------------------------------------------------------------------
 !
-!*      22.    UPDATE HALO
+!*      21.    UPDATE HALO
 !              -----------
 !
 !
@@ -2102,7 +2110,7 @@ CALL CLEANLIST_ll(TZINITHALO2D_ll)
 !
 !-------------------------------------------------------------------------------
 !
-!*      23.    DEALLOCATION
+!*      22.    DEALLOCATION
 !              -------------
 !
 DEALLOCATE(ZJ)
@@ -2115,7 +2123,7 @@ DEALLOCATE(XSPOWATM)
 !
 !-------------------------------------------------------------------------------
 !
-!*      24.     BALLOON and AIRCRAFT initializations
+!*      23.     BALLOON and AIRCRAFT initializations
 !              ------------------------------------
 !
 CALL INI_AIRCRAFT_BALLOON(HINIFILE,CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
@@ -2124,7 +2132,7 @@ CALL INI_AIRCRAFT_BALLOON(HINIFILE,CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
 !
 !-------------------------------------------------------------------------------
 !
-!*      25.     STATION initializations
+!*      24.     STATION initializations
 !              -----------------------
 !
 CALL INI_SURFSTATION_n(CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
@@ -2133,7 +2141,7 @@ CALL INI_SURFSTATION_n(CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
 !
 !-------------------------------------------------------------------------------
 !
-!*      26.     PROFILER initializations
+!*      25.     PROFILER initializations
 !              ------------------------
 !
 CALL INI_POSPROFILER_n(CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
@@ -2142,7 +2150,7 @@ CALL INI_POSPROFILER_n(CLUOUT,XTSTEP, TDTSEG, XSEGLEN, NRR, NSV,  &
 !
 !-------------------------------------------------------------------------------
 !
-!*      28.     Prognostic aerosols          
+!*      26.     Prognostic aerosols          
 !              ------------------------
 !
 CALL INI_AEROSET1
@@ -2155,7 +2163,7 @@ CALL INI_AEROSET6
 ! 
 !-------------------------------------------------------------------------------
 !
-!*      29.    FOREFIRE initializations
+!*      27.    FOREFIRE initializations
 !              ------------------------
 !
 
diff --git a/src/MNH/modeln.f90 b/src/MNH/modeln.f90
index 885204b0f..5d297c5ea 100644
--- a/src/MNH/modeln.f90
+++ b/src/MNH/modeln.f90
@@ -419,8 +419,6 @@ INTEGER :: IIB,IIE,IJB,IJE,IKB,IKE ! index values for the physical subdomain
 INTEGER :: JSV,JRR     ! Loop index for scalar and moist variables
 CHARACTER (LEN=28) :: YFMFILE   ! name of the OUTPUT FM-file
 CHARACTER (LEN=28) :: YDADFILE  ! name of the corresponding DAD model OUTPUT FM-file
-CHARACTER (LEN=4)  :: YNUMBER   ! character string for the OUTPUT FM-file number
-CHARACTER (LEN=4)  :: YDADNUMBER! character string for the DAD model OUTPUT FM-file number
 CHARACTER (LEN=32) :: YDESFM    ! name of the desfm part of this FM-file
 INTEGER  :: INBVAR              ! number of HALO2_lls to allocate
 INTEGER  :: IRESP               ! return code in FM routines
@@ -909,23 +907,13 @@ IF (IOUT < NOUT_NUMB ) THEN
     IOUT=IOUT+1
     GCLOSE_OUT=.TRUE.
     INPRAR = 22 +2*(4+NRR+NSV)
-    WRITE (YNUMBER,FMT="('.',I3.3)") IOUT
-    YFMFILE=ADJUSTL(ADJUSTR(COUTFILE)//YNUMBER)
-!
-!        search for the corresponding Output of the DAD model
-!
-    IF (TOUTBAKN(IOUT)%NOUTDAD < 0) THEN
-      WRITE (YDADFILE,FMT="('NO_DAD_FILE')")
-    ELSE IF (TOUTBAKN(IOUT)%NOUTDAD == 0) THEN
-      YDADFILE=YFMFILE
-    ELSE
-      WRITE (YDADNUMBER,FMT="('.',I3.3)") TOUTBAKN(IOUT)%NOUTDAD
-      YDADFILE=ADJUSTL(ADJUSTR(CDAD_NAME(IMI))//YDADNUMBER)
-    END IF
-!
+    !
+    YFMFILE  = TOUTBAKN(IOUT)%CFILENAME
+    YDADFILE = TOUTBAKN(IOUT)%CDADFILENAME
+    !
     CALL FMOPEN_ll(YFMFILE,'WRITE',CLUOUT,INPRAR,ITYPE,NVERB,ININAR,IRESP)
     YDESFM=ADJUSTL(ADJUSTR(YFMFILE)//'.des')
-!  
+    !    
     CALL WRITE_DESFM_n(IMI,YDESFM,CLUOUT)
     CALL WRITE_LFIFM_n(YFMFILE,YDADFILE)
     COUTFMFILE = YFMFILE
diff --git a/src/MNH/set_grid.f90 b/src/MNH/set_grid.f90
index 158555ea3..8921a11ef 100644
--- a/src/MNH/set_grid.f90
+++ b/src/MNH/set_grid.f90
@@ -251,9 +251,10 @@ USE MODE_ll
 USE MODI_GATHER_ll  !!!! a mettre dans mode_ll
 !
 USE MODE_FMREAD
-USE MODD_OUT_n, ONLY : OUT_MODEL
-USE MODD_VAR_ll, ONLY : IP,NPROC
-USE MODD_DYN_n, ONLY : DYN_MODEL
+USE MODD_OUT_n,       ONLY : OUT_MODEL
+USE MODD_VAR_ll,      ONLY : IP,NPROC
+USE MODD_DYN_n,       ONLY : DYN_MODEL
+USE MODD_IO_SURF_MNH, ONLY : IO_SURF_MNH_MODEL
 !
 IMPLICIT NONE
 !
@@ -346,6 +347,8 @@ INTEGER                :: IIUP,IJUP ,ISUP=1         ! size  of working
 INTEGER                :: IMASDEV                   ! masdev of the file
 INTEGER                :: IMI                       ! model number for loop
 INTEGER                :: IOUT_NUMB                 ! number of outputs
+CHARACTER (LEN=4)      :: YNUMBER   ! character string for the OUTPUT FM-file number
+CHARACTER (LEN=4)      :: YDADNUMBER! character string for the DAD model OUTPUT FM-file number
 !-------------------------------------------------------------------------------
 !
 YRECFM='MASDEV'
@@ -517,6 +520,7 @@ KSTOP = NINT(PSEGLEN/PTSTEP)
 !
 ! The output/backups times have been read only by model 1
 IF (KMI == 1) THEN
+!
 DO IMI = 1, NMODEL
   !
   !*       2.3.1  Synchronization between nested models through XBAK_TIME arrays (MODD_FMOUT)
@@ -543,7 +547,7 @@ DO IMI = 1, NMODEL
     END IF
   END DO
   !
-  !*       2.3.2 Find duplicated entries
+  !*       2.3.2 Find and remove duplicated entries
   !
   DO JOUT = 1,JPOUTMAX
     DO JKLOOP = JOUT+1,JPOUTMAX
@@ -572,7 +576,7 @@ DO IMI = 1, NMODEL
     END IF
   END DO
   !
-  !*       2.3.4 counting the output number of model IMI
+  !*       2.3.4 Counting the number of backups of model IMI
   !
   IOUT_NUMB = 0
   DO JOUT = 1,JPOUTMAX
@@ -588,12 +592,16 @@ DO IMI = 1, NMODEL
   DO JOUT = 1,JPOUTMAX
     IF (XBAK_TIME(IMI,JOUT) >= 0.) THEN
         IPOS = IPOS + 1
+        OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NBAKID = IPOS
+        OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NOUTID = -1
         OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NSTEP = NINT(XBAK_TIME(IMI,JOUT)/DYN_MODEL(IMI)%XTSTEP) + 1
         OUT_MODEL(IMI)%TOUTBAKN(IPOS)%XTIME = XBAK_TIME(IMI,JOUT)
+        WRITE (YNUMBER,FMT="('.',I3.3)") IPOS
+        OUT_MODEL(IMI)%TOUTBAKN(IPOS)%CFILENAME=ADJUSTL(ADJUSTR(IO_SURF_MNH_MODEL(IMI)%COUTFILE)//YNUMBER)
     END IF
   END DO
   !
-  !*       2.3.5 finding dad output number
+  !*       2.3.5 Find dad output number
   !
   !Security check (if it happens, this part of the code should be exported outside of the IMI loop)
   IF (NDAD(IMI)>IMI) THEN
@@ -602,6 +610,7 @@ DO IMI = 1, NMODEL
   END IF
   IF (NDAD(IMI) == IMI .OR.  IMI == 1) THEN
     OUT_MODEL(IMI)%TOUTBAKN(:)%NOUTDAD = 0
+    OUT_MODEL(IMI)%TOUTBAKN(IPOS)%CDADFILENAME = OUT_MODEL(IMI)%TOUTBAKN(IPOS)%CFILENAME
   ELSE
     DO IPOS = 1,OUT_MODEL(IMI)%NOUT_NUMB
       IDX = 0
@@ -614,8 +623,11 @@ DO IMI = 1, NMODEL
       END DO
       IF (IDX>0) THEN
         OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NOUTDAD = IDX
+        WRITE (YDADNUMBER,FMT="('.',I3.3)") OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NOUTDAD
+        OUT_MODEL(IMI)%TOUTBAKN(IPOS)%CDADFILENAME = ADJUSTL(ADJUSTR(CDAD_NAME(IMI))//YDADNUMBER)
       ELSE
         OUT_MODEL(IMI)%TOUTBAKN(IPOS)%NOUTDAD = -1
+        WRITE ( OUT_MODEL(IMI)%TOUTBAKN(IPOS)%CDADFILENAME , FMT="('NO_DAD_FILE')" )
       END IF
     END DO
   END IF
-- 
GitLab