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

BREAKING CHANGE: refactor to use last version of VGEST

Close #9
parent 1f7915c7
Package: irmara
Title: Interactive Reservoir MAnagement Risk Assessment
Version: 0.0.0.9000
Version: 0.0.1
Authors@R: person('David', 'Dorchies', email = 'david.dorchies@inrae.fr', role = c('cre', 'aut'))
Description: IRMaRA is a R Shiny interface providing probability of failure of flood and drought objective at key locations downstream of the 4 lakes regulating the Seine River.
License: AGPL-3
Imports:
Imports:
config,
golem,
shiny,
......@@ -16,8 +16,9 @@ Imports:
readr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Roxygen: list(markdown = TRUE)
URL: https://gitlab.irstea.fr/in-wop/irmara
BugReports: https://gitlab.irstea.fr/in-wop/irmara/issues
Suggests:
Suggests:
testthat
#' Load objective data for \code{\link{vgest_run}}
#'
#' @param objective_thresholds File location of threshold table
#' @param objective_stations File location of objective station table
#' @param lakes File location of lake table
#'
#' @return A dataframe containing one line by objective and the following columns:
#'
#' - station (character): identifier of the objective station
#' - flood (boolean): TRUE for high flow mitigation objective, and FALSE for low flow support
#' - level (character): code composed with "l" for low-flow and "h" for high flow followed by the number of the level
#' - threshold (numeric): value of the threshold in m3/s
#' - lakes (dataframe): Dataframe containing lake details (name, min storage, max storage)
#'
#' @export
#'
#' @examples
#' # Get objectives stored in IRMaRA package
#' df <- get_objectives()
#' # Objectives at Paris
#' df[df[,"station"] == "PARIS_05",]
#' # Lake details concerning the 40th objective in the table
#' df[57, "lakes"]
get_objectives <- function(
objective_thresholds = app_sys("seine", "objective_thresholds.txt"),
objective_stations = app_sys("seine", "objective_stations.json"),
......
#' Read result file `VOBJi.DAT` for one lake of one objective at one station
#'
#' This function is called by [vgest_read_one()], please run this one instead.
#'
#' @param file complete path to the `VOBJi.DAT` file
#' @param bFlood boolean `TRUE` for high flow mitigation objective, `FALSE` for low flow support
#'
#' @return dataframe with the content of the `VOBJi.DAT` file
#' @export
#'
#' @examples
#' \donttest{
#' df <- vgest_read("database/PARIS_05/high_800/VOBJ1.dat")
#' }
vgest_read <- function(file, bFlood) {
# Managing headers
s <- readLines(file, n = 11)
......@@ -23,6 +37,21 @@ vgest_read <- function(file, bFlood) {
return(df)
}
#' Read result file `VOBJi.DAT` for one lake of one objective at one station
#'
#' This function is preferred to [vgest_read()] because it builds the path for the file to read.
#'
#' @param iLake Lake number for the current station
#' @param x one line of the dataframe produced by [get_objectives()]
#' @param result.dir path where results of VGEST runs are stored (See [vgest_run_store()])
#'
#' @return dataframe with the content of the `VOBJi.DAT` file
#' @export
#'
#' @examples
#' \donttest{
#' vgest_read_one(1, get_objectives()[1,], "./database")
#' }
vgest_read_one <- function(iLake, x, result.dir) {
sLowHigh <- c("low", "high")
file <- paste0(
......@@ -34,6 +63,15 @@ vgest_read_one <- function(iLake, x, result.dir) {
vgest_read(file, x$flood)
}
#' Read all result files `VOBJi.DAT` for all the lakes of one objective at one station
#'
#' @param x one line of the dataframe produced by [get_objectives()]
#' @param result.dir path where results of VGEST runs are stored (See [vgest_run_store()])
#'
#' @return list with dataframes produced by
#' @export
#'
#' @examples
vgest_read_all <- function(x, result.dir = "database") {
lObj <- lapply(1:nrow(x$lakes), vgest_read_one, x, result.dir)
names(lObj) <- x$lakes$name
......
#' Run VGEST and stop execution if an error is encountered during execution
#'
#' @details
#' Delete the content of the `RESULTAT` folder before running
#'
#' @param vgest_location
#' @param vgest_location Location of VGEST installation (Default "../vgest")
#'
#' @return
#' @return Nil
#' @export
#'
#' @examples
#' \donttest{
#' # Run vgest with the current
#' vgest_run()
#' }
vgest_run <- function(vgest_location = "../vgest") {
workingDir <- getwd()
setwd(vgest_location)
file.remove(list.files(path = "RESULTAT", full.names = TRUE))
iError <- system(command = "VGEST.exe")
setwd(workingDir)
if(iError != 0) stop("Running VGEST has returned an error", iError)
......
vgest_run_all <- function(Qfile, startDate, endDate, vgest_location = "../vgest", objective_file = "BATCH", result.dir = "database", ...) {
#' Run simulations of VGEST for all objectives and store results
#'
#' @param reservoirRuleSet rank of the set of parameters describing the reservoirs and their management rules (constraints and local instructions)
#' @param networkSet rank of the set of parameters describing the network
#' @param Qfile name of the flow data file (hydrological regime without reservoirs)
#' @param startDate start of calculation period in "dd/mm/yyyy" format
#' @param endDate end of calculation period in "dd/mm/yyyy" format
#' @param distributionType see Details section of [vgest_write_batch()]
#' @param distributionOption see Details section of [vgest_write_batch()]
#' @param vgest_location location of the vgest software (default "../vgest")
#' @param objective_file name of the file used for storing the threshold hydrograph
#' @param result.dir path for storing the result of vgest run. The result is stored in a subfolder named high or low (depending on \code{bFlood}) followed by the threshold
#' @param ... Extra parameters used by \code{\link{get_objectives}}
#'
#' @return
#' @export
#'
#' @examples
#' \donttest{
#' # Run all objectives for reservoir configuration #1,
#' # network configuration #1 and a task repartition
#' # function of present volumes and maximum usable volume
#' # replenishment times from the start of time steps
#' vgest_run_all_objectives(1, 1, "Q_NAT_1900-2009.txt", "01/01/1900", "31/12/2009", 2)
#' }
vgest_run_all_objectives <- function(reservoirRuleSet, networkSet,
Qfile, startDate, endDate,
distributionType, distributionOption = NULL,
vgest_location = "../vgest",
objective_file = "BATCH",
result.dir = "database",
formatResult = 1, ...) {
df <- get_objectives(...)
apply(df, 1, function(x) {
vgest_run_store(Qfile, startDate, endDate, x$station, x$flood, x$threshold, vgest_location, objective_file, result.dir)
vgest_run_store(reservoirRuleSet, networkSet, Qfile, startDate, endDate,
x$station, x$flood, x$threshold, distributionType, distributionOption,
vgest_location, objective_file, formatResult)
})
}
\ No newline at end of file
#' Prepare, run and store results of a VGEST instance
#'
#' @param Qfile
#' @param startDate
#' @param endDate
#' @param station
#' @param bFlood
#' @param threshold
#' @param vgest_location
#' @param objective_file
#' @param result.dir
#' @param reservoirRuleSet rank of the set of parameters describing the reservoirs and their management rules (constraints and local instructions)
#' @param networkSet rank of the set of parameters describing the network
#' @param station name of the station where the flow target is located
#' @param Qfile name of the flow data file (hydrological regime without reservoirs)
#' @param bFlood boolean describing the objective type: \code{FALSE} for low flow support, \code{TRUE} for high flow lamination
#' @param startDate start of calculation period in "dd/mm/yyyy" format
#' @param endDate end of calculation period in "dd/mm/yyyy" format
#' @param threshold value of the threshold in m3/s
#' @param distributionType see in details the description of line 10 of CHOIX.TXT
#' @param distributionOption for \code{distributionType=1}, this should contains the relative change in comparison with a repartition based on storage capacity of fixed repartition between reservoirs. If \code{distributionType=4} or \code{5}, see in details the description of line 11 of CHOIX.TXT (default \code{NULL})
#' @param vgest_location location of the vgest software (default "../vgest")
#' @param objective_file name of the file used for storing the threshold hydrograph
#' @param result.dir path for storing the result of vgest run. The result is stored in a subfolder named high or low (depending on \code{bFlood}) followed by the threshold
#'
#' @return
#' @export
#'
#' @examples
vgest_run_store <- function(Qfile, startDate, endDate, station, bFlood, threshold, vgest_location = "../vgest", objective_file = "BATCH", result.dir = "database") {
cat("Run VGEST for configuration: ", station, Qfile, as.numeric(bFlood), threshold, "...")
vgest_write_batch(Qfile, startDate, endDate, station, bFlood, threshold, vgest_location, objective_file)
vgest_run(vgest_location)
#' \donttest{
#' # Preparing the run of vgest for:
#' # - the first configuration of reservoir rules
#' # - the first configuration of network
#' # - the naturalised hydrological flows of the file located in DONNEES/Q_NAT_1900-2009.txt
#' # - doing the optimisation on the period between 01/01/1900 and 31/12/2009
#' # - an objective located at "PARIS_05"
#' # - for a flood threshold of 800 m3/s
#' # - a fixed task distribution between reservoirs with a relative change of -20%, -10%, +50%, -30%
#' vgest_run_store(1, 1, "Q_NAT_1900-2009.txt", "01/01/1900", "31/12/2009",
#' "PARIS_05", TRUE, 800, 1, c(-0.2, -0.1, 0.5, -0.3))
#' }
vgest_run_store <- function(reservoirRuleSet, networkSet,
Qfile, startDate, endDate,
station, bFlood, threshold,
distributionType, distributionOption = NULL,
vgest_location = "../vgest",
objective_file = "BATCH",
formatResult = 1,
result.dir = "database") {
sLowHigh <- c("low", "high")
cat("Run VGEST for configuration: ", station, Qfile, sLowHigh[bFlood + 1], threshold, "...")
vgest_write_batch(reservoirRuleSet, networkSet, Qfile, startDate, endDate,
station, bFlood, threshold, distributionType, distributionOption,
vgest_location, objective_file, formatResult)
vgest_run(vgest_location)
sTo <- file.path(result.dir, station, paste0(sLowHigh[bFlood + 1], "_", threshold))
cat(" Store files to", sTo)
dir.create(sTo, showWarnings = FALSE, recursive = TRUE)
file.remove(list.files(path = sTo, full.names = TRUE))
if(!all(file.copy(
list.files(file.path(vgest_location, "RESULTAT"), pattern = "Vobj", full.names = TRUE),
sTo
list.files(file.path(vgest_location, "RESULTAT"), full.names = TRUE),
sTo, overwrite = TRUE
))) stop("Error copying Vojb files from VGEST to Irmara")
cat(" - OK\n")
}
#' Write CHOIX.TXT and objective file in VGEST folder
#' Write configuration files for running VGEST
#'
#' Write CHOIX.TXT, objective file and eventually REGLAGES.TXT in PARAMETR folder of VGEST.
#'
#' @param station
#' @param Qfile
#' @param bFlood
#' @param startDate
#' @param endDate
#' @param threshold
#' @param vgest_location
#' @param objective_file
#' @details
#' The format of the PARAMETR/CHOIX.TXT file is as follow:
#'
#' - line 1: name of the station where the flow target is located
#' - line 2: rank of the set of parameters describing the reservoirs and their management rules (constraints and local instructions)
#' - line 3: rank of the set of parameters describing the network
#' - line 4: name of the flow data file (hydrological regime without reservoirs)
#' - line 5: name of the file describing the annual objective hydrograph at the bottom station of the system
#' - line 6: objective type: 0 for low flow support, 1 for high flow lamination
#' - line 7: start of calculation period
#' - line 8: end of calculation period
#' - line 9: code for output format of volume results: 1 for absolute values in m3 , 2 for relative values with respect to Vtot or sum(Vtot)
#' - line 10: code indicating how the flow to be stored is distributed between the reservoirs:
#' - 1=fixed;
#' - 2=function of present volumes and maximum usable volume replenishment times from the start of time steps;
#' - 3=aimed at balancing the filling rates at the end of the time step;
#' - 4=aiming to balance at the end of the time step the times Tpot of reservoir evolution towards extreme state (see line 11) with average inputs; 5=aiming to balance at the end of the time step the times Tpot of reservoir evolution towards extreme state (see line 11) with average inputs.
#' - 5=aiming to balance at the end of the time step the times Tpot of evolution of the reservoir towards extreme state (see line 11) with given quantity of the contributions of each quantity.
#' - line 11 (used only if 4 or 5 on line 10): code indicating the nature of Tpot :
#' - 1 : Tpot is the minimum potential duration Tpot1 of reconstitution of the maximum usable volume (obtaining V=Vtot or V=0, depending on the nature of the objective (support or rolling) and the direction of the calculations)
#' - 2 : Tpot is the minimum potential duration Tpot2 of exhaustion of the usable volume (obtaining V=Vtot or V=0, depending on the nature of the objective (support or rolling) and the direction of the calculations)
#'
#' @param reservoirRuleSet rank of the set of parameters describing the reservoirs and their management rules (constraints and local instructions)
#' @param networkSet rank of the set of parameters describing the network
#' @param station name of the station where the flow target is located
#' @param Qfile name of the flow data file (hydrological regime without reservoirs)
#' @param bFlood boolean describing the objective type: \code{FALSE} for low flow support, \code{TRUE} for high flow lamination
#' @param startDate start of calculation period in "dd/mm/yyyy" format
#' @param endDate end of calculation period in "dd/mm/yyyy" format
#' @param threshold value of the threshold in m3/s
#' @param distributionType see in details the description of line 10 of CHOIX.TXT
#' @param distributionOption for \code{distributionType=1}, this should contains the relative change in comparison with a repartition based on storage capacity of fixed repartition between reservoirs. If \code{distributionType=4} or \code{5}, see in details the description of line 11 of CHOIX.TXT (default \code{NULL})
#' @param vgest_location location of the vgest software (default "../vgest")
#' @param objective_file name of the file used for storing the threshold hydrograph
#'
#' @return
#' @export
#'
#' @examples
vgest_write_batch <- function(Qfile, startDate, endDate, station, bFlood, threshold, vgest_location = "../vgest", objective_file = "BATCH") {
#' \donttest{
#' # Preparing the run of vgest for:
#' # - the first configuration of reservoir rules
#' # - the first configuration of network
#' # - the naturalised hydrological flows of the file located in DONNEES/Q_NAT_1900-2009.txt
#' # - doing the optimisation on the period between 01/01/1900 and 31/12/2009
#' # - an objective located at "PARIS_05"
#' # - for a flood threshold of 800 m3/s
#' # - a fixed task distribution between reservoirs with a relative change of -20%, -10%, +50%, -30%
#' vgest_write_batch(1, 1, "Q_NAT_1900-2009.txt", "01/01/1900", "31/12/2009",
#' "PARIS_05", TRUE, 800, 1, c(-0.2, -0.1, 0.5, -0.3))
#' }
vgest_write_batch <- function(reservoirRuleSet, networkSet,
Qfile, startDate, endDate,
station, bFlood, threshold,
distributionType, distributionOption = NULL,
vgest_location = "../vgest",
objective_file = "BATCH",
formatResult = 1) {
if(distributionType %in% c(1, 4, 5) & is.null(distributionOption)) {
stop("distribution should be defined for distributionType = 1, 4 or 5")
}
sMode <- as.numeric(bFlood) # 0 for drought, 1 for flood
s <- c(station, Qfile, objective_file, sMode, startDate, endDate)
writeLines(s, file.path(vgest_location, "CHOIX.TXT"))
s <- c(station, reservoirRuleSet, networkSet,
Qfile, objective_file, sMode, startDate, endDate,
formatResult, distributionType)
if(distributionType == 1) {
writeLines(as.character(distributionOption), file.path(vgest_location, "PARAMETR", "REGLAGE.txt"))
} else if(distributionType %in% c(4,5)) {
s <- c(s,distributionOption)
}
writeLines(s, file.path(vgest_location, "PARAMETR", "CHOIX.txt"))
writeLines(paste("01/01", threshold), file.path(vgest_location, "OBJECTIF", objective_file))
}
......@@ -15,3 +15,4 @@ LaTeX: pdfLaTeX
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
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