From 56cc223f3e5262af2be237e5cce4ba9db3c0859a Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Wed, 22 May 2024 15:47:40 +0200
Subject: [PATCH] Philippe 22/05/2024: IO_Field_header_split_write_nc4: add
 optional arguments to force size of horizontal dimensions

---
 src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90 | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90 b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
index 2e9a95b4a..a395548de 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_write_nc4.f90
@@ -88,15 +88,17 @@ integer(kind=CDFINT),parameter :: DEFLATE = 1
 
 contains
 
-subroutine IO_Field_header_split_write_nc4( tpfile, tpfield, knblocks )
+subroutine IO_Field_header_split_write_nc4( tpfile, tpfield, knblocks, kisize, kjsize )
 use modd_field,      only: TYPEREAL
 use modd_parameters, only: jphext
 
 use mode_tools_ll,   only: Get_globaldims_ll
 
-type(tfiledata),       intent(in) :: tpfile
-class(tfieldmetadata), intent(in) :: tpfield
-integer,               intent(in) :: knblocks
+type(tfiledata),                 intent(in) :: tpfile
+class(tfieldmetadata),           intent(in) :: tpfield
+integer,                         intent(in) :: knblocks
+integer,               optional, intent(in) :: kisize ! Size of the field in the first dimension
+integer,               optional, intent(in) :: kjsize ! Size of the field in the second dimension
 
 character(len=len(tpfield%cmnhname))  :: yvarname
 integer                               :: iimax, ijmax
@@ -133,6 +135,7 @@ if ( istatus /= NF90_NOERR ) then
                   trim( tpfile%cname )//': '//trim( yvarname )//': CDIR should be XY' )
 
   call Get_globaldims_ll( iimax, ijmax )
+  ! Use global dims and not kisize/kjsize (if provided) to set the correct 'coordinates' attribute
   ishape(1) = int( iimax + 2 * jphext, kind = CDFINT )
   ishape(2) = int( ijmax + 2 * jphext, kind = CDFINT )
   ishape(3) = knblocks
@@ -164,6 +167,14 @@ if ( istatus /= NF90_NOERR ) then
   if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
                                                      'ndims for '//trim( tpfield%cmnhname ) )
 
+  ! Force ishape to the provided dimensions (if present)
+  if ( present( kisize ) ) then
+    ishape(1) = kisize
+  end if
+  if ( present( kjsize) ) then
+    ishape(2) = kjsize
+  end if
+
   istatus = NF90_PUT_ATT( incid, ivarid,'dims', ishape )
   if ( istatus /= NF90_NOERR ) call IO_Err_handle_nc4( istatus, 'IO_Field_header_split_write_nc4', 'NF90_PUT_ATT', &
                                                      'dims for '//trim( tpfield%cmnhname ) )
-- 
GitLab