Skip to content

Set string length in write_netcdf()#641

Merged
xylar merged 5 commits into
MPAS-Dev:masterfrom
xylar:fix-consistent-strlen
Jun 12, 2025
Merged

Set string length in write_netcdf()#641
xylar merged 5 commits into
MPAS-Dev:masterfrom
xylar:fix-consistent-strlen

Conversation

@xylar
Copy link
Copy Markdown
Collaborator

@xylar xylar commented Jun 12, 2025

This pull request introduces enhancements and refactoring to the mpas_tools package, primarily focusing on improving the handling of string variables in NetCDF files, simplifying function signatures, and improving code readability. The most significant changes include adding support for configurable string length (nchar) in NetCDF files, removing redundant nchar arguments from various functions, and reformatting code for better clarity.

There is also a fix to a bug introduced in #638 related to adding non-string properties to mask datasets.

Enhancements to NetCDF handling:

  • Added a new default variable default_nchar in mpas_tools/io.py to define the default string length for NetCDF files. Updated the write_netcdf function to include an optional nchar parameter for string length customization. [1] [2] [3]
  • Modified the encoding logic in write_netcdf to handle string variables with the specified nchar and to improve compatibility with MPAS by converting int64 variables to int32.

Refactoring and simplification:

  • Removed the nchar argument from multiple functions in mpas_tools/mesh/mask.py, such as _add_properties, _compute_region_masks, and _compute_transect_masks. The string length is now handled internally using default_nchar. [1] [2] [3]
  • Updated function calls and definitions across mpas_tools/mesh/mask.py to align with the removal of nchar. This change simplifies the function signatures and reduces redundancy. [1] [2] [3]

Code readability improvements:

  • Reformatted code in mpas_tools/ocean/moc.py for better readability, including breaking long lines, improving argument alignment, and using parentheses for multi-line expressions. [1] [2] [3]
  • Updated the moc_southern_boundary_extractor function to use a more readable argument parser configuration.

These changes improve the usability, maintainability, and clarity of the mpas_tools package.

xylar added 4 commits June 12, 2025 04:28
This merge also cleans up the way that fill values and int64-->int32
conversions are handled.
This is necessary because we can't encode strings of different
lengths with the same dimension name.
@xylar xylar force-pushed the fix-consistent-strlen branch from 0ae3649 to f58fa91 Compare June 12, 2025 09:28
@xylar
Copy link
Copy Markdown
Collaborator Author

xylar commented Jun 12, 2025

Testing

This has been tested with the MPAS-Analysis test suite. Without these fixes, the suite was failing during mask creation steps but with these fixes runs successfully.

@xylar
Copy link
Copy Markdown
Collaborator Author

xylar commented Jun 12, 2025

Polaris' pr suite also runs fine with these changes.

@xylar xylar merged commit 676f2ef into MPAS-Dev:master Jun 12, 2025
6 checks passed
@xylar xylar deleted the fix-consistent-strlen branch June 12, 2025 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant