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.
#' Calculate the cost function for one simulation (One objective at one station)
#'
#' It's the mean daily storage for a low-flow support objective and the mean daily available storage capacity for a high flow mitigation objective.
#' Uses ouput of PaChrono.txt or VObj\[1-4\].dat for the calculation.
#'
#' See [Vgest_cost.Objectives], [vgest_cost.ListVobj] and [vgest_obj.Chrono] for details.
#'
#' @param data Data source, see details
#' @param ... Other arguments passed to the methods
#'
#' @return
#' @export
#'
#' @examples
vgest_cost<-function(data,...){
UseMethod("vgest_cost",data)
}
#' Calculate the total cost for all the lakes
#'
#' @param dfVobj one item of the list given by [vgest_read_all()]
#' @param floodMaxCapacity the net storage capacity of the lake if the objective is high flow mitigation, set to `NULL` if the objective is low-flow support
#' For each lake, it's the mean daily storage for a low-flow support objective and the mean daily available storage capacity for a high flow mitigation objective.
#'
#' @return cost of the lake in m3/day
#' @param Vobj A [matrix] or a [data.frame] with one column by lake, in the same order as `objective$lakes`
#' @param objective one row of the dataframe given by [get_objectives()]
#'
#' @return total cost of all the lakes in m3/day
#'
vgest_cost_lakes<-function(Vobj,objective){
lakes<-objective$lakes[[1]]
sum(
sapply(
1:nrow(lakes),function(i){
V<-Vobj[,i]
if(objective$flood){
V<-(lakes$max[i]-lakes$min[i])*1E6-V
}
mean(V,na.rm=TRUE)
}
)
)
}
#' Calculate the total cost for one objective at one station
#'
#'
#' @param data the list given by [vgest_read_all()]
#' @param objective one row of the dataframe given by [get_objectives()]
#'
#' @return the total cost for one objective at one station in m3/day
#' @export
#'
#' @examples
...
...
@@ -13,25 +55,20 @@
#' # This should be done after the execution of vgest for the concerned objective
#' Read the time series provided by VGEST for the forward calculation (Chrono.txt) and backward calculation (PaChrono.txt)
#'
#' The format of the file is has follow. Headers are in line 53 followed by the complete time series which is backward in time for PaChrono.txt. The columns and respected widths are:
#' - Downstream date (10)
#' - QXsous (22)
#'
#'
#' For each lake, we next have:
#' - Upstream date (14)
#' - 25 columns of flow and storage data (22)
#' - if `DistributionType %in% c(4,5)`, 1 extra column (22)
#' - 5 columns of codes (6)
#'
#'
#' And then at the end:
#' - 7 columns of flow (22)
#' - 3 columns of codes (8)
#' - 1 extra column of code for `DistributionType==5` (8)
#'
#'
#' The file is saved in RDS format for quicker reading the next time.
#'
#' @param x file to read with txt extension
#' @param nLakes number of lakes in the file
#' @param distributionType Distribution type. See [vgest_write_batch] details
#' @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 distributionType Distribution type. See [vgest_write_batch] details
#' @param backward boolean `TRUE` for reading "PaChrono.txt", `FALSE` for reading "Chrono.txt"
#'
#' @return
#' @return A list with items named [station]_[high/low]_[threshold] containing [data.frame] with the content of each file