|
| 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 | + |
0 commit comments