From 84237af7e0d08b25739845e71b40a5d9ecbd668f Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Tue, 17 Oct 2017 15:57:02 +0200
Subject: [PATCH] Philippe 17/10/2017: bug corrections

---
 src/MNH/default_desfmn.f90  |  2 +-
 src/MNH/ini_segn.f90        | 30 ++++++------------------------
 src/MNH/ini_sizen.f90       | 11 +++++++----
 src/MNH/init_mnh.f90        |  2 +-
 src/MNH/prep_ideal_case.f90 |  6 ++----
 src/MNH/prep_real_case.f90  |  1 +
 src/MNH/read_exsegn.f90     |  2 +-
 src/MNH/spawning.f90        |  1 -
 8 files changed, 19 insertions(+), 36 deletions(-)

diff --git a/src/MNH/default_desfmn.f90 b/src/MNH/default_desfmn.f90
index 0e5a2c839..c0902952d 100644
--- a/src/MNH/default_desfmn.f90
+++ b/src/MNH/default_desfmn.f90
@@ -298,7 +298,7 @@ INTEGER             :: JM      ! loop index
 !             ----------------------------------
 !
 ! CINIFILE='INIFILE'
-! CINIFILEPGD=''
+CINIFILEPGD='' !Necessary to keep this line to prevent problems with spawning
 CCPLFILE(:)='    '
 !
 !-------------------------------------------------------------------------------
diff --git a/src/MNH/ini_segn.f90 b/src/MNH/ini_segn.f90
index f70fe6d0a..4bf2c6de7 100644
--- a/src/MNH/ini_segn.f90
+++ b/src/MNH/ini_segn.f90
@@ -176,7 +176,7 @@ USE MODN_CONFZ
 USE MODD_DYN
 USE MODD_IO_ll,   ONLY : ISP,LIOCDF4,LLFIREAD,NVERB_FATAL,NVERB_WARNING,TFILE_OUTPUTLISTING,TFILEDATA
 USE MODD_LUNIT
-USE MODD_LUNIT_n, ONLY : CINIFILE_n=> CINIFILE, TINIFILE_n => TINIFILE, CINIFILEPGD_n=> CINIFILEPGD, TLUOUT
+USE MODD_LUNIT_n, ONLY : CINIFILE_n=> CINIFILE, TINIFILE_n => TINIFILE, CINIFILEPGD_n=> CINIFILEPGD, TLUOUT, LUNIT_MODEL
 USE MODD_PARAM_n, ONLY : CSURF
 USE MODD_PARAMETERS
 USE MODD_REF,   ONLY : LBOUSS
@@ -264,7 +264,8 @@ TPINIFILE => NULL()
 !
 WRITE(YMI,'(I2.0)') KMI
 HLUOUT='OUTPUT_LISTING'//ADJUSTL(YMI)
-CALL IO_FILE_ADD2LIST(TLUOUT,HLUOUT,'OUTPUTLISTING','WRITE')
+CALL IO_FILE_ADD2LIST(LUNIT_MODEL(KMI)%TLUOUT,HLUOUT,'OUTPUTLISTING','WRITE')
+TLUOUT => LUNIT_MODEL(KMI)%TLUOUT !Necessary because TLUOUT was initially pointing to NULL
 CALL IO_FILE_OPEN_ll(TLUOUT)
 !
 !Set output file for PRINT_MSG
@@ -344,9 +345,9 @@ IF (CPROGRAM=='MESONH') THEN
   HINIFILEPGD=CINIFILEPGD_n
   YINIFILE=CINIFILE_n
 
-  CALL IO_FILE_ADD2LIST(TINIFILE_n,TRIM(YINIFILE),'PREPIDEALCASE','READ',KLFINPRAR=0,KLFITYPE=2,KLFIVERB=NVERB)
-
-  CALL IO_FILE_OPEN_ll(TINIFILE_n)
+  CALL IO_FILE_ADD2LIST(TPINIFILE,TRIM(YINIFILE),'PREPIDEALCASE','READ',KLFINPRAR=0,KLFITYPE=2,KLFIVERB=NVERB)
+  TINIFILE_n => TPINIFILE !Necessary because TINIFILE was initially pointing to NULL
+  CALL IO_FILE_OPEN_ll(TPINIFILE)
 END IF
 !
 !-------------------------------------------------------------------------------
@@ -389,25 +390,6 @@ END IF
 !*      6.    READ in the LFI file SOME VARIABLES of MODD_CONF
 !             ------------------------------------------------
 !
-NULLIFY(TPINIFILE)
-ALLOCATE(TPINIFILE) !TODO: deallocate it
-!
-TPINIFILE%CNAME  = YINIFILE
-!TPINIFILE%CTYPE  = ''
-CALL PRINT_MSG(NVERB_WARNING,'IO','INI_SEG_n','filetype not (yet) set')
-TZFD=>GETFD(TRIM(ADJUSTL(TPINIFILE%CNAME))//'.lfi')
-IF (.NOT.ASSOCIATED(TZFD)) CALL PRINT_MSG(NVERB_FATAL,'IO','INI_SEG_n','file '//TRIM(TPINIFILE%CNAME)//' not found')
-IF (LIOCDF4 .AND. .NOT.LLFIREAD) THEN
-  TPINIFILE%CFORMAT = 'NETCDF4'
-  IF (ISP == TZFD%OWNER) TPINIFILE%NNCID = TZFD%CDF%NCID
-ELSE
-  TPINIFILE%CFORMAT   = 'LFI'
-  TPINIFILE%NLFINPRAR = 0
-  IF (ISP == TZFD%OWNER) TPINIFILE%NLFIFLU = TZFD%FLU
-ENDIF
-TPINIFILE%CMODE    = 'READ'
-TPINIFILE%NLFITYPE = 2
-TPINIFILE%NLFIVERB = NVERB
 CALL IO_READ_FIELD(TPINIFILE,'MASDEV',IMASDEV)
 !
 IF (CPROGRAM=='MESONH' .OR. CPROGRAM=='SPAWN ') THEN
diff --git a/src/MNH/ini_sizen.f90 b/src/MNH/ini_sizen.f90
index 7240fce03..fa151291a 100644
--- a/src/MNH/ini_sizen.f90
+++ b/src/MNH/ini_sizen.f90
@@ -13,13 +13,14 @@
 !
 INTERFACE
 !
-SUBROUTINE INI_SIZE_n(KMI,HLUOUT,TPINIFILE)
+SUBROUTINE INI_SIZE_n(KMI,HLUOUT,TPINIFILE,HINIFILEPGD)
 !
 USE MODD_IO_ll, ONLY : TFILEDATA
 !
 INTEGER,            INTENT(IN)    :: KMI          !Model Index
 CHARACTER (LEN=*),  INTENT(IN)    :: HLUOUT       !Name for output-listing of nested models
 TYPE(TFILEDATA),    INTENT(IN)    :: TPINIFILE    !Initial file
+CHARACTER (LEN=*),  INTENT(IN)    :: HINIFILEPGD
 !
        END SUBROUTINE INI_SIZE_n
 !
@@ -27,9 +28,9 @@ END INTERFACE
 !
 END MODULE MODI_INI_SIZE_n
 !-----------------------------------------------------------------
-!     ###########################################
-      SUBROUTINE INI_SIZE_n(KMI,HLUOUT,TPINIFILE)
-!     ###########################################
+!     #######################################################
+      SUBROUTINE INI_SIZE_n(KMI,HLUOUT,TPINIFILE,HINIFILEPGD)
+!     #######################################################
 !
 !!
 !!****  *INI_SIZE_n* - routine to initialize the sizes ratio positions of nested model _n
@@ -138,6 +139,7 @@ IMPLICIT NONE
 INTEGER,            INTENT(IN)    :: KMI          !Model Index
 CHARACTER (LEN=*),  INTENT(IN)    :: HLUOUT       !Name for output-listing of nested models
 TYPE(TFILEDATA),    INTENT(IN)    :: TPINIFILE    !Initial file
+CHARACTER (LEN=*),  INTENT(IN)    :: HINIFILEPGD
 !
 !*       0.2   declarations of local variables
 !
@@ -152,6 +154,7 @@ INTEGER             :: IJPHEXT
 !
 CALL FMLOOK_ll(HLUOUT,HLUOUT,ILUOUT,IRESP)
 CLUOUT = HLUOUT
+CINIFILEPGD=HINIFILEPGD
 !
 !-------------------------------------------------------------------------------
 !
diff --git a/src/MNH/init_mnh.f90 b/src/MNH/init_mnh.f90
index 8a7b928f0..dec85eaab 100644
--- a/src/MNH/init_mnh.f90
+++ b/src/MNH/init_mnh.f90
@@ -200,7 +200,7 @@ IF (CPROGRAM=='DIAG') CALL RESET_EXSEG(YLUOUT(1))
 !
 DO JMI=1,NMODEL
   CALL GOTO_MODEL(JMI)
-  CALL INI_SIZE_n(JMI,YLUOUT(JMI),LUNIT_MODEL(JMI)%TINIFILE)
+  CALL INI_SIZE_n(JMI,YLUOUT(JMI),LUNIT_MODEL(JMI)%TINIFILE,YINIFILEPGD(JMI))
 END DO
 !
 IF (CPROGRAM=='SPAWN ') THEN 
diff --git a/src/MNH/prep_ideal_case.f90 b/src/MNH/prep_ideal_case.f90
index 304057d26..79bc36abf 100644
--- a/src/MNH/prep_ideal_case.f90
+++ b/src/MNH/prep_ideal_case.f90
@@ -557,8 +557,6 @@ REAL, DIMENSION(:,:,:), ALLOCATABLE ::ZTHL,ZT,ZRT,ZFRAC_ICE,&
                                       ZEXN,ZLVOCPEXN,ZLSOCPEXN,ZCPH, &
                                       ZRSATW, ZRSATI
                                  ! variables for adjustement
-INTEGER             :: ILENCH, IGRID, IRESP
-CHARACTER (LEN=100) :: YCOMMENT
 REAL                :: ZDIST
 !
 !JUAN TIMING
@@ -679,8 +677,8 @@ CLUOUT  = 'OUTPUT_LISTING1'
 CLUOUT0 = CLUOUT
 CEXPRE  = 'PRE_IDEA1.nam'
 CALL IO_FILE_ADD2LIST(TLUOUT0,CLUOUT0,'OUTPUTLISTING','WRITE')
-CALL IO_FILE_OPEN_ll(TLUOUT)
-NLUOUT = TLUOUT%NLU
+CALL IO_FILE_OPEN_ll(TLUOUT0)
+NLUOUT = TLUOUT0%NLU
 !Set output files for PRINT_MSG
 TLUOUT              => TLUOUT0
 TFILE_OUTPUTLISTING => TLUOUT0
diff --git a/src/MNH/prep_real_case.f90 b/src/MNH/prep_real_case.f90
index c03c6b73d..55af9a27b 100644
--- a/src/MNH/prep_real_case.f90
+++ b/src/MNH/prep_real_case.f90
@@ -577,6 +577,7 @@ CALL OPEN_PRC_FILES(YPRE_REAL1,YATMFILE, YATMFILETYPE  &
                               ,YSURFFILE,YSURFFILETYPE &
                               ,YPGDFILE,TZPGDFILE)
 ILUOUT0 = TLUOUT0%NLU
+TLUOUT => TLUOUT0
 !
 CPGDFILE = YPGDFILE
 !
diff --git a/src/MNH/read_exsegn.f90 b/src/MNH/read_exsegn.f90
index 9f2ed142f..b0d732817 100644
--- a/src/MNH/read_exsegn.f90
+++ b/src/MNH/read_exsegn.f90
@@ -2699,7 +2699,7 @@ IF ( LEN_TRIM(HINIFILEPGD)>0 ) THEN
   IF ( CINIFILEPGD/=HINIFILEPGD ) THEN 
     WRITE(UNIT=ILUOUT,FMT=9001) KMI
     WRITE(ILUOUT,FMT=*) ' ERROR : in EXSEG1.nam, in NAM_LUNITn you have CINIFILEPGD= ',CINIFILEPGD
-    WRITE(ILUOUT,FMT=*) ' whereas in .des you have CINIFLEPGD= ',HINIFILEPGD
+    WRITE(ILUOUT,FMT=*) ' whereas in .des you have CINIFILEPGD= ',HINIFILEPGD
     WRITE(ILUOUT,FMT=*) ' Please check your Namelist '
     WRITE(ILUOUT,FMT=*) ' For example, you may have specified the un-nested PGD file instead of the nested PGD file '
     WRITE(ILUOUT,FMT=*) 
diff --git a/src/MNH/spawning.f90 b/src/MNH/spawning.f90
index e35b0cf51..41bec123c 100644
--- a/src/MNH/spawning.f90
+++ b/src/MNH/spawning.f90
@@ -182,7 +182,6 @@ CALL READ_EXSPA(CINIFILE,CINIFILEPGD,&
 YEXSPA  = 'SPAWN1.nam'
 CALL OPEN_ll(unit=ILUSPA,FILE=YEXSPA,iostat=IRESP,status="OLD",action='READ',  &
              form='FORMATTED',position="REWIND",mode='GLOBAL')
-ILUOUT = TLUOUT%NLU
 !
 CALL INIT_NMLVAR
 CALL POSNAM(ILUSPA,'NAM_SPAWN_SURF',GFOUND)
-- 
GitLab