Skip to content

Commit 11f0c32

Browse files
committed
Add function for all results for one connection
1 parent b9f873c commit 11f0c32

4 files changed

Lines changed: 172 additions & 56 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export("%>%")
44
export(add_overflow_events_and_waterbalance)
55
export(find_single_param_variations)
6+
export(get_simulation_results_all)
67
export(get_simulation_results_optim)
78
export(h5_read_values)
89
export(h5_validate_write)

R/get_simulation_results_all.R

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#' Read Raindrop optimisation simulation results (all) from HDF5
2+
#'
3+
#' Reads per-run result files (HDF5) for both the *measure element* and the
4+
#' *connected area* results and returns a named list (one entry per simulation).
5+
#'
6+
#' For each simulation name (e.g. `"s00001"`), the function resolves the run
7+
#' directory via \code{kwb.utils::resolve(path_list, dir_target = s_name)} and
8+
#' then loads standard result groups from two HDF5 files:
9+
#' \itemize{
10+
#' \item \strong{element}: \code{Metainfo}, \code{Raten}, \code{Wasserbilanz}, \code{Zustandsvariablen}
11+
#' \item \strong{connected_area}: \code{Metainfo}, \code{Raten}, \code{Wasserbilanz}, \code{Zustandsvariablen}
12+
#' }
13+
#'
14+
#' If either of the expected HDF5 files is missing for a run, the corresponding
15+
#' list entry will be \code{NULL}.
16+
#'
17+
#' @param paths A list of path definitions. Used for messaging and expected to
18+
#' contain \code{file_results_hdf5_element} and \code{file_results_hdf5_flaeche}
19+
#' (file names). Note: within the loop, \code{paths} is overwritten by the
20+
#' resolved paths for the current simulation run.
21+
#' @param path_list A list passed to \code{kwb.utils::resolve()} to generate
22+
#' run-specific paths (must yield \code{path_results_hdf5_element},
23+
#' \code{path_results_hdf5_flaeche}, and \code{dir_target_output}).
24+
#' @param simulation_names Character vector of simulation run identifiers
25+
#' (e.g. \code{c("s00001", "s00002")}).
26+
#' @param debug print debug messages (default: TRUE)
27+
#' @return A named list with one entry per \code{simulation_names}. Each entry is
28+
#' either \code{NULL} (missing files) or a nested list:
29+
#' \describe{
30+
#' \item{element}{\describe{
31+
#' \item{meta}{Data.frame/list of scalar metadata (from \code{Metainfo}).}
32+
#' \item{rates}{Time series table (from \code{Raten}).}
33+
#' \item{water_balance}{Scalars table (from \code{Wasserbilanz}).}
34+
#' \item{states}{Time series table (from \code{Zustandsvariablen}).}
35+
#' }}
36+
#' \item{connected_area}{Same structure as \code{element}, read from the area HDF5.}
37+
#' }
38+
#'
39+
#' @details
40+
#' The function uses \code{hdf5r::H5File$new(..., mode = "r")} to open the files.
41+
#' The HDF5 handles are not explicitly closed; depending on your workflow, you
42+
#' may want to close them (see \code{hdf5r::H5File$close_all()} / \code{$close()}).
43+
#'
44+
#' @seealso
45+
#' \code{\link[kwb.utils]{resolve}},
46+
#' \code{\link[kwb.raindrop]{read_hdf5_scalars}},
47+
#' \code{\link[kwb.raindrop]{read_hdf5_timeseries}}
48+
#'
49+
#' @export
50+
#' @importFrom stats setNames
51+
#' @importFrom hdf5r H5File
52+
get_simulation_results_all <- function(paths,
53+
path_list,
54+
simulation_names,
55+
debug = TRUE) {
56+
57+
message(sprintf("Reading results files ('%s') for %d model runs",
58+
paste0(c(paths$file_results_hdf5_element, paths$file_results_hdf5_flaeche), collapse = "|"),
59+
length(simulation_names)))
60+
stats::setNames(lapply(simulation_names, function(s_name) {
61+
62+
63+
s_id <- s_name %>% stringr::str_remove("s") %>% as.integer()
64+
65+
paths <- kwb.utils::resolve(path_list, dir_target = s_name)
66+
67+
if(all(file.exists(c(paths$path_results_hdf5_element,
68+
paths$path_results_hdf5_flaeche)))) {
69+
70+
kwb.utils::catAndRun(messageText = sprintf("(%d/%d)) Reading results files for model run %s",
71+
which(simulation_names == s_name),
72+
length(simulation_names),
73+
paths$dir_target_output),
74+
expr = {
75+
76+
# "a" = read/write (legt an, falls nicht da); alternativ "r+" = read/write, aber nicht neu anlegen
77+
res_hdf5_element <- hdf5r::H5File$new(paths$path_results_hdf5_element, mode = "r")
78+
res_hdf5_flaeche <- hdf5r::H5File$new(paths$path_results_hdf5_flaeche, mode = "r")
79+
res_hdf5_verschaltungen <- hdf5r::H5File$new(paths$path_results_hdf5_verschaltungen, mode = "r")
80+
81+
hdf5_results <- list(
82+
element = list(
83+
meta = kwb.raindrop::read_hdf5_scalars(res_hdf5_element[["Metainfo"]], numeric_only = FALSE),
84+
rates = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Raten"]]),
85+
water_balance = kwb.raindrop::read_hdf5_scalars(res_hdf5_element[["Wasserbilanz"]]),
86+
additional_evapotranspiration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zusaetzliche Variablen Evapotranspiration"]]),
87+
additional_infiltration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zusaetzliche Variablen Infiltration"]]),
88+
states = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zustandsvariablen"]])),
89+
connected_area = list(
90+
meta = kwb.raindrop::read_hdf5_scalars(res_hdf5_flaeche[["Metainfo"]], numeric_only = FALSE),
91+
rates = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Raten"]]),
92+
water_balance = kwb.raindrop::read_hdf5_scalars(res_hdf5_flaeche[["Wasserbilanz"]]),
93+
additional_evapotranspiration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zusaetzliche Variablen Evapotranspiration"]]),
94+
additional_infiltration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zusaetzliche Variablen Infiltration"]]),
95+
states = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zustandsvariablen"]])),
96+
connections = kwb.raindrop::read_hdf5_connections(res_hdf5_verschaltungen)
97+
)
98+
99+
hdf5_results
100+
},
101+
dbg = debug)}}), nm = simulation_names)
102+
}
103+

man/get_simulation_results_all.Rd

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/workflow_120min_40mm.Rmd

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ knitr::opts_chunk$set(
1616
# is_ci <- tolower(Sys.getenv("CI")) %in% c("true", "1", "yes")
1717
#
1818
# is_ghactions <- is_ghactions || is_ci
19-
is_ghactions <- TRUE
19+
is_ghactions <- FALSE
2020
2121
```
2222

@@ -26,7 +26,7 @@ is_ghactions <- TRUE
2626
library(kwb.raindrop)
2727
2828
path_list <- list(
29-
root_path = "C:/kwb/projects/raindrop/2025-12-19_Raindrop_Daten",
29+
root_path = "D:/raindrop/2025-12-19_Raindrop_Daten",
3030
dir_base = "<root_path>/Optimierungsfall",
3131
dir_exe = "<root_path>/Berechnungskern",
3232
dir_input = "<root_path>/Optimierungsfall/models/120min_40mm/input",
@@ -233,66 +233,17 @@ errors_df <- lapply(simulation_names, function(s_name) {
233233
### Analyse Results
234234

235235
```{r analyse_results, eval = !is_ghactions}
236-
import_results_from_rds <- FALSE
237236
debug <- TRUE
238237
paths <- kwb.utils::resolve(path_list, dir_target = sprintf("s%05d", i = 1))
239238
240239
#simulation_names <- basename(fs::dir_ls(paths$dir_output))
241240
simulation_names <- scenarios_with_single_parameter_variation
242241
243-
simulation_results <- if(import_results_from_rds == FALSE) {
244-
245-
message(sprintf("Reading results files ('%s') for %d model runs",
246-
paste0(c(paths$file_results_hdf5_element, paths$file_results_hdf5_flaeche,
247-
stringr::str_replace(paths$file_results_hdf5_verschaltungen,
248-
"[0-9]+", "xxxxx")), collapse = "|"),
249-
length(simulation_names)))
250-
stats::setNames(lapply(simulation_names, function(s_name) {
251-
252-
253-
s_id <- s_name %>% stringr::str_remove("s") %>% as.integer()
254-
255-
paths <- kwb.utils::resolve(path_list, dir_target = s_name)
256-
257-
if(all(file.exists(c(paths$path_results_hdf5_verschaltungen,
258-
paths$path_results_hdf5_element,
259-
paths$path_results_hdf5_flaeche)))) {
260-
261-
kwb.utils::catAndRun(messageText = sprintf("(%d/%d)) Reading results files for model run %s",
262-
which(simulation_names == s_name),
263-
length(simulation_names),
264-
paths$dir_target_output),
265-
expr = {
266-
267-
# "a" = read/write (legt an, falls nicht da); alternativ "r+" = read/write, aber nicht neu anlegen
268-
res_hdf5_element <- hdf5r::H5File$new(paths$path_results_hdf5_element, mode = "r")
269-
res_hdf5_flaeche <- hdf5r::H5File$new(paths$path_results_hdf5_flaeche, mode = "r")
270-
res_hdf5_verschaltungen <- hdf5r::H5File$new(paths$path_results_hdf5_verschaltungen, mode = "r")
271-
272-
hdf5_results <- list(
273-
element = list(
274-
meta = kwb.raindrop::read_hdf5_scalars(res_hdf5_element[["Metainfo"]], numeric_only = FALSE),
275-
rates = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Raten"]]),
276-
water_balance = kwb.raindrop::read_hdf5_scalars(res_hdf5_element[["Wasserbilanz"]]),
277-
additional_evapotranspiration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zusaetzliche Variablen Evapotranspiration"]]),
278-
additional_infiltration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zusaetzliche Variablen Infiltration"]]),
279-
states = kwb.raindrop::read_hdf5_timeseries(res_hdf5_element[["Zustandsvariablen"]])),
280-
connected_area = list(
281-
meta = kwb.raindrop::read_hdf5_scalars(res_hdf5_flaeche[["Metainfo"]], numeric_only = FALSE),
282-
rates = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Raten"]]),
283-
water_balance = kwb.raindrop::read_hdf5_scalars(res_hdf5_flaeche[["Wasserbilanz"]]),
284-
additional_evapotranspiration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zusaetzliche Variablen Evapotranspiration"]]),
285-
additional_infiltration = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zusaetzliche Variablen Infiltration"]]),
286-
states = kwb.raindrop::read_hdf5_timeseries(res_hdf5_flaeche[["Zustandsvariablen"]])),
287-
connections = kwb.raindrop::read_hdf5_connections(res_hdf5_verschaltungen)
288-
)
289-
290-
hdf5_results
291-
},
292-
dbg = debug)}}), nm = simulation_names)
293-
} else {
294-
readRDS(file = "../simulation_results.Rds")
295-
}
242+
simulation_results <- kwb.raindrop::get_simulation_results_all(
243+
paths = paths,
244+
path_list = path_list,
245+
simulation_names = simulation_names,
246+
debug = debug)
296247
297248
298249
pdff <- "simulation_results_per_scenario_120min-40mm.pdf"

0 commit comments

Comments
 (0)