Repository navigation
Write the renamed climate layers into the raster files when caching is on - #31
Open
eliotmcintire wants to merge 1 commit into
Open
eliotmcintire wants to merge 1 commit into
eliotmcintire wants to merge 1 commit into
Conversation
…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
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.
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
Init()renames layers withterra::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 onCache();Cache()restores the names to the objects, not to the files. This affected the historical files too, not only the forecast ones.writeLayerNamesInPlace(), besidewriteUpdatedLayerNames(), used for each stack that is not rewritten. It callsterra::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.Init()and theskip_if_not()intest-Init-layerNames.Rare 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.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 consecutiveInit()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.Init()with terra's "cannot open source" error. The files are in the module's own output folder, whichprepClimateLayers()writes, so I did not add a fallback; I would be happy to add one if you think it is needed.terra::update(names = TRUE), and the module's existingclimateRastersToRewrite()/writeUpdatedLayerNames()split.🤖 Generated with Claude Code
https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6