Commit 0f93d593 authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '15-get_result-add-a-t-column-for-the-simulation-time' into 'main'

Resolve "get_result: add a `t` column for the simulation time"

Closes #15

See merge request !11
1 merge request!11Resolve "get_result: add a `t` column for the simulation time"
Pipeline #34271 passed with stage
in 2 minutes and 32 seconds
Showing with 40 additions and 4 deletions
+40 -4
......@@ -4,7 +4,15 @@
#' @param filters [character] conditions to select columns in result table, see details
#' @param m [matrix] of results produced by [read_bin_result_matrix]
#'
#' @return [matrix] of results with columns selected by `filters`.
#' @return [matrix] of results with a first column "t" with the simulation time
#' in seconds followed by columns selected by `filters`.
#'
#' Column names are a concatenation of nested SIC model elements separated by
#' the character "|" and numbered after the character ":". The variable is
#' represented by the item "var".
#' For example, water elevation in the first section of the first reach is:
#' "bf:1|sn:1|var:Z".
#'
#' @export
#' @import magrittr
#'
......@@ -26,6 +34,24 @@ get_result <- function(cfg,
df_col %<>% tidyquery::query(paste("SELECT * WHERE", filters))
}
m <- m[, df_col$col, drop = FALSE]
# Compute time column
x <- read_xml(cfg$project$path)
xpath <-
sprintf("/Reseau/Flu[@nScenario=%d]/ListeRes/Res[@nVar=%d]",
scenario,
variant)
x_res <- xml_find_first(x, xpath)
attrs <- paste0("Tps", c("Debut", "Pas", "Sauv", "Fin"))
names(attrs) <- attrs
time_prms <- sapply(attrs, function(attr) {
as.numeric(xml_attr(x_res, attr))
})
tms <- seq(from = time_prms["TpsDebut"],
to = time_prms["TpsFin"],
by = time_prms["TpsPas"] * time_prms["TpsSauv"])
# set column names
column_names <- sapply(seq_len(nrow(df_col)),
function(i) {
df_col$col <- NULL
......@@ -40,7 +66,10 @@ get_result <- function(cfg,
cols[sapply(cols, is.null)] <- NULL
paste(cols, collapse = "|")
})
colnames(m) <- column_names
m <- cbind(tms, m)
colnames(m) <- c("t", column_names)
return(m)
}
......
......@@ -24,7 +24,14 @@ get_result(
\item{m}{\link{matrix} of results produced by \link{read_bin_result_matrix}}
}
\value{
\link{matrix} of results with columns selected by \code{filters}.
\link{matrix} of results with a first column "t" with the simulation time
in seconds followed by columns selected by \code{filters}.
Column names are a concatenation of nested SIC model elements separated by
the character "|" and numbered after the character ":". The variable is
represented by the item "var".
For example, water elevation in the first section of the first reach is:
"bf:1|sn:1|var:Z".
}
\description{
Get a selection of variables from a simulation result
......
......@@ -7,5 +7,5 @@ test_that("get_result returns a matrix with correct colnames", {
result <- get_result(cfg, 1, filters = c("bf=4", "var='Z'"))
expect_true(is.matrix(result))
expect_type(result, "double")
expect_equal(colnames(result), sprintf("bf:4|sn:%d|var:Z", 1:4))
expect_equal(colnames(result), c("t", sprintf("bf:4|sn:%d|var:Z", 1:4)))
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment