RS Minerve can plot simulation results and print result files for selections of simulation results. These selections can be edited in the user interface, saved to a chk file and also loaded from chk files. As the manual editing of the plot selection can be tedious for large models, it can be useful to automate the reading and writing of the chk files in R. The present vignette demonstrates how to do this.
filepath <- if (file.exists("../tests/testthat/test_selection.chk")) {
"../tests/testthat/test_selection.chk"
} else {
"https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/main/tests/testthat/test_selection.chk"
}
xmldata <- readSelectionCHK(filepath)
The name of the selection:
selection_name <- xmldata[[1]]
print(selection_name)
#> [1] "New selection"
The description of the objects in the selection:
selection_data <- xmldata[[2]]
print(selection_data)
#> # A tibble: 29 × 4
#> Model Object ID Variable
#> <chr> <chr> <chr> <chr>
#> 1 Model Koksu Source QSpring Kichkinesay - QUp (m3/s)
#> 2 Model Koksu Comparator Comparator 1 QReference (m3/s)
#> 3 Model Koksu Comparator Comparator 1 QSimulation (m3/s)
#> 4 Model Koksu HBV92 Koksu downstream_eb1 Qr (m3/s)
#> 5 Model Koksu HBV92 Koksu downstream_eb1 Qu (m3/s)
#> 6 Model Koksu HBV92 Koksu downstream_eb1 Ql (m3/s)
#> 7 Model Koksu HBV92 Koksu downstream_eb1 ETR (m/s)
#> 8 Model Koksu HBV92 Koksu downstream_eb1 Peq (m/s)
#> 9 Model Koksu HBV92 Koksu downstream_eb1 SWE (m)
#> 10 Model Koksu HBV92 Koksu downstream_eb1 WH (-)
#> # … with 19 more rows
New selection name
new_selection_name <- "SOCONT Qtot"
Edit the selection to include all data for variable Qtot of all
SOCONT objects in the model. To get a list of all SOCONT objects the
user can read a parameter file or a file of initial conditions of the
model. Another possibility is to read the gis layer with the
hydrological response units used for the semi-automatic model creation
in RSMinerve.
Let’s read the names of the SOCONT objects from the parameter file of
one of the tutorials. The procedure is, of course, analogous for any
other RSMinerve Object for which data should be extracted.
parfilepath <- if (file.exists("../tests/testthat/Tutorial_Parameters.txt")) {
"../tests/testthat/Tutorial_Parameters.txt"
} else {
"https://raw.githubusercontent.com/hydrosolutions/RSMinerveR/main/tests/testthat/Tutorial_Parameters.txt"
}
par <- readRSMParameters(parfilepath) |>
dplyr::filter(Object == "SOCONT")
Object_IDs <- unique(par$Name)
print(Object_IDs)
#> [1] "SOCONT 1" "SOCONT 2" "SOCONT 3"
Now construct a tibble of the variables for each object we’d like to select results from.
new_selection <- tibble::tibble(
Model = rep("Tutorial_Model", length(Object_IDs)),
Object = rep("SOCONT", length(Object_IDs)),
ID = Object_IDs,
Variable = rep("Qtot (m3/s)")
)
print(new_selection)
#> # A tibble: 3 × 4
#> Model Object ID Variable
#> <chr> <chr> <chr> <chr>
#> 1 Tutorial_Model SOCONT SOCONT 1 Qtot (m3/s)
#> 2 Tutorial_Model SOCONT SOCONT 2 Qtot (m3/s)
#> 3 Tutorial_Model SOCONT SOCONT 3 Qtot (m3/s)
Now write the data to a new file. In the codechuck below you will
need to replace <filepath>
with an actual path to a
file to write.
writeSelectionCHK(<filepath>, new_selection, new_selection_name)