Skip to content

Commit 1f1010e

Browse files
Merge pull request #400 from Uni-of-Exeter/399-save-and-load-downscaling-results-to-disk
399 save and load downscaling results to disk
2 parents aa79858 + af797cf commit 1f1010e

File tree

3 files changed

+227
-42
lines changed

3 files changed

+227
-42
lines changed

ShinyForestry/backend/plumber_apis.R

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,14 +1157,104 @@ function(res, MAX_LIMIT_LOG_LEVEL = "debug") {
11571157

11581158
# Downscaling
11591159
if (isTRUE(RUN_DOWNSCALING)) {
1160-
msg <- "Downscaling ..."
1161-
notif(msg)
1162-
downscaling_result <- value(future(expr = { downscaling(max_limit_log_level = MAX_LIMIT_LOG_LEVEL, log_ntfy = FALSE) },
1163-
globals = list(downscaling = downscaling,
1164-
MAX_LIMIT_LOG_LEVEL = MAX_LIMIT_LOG_LEVEL),
1165-
seed = TRUE))
1166-
notif(paste(msg, "done"))
1160+
1161+
# If downscaling results already exist, load them. Otherwise, downscale and save to the downscaling_result_folder folder
1162+
downscaling_result_folder <- normalizePath(file.path(ElicitorAppFolder, "downscaling_results"))
1163+
dir.create(downscaling_result_folder, showWarnings = FALSE)
1164+
downscaling_result_file <- normalizePath(file.path(downscaling_result_folder, "downscaling_result.RData"))
1165+
downscaling_mesoclimate_list_file <- normalizePath(file.path(downscaling_result_folder, "downscaling_mesoclimate_list.RData"))
1166+
1167+
if (file.exists(downscaling_result_file) &&
1168+
file.exists(downscaling_mesoclimate_list_file)) {
1169+
1170+
msg <- "Loading downscaling results ..."
1171+
notif(msg, log_level = "debug")
1172+
1173+
downscaling_result <- value(future(get(load(downscaling_result_file))))
1174+
temp <- value(future(get(load(downscaling_mesoclimate_list_file))))
1175+
downscaling_mesoclimate_list <- lapply(temp, function(x) {
1176+
1177+
lapply(x, function(y) {
1178+
if ("PackedSpatRaster" %in% class(y)) {
1179+
return(terra::unwrap(y))
1180+
} else {
1181+
return(y)
1182+
}
1183+
})
1184+
1185+
})
1186+
rm(temp)
1187+
1188+
notif(paste(msg, "done"), log_level = "debug")
1189+
1190+
} else {
1191+
1192+
msg <- "Downscaling ..."
1193+
notif(msg)
1194+
downscaling_results <- value(future(expr = { downscaling(max_limit_log_level = MAX_LIMIT_LOG_LEVEL,
1195+
log_ntfy = FALSE) },
1196+
globals = c("downscaling",
1197+
"MAX_LIMIT_LOG_LEVEL",
1198+
"get_backend_folder_save_data",
1199+
"get_foldersource",
1200+
"notif"),
1201+
seed = TRUE))
1202+
downscaling_result <- downscaling_results$results
1203+
# The result requires terra stuff which cannot be passed by the future unless terra::wrap
1204+
# So now, it needs to terra::unwrap
1205+
downscaling_mesoclimate_list <- lapply(downscaling_results$mesoclimate_list_wrapped, function(x) {
1206+
1207+
lapply(x, function(y) {
1208+
if ("PackedSpatRaster" %in% class(y)) {
1209+
return(terra::unwrap(y))
1210+
} else {
1211+
return(y)
1212+
}
1213+
})
1214+
1215+
})
1216+
notif(paste(msg, "done"))
1217+
1218+
msg <- "Saving downscaling normal results to disk ..."
1219+
notif(msg)
1220+
save(downscaling_result, file = downscaling_result_file)
1221+
notif(paste(msg, "done"))
1222+
1223+
msg <- "Saving downscaling mesoclimates to disk ..."
1224+
notif(msg, log_level = "debug")
1225+
mesoclimate_list_wrapped <- downscaling_results$mesoclimate_list_wrapped
1226+
save(mesoclimate_list_wrapped, file = downscaling_mesoclimate_list_file)
1227+
rm(mesoclimate_list_wrapped)
1228+
notif(paste(msg, "done"), log_level = "debug")
1229+
1230+
rm(downscaling_results)
1231+
# Alternative by David Baker TODO: needs testing
1232+
# a <- lapply(downscaled_results_original_grid_list, function(x) {
1233+
#
1234+
# lapply(x, function(y) {
1235+
# if ("PackedSpatRaster" %in% class(y)) {
1236+
# return(terra::unwrap(y))
1237+
# } else {
1238+
# return(y)
1239+
# }
1240+
# })
1241+
#
1242+
# })
1243+
# library(addTreesBiodiv)
1244+
#
1245+
# # Compute bioclimate variables per time period from mesoclimate output
1246+
# bioclimate_meso <- mesoclimate_to_bioclimate(mesoclimate_list)
1247+
#
1248+
# outpath <- file.path("mesoclim_rast")
1249+
# dir.create(outpath)
1250+
# for (i in 1:length(bioclimate_meso)) {
1251+
# r_i <- bioclimate_meso[[i]]
1252+
# v_i <- sub("^([^_]+)_.*$", "\\1", names(r_i)[1])
1253+
# writeRaster(r_i, paste0(outpath, "/", v_i, ".tif"))
1254+
# }
1255+
}
11671256
}
1257+
11681258
notif(paste("Waiting for", normalizePath(file.path(ElicitorAppFolder, "decision_units.json"))))
11691259
if (!file.exists(normalizePath(file.path(ElicitorAppFolder, "decision_units.json")))) {
11701260
res$status <- 403
@@ -1387,7 +1477,7 @@ function(res, MAX_LIMIT_LOG_LEVEL = "debug") {
13871477

13881478
# Outcomes
13891479
notif(paste("Waiting for", normalizePath(file.path(ElicitorAppFolder, "outcomes.json"))))
1390-
while (!file.exists(normalizePath(file.path(ElicitorAppFolder, "outcomes.json")))) {
1480+
if (!file.exists(normalizePath(file.path(ElicitorAppFolder, "outcomes.json")))) {
13911481
res$status <- 403
13921482
return("Please upload outcomes.json")
13931483
}

ShinyForestry/backend/trigger_plumber_for_dev.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ if (file.exists(file.path(getwd(), "ShinyForestry", "backend", "plumber_apis.R")
1313
plumb(path) |>
1414
pr_set_debug(debug = TRUE) |>
1515
pr_run(port = port,
16-
host = "0.0.0.0")
16+
host = "0.0.0.0",
17+
docs = isFALSE(interactive()))

0 commit comments

Comments
 (0)