From a3efb6a43a6bd5dca0eee24bc403b0f0c60a60b9 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Fri, 1 Sep 2023 15:59:50 +0200
Subject: [PATCH] Philippe 01/09/2023: IO: bugfix: fix a memory leak for
 Z-split files

(cherry picked from commit a74130738ffbb1dda30ca1fc28c9e1d5227145d8)
---
 src/LIB/SURCOUCHE/src/mode_io_file.f90      | 1 +
 src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90 | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/src/LIB/SURCOUCHE/src/mode_io_file.f90 b/src/LIB/SURCOUCHE/src/mode_io_file.f90
index e269accf0..5b0c3419d 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_file.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_file.f90
@@ -590,6 +590,7 @@ SELECT CASE(TPFILE%CTYPE)
 #ifdef MNH_IOCDF4
 !Remark: IO_Coordvar_write_nc4 disabled (for the moment) for Z-split files
 !        because it introduce a serialization due to MPI communications inside the call
+!        WARNING: if uncommented, please modify IO_Coordvar_write_nc4 to enable block concerning gdealloc
 !       !Write coordinates variables in netCDF file
 !       IF (TZFILE_IOZ%CMODE == 'WRITE' .AND. (TZFILE_IOZ%CFORMAT=='NETCDF4' .OR. TZFILE_IOZ%CFORMAT=='LFICDF4')) THEN
 !         CALL IO_Coordvar_write_nc4(TZFILE_IOZ,HPROGRAM_ORIG=HPROGRAM_ORIG)
diff --git a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90 b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
index 564a039b2..745b2e44b 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
@@ -1596,6 +1596,8 @@ call Write_hor_coord1d( tzdim_ni_v, 'x-dimension of the grid at v location', &
 call Write_hor_coord1d( tzdim_nj_v, 'y-dimension of the grid at v location', &
                         trim(ystdnameprefix)//'_y_coordinate_at_v_location', 'Y', -0.5, jphext, 0,      zyhat_glob  )
 
+!Warning: the following block has to be reenabled if IO_Coordvar_write_nc4 is again called for Z-split files
+#if 0
 !The z?hat*_glob were allocated in Gather_hor_coord1d calls
 !Deallocate only if it is a non Z-split file or the last Z-split subfile
 gdealloc = .false.
@@ -1604,6 +1606,9 @@ if ( Associated( tpfile%tmainfile ) ) then
 else if ( tpfile%nsubfiles_ioz == 0 .and. .not. Associated( tpfile%tmainfile ) ) then
   gdealloc = .true.
 end if
+#else
+gdealloc = .true.
+#endif
 
 if ( .not. lcartesian ) then
   !Compute latitude/longitude for the Arakawa points
-- 
GitLab