Skip to content

improve SOFA attribute and string variable handling and testing thereof - #149

Open
f-brinkmann wants to merge 5 commits into
mainfrom
hot/fix_character_encoding
Open

f-brinkmann wants to merge 5 commits into
mainfrom
hot/fix_character_encoding

Conversation

@f-brinkmann

@f-brinkmann f-brinkmann commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Which issue(s) are closed by this pull request?

Closes #147 and fixes #140. Also fixes failing tests in #148

Changes proposed in this pull request:

Improved writing of SOFA attributes and string variables to be fully conform with the SOFA standard

  • SOFA attributes and string variables are now always written as netCDF char array as required by the SOFA standard
    • To this end all conversion between python/numpy and netCDF data types is done in sofar.io._format_value_for/from_netcdf and the use of the netCDF functions stringtochar and chartostring that caused the error in the first place is now avoided.
  • SOFA attributes and string variables can be read even if tey were written as a netCDF string to be user friendly and backwards compatible
  • Remove restriction on numpy 2.5
    • this required the new restriction on netCDF4 >= 1.7.5 due to numpy and
    • the restriction to Python >= 3.11 due to netCDF (Python 3.10 is close to end of life)
  • Failing ruff checks are fixed in Fix ruff #150 to keep this PR more focused.

All changes are fully backwards compatible because they only changed how data are handled internally in Sofa and SofaStream.

AI disclaimer:

The fixes were done by an LMM. I checked the SOFA standard before hand. I discussed and instructed the LMM with care, and checked all output.

@f-brinkmann f-brinkmann self-assigned this Oct 24, 2026
@f-brinkmann f-brinkmann added this to the v1.3.1 milestone Oct 24, 2026
@f-brinkmann f-brinkmann moved this from Backlog to Require review in Weekly Planning Oct 24, 2026
@f-brinkmann f-brinkmann mentioned this pull request Oct 24, 2026
@f-brinkmann
f-brinkmann requested review from a team, ahms5 and hoyer-a and a balanced review from Copilot October 24, 2026 16:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

UTF-8 byte-array comparisons can raise decoding errors, and custom netCDF string variables lack dimension metadata needed for reliable read/write round trips.

2 open issues
What changed in this PR

This PR addresses #147 by improving UTF-8 handling in SOFA file I/O, writing text as standard-compliant netCDF character data while accepting netCDF string data when reading.

Changes:

  • Updates text encoding, decoding, comparison, and byte-length calculation.
  • Adds tests for UTF-8 data and netCDF attribute types.
  • Raises minimum requirements to Python 3.11 and netCDF4 1.7.5, removes the NumPy upper bound, and updates CI and documentation.
File Description
tests/​test_io.py Adds encoding and netCDF type checks.
sofar/​utils.py Changes string comparisons to Unicode.
sofar/​sofastream.py Accepts netCDF string variables during verification.
sofar/​sofa.py Calculates string lengths in UTF-8 bytes.
sofar/​io.py Updates text serialization and decoding.
README.md Updates the minimum Python version.
pyproject.toml Revises Python and dependency requirements.
HISTORY.rst Documents text handling and support changes.
.circleci/​config.yml Removes unsupported Python versions from CI.

Review effort: Balanced

Comment thread sofar/io.py
Comment on lines +178 to +179
if file[var].dtype in ["S1", str]:
dtype = "string"
Comment thread sofar/utils.py
try:
assert np.all(
np.squeeze(a).astype("S") == np.squeeze(b).astype("S"))
np.squeeze(a).astype("U") == np.squeeze(b).astype("U"))

@hoyer-a hoyer-a left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as i can judge, this looks good!

Comment thread sofar/io.py Resolved
Co-authored-by: Anton Hoyer <156099087+hoyer-a@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Require review

Development

Successfully merging this pull request may close these issues.

invalid SOFA files for UTF-8 strings [MAINT] Remove restrcition for Numpy 2.5

3 participants