Newer
Older
Delaigue Olivier
committed
Delaigue Olivier
committed
Imax <- function(InputsModel,
IndPeriod_Run,
TestedValues = seq(from = 0.1, to = 3, by = 0.1)) {
Delaigue Olivier
committed
## ---------- check arguments
## InputsModel
Delaigue Olivier
committed
if (!inherits(InputsModel, "InputsModel")) {
stop("'InputsModel' must be of class 'InputsModel'")
Delaigue Olivier
committed
}
Delaigue Olivier
committed
if (!inherits(InputsModel, "hourly")) {
stop("'InputsModel' must be of class 'hourly'")
Delaigue Olivier
committed
}
## IndPeriod_Run
Delaigue Olivier
committed
if (!is.vector(IndPeriod_Run)) {
stop("'IndPeriod_Run' must be a vector of numeric values")
}
if (!inherits(IndPeriod_Run, "integer")) {
stop("'IndPeriod_Run' must be of type integer")
Delaigue Olivier
committed
}
Delaigue Olivier
committed
if (!identical(as.integer(IndPeriod_Run), IndPeriod_Run[1]:IndPeriod_Run[length(IndPeriod_Run)])) {
Delaigue Olivier
committed
stop("'IndPeriod_Run' must be a continuous sequence of integers")
}
Delaigue Olivier
committed
## TestedValues
Delaigue Olivier
committed
if (!(is.numeric(TestedValues))) {
stop("'TestedValues' must be 'numeric'")
Delaigue Olivier
committed
}
Delaigue Olivier
committed
## ---------- hourly inputs aggregation
## aggregate data at the daily time step
Delaigue Olivier
committed
daily_data <- SeriesAggreg(InputsModel[IndPeriod_Run], Format = "%Y%m%d")
Delaigue Olivier
committed
## ---------- calculate interception
## calculate total interception of daily GR models on the period
Delaigue Olivier
committed
cum_daily <- sum(pmin(daily_data$Precip, daily_data$PotEvap))
Delaigue Olivier
committed
if (anyNA(cum_daily)) {
stop("'IndPeriod_Run' must be set to select 24 hours by day")
}
## calculate total interception of the GR5H interception store on the period
Delaigue Olivier
committed
## and compute difference with daily values
Delaigue Olivier
committed
differences <- array(NA, c(length(TestedValues)))
for (Imax in TestedValues) {
Delaigue Olivier
committed
C0 <- 0
cum_hourly <- 0
for (i in IndPeriod_Run) {
Ec <- min(InputsModel$PotEvap[i], InputsModel$Precip[i] + C0)
Pth <- max(0, InputsModel$Precip[i] - (Imax-C0) - Ec)
Delaigue Olivier
committed
C0 <- C0 + InputsModel$Precip[i] - Ec - Pth
cum_hourly <- cum_hourly + Ec
}
Delaigue Olivier
committed
differences[which(Imax == TestedValues)] <- abs(cum_hourly - cum_daily)
Delaigue Olivier
committed
}
Delaigue Olivier
committed
## return the Imax value that minimises the difference
Delaigue Olivier
committed
return(TestedValues[which.min(differences)])
Delaigue Olivier
committed