Read NetCDF-4 files with scalar variables - #86
Merged
i-norden merged 1 commit intoJul 29, 2026
Conversation
A rank-0 variable has no dimensions to reference, so NetCDF-4 writes it
without a DIMENSION_LIST attribute. resolve_dimension_scale_addresses
required that attribute before inspecting the rank, so its ndim == 0
early return was unreachable and scalar variables raised
dataset '<name>' is missing required DIMENSION_LIST metadata
Because NcMetadataMode::Strict is the default, the error propagated out
of NcFile::variables() and failed the whole file, not just the scalar
variable. Check the rank first.
Copernicus CDS ERA5 downloads hit this: their scalar `number` (ensemble
member) and `expver` variables sit alongside the gridded fields, so no
such file could be opened at all. The new nc4_scalar_variable.nc fixture
is shaped after one.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes NetCDF-4 scalar (rank-0) variable handling in the reader by ensuring dimension-scale resolution checks the dataset rank before requiring DIMENSION_LIST, preventing strict-metadata opens from failing on files that contain scalar variables alongside gridded data.
Changes:
- Fix
resolve_dimension_scale_addressesto early-return an empty dimension list for rank-0 variables before attempting to readDIMENSION_LIST. - Add a NetCDF-4 fixture and an integration test covering scalar variables coexisting with dimensioned variables.
- Document the fix in the changelog.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| testdata/generate_fixtures.py | Adds a new NetCDF-4 fixture shaped to include a scalar (rank-0) variable scenario. |
| netcdf-reader/tests/integration.rs | Adds an integration test ensuring scalar variables don’t break variables() and have no dimensions. |
| netcdf-reader/src/nc4/variables.rs | Reorders logic to check ndim == 0 before requiring DIMENSION_LIST. |
| CHANGELOG.md | Notes the strict-mode fix for scalar variables missing DIMENSION_LIST. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+854
to
+857
| # Scalar: no dimensions, therefore no DIMENSION_LIST on disk. | ||
| number = ds.createVariable("number", "i8", ()) | ||
| number.long_name = "ensemble member numerical id" | ||
| number[...] = 0 |
Collaborator
|
Thank you for the fix @ctessum, LGTM! Will cut a patch shortly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A rank-0 variable has no dimensions to reference, so NetCDF-4 writes it without a DIMENSION_LIST attribute. resolve_dimension_scale_addresses required that attribute before inspecting the rank, so its ndim == 0 early return was unreachable and scalar variables raised
Because NcMetadataMode::Strict is the default, the error propagated out of NcFile::variables() and failed the whole file, not just the scalar variable. Check the rank first.
Copernicus CDS ERA5 downloads hit this: their scalar
number(ensemble member) andexpvervariables sit alongside the gridded fields, so no such file could be opened at all. The new nc4_scalar_variable.nc fixture is shaped after one.