Repository navigation
Silence netCDF4 ndarray.shape DeprecationWarning at each write site - #419
Merged
Merged
Conversation
flopy sets warnings.simplefilter('always', DeprecationWarning) at import,
overriding pytest.ini/PYTHONWARNINGS filters and reaching notebooks and
user scripts. Wrap every pywatershed netCDF4 write in
nc4_shape_warning_filter (warnings.catch_warnings); keep the pytest.ini
line only for xarray's own to_netcdf path. MAINTENANCE.md lists every
site for removal once netCDF4 ships PR #1469.
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.
With netCDF4 <= 1.7.4 and NumPy >= 2.5 every netCDF variable write emits
Setting the shape on a NumPy array has been deprecated (netCDF4 assigns
to ndarray.shape in _netCDF4.pyx; fixed upstream in
Unidata/netcdf4-python#1469, unreleased). The pytest.ini filter hid it
in the test suite only: the example notebooks (run by
autotest_exs/test_notebooks.py via ipython in a subprocess) and any user
script still got one warning per variable per timestep.
A global filter cannot fix this. flopy 3.11 runs
warnings.simplefilter("always", DeprecationWarning) at import
(flopy/utils/rasters.py:10, flopy/mf6/utils/binarygrid_util.py:14,
flopy/export/vtk.py:17), which is inserted ahead of any filter set
earlier, including PYTHONWARNINGS, and import pywatershed imports
flopy. Reported informally to the flopy developers.
Changes:
nc4_shape_warning_filter, a warnings.catch_warnings context manager
in pywatershed/base/data_model.py, wraps every pywatershed netCDF4
write: NetCdfWrite (coordinate writes, add_simulation_time,
add_data, add_all_data), Budget.output_netcdf, and
dd_to_nc4_ds. It is inserted at write time, so import order does
not matter.
The pytest.ini line is kept only for xarray's own to_netcdf path
(xarray/backends/netCDF4.py, setitem), which pywatershed
calls from about a dozen places and does not wrap.
MAINTENANCE.md lists every site (and a grep to find them) for
removal once the netCDF4 release carrying the fix is required.
Verified: 01_multi-process_models.ipynb runs clean;
pytest test_prms_canopy.py test_model.py --domain drb_2yr shows zero
occurrences and passes with -W error::DeprecationWarning:pywatershed.
Docs build for this PR: https://app.readthedocs.org/projects/pywatershed/builds/34520888/
Rendered: https://pywatershed--419.org.readthedocs.build/en/419/
Checklist
[x] Closes #418
[ ]
Tests added[ ]
Performance benchmarks added or run[x] User visible changes (including notable bug fixes) are documented in whats-new.rst
[x] The
(:pull:XXX)placeholder in whats-new.rst is replaced with this PR's number[ ]
New public classes/functions are exported in pywatershed/init.py and listed in doc/api/*.rst