Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: climateData
Title: Utilities For Working With North American Climate Data
Version: 2.2.3.9009
Version: 2.2.3.9010
Date: 2026-04-15
Authors@R: c(
person("Alex M", "Chubaty", email = "achubaty@for-cast.ca",
Expand Down Expand Up @@ -30,7 +30,7 @@ Imports:
parallel,
parallelly,
purrr,
reproducible (>= 2.0.8),
reproducible (>= 3.2.1.9057),
RSQLite,
sf,
stats,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(calcStackLayersType)
export(checkCalcStackLayers)
export(checksums_sql)
export(climateLayers)
export(climateStackGdalOptions)
export(climateStacksByPeriod)
export(climateStacksByYear)
export(extractTimes)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# climateData (development version)

- `prepClimateLayers()` now writes multi-layer climate stacks band-interleaved in 256 x 256 tiles (`INTERLEAVE=BAND`, `TILED=YES`) through `postProcessTo(writeTo = , gdal = )`, instead of pixel-interleaved 1-row strips, so reading one year's layer no longer decompresses every layer in the file (about 8.8 s down to 0.14 s for a 90-layer stack). Values are unchanged. Needs reproducible >= 3.2.1.9057, in which `writeTo()` honours `gdal`. The options are an argument of the cached `postProcessTo()` call, so existing multi-layer stacks are rebuilt the next time `prepClimateLayers()` runs. New exported `climateStackGdalOptions()` returns them, for code that rewrites the stacks or caches `prepClimateLayers()` (e.g., canClimateData).

- The `prepClimateLayers()` cache key now includes the bodies of the functions that build the layers
(`maskInvalidPPT()`, `climateMosaicsParallel()`, the `calc*()` functions ...). Before, layers cached
before the PPT guard or before the `reproducible` 3.2.1.9060 `postProcessTo()` smoothing fix kept
Expand Down
21 changes: 21 additions & 0 deletions R/prepClimateLayers.R
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ prepClimateLayers <- function(
to = rasterToMatch,
maskTo = studyArea,
writeTo = file.path(climatePathOut, paste0(fname, ".tif")),
gdal = if (terra::nlyr(newClimRast) > 1) climateStackGdalOptions(), ## an argument, so in the Cache() key
useCache = FALSE, ## use internal cache for postProcessTo
overwrite = TRUE
) |>
Expand Down Expand Up @@ -538,3 +539,23 @@ prepClimateLayers <- function(
}
unique(types)
}

#' GDAL creation options for the multi-layer climate stacks
#'
#' A yearly layer is read on its own (one `year<YYYY>` layer out of ~90), which is slow if the
#' layers are pixel-interleaved in 1-row strips (GDAL's default): every read decompresses all
#' layers. Band-interleaved 256 x 256 tiles (GDAL's usual tile size: small enough that one layer
#' touches few tiles, big enough to compress well) let a single layer be read without touching
#' the others. Compression is left to terra's default (LZW).
#'
#' `prepClimateLayers()` writes its multi-layer stacks with these options. Code that rewrites
#' those stacks, or caches a call to `prepClimateLayers()`, can use them too: add the value to
#' the cache key (e.g., `.cacheExtra`), because `Cache()` digests a function's source, not the
#' values it refers to.
#'
#' @return character vector of GDAL creation options, for `terra::writeRaster(gdal = )` or
#' `reproducible::postProcessTo(gdal = )`.
#' @export
climateStackGdalOptions <- function() {
c("INTERLEAVE=BAND", "TILED=YES", "BLOCKXSIZE=256", "BLOCKYSIZE=256")
}
25 changes: 25 additions & 0 deletions man/climateStackGdalOptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/test-writeClimateStack.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## needs reproducible (>= 3.2.1.9057), in which `writeTo()` honours `gdal` (DESCRIPTION Imports)
test_that("postProcessTo(writeTo, gdal = climateStackGdalOptions()) writes band-interleaved tiles with identical values", {
skip_if_not_installed("terra")
skip_if_not_installed("sf")
skip_if_not_installed("withr")
r <- terra::rast(nrows = 300, ncols = 400, nlyrs = 6, xmin = 0, xmax = 400, ymin = 0, ymax = 300,
crs = "EPSG:3857")
set.seed(1)
terra::values(r) <- round(stats::rnorm(terra::ncell(r) * 6, 100, 30), 1)
names(r) <- paste0("year", 2001:2006)
to <- terra::rast(terra::ext(20, 380, 10, 280), resolution = 1, crs = "EPSG:3857")
mask <- terra::as.polygons(terra::ext(30, 300, 20, 250), crs = "EPSG:3857")

oldFile <- withr::local_tempfile(fileext = ".tif")
newFile <- withr::local_tempfile(fileext = ".tif")
## how the stacks were written before: no creation options
old <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = oldFile,
useCache = FALSE, overwrite = TRUE)
new <- reproducible::postProcessTo(r, to = to, maskTo = mask, writeTo = newFile,
gdal = climateStackGdalOptions(),
useCache = FALSE, overwrite = TRUE)
expect_match(sf::gdal_utils("info", oldFile, quiet = TRUE), "INTERLEAVE=PIXEL")
info <- sf::gdal_utils("info", newFile, quiet = TRUE)
expect_match(info, "INTERLEAVE=BAND")
expect_match(info, "Block=256x256")
expect_match(info, "COMPRESSION=LZW")

expect_identical(names(terra::rast(newFile)), names(terra::rast(oldFile)))
expect_equal(terra::values(terra::rast(newFile)), terra::values(terra::rast(oldFile)),
tolerance = 0)
expect_equal(terra::values(new), terra::values(old), tolerance = 0)
## one layer on its own is the same too
expect_equal(terra::values(terra::rast(newFile, lyrs = "year2004")),
terra::values(terra::rast(oldFile, lyrs = "year2004")), tolerance = 0)
})