Commit ae1a6a8f authored by Dorchies David's avatar Dorchies David
Browse files

feat(saveFlowDB): compute indicators over several periods

Refs #12
parent 4e64e3ab
Pipeline #31995 passed with stages
in 29 minutes and 48 seconds
......@@ -3,12 +3,13 @@
#' @param path [character] the location used to save the database
#' @param InputsModel `GRiwrmInputsModel` object. See [CreateInputsModel.BasinsObs]
#' @param OutputsModel `GriwrmOutputsModel` object. See [RunModel.BasinsObs]
#' @param indicators_periods [list] containing item with 2-length [POSIXct] for starting and ending date of each period used for calculating hydrological indicators
#' @template param_cfg
#'
#' @return `TRUE` if the last copy successed
#' @export
#'
saveFlowDB <- function(path, InputsModel, OutputsModel, cfg = loadConfig()) {
saveFlowDB <- function(path, InputsModel, OutputsModel, indicators_periods = NULL, cfg = loadConfig()) {
if (cfg$data$write_results) {
dir.create(path, showWarnings = FALSE, recursive = TRUE)
......@@ -41,20 +42,19 @@ saveFlowDB <- function(path, InputsModel, OutputsModel, cfg = loadConfig()) {
readr::write_tsv(Qcontrib, file.path(path, "ts_Qcontrib.tsv"))
# Global hydrological indicators
indicators <- c("QA",
"VCN10_2", "VCN30_2", "QMNA2",
"VCN10_5", "VCN30_5", "QMNA5",
"VCN10_10", "VCN30_10", "QMNA10",
"QJXA2", "QJXA10", "QJXA20")
lInd <- lapply(
indicators,
calcQcaract,
Q = attr(OutputsModel, "Qm3s")
)
mInd <- do.call(cbind, lInd)
colnames(mInd) <- indicators
dfInd <- cbind(data.frame(Id = rownames(mInd)), mInd)
readr::write_tsv(dfInd, file.path(path, "Q_indicators.tsv"))
if (!is.null(indicators_periods)) {
lapply(indicators_periods, function(period) {
Qsim <- attr(OutputsModel, "Qm3s")
Qsim <- Qsim[Qsim$DatesR >= period[1] & Qsim$DatesR <= period[2], ]
dfInd <- calcHydrologicalIndicators(Qsim)
y <- format(period, "%Y")
readr::write_tsv(dfInd,
file.path(path,
paste0("Q_indicators_",
paste(y, collapse = "-"),
".tsv")))
})
}
# README
file.copy(system.file("LISEZMOI.md", package = "seinebasin2"),
......@@ -67,3 +67,19 @@ calcQcaract <- function(indicator, Q) {
FUN <- match.fun(paste0("calc", indicator))
FUN(Q)
}
calcHydrologicalIndicators <- function(Q) {
indicators <- c("QA",
"VCN10_2", "VCN30_2", "QMNA2",
"VCN10_5", "VCN30_5", "QMNA5",
"VCN10_10", "VCN30_10", "QMNA10",
"QJXA2", "QJXA10", "QJXA20")
lInd <- lapply(
indicators,
calcQcaract,
Q = Q
)
mInd <- do.call(cbind, lInd)
colnames(mInd) <- indicators
cbind(data.frame(Id = rownames(mInd)), mInd)
}
......@@ -33,9 +33,14 @@ plot(OutputsModel[[station]], Qobs = BasinsObs$Q[IndPeriod_Run, station], which
Les débits en m<sup>3</sup>/s sont enregistrés pour archivage avec la fonction suivante :
```{r, eval=!cfg$data$cloud}
indicators_periods <- list(
calage = as.POSIXct(c(cfg$calibration$date$start, cfg$calibration$date$end), tz = "UTC"),
reference = as.POSIXct(cfg$hydroclim$drias$periods$ref, tz = "UTC")
)
saveFlowDB(path = getDataPath(cfg$Qnat$path, "Qnat-v1", cfg = cfg),
InputsModel = InputsModel,
OutputsModel = OutputsModel,
indicators_periods = indicators_periods,
cfg = cfg)
```
......
......@@ -19,9 +19,11 @@ LoadRunSave <- function(rcp, scenario) {
InputsModel <- suppressMessages(CreateInputsModel(BasinsObs))
OutputsModel <- RunModel(BasinsObs, "calibration_with_reservoirs.txt", InputsModel = InputsModel)
path <- getDataPath(cfg$Qnat$path, "Drias2020/Qnat-v1", rcp, scenario, cfg = cfg)
indicators_periods <- lapply(cfg$hydroclim$drias$periods, as.POSIXct, tz = "UTC")
saveFlowDB(path = path,
InputsModel = InputsModel,
OutputsModel = OutputsModel,
indicators_periods = indicators_periods,
cfg = cfg)
}
```
......
......@@ -9,7 +9,7 @@ Il contient les fichiers suivants:
- stations.tsv
- ts_Qsim.tsv
- ts_Qcontrib.tsv
- Q_indicators.tsv
- Q_indicators_[DDDD]-[FFFF].tsv
Ce sont des fichiers texte avec séparateur tabulation (Tab-Separated-Values).
......@@ -43,9 +43,9 @@ La première colonne `DatesR` contient la date de la données moyenne journaliè
Les colonnes suivantes portent le nom de la station hydrométique correspondant à la colonne `CdSiteHydro` du fichier `stations.tsv` et elles contiennent la contribution moyenne journalière du bassin versant intermédiaire modélisée en m<sup>3</sup>/s.
## Description du fichier "Q_indicators.tsv"
## Description des fichiers "Q_indicators_[DDDD]-[FFFF].tsv"
Ce fichier contient les indicateurs hydrologiques suivants en m³/s:
Ce fichier contient des indicateurs hydrologiques calculés entre l'année `[DDDD]` et l'année `[FFFF]`. Les indicateurs calculés sont les suivants (unités m³/s):
- `QA`: débit annuel moyen
- `VCN10_2`, `VCN30_2`, `VCN10_5`, `VCN_30_5`, `VCN10_10`, `VCN30_10`: avec `VCN[k]_[Y]` le débit moyen minimum sur une durée de `k` jours de période de retour `Y` ans
......
......@@ -27,6 +27,19 @@ default:
"y":
min: 2217000
max: 2561000
periods:
ref:
- 1976-01-01
- 2005-12-31
near:
- 2021-01-01
- 2050-12-31
middle:
- 2041-01-01
- 2070-12-31
end:
- 2071-01-01
- 2100-12-31
model: RunModel_CemaNeigeGR4J
calibration:
eval: false
......
......@@ -4,7 +4,13 @@
\alias{saveFlowDB}
\title{Save database of one simulation}
\usage{
saveFlowDB(path, InputsModel, OutputsModel, cfg = loadConfig())
saveFlowDB(
path,
InputsModel,
OutputsModel,
indicators_periods = NULL,
cfg = loadConfig()
)
}
\arguments{
\item{path}{\link{character} the location used to save the database}
......@@ -13,6 +19,8 @@ saveFlowDB(path, InputsModel, OutputsModel, cfg = loadConfig())
\item{OutputsModel}{\code{GriwrmOutputsModel} object. See \link{RunModel.BasinsObs}}
\item{indicators_periods}{\link{list} containing item with 2-length \link{POSIXct} for starting and ending date of each period used for calculating hydrological indicators}
\item{cfg}{a config object. Configuration to use. See \link{loadConfig} for details}
}
\value{
......
Markdown is supported
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