Commit a46e9773 authored by Delaigue Olivier's avatar Delaigue Olivier
Browse files

v1.6.9.28 fix: stop Imax when IndPeriod_Run doesn't select 24 hours by day

Refs #92
Showing with 46 additions and 24 deletions
+46 -24
Package: airGR Package: airGR
Type: Package Type: Package
Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling Title: Suite of GR Hydrological Models for Precipitation-Runoff Modelling
Version: 1.6.9.27 Version: 1.6.9.28
Date: 2021-01-18 Date: 2021-01-25
Authors@R: c( Authors@R: c(
person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")), person("Laurent", "Coron", role = c("aut", "trl"), comment = c(ORCID = "0000-0002-1503-6204")),
person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"), person("Olivier", "Delaigue", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-7668-8468"), email = "airGR@inrae.fr"),
......
...@@ -2,6 +2,15 @@ ...@@ -2,6 +2,15 @@
### Unreleased
#### Major user-visible changes
- `Imax()` now returns an error message when `IndPeriod_Run` doesn't select 24 hours by day). ([#92](https://gitlab.irstea.fr/HYCAR-Hydro/airgr/-/issues/92))
____________________________________________________________________________________
### 1.6.9.27 Release Notes (2021-01-18) ### 1.6.9.27 Release Notes (2021-01-18)
#### New features #### New features
......
Imax <- function(InputsModel, Imax <- function(InputsModel,
IndPeriod_Run, IndPeriod_Run,
TestedValues = seq(from = 0.1, to = 3, by = 0.1)) { TestedValues = seq(from = 0.1, to = 3, by = 0.1)) {
##_____Arguments_check_____________________________________________________________________
## ---------- check arguments
## InputsModel
if (!inherits(InputsModel, "InputsModel")) { if (!inherits(InputsModel, "InputsModel")) {
stop("'InputsModel' must be of class 'InputsModel'") stop("'InputsModel' must be of class 'InputsModel'")
} }
if (!inherits(InputsModel, "hourly")) { if (!inherits(InputsModel, "hourly")) {
stop("'InputsModel' must be of class 'hourly'") stop("'InputsModel' must be of class 'hourly'")
} }
##check_IndPeriod_Run ## IndPeriod_Run
if (!is.vector(IndPeriod_Run)) { if (!is.vector(IndPeriod_Run)) {
stop("'IndPeriod_Run' must be a vector of numeric values") stop("'IndPeriod_Run' must be a vector of numeric values")
} }
...@@ -21,26 +24,36 @@ Imax <- function(InputsModel, ...@@ -21,26 +24,36 @@ Imax <- function(InputsModel,
if (!identical(as.integer(IndPeriod_Run), IndPeriod_Run[1]:IndPeriod_Run[length(IndPeriod_Run)])) { if (!identical(as.integer(IndPeriod_Run), IndPeriod_Run[1]:IndPeriod_Run[length(IndPeriod_Run)])) {
stop("'IndPeriod_Run' must be a continuous sequence of integers") stop("'IndPeriod_Run' must be a continuous sequence of integers")
} }
##TestedValues ## TestedValues
if (!(is.numeric(TestedValues))) { if (!(is.numeric(TestedValues))) {
stop("'TestedValues' must be 'numeric'") stop("'TestedValues' must be 'numeric'")
} }
##aggregate data at the daily time step ## ---------- hourly inputs aggregation
TabSeries <- data.frame(DatesR = InputsModel$DatesR[IndPeriod_Run],
Precip = InputsModel$Precip[IndPeriod_Run], ## aggregate data at the daily time step
TabSeries <- data.frame(DatesR = InputsModel$DatesR[IndPeriod_Run],
Precip = InputsModel$Precip[IndPeriod_Run],
PotEvap = InputsModel$PotEvap[IndPeriod_Run]) PotEvap = InputsModel$PotEvap[IndPeriod_Run])
daily_data <- SeriesAggreg(TabSeries, Format = "%Y%m%d", daily_data <- SeriesAggreg(TabSeries, Format = "%Y%m%d",
ConvertFun = c("sum", "sum")) ConvertFun = c("sum", "sum"))
##calculate total interception of daily GR models on the period
## ---------- calculate interception
## calculate total interception of daily GR models on the period
cum_daily <- sum(pmin(daily_data$Precip, daily_data$PotEvap)) cum_daily <- sum(pmin(daily_data$Precip, daily_data$PotEvap))
##calculate total interception of the GR5H interception store on the period 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
## and compute difference with daily values ## and compute difference with daily values
differences <- array(NA, c(length(TestedValues))) differences <- array(NA, c(length(TestedValues)))
print(differences)
for (Imax in TestedValues) { for (Imax in TestedValues) {
C0 <- 0 C0 <- 0
cum_hourly <- 0 cum_hourly <- 0
...@@ -52,8 +65,8 @@ Imax <- function(InputsModel, ...@@ -52,8 +65,8 @@ Imax <- function(InputsModel,
} }
differences[which(Imax == TestedValues)] <- abs(cum_hourly - cum_daily) differences[which(Imax == TestedValues)] <- abs(cum_hourly - cum_daily)
} }
##return the Imax value that minimises the difference ## return the Imax value that minimises the difference
return(TestedValues[which.min(differences)]) return(TestedValues[which.min(differences)])
} }
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