diff --git a/NEWS.md b/NEWS.md index 3769584..0ac7606 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # canClimateData (development version) +- Fixed: with caching on, the climate raster files kept the layer names `prepClimateLayers()` gave them (e.g. `MDC_historical_1991`); only the objects had the new names (`year1991`). `Init()` now writes the new names into the files it does not rewrite, in place (`terra::update()`), once, so no second copy is made. + # canClimateData 1.1.1 - Fixed: `projectedType = "hindcast"` failed in `Init()` with "object 'projected_yrs' not found", diff --git a/canClimateData.R b/canClimateData.R index 1b3bca5..d4b5491 100644 --- a/canClimateData.R +++ b/canClimateData.R @@ -135,13 +135,29 @@ doEvent.canClimateData = function(sim, eventTime, eventType) { ## the renamed layers, so both are written. With caching on, `reproducible::Cache()` restores a ## file-backed SpatRaster's layer names from its cache tags, so only `projectedClimateRasters` ## under hindcast is written: it is subset with replacement (repeated, out-of-order layers), -## which `Cache()` cannot reliably restore when the layer count is unchanged. +## which `Cache()` cannot reliably restore when the layer count is unchanged. The tags are in the +## cache, not in the files, so the stacks that are not rewritten get their names written into +## their files in place (`writeLayerNamesInPlace()`). climateRastersToRewrite <- function(projectedType, useCache = getOption("reproducible.useCache", TRUE)) { if (isFALSE(as.logical(useCache))) return(c("historical", "projected")) if (identical(projectedType, "hindcast")) "projected" else character(0) } +## Write each raster's layer names into the file behind it, in place (no copy), when the file's +## names differ; a file already carrying them is not touched, so it changes once, on the first +## run, and later cache keys that digest it stay the same. In-memory rasters and rasters from +## several files are left as they are. +writeLayerNamesInPlace <- function(rasters) { + lapply(rasters, function(x) { + src <- terra::sources(x) + if (length(src) == 1 && nzchar(src) && !identical(names(terra::rast(src)), names(x))) { + terra::update(x, names = TRUE) + } + x + }) +} + ## Write each raster to a new file so the file carries the updated layer names. ## Multi-layer stacks are written band-interleaved in 256 x 256 tiles, the layout climateData#33 ## proposes for climateData::prepClimateLayers() (its `climateStackGdalOptions()`), so reading one @@ -251,15 +267,17 @@ Init <- function(sim) { } ## save updated layer names to disk where `Cache()` won't restore them; ## see `climateRastersToRewrite()` above for why. - ## TODO: with caching on, forecast rasters are not rewritten, so their files keep the old layer - ## names; only the objects (and anything restored from the cache) have the new ones. toRewrite <- climateRastersToRewrite(P(sim)$projectedType) if ("historical" %in% toRewrite) { ## only when caching is off, so no `Cache()` here historicalClimateRasters <- writeUpdatedLayerNames(historicalClimateRasters) + } else { + historicalClimateRasters <- writeLayerNamesInPlace(historicalClimateRasters) } if ("projected" %in% toRewrite) { projectedClimateRasters <- writeUpdatedLayerNames(projectedClimateRasters) |> Cache() + } else { + projectedClimateRasters <- writeLayerNamesInPlace(projectedClimateRasters) } sim$historicalClimateRasters <- historicalClimateRasters diff --git a/tests/testthat/test-Init-layerNames.R b/tests/testthat/test-Init-layerNames.R index b747062..9b6d1dc 100644 --- a/tests/testthat/test-Init-layerNames.R +++ b/tests/testthat/test-Init-layerNames.R @@ -38,13 +38,10 @@ test_that("Init() with caching on gives the renamed layers on a first and a cach } } - ## TODO (see Init()): with caching on, forecast rasters are not rewritten, so their files keep - ## the old layer names. Once that is fixed, this check runs instead of skipping. fileNamesOK <- unlist(lapply(names(expectedNames), function(obj) { vapply(names(expectedNames[[obj]]), function(v) { identical(fileLayerNames(sims[[2]][[obj]][[v]]), expectedNames[[obj]][[v]]) }, logical(1)) })) - skip_if_not(all(fileNamesOK), "TODO: with caching on, forecast files keep the old layer names") expect_true(all(fileNamesOK)) }) diff --git a/tests/testthat/test-climateRasterRewrite.R b/tests/testthat/test-climateRasterRewrite.R index 646f0c6..8f3a9f6 100644 --- a/tests/testthat/test-climateRasterRewrite.R +++ b/tests/testthat/test-climateRasterRewrite.R @@ -77,3 +77,24 @@ test_that("Cache() restores a renamed file-backed raster's layer names without a expect_identical(terra::names(r2), "renamedLayer") expect_equal(terra::values(r2), terra::values(r1)) }) + +test_that("writeLayerNamesInPlace() writes the names into the same file, once", { + f <- withr::local_tempfile(fileext = ".tif") + terra::writeRaster(terra::rast(nrows = 2, ncols = 2, nlyrs = 2, vals = 1:8), f, overwrite = TRUE) + r <- terra::rast(f) + terra::set.names(r, c("year2001", "year2002")) + + out <- writeLayerNamesInPlace(list(MDC = r)) + expect_identical(terra::sources(out$MDC), f) ## no copy + expect_identical(names(terra::rast(f)), c("year2001", "year2002")) + expect_equal(terra::values(out$MDC), terra::values(r)) + + md5 <- tools::md5sum(f) + writeLayerNamesInPlace(list(MDC = terra::rast(f))) + expect_identical(tools::md5sum(f), md5) ## names already there: file not touched + + m <- terra::rast(nrows = 2, ncols = 2, nlyrs = 2, vals = 1:8) + terra::set.names(m, c("a", "b")) + expect_no_warning(out <- writeLayerNamesInPlace(list(m = m))) ## in memory: left as is + expect_identical(names(out$m), c("a", "b")) +})