Skip to content

prepClimateLayers: write stacks band-interleaved and tiled (needs reproducible#622) - #29

Closed
eliotmcintire wants to merge 4 commits into
developmentfrom
perf/band-tiled-climate-stacks
Closed

eliotmcintire wants to merge 4 commits into
developmentfrom
perf/band-tiled-climate-stacks

Conversation

@eliotmcintire

@eliotmcintire eliotmcintire commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Climate data files are now laid out on disk so that reading one year is fast, instead of having to unpack all years every time. Reading one year's climate goes from about 9 seconds to a fraction of a second.

Affects: speed of climate-driven runs; existing multi-year climate files are rebuilt once on the next run, with identical values.

Technical details

prepClimateLayers() (R/prepClimateLayers.R, the postProcessTo(writeTo = ) call) wrote the yearly stacks with GDAL defaults, pixel-interleaved in 1-row compressed strips, so reading one year's layer decompresses all 90 (about 8.8 s per variable; ~25 s per simulated year in climateYear). Multi-layer stacks are now written with gdal = climateStackGdalOptions() (INTERLEAVE=BAND, TILED=YES, 256x256 blocks, LZW kept), and the same layer reads in about 0.14 s. Needs reproducible >= 3.2.1.9057 (reproducible#622), now the Imports floor.

After review: climateStackGdalOptions() is exported, so canClimateData can use it in its own rewrite and cache key instead of a copy. gdal is an argument of the cached postProcessTo() call, so it is already in that Cache() key (checked with CacheDigest()); the extra digest4cache entry is removed, so single-layer stacks are not rebuilt for it. Version 2.2.3.9010. Merged with development (#30, #31).

The test checks INTERLEAVE=BAND, 256x256 blocks, LZW and values identical at tolerance 0 to the old layout. Local suite: 62 passed, 0 failed, 1 skipped; the 2 errors are the prepClimateLayers tests that need Google Drive credentials, which this session does not have.

🤖 Generated with Claude Code

@eliotmcintire eliotmcintire changed the title prepClimateLayers: write stacks band-interleaved and tiled prepClimateLayers: write stacks band-interleaved and tiled (needs reproducible#622) Oct 1, 2026
@eliotmcintire
eliotmcintire marked this pull request as draft October 1, 2026 02:12
…rop the test's skip-if-ignored probe

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CwcjqqK59FmTJscyi7xUqv
@eliotmcintire
eliotmcintire marked this pull request as ready for review October 1, 2026 04:13
@eliotmcintire

Copy link
Copy Markdown
Collaborator Author

@achubaty This code change changes the way the rasters are stored. This allows them to be read back downstream vastly faster in the cases that I am needing in fireSense --> estimated 350-500s reads of one layer drops to sub second. With no change in actual data.

@achubaty

achubaty commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

The change does what it says: the stacks prepClimateLayers() writes are band-interleaved in 256 x 256 tiles, values unchanged, and the test checks both.
In canClimateData, though, the layout only survives when the module does not rewrite the stacks, so the speed-up depends on how the module is run.

In canClimateData

I ran canClimateData's real Init() with a prepClimateLayers() stand-in that writes 1000 x 1000 stacks through postProcessTo(writeTo =, gdal = .climateStackGdalOptions), using reproducible 3.2.1.9057.
The code was development plus PredictiveEcology/canClimateData#15, PredictiveEcology/canClimateData#16 and PredictiveEcology/canClimateData#17.
Each line is the file behind the 90-layer projected MDC raster:

run file layout read one layer
forecast, caching on (first run and cache hit) BAND, 256x256 tiles 0.04 s
forecast, reproducible.useCache = FALSE PIXEL, 1-row strips (*_updated.tif) 1.60 s
hindcast, caching off or on PIXEL, 1-row strips (*_updated.tif) ~2.0 s

Cache() keeps the layout: it stores and restores a copy of the file.
The loss comes from canClimateData's rewrite of renamed layers (terra::writeRaster() with no gdal).
That rewrite runs whenever caching is off (PredictiveEcology/canClimateData#15), and for projected rasters under hindcast.
Passing the same creation options to that writeRaster() gives BAND, 256x256 and 0.03 to 0.07 s in all four runs.
PredictiveEcology/canClimateData#15 now does this, with a copy of the options so it does not depend on this PR.

Suggestions for this PR

  • Export the creation options (e.g., climateStackGdalOptions()), so canClimateData can use climateData's rather than its copy, and add them to its own Cache() key.
    canClimateData caches the whole prepClimateLayers() call, and Cache() keys a function on its source, not on the values it refers to.
    This PR changes the function body, so the first run after updating rebuilds.
    A later change to .climateStackGdalOptions alone would not reach the module's cache, which would keep returning the old files.
  • Bump the version (2.2.3.9007 is unchanged), so canClimateData can require the version that writes tiled stacks.
  • .cacheExtra = c(..., gdalOptions = .climateStackGdalOptions) looks redundant: gdal is an argument of the cached postProcessTo() call, and arguments passed through ... are part of the Cache() key (checked).
    As written, it also forces a one-time rebuild of the single-layer stacks, which do not use the options.
  • The PR description is out of date: it still says it waits for writeTo/postProcessTo: honour gdal creation options reproducible#622 and that no version floor can be set, and that the test skips with an older reproducible.
    writeTo/postProcessTo: honour gdal creation options reproducible#622 is merged, DESCRIPTION now requires reproducible (>= 3.2.1.9057), and the test has no such skip.

@eliotmcintire eliotmcintire added enhancement New feature or request backwards compatible Existing code and results keep working unchanged labels Oct 6, 2026
…drop redundant cache-key entry, bump to 2.2.3.9010

The gdal options are an argument of the cached postProcessTo() call, so they
are already in its Cache() key; the extra digest4cache entry only forced a
rebuild of single-layer stacks. climateStackGdalOptions() is exported so
canClimateData can use these options instead of a copy, and add them to its
own cache key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eliotmcintire

Copy link
Copy Markdown
Collaborator Author

@achubaty Thanks, all four are done in d61ba4d. climateStackGdalOptions() is now exported, so canClimateData#15 can call it instead of keeping a copy. The version is 2.2.3.9010. I removed the extra cache-key entry, after confirming with CacheDigest() that gdal already changes the key. The description is updated. I also merged development (#30, #31), which conflicted.

🤖 Generated with Claude Code

eliotmcintire added a commit that referenced this pull request Oct 16, 2026
Conflicts resolved by hand: R/prepClimateLayers.R: PR side (cache-key gdalOptions line dropped in review). NEWS: both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014G18Mik3fH4z5M5CyMSUS6
@eliotmcintire

Copy link
Copy Markdown
Collaborator Author

Closing: this change is carried on modsForFireSense and offered upstream together with our other FireSense fixes in #33. That PR lists it; the discussion here stays for reference.

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 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants