diff --git a/DESCRIPTION b/DESCRIPTION index 1976d8d..241d0db 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", @@ -30,7 +30,7 @@ Imports: parallel, parallelly, purrr, - reproducible (>= 2.0.8), + reproducible (>= 3.2.1.9057), RSQLite, sf, stats, diff --git a/NAMESPACE b/NAMESPACE index 23e8fc2..8f7f11c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(calcStackLayersType) export(checkCalcStackLayers) export(checksums_sql) export(climateLayers) +export(climateStackGdalOptions) export(climateStacksByPeriod) export(climateStacksByYear) export(extractTimes) diff --git a/NEWS.md b/NEWS.md index 4944f42..ac0576e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/prepClimateLayers.R b/R/prepClimateLayers.R index 652a536..264e28b 100644 --- a/R/prepClimateLayers.R +++ b/R/prepClimateLayers.R @@ -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 ) |> @@ -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` 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") +} diff --git a/man/climateStackGdalOptions.Rd b/man/climateStackGdalOptions.Rd new file mode 100644 index 0000000..0883bad --- /dev/null +++ b/man/climateStackGdalOptions.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/prepClimateLayers.R +\name{climateStackGdalOptions} +\alias{climateStackGdalOptions} +\title{GDAL creation options for the multi-layer climate stacks} +\usage{ +climateStackGdalOptions() +} +\value{ +character vector of GDAL creation options, for \code{terra::writeRaster(gdal = )} or +\code{reproducible::postProcessTo(gdal = )}. +} +\description{ +A yearly layer is read on its own (one \verb{year} 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). +} +\details{ +\code{prepClimateLayers()} writes its multi-layer stacks with these options. Code that rewrites +those stacks, or caches a call to \code{prepClimateLayers()}, can use them too: add the value to +the cache key (e.g., \code{.cacheExtra}), because \code{Cache()} digests a function's source, not the +values it refers to. +} diff --git a/tests/testthat/test-writeClimateStack.R b/tests/testthat/test-writeClimateStack.R new file mode 100644 index 0000000..7d5eaa3 --- /dev/null +++ b/tests/testthat/test-writeClimateStack.R @@ -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) +})