Skip to content

Write the renamed climate layers into the raster files when caching is on - #31

Open
eliotmcintire wants to merge 1 commit into
developmentfrom
fix/layer-names-on-disk-cached
Open

eliotmcintire wants to merge 1 commit into
developmentfrom
fix/layer-names-on-disk-cached

Conversation

@eliotmcintire

Copy link
Copy Markdown
Collaborator

Thank you, Alex, for the layer-name tests in 1.1.1 and for marking this case so clearly with a TODO. It made the fix easy to find. When caching is on, the climate raster files on disk kept their original layer names (for example MDC_historical_1991), while the objects in the simulation had the new ones (year1991). This PR writes the new names into those files as well, without making a second copy. I hope it fits what you had in mind, and I would be glad to change anything you would prefer done differently.

Affects: with caching on, anyone who opens a climate raster file directly now sees the same layer names as the module's objects; the values, the objects and the cache behaviour are unchanged.

Technical details
  • Cause: Init() renames layers with terra::set.names(), which changes the object only. climateRastersToRewrite() (canClimateData.R:151) rewrites nothing for a forecast with caching on (and only the projected stack for a hindcast), relying on Cache(); Cache() restores the names to the objects, not to the files. This affected the historical files too, not only the forecast ones.
  • Fix: new writeLayerNamesInPlace(), beside writeUpdatedLayerNames(), used for each stack that is not rewritten. It calls terra::update(x, names = TRUE), which writes the names into the existing file (no copy, no .aux.xml), and only when the file's names differ, so a file changes once, on the first run, and cache keys that digest it stay the same afterwards. In-memory rasters and rasters from several files are left as they are.
  • The TODO in Init() and the skip_if_not() in test-Init-layerNames.R are removed, so that test now checks the files on the cached run. A new test checks the helper: same file, names written, values kept, file untouched on a second call, in-memory rasters left alone.
  • Verified: on development, the un-skipped test fails without the fix (1 of 79) and passes with it; full suite 85 tests, 0 failed, 0 skipped. Three consecutive Init() runs with caching on: prepClimateLayers() runs once, then two cache hits; objects and files carry the new names; file checksums identical on runs 1 to 3.
  • One case for your judgement: a read-only file that still has the old names would now stop Init() with terra's "cannot open source" error. The files are in the module's own output folder, which prepClimateLayers() writes, so I did not add a fallback; I would be happy to add one if you think it is needed.
  • Reuses: terra::update(names = TRUE), and the module's existing climateRastersToRewrite() / writeUpdatedLayerNames() split.

🤖 Generated with Claude Code

https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6

…aster files

Renaming in Init() (terra::set.names) changes only the objects; Cache() restores the names to
the objects, not the files, so the stacks climateRastersToRewrite() does not rewrite kept
prepClimateLayers()'s names on disk. New writeLayerNamesInPlace() writes them into each file in
place (terra::update(names = TRUE), no copy), only when the file's names differ, so a file
changes once and later cache keys stay stable. Removes the TODO in Init() and the skip in
test-Init-layerNames.R; adds a test for the helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6
@eliotmcintire eliotmcintire added bug Something isn't working backwards compatible Existing code and results keep working unchanged labels Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backwards compatible Existing code and results keep working unchanged bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant