Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sofar/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def _read_netcdf(filename, verify, verbose, mode):
# (the datatype of 'string' is a VLType but its dtype is str)
if file[var].dtype in ["S1", str]:
dtype = "string"
if file[var].dtype is str:
dimensions += "S"
else:
dtype = "double"
sofa._add_custom_api_entry(var.replace(".", "_"), value, None,
Expand Down
2 changes: 2 additions & 0 deletions sofar/sofastream.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ def _read_open_file_for_verification(self):
setattr(sofa, key, value)
else:
dimensions = "".join(list(netcdf_variable.dimensions))
if netcdf_variable.dtype is str:
dimensions += "S"
sofa._add_custom_api_entry(
key, value, None, dimensions, dtype)
sofa.protected = False
Expand Down
11 changes: 11 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ def test_read_netcdf_string():
assert sofa_r.SourcePosition_Comment == text
assert sofa_r.CustomStrings == text
assert sofa_r._convention["CustomStrings"]["type"] == "string"
assert sofa_r._convention["CustomStrings"]["dimensions"] == "MS"

with sf.SofaStream(file) as streamed_sofa:
streamed = streamed_sofa._read_open_file_for_verification()
assert streamed._convention["CustomStrings"]["dimensions"] == "MS"

roundtrip_file = os.path.join(temp_dir.name, "roundtrip.sofa")
sf.write_sofa(roundtrip_file, sofa_r)
sofa_roundtrip = sf.read_sofa(roundtrip_file, verify=False, verbose=False)
assert sofa_roundtrip.CustomStrings == text
assert sofa_roundtrip._convention["CustomStrings"]["dimensions"] == "MS"


def test_format_value_for_netcdf():
Expand Down