From 1b17c99244d88901c8ffe86da79c64bb903f1cce Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 5 Sep 2019 15:23:42 +0200
Subject: [PATCH] Philippe 05/09/2019: IO: Z-split files: to prevent
 serialization between files, nmnhversion is taken from the main file (modifs
 taken from 55X-dev-IO)

---
 src/LIB/SURCOUCHE/src/modd_io.f90               | 2 ++
 src/LIB/SURCOUCHE/src/mode_fm.f90               | 2 +-
 src/LIB/SURCOUCHE/src/mode_io.f90               | 4 +++-
 src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90 | 2 ++
 src/LIB/SURCOUCHE/src/mode_io_tools.f90         | 8 ++++++++
 5 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/modd_io.f90 b/src/LIB/SURCOUCHE/src/modd_io.f90
index 62a02bf94..0970816a7 100644
--- a/src/LIB/SURCOUCHE/src/modd_io.f90
+++ b/src/LIB/SURCOUCHE/src/modd_io.f90
@@ -7,6 +7,7 @@
 !  Philippe Wautelet: 05/2016-04/2018: new data structures and calls for I/O
 !  Philippe Wautelet: 10/01/2019: use NEWUNIT argument of OPEN (removed ISTDOUT, ISTDERR, added NNULLUNIT, CNULLFILE)
 !  Philippe Wautelet: 21/01/2019: add LIO_ALLOW_NO_BACKUP and LIO_NO_WRITE to modd_io_ll to allow to disable writes (for bench purposes)
+!  P. Wautelet 12/03/2019: add TMAINFILE field in TFILEDATA
 !-----------------------------------------------------------------
 
 MODULE MODD_IO_ll
@@ -117,6 +118,7 @@ TYPE TFILEDATA
   TYPE(TFILEDATA),POINTER :: TDADFILE   => NULL() !Corresponding dad file
   TYPE(TFILEDATA),POINTER :: TDESFILE   => NULL() !Corresponding .des file
   TYPE(TFILEDATA),POINTER :: TDATAFILE  => NULL() !Corresponding data file (if .des file)
+  TYPE(TFILEDATA),POINTER :: TMAINFILE  => NULL() !Corresponding main file if the file is an sub-file
   TYPE(TFILEDATA),POINTER :: TFILE_PREV => NULL()
   TYPE(TFILEDATA),POINTER :: TFILE_NEXT => NULL()
 END TYPE TFILEDATA
diff --git a/src/LIB/SURCOUCHE/src/mode_fm.f90 b/src/LIB/SURCOUCHE/src/mode_fm.f90
index 02265538c..55f00cfea 100644
--- a/src/LIB/SURCOUCHE/src/mode_fm.f90
+++ b/src/LIB/SURCOUCHE/src/mode_fm.f90
@@ -174,7 +174,7 @@ use mode_io_file_nc4, only: io_create_file_nc4, io_open_file_nc4
 #endif
 use mode_io_file_lfi, only: io_create_file_lfi, io_open_file_lfi
 
-TYPE(TFILEDATA), INTENT(INOUT) :: TPFILE ! File structure
+TYPE(TFILEDATA), pointer, INTENT(INOUT) :: TPFILE ! File structure
 INTEGER,         INTENT(OUT)   :: KRESP  ! return-code
 LOGICAL,         INTENT(IN),  OPTIONAL :: OPARALLELIO
 CHARACTER(LEN=*),INTENT(IN),  OPTIONAL :: HPROGRAM_ORIG !To emulate a file coming from this program
diff --git a/src/LIB/SURCOUCHE/src/mode_io.f90 b/src/LIB/SURCOUCHE/src/mode_io.f90
index 430fe98de..fbe696ada 100644
--- a/src/LIB/SURCOUCHE/src/mode_io.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io.f90
@@ -168,7 +168,7 @@ CONTAINS
   USE MODE_IO_MANAGE_STRUCT,    ONLY: IO_FILE_ADD2LIST, IO_FILE_FIND_BYNAME
   use mode_io_tools,            only: io_rank
 
-    TYPE(TFILEDATA), INTENT(INOUT)         :: TPFILE
+    TYPE(TFILEDATA), pointer, INTENT(INOUT)         :: TPFILE
     CHARACTER(len=*),INTENT(IN),  OPTIONAL :: MODE
     CHARACTER(len=*),INTENT(IN),  OPTIONAL :: STATUS
     CHARACTER(len=*),INTENT(IN),  OPTIONAL :: ACCESS
@@ -604,6 +604,8 @@ CONTAINS
                                        KLFINPRAR=TPFILE%NLFINPRAR,KLFITYPE=TPFILE%NLFITYPE,KLFIVERB=TPFILE%NLFIVERB, &
                                        HFORMAT=TPFILE%CFORMAT)
                END IF
+
+               TZSPLITFILE%TMAINFILE => TPFILE
              END IF
 
              IF (ALLOCATED(TPFILE%CDIRNAME)) THEN
diff --git a/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90 b/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
index 8745773f0..19776416c 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
@@ -11,6 +11,7 @@
 !  Philippe Wautelet: 05/2016-04/2018: new data structures and calls for I/O
 !  Philippe Wautelet: 21/01/2019: add LIO_ALLOW_NO_BACKUP and LIO_NO_WRITE to modd_io_ll
 !                                 to allow to disable writes (for bench purposes)
+!  P. Wautelet 12/03/2019: add TMAINFILE field in TFILEDATA
 !-----------------------------------------------------------------
 MODULE MODE_IO_MANAGE_STRUCT
 !
@@ -618,6 +619,7 @@ SUBROUTINE POPULATE_STRUCT(TPFILE_FIRST,TPFILE_LAST,KSTEPS,HFILETYPE,TPBAKOUTN)
             ELSE
               CALL PRINT_MSG(NVERB_FATAL,'IO','POPULATE_STRUCT','unknown backup/output fileformat')
             ENDIF
+            TPBAKOUTN(IPOS)%TFILE%TFILES_IOZ(JI)%TFILE%TMAINFILE => TPBAKOUTN(IPOS)%TFILE
           END DO
         END IF
         !
diff --git a/src/LIB/SURCOUCHE/src/mode_io_tools.f90 b/src/LIB/SURCOUCHE/src/mode_io_tools.f90
index 9e8dd1fc8..5eae7007a 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_tools.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_tools.f90
@@ -6,6 +6,8 @@
 !  Modifications:
 !    P. Wautelet : 13/12/2018 : extracted from mode_io.f90
 !    P. Wautelet : 14/12/2018 : added io_construct_filename
+!  P. Wautelet 05/09/2019: io_get_mnhversion: Z-split files: to prevent serialization between files,
+!                          nmnhversion is taken from the main file
 !-----------------------------------------------------------------
 module mode_io_tools
 
@@ -118,6 +120,7 @@ contains
     if ( trim(tpfile%cmode) /= 'READ' ) &
       call print_msg(NVERB_FATAL,'IO','io_get_mnhversion',trim(tpfile%cname)// 'not opened in read mode')
 
+  if ( .not. associated( tpfile%tmainfile ) ) then
     imnhversion(:) = 0
     !use tzfield because tfieldlist could be not initialised
     tzfield%cmnhname   = 'MNHVERSION'
@@ -182,6 +185,11 @@ contains
       end if
       !
       tpfile%nmnhversion(:) = imnhversion(:)
+  else ! associated( tpfile%tmainfile )
+    if ( .not. tpfile%tmainfile%lopened ) &
+      call Print_msg( NVERB_FATAL, 'IO', 'io_get_mnhversion', 'tmainfile should be opened' )
+    tpfile%nmnhversion(:) = tpfile%tmainfile%nmnhversion(:)
+  end if
   end subroutine io_get_mnhversion
 
 
-- 
GitLab