Skip to content

grid.data.from_h5: read a netCDF4 file's grid mapping (bytes attributes) - #66

Closed
SmithB wants to merge 1 commit into
mainfrom
from_h5_netcdf_grid_mapping
Closed

SmithB wants to merge 1 commit into
mainfrom
from_h5_netcdf_grid_mapping

Conversation

@SmithB

@SmithB SmithB commented Oct 22, 2026

Copy link
Copy Markdown
Owner

What

grid.data.from_h5 crashed on netCDF4 files that have a grid_mapping attribute, e.g. every ATL15:

File ".../pointCollection/grid/data.py", line 1111, in from_h5
    map_field_name = posixpath.join(group,grid_mapping_name)
TypeError: Can't mix strings and bytes in path components

netCDF4 writes text attributes as fixed-length strings, and h5py returns those as bytes. With this change, the grid_mapping name and the grid-mapping variable's attributes go through nc_h5._attribute_value. That is the conversion from_nc's h5py reader already uses, so self.projection holds str and scalars, as from_nc returns them. Before, it held bytes and 1-element arrays.

Files written by to_h5 store str and scalar attributes already, so they read back unchanged.

Why

The ATL15 interactive notebook reads the rel006 ATL15s on S3. It had to use h5py directly because from_h5 failed on them.

Checked

On ATL15_A1_0332_3mo_40km_006_02.nc (groups delta_h and dhdt_012mo, t_axis=0), from_h5 now returns the same projection as from_nc (15 attributes, same values and types) and the same data.

Not changed

from_h5 does not read dimension order. ATL15 variables are (t, y, x), so it needs t_axis=0; with the default it raises the existing shape IndexError. from_nc reads ATL15s with either t_axis.

Tests

tests/test_nc_h5.py:

  • from_h5 on the packed netCDF4 fixture (root group and a subgroup) returns the projection from_nc returns. Both cases fail on main with the TypeError above.
  • A to_h5(srs_epsg=3413) / from_h5 round trip returns crs unchanged. This passes on main too.

Full suite: 348 passed, 3 skipped.

🤖 Generated with Claude Code

…ibutes

netCDF4 writes text attributes that h5py returns as bytes, so from_h5 on
a netCDF4 file (an ATL15, for one) passed a bytes grid_mapping name to
posixpath.join() and raised "Can't mix strings and bytes in path
components".  The name and the grid-mapping variable's attributes now go
through nc_h5._attribute_value, the conversion the h5py reader of
from_nc already uses, so the projection holds str and scalars as from_nc
returns them.  Files written by to_h5 store str and scalars already and
read back unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SmithB SmithB closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant