Repository navigation
ENH: cache downloaded atmosphere netCDF datasets (#654) - #1137
Gui-FernandesBR merged 6 commits into
Conversation
Collapse the cache-key assignment the formatter wants on one line. No behaviour change. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Not ready to merge yet. Three items, the third being the one I care most about. 1. Three of this PR's own new tests fail on all six platforms. 2. pylint fails (exit 8): Please split those into smaller functions rather than adding 3. Cache invalidation — the design point I want resolved before this lands. The cache is enabled by default, and the key contains neither a TTL nor the forecast model run/cycle. So a user who runs a GFS forecast today and again tomorrow silently gets yesterday's forecast, with no indication anything is stale. For forecast products that is a wrong-answer bug, not a stale-file annoyance, and it is the kind of thing that is very hard for a user to notice. What I would want:
Happy to revisit once those are addressed. |
The cache restored only the eight profile Functions, elevation and _max_expected_height, so every attribute Environment derives from the dataset was missing on the second run of an otherwise identical script. atmospheric_model_type was still set to "Forecast", so info() and all_info() walked into the Forecast branch of the prints and raised AttributeError on atmospheric_model_init_date, and to_dict() serialized those fields as None. Persist the full metadata (date range, grid bounds, and the raw interpolation inputs) alongside the profiles and reinstate it on a hit, bumping the cache format to v2. Saving also indexed temperature and both wind profiles as 2-D arrays while guarding only pressure, which raised IndexError for a constant wind and broke three existing tests. Guarding pressure alone is not sufficient either: np.asarray(None, dtype=float) yields nan, so a missing column would be written out as an entry full of NaN winds. Check every column against the pressure grid before writing. Also: - Expire forecast entries after ROCKETPY_CACHE_TTL seconds (default 6h, the GFS cycle). A launch date days out would otherwise pin the first forecast ever downloaded for it and silently reuse it forever. Reanalysis is immutable and never expires; a TTL of 0 disables expiry. - Let ROCKETPY_CACHE=0/off/false/no/none/disabled turn the cache off. It previously only relocated the directory, so there was no way to opt out globally, and an empty value cached into the working directory. - Include the variable dictionary and pressure conversion factor in the cache key, so the same source decoded two ways no longer collides. - Add clear_atmosphere_cache() to remove the entries. - Treat a damaged cache file as a miss rather than letting netCDF4's RuntimeError escape and take down the simulation with it. - Pass usedforsecurity=False to hashlib.md5 so the key still builds under a FIPS-enabled Python. - Isolate ROCKETPY_CACHE per test via an autouse fixture. The suite was writing to the real ~/.rocketpy_cache, which let one test read profiles cached by an earlier one. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The lint job installs pylint unpinned and has not run green on develop since 2026-07-19. Pylint 4.x reports these four disables as useless-suppression, which fails the job with exit code 8 on every open PR regardless of what the PR changes -- two of them already appear in this PR's first CI run from 2026-08-15. These files are untouched by the caching work; this commit only removes the stale comments so the lint job can go green. It is separate so it can be dropped and landed on its own if maintainers prefer. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1137 +/- ##
===========================================
- Coverage 91.51% 91.41% -0.10%
===========================================
Files 131 132 +1
Lines 17733 18133 +400
===========================================
+ Hits 16229 16577 +348
- Misses 1504 1556 +52 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ples The five `except Exception` handlers swallowed anything, so a NameError or any other real defect in this module would have been reported to the user as an innocuous cache miss and a re-download. Probed netCDF4 1.7.4 for what it actually raises in these paths: a damaged, empty, truncated, missing or unwritable file gives OSError (or one of its subclasses); a missing variable KeyError; a missing or non-numeric attribute AttributeError; a wrong-length array or undeclared dimension ValueError; an invalid dtype or attribute type TypeError; and touching a closed dataset RuntimeError. RuntimeError is also kept on the open path because netCDF4 surfaces some HDF5-level failures that way and the installed version is not pinned. Those become two documented module constants: CACHE_OPEN_ERRORS for _open_valid_cache, whose only risky call is the open itself, and CACHE_FILE_ERRORS for the read and write bodies, which also touch the file's contents. Tests pin both halves of the contract: each of the six error types still degrades to a cache miss, while an unexpected exception now propagates from the open path and from the read path instead of being silenced. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This reverts 88aed5a ("ENH: cache downloaded atmosphere netCDF datasets (#654) (#1137)"): atmosphere_cache.py, its tests, the cache code in environment.py and the no_cache parameter of set_atmospheric_model. The cache judged a saved forecast by its age alone (6 hours for every model), was on by default, wrote to the home folder and said nothing when it was used. The next commit replaces it. The pylint comment moves that came with #1137 in motor.py, sensor.py, monte_carlo.py and test_plots.py are kept as they are. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…che (#1219) * REV: revert the atmosphere disk cache of #1137 This reverts 88aed5a ("ENH: cache downloaded atmosphere netCDF datasets (#654) (#1137)"): atmosphere_cache.py, its tests, the cache code in environment.py and the no_cache parameter of set_atmospheric_model. The cache judged a saved forecast by its age alone (6 hours for every model), was on by default, wrote to the home folder and said nothing when it was used. The next commit replaces it. The pylint comment moves that came with #1137 in motor.py, sensor.py, monte_carlo.py and test_plots.py are kept as they are. Co-Authored-By: Claude Fable 5.2 <[email protected]> * ENH: reuse a forecast in memory while its model run is current Within one Python session, a forecast downloaded with one of the latest forecast shortcuts (GFS, NAM, RAP, HRRR, AIGFS) is kept in memory. When the same model, place and launch time are asked for again, the server is asked which model run supplies the launch time, and the data is downloaded again only if that run changed. - The server is always contacted. If it cannot be reached the request fails as before, so a forecast that cannot be confirmed as current is never used. - Nothing is written to disk and there is no new parameter. - A reused Environment gets its own copies of the results. The gain is small: on GFS a repeated request takes about 2.3 s in place of about 6.5 s, since the check with the server remains. Co-Authored-By: Claude Fable 5.2 <[email protected]> * BUG: fix the forecast reuse problems found in review - Windy without a model uses its default again. - A forecast is identified by its model run and by the first and last times of the dataset, and a launch time outside them is never reused. - The elevation given by the user is not part of the request. - The attributes kept for reuse are listed, not found by comparing objects. - A model run that cannot be read means the forecast is processed again. - The wind heading and direction are rebuilt on a reuse, so they read the Environment's own data. - The 100 most recent forecasts are kept. - The reuse tests read small GFS-shaped netCDF files with the real processing. Co-Authored-By: Claude Fable 5.2 <[email protected]> * DOC: say why forecasts are kept in memory and how many Co-Authored-By: Claude Fable 5.2 <[email protected]> * TST: split the helper that writes the forecast test files Pylint counted too many statements in it. Co-Authored-By: Claude Fable 5.2 <[email protected]> * ENH: add an opt-in disk cache for forecasts and reanalysis set_atmospheric_model(..., cache=True) saves the latest forecast shortcuts and reanalysis given by a web address in the system cache folder (platformdirs). A saved forecast is used while its model run is still the latest one, or with a warning when the server cannot be reached; a saved reanalysis is always used. A warning is given on every use of saved data, and forecasts saved more than 7 days ago are deleted. The files are JSON. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Fable 5.2 <[email protected]>
Summary
.ncfiles under~/.rocketpy_cache/atmosphere(override withROCKETPY_CACHE).set_atmospheric_modelcalls reuse disk.no_cache=FalsetoEnvironment.set_atmospheric_model(and Windy processing) so callers can force a fresh download without breaking existing callers.Notes / TODOs
OPeNDAP "Best" aggregations are virtual catalogs, not downloadable files, so this PR caches the extracted profiles RocketPy already materializes after the first fetch (same end-user outcome: reruns skip the network). Remaining follow-ups:
.nc(non-OPeNDAP)Fixes #654
Test plan
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 pytest tests/unit/environment/test_atmosphere_cache.pyno_cache=Trueforces re-download