Repository navigation
Conversation
Add an extra_mode_flag kwarg to Dataset that is ORed into the mode passed to nc_open, so files without a magic number can be opened with a user-defined format (UDF) dispatch layer, such as those provided by the NetCDF Expansion Pack (NEP). Add NEP_UDF_* module constants (names match nep.h). Only allowed for plain read-only opens (mode='r', not diskless, memory or parallel), requires netcdf-c >= 4.10.0. Add tests for the kwarg (opening an mmCIF file with no magic number) and for reading GRIB2 with NEP (test/gfs_sample.grb2, the first 3 messages of pygrib's sampledata/gfs.grb). The NEP read tests are skipped unless the NEP readers are registered with netcdf-c. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
This PR currently has no test coverage under Github Actions - the tests are skipped because NEP is not installed in any of the environments the tests run in. This can be remedied once the conda-forge package exists (conda-forge/staged-recipes#34309). |
This branch has not been deployed
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.
Add
extra_mode_flagkwarg toDatasetfor user-defined formats (closes #1490)netcdf-c (>= 4.10.0) supports user-defined formats (UDFs), which let
external dispatch layers read non-netCDF formats through the netCDF API.
The NetCDF Expansion Pack (NEP)
uses this to add readers for GRIB2, FITS, NASA CDF, PDS4, DICOM, PDB,
mmCIF and GeoTIFF.
netcdf-c normally picks the dispatch layer from the file's magic number,
so
Dataset()already works for these formats with no changes. Some fileshave no magic number, though (mmCIF, for example). For those, netcdf-c
needs a UDF mode flag passed to
nc_open(), such asnc_open(path, NC_NOWRITE|NC_UDF8, &ncid). Until now there was no way topass one from Python.
Almost all NEP supported formats contain the "magic number" and can be read by netcdf-c 4.10.0 without the extra flag. AFAICT, only DICOM files require it.
Changes
Datasetkwargextra_mode_flag(defaultNone). An integerthat is ORed into the mode passed to
nc_open():NEP_UDF_*: the names match the macros in NEP'snep.h, and each maps a format to itsNC_UDFnslot:NEP_UDF_GEOTIFF_BIGTIFF(UDF0),NEP_UDF_GEOTIFF_STANDARD(UDF1),NEP_UDF_GRIB2(UDF2),NEP_UDF_FITS(UDF3),NEP_UDF_CDF(UDF4),NEP_UDF_PDS4(UDF5),NEP_UDF_DICOM(UDF6),NEP_UDF_PDB(UDF7),NEP_UDF_MMCIF(UDF8),NEP_UDF_NEXTCDF4(UDF9). They are hard-coded,like
NC_DISKLESS/NC_PERSIST, so the module still compiles againstolder netcdf-c.
nc_open()(
cmode == NC_NOWRITE). These cases raise an error:modeother than'r'(including'rs'), ordiskless,memoryorparallel‚ÜíValueError. NEP readers are read-only, and parallel orin-memory opens aren't expected to work with UDFs.
NEP_UDF_*constants ‚ÜíValueError.This stops arbitrary mode bits (such as
NC_WRITE) from being passedthrough.
NetCDF4MissingFeatureException.__init__.pyi,_netCDF4.pyi) updated.The UDF itself still has to be registered with netcdf-c. For NEP that's
done through its
.ncrcfile, for example withNCRCENV_RCpointing at it.Tests
The NEP-dependent tests are skipped unless the NEP readers are registered
with netcdf-c, so CI without NEP is unaffected.
test/test_extra_mode_flag.pydata_magic number removed, checksthat it fails to open by default, and checks that it opens and reads
correctly with
extra_mode_flag=NEP_UDF_MMCIF.test/test_nep_grib2.pywithtest/gfs_sample.grb2(27 KB, the first 3messages of pygrib's
sampledata/gfs.grb): reads HGT, TMP and RH andchecks them against reference values from pygrib. It runs twice, once with
the format detected from the magic number and once with
extra_mode_flag=NEP_UDF_GRIB2.MANIFEST.innow includestest/*grb2.Tested on macOS arm64 with netcdf-c 4.10.1, HDF5 2.2.0 and NEP v4.1.0 built
from source. The full test suite passes with and without NEP, and so do
stubtestandmypy test.Notes
2.5° grid it gives
(y=144, x=73), but the data are stored as(lat=73, lon=144). The GRIB2 test reshapes before comparing, so it will
keep passing once that's fixed in NEP.
nepisn't on conda-forge yet(Add nep 3.0.0 conda-forge/staged-recipes#34309 is still open), and that package
(v3.0.0) doesn't include the format readers, so testing currently needs
a source build of NEP.
ü§ñ Generated with Claude Code