Skip to content

Commit 16d80c8

Browse files
authored
Merge pull request #657 from E3SM-Project/dqwu/align_hdf5_string_with_netcdf4
This PR updates SCORPIO’s HDF5 string type handling to match NetCDF4 behavior. It addresses differences in STRSIZE and empty string handling, ensuring consistent results in h5dump (between NetCDF4 and HDF5 outputs) and improving overall compatibility. Fixes #656
2 parents 8b84504 + 97582b5 commit 16d80c8

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/clib/pioc_support.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6628,12 +6628,13 @@ int spio_hdf5_create(iosystem_desc_t *ios, file_desc_t *file, const char *filena
66286628
filename);
66296629
}
66306630

6631-
if (H5Tset_size(h5_string_type, asize + 1) < 0)
6631+
assert(asize > 0);
6632+
if (H5Tset_size(h5_string_type, asize) < 0)
66326633
{
66336634
return pio_err(ios, file, PIO_EHDF5ERR, __FILE__, __LINE__,
66346635
"Creating file (%s) using HDF5 iotype failed. "
66356636
"The low level (HDF5) I/O library call failed to set the total size (%ld bytes) for a derived C-style string datatype",
6636-
filename, asize + 1);
6637+
filename, asize);
66376638
}
66386639

66396640
if (H5Tset_strpad(h5_string_type, H5T_STR_NULLTERM) < 0)
@@ -7235,7 +7236,11 @@ int spio_hdf5_put_att(iosystem_desc_t *ios, file_desc_t *file, int varid, const
72357236
if (atttype == NC_CHAR)
72367237
{
72377238
/* String type */
7238-
space_id = H5Screate(H5S_SCALAR);
7239+
if (asize == 0)
7240+
space_id = H5Screate(H5S_NULL);
7241+
else
7242+
space_id = H5Screate(H5S_SCALAR);
7243+
72397244
if (space_id == H5I_INVALID_HID)
72407245
{
72417246
return pio_err(ios, file, PIO_EHDF5ERR, __FILE__, __LINE__,
@@ -7254,12 +7259,12 @@ int spio_hdf5_put_att(iosystem_desc_t *ios, file_desc_t *file, int varid, const
72547259
}
72557260

72567261
/* For empty strings, asize is 0, while H5Tset_size() requires that size must be positive */
7257-
if (H5Tset_size(h5_xtype, asize + 1) < 0)
7262+
if (H5Tset_size(h5_xtype, (asize == 0 ? 1 : asize)) < 0)
72587263
{
72597264
return pio_err(ios, file, PIO_EHDF5ERR, __FILE__, __LINE__,
72607265
"Writing attribute (%s) associated with variable (varid=%d) to file (%s, ncid=%d) using HDF5 iotype failed. "
72617266
"The low level (HDF5) I/O library call failed to set the total size (%ld bytes) for a derived C-style string datatype",
7262-
name, varid, pio_get_fname_from_file(file), file->pio_ncid, asize + 1);
7267+
name, varid, pio_get_fname_from_file(file), file->pio_ncid, (asize == 0 ? 1 : asize));
72637268
}
72647269

72657270
if (H5Tset_strpad(h5_xtype, H5T_STR_NULLTERM) < 0)

0 commit comments

Comments
 (0)