From 060487c9bc370b5d57e2b1734d0d82eb66baaed1 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 1 Aug 2019 16:32:30 +0200
Subject: [PATCH] Philippe 01/08/2019: IO: add information in main file for
 split variables

(cherry picked from commit 9b00415b67967a44c9ee48df767a5a2df0960bbc)
---
 src/LIB/SURCOUCHE/src/mode_io_field_write.f90 |  5 ++
 src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90   | 87 +++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/src/LIB/SURCOUCHE/src/mode_io_field_write.f90 b/src/LIB/SURCOUCHE/src/mode_io_field_write.f90
index 4a1c58a87..3d37d5858 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_field_write.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_field_write.f90
@@ -913,6 +913,11 @@ CONTAINS
                 END IF
              END DO
              !
+             ! Write the variable attributes in the non-split file
+             !
+             if ( tpfile%nmaster_rank==isp .and. gnc4 ) &
+               call IO_Field_header_split_write_nc4( tpfile, tpfield, size( pfield, 3 ) )
+             !
              ! write the data
              !
              DO JKK=JK,JK_MAX
diff --git a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90 b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
index f79e1cb14..aff7f36a2 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
@@ -34,6 +34,7 @@ implicit none
 private
 
 public :: IO_Coordvar_write_nc4, IO_Field_write_nc4, IO_Header_write_nc4
+public :: IO_Field_header_split_write_nc4
 
 INTERFACE IO_Field_write_nc4
    MODULE PROCEDURE IO_Field_write_nc4_X0,IO_Field_write_nc4_X1, &
@@ -55,6 +56,92 @@ integer(kind=CDFINT),parameter :: DEFLATE = 1
 
 contains
 
+subroutine IO_Field_header_split_write_nc4( tpfile, tpfield, knblocks )
+use modd_parameters, only : jphext
+
+use mode_field,      only: TYPEREAL
+use mode_tools_ll,   only: Get_globaldims_ll
+
+type(tfiledata),       intent(in) :: tpfile
+type(tfielddata),      intent(in) :: tpfield
+integer,               intent(in) :: knblocks
+
+character(len=len(tpfield%cmnhname))  :: yvarname
+integer                               :: iimax, ijmax
+integer(kind=CDFINT)              :: istatus
+integer(kind=CDFINT)              :: incid
+integer(kind=CDFINT)              :: ivarid
+integer(kind=CDFINT),dimension(3) :: ishape
+
+call Print_msg( NVERB_DEBUG, 'IO', 'IO_Field_header_split_write_nc4', 'called for field '//trim( tpfield%cmnhname ) )
+
+if ( tpfield%ntype /= TYPEREAL ) then
+  call Print_msg( NVERB_ERROR, 'IO', 'IO_Field_header_split_write_nc4', 'invalid ntype for field '//trim( tpfield%cmnhname ) )
+  return
+end if
+
+! Get the Netcdf file ID
+incid = tpfile%nncid
+
+call IO_Mnhname_clean( tpfield%cmnhname, yvarname )
+
+istatus = NF90_INQ_VARID( incid, yvarname, ivarid )
+if ( istatus /= NF90_NOERR ) then
+
+  istatus = NF90_DEF_VAR( incid, yvarname, MNHREAL_NF90, ivarid)
+
+  if ( tpfield%ndims /= 3 ) call Print_msg( NVERB_FATAL, 'IO', 'IO_Field_header_split_write_nc4', &
+                  trim( tpfile%cname )//': '//trim( yvarname )//': NDIMS should be 3' )
+
+  if ( tpfield%cdir /= 'XY' ) call Print_msg( NVERB_FATAL, 'IO', 'IO_Field_header_split_write_nc4', &
+                  trim( tpfile%cname )//': '//trim( yvarname )//': CDIR should be XY' )
+
+  call Get_globaldims_ll( iimax, ijmax )
+  ishape(1) = int( iimax + 2 * jphext, kind = CDFINT )
+  ishape(2) = int( ijmax + 2 * jphext, kind = CDFINT )
+  ishape(3) = knblocks
+  call IO_Field_attr_write_nc4( tpfile, tpfield, ivarid, .false., kshape = ishape )
+
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_DEF_VAR', trim(yvarname) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'split_variable', 'yes')
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'split_variable for '//trim( tpfield%cmnhname ) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'split_mode', 'Z')
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'split_mode for '//trim( tpfield%cmnhname ) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'split_nblocks', knblocks )
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'split_nblocks for '//trim( tpfield%cmnhname ) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'split_nfiles', tpfile%nsubfiles_ioz )
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'split_nfiles for '//trim( tpfield%cmnhname ) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'split_distribution', 'round-robin' )
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'split_distribution for '//trim( tpfield%cmnhname ) )
+
+  istatus = NF90_PUT_ATT( incid, ivarid,'ndims', tpfield%ndims )
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'ndims for '//trim( tpfield%cmnhname ) )
+
+  if ( tpfield%ltimedep ) then
+    istatus = NF90_PUT_ATT( incid, ivarid,'time_dependent', 'yes' )
+  else
+    istatus = NF90_PUT_ATT( incid, ivarid,'time_dependent', 'no' )
+  end if
+  if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
+                                                     'time_dependent for '//trim( tpfield%cmnhname ) )
+else
+  call Print_msg( NVERB_WARNING, 'IO', 'IO_Field_header_split_write_nc4', &
+                  trim( tpfile%cname )//': '//trim( yvarname )//' already defined' )
+end if
+
+end subroutine IO_Field_header_split_write_nc4
+
 SUBROUTINE IO_Field_attr_write_nc4(TPFILE,TPFIELD,KVARID,OEXISTED,KSHAPE,HCALENDAR,OISCOORD)
 !
 USE MODD_CONF,   ONLY: CPROGRAM, LCARTESIAN
-- 
GitLab