Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

237 out-of-memory warnings/solutions #238

Merged
merged 5 commits into from
Apr 23, 2024
Merged
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
24 changes: 21 additions & 3 deletions R/downscale.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ downscale <- function(xyz, normal, gcm = NULL, historic = NULL, gcm_ts = NULL,

if (!is.null(plot)) {
if (!plot %in% vars) {
stop("The variable you wish to plot was not dowscaled. Please pass a variable listed in 'vars'")
stop("The variable you wish to plot was not downscaled. Please pass a variable listed in 'vars'")
}
## make a mask of the normals data "extent"
msk <- normal[[1]]
Expand Down Expand Up @@ -455,9 +455,27 @@ process_one_climaterast <- function(climaterast, res, xyz, timeseries = FALSE,
)
# Extract gcm bilinear interpolations
# Cropping will reduce the size of data to load in memory
climaterast <- crop(climaterast, ex, snap = "out")
climaterast <- extract(x = climaterast, y = xyz[, .(lon, lat)], method = "bilinear")

climaterast <- crop(climaterast, ex, snap = "out")
gc(reset = TRUE) ## free unused memory

climaterast <- try(extract(x = climaterast, y = xyz[, .(lon, lat)], method = "bilinear"))

## we may have run out of memory if there are MANY rasters
## attempt to get only unique raster cell values
## (i.e. xyz may be at higher res than the climaterast leading to extracting the same values many times)
if (is(climaterast, "try-error")) {
if (grepl("bad_alloc", climaterast)) {
message("System is out of memory to extract climate values for the supplied coordinates")
stop("Insufficient memory to downscale climate data for these many points/climate layers.\n",
" Try reducing number of points/layers.")
}
} else {
stop("Climate value extraction failed.",
"\n Please contact developers with a reproducible example and the error:\n",
climaterast)
}

# Create match set to match with res names
labels <- vapply(
strsplit(nm, "_"),
Expand Down
Loading