## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.asp = 0.68,
  out.width = "70%",
  fig.align = "center"
)

## ----setup--------------------------------------------------------------------
library(airGRiwrm)

## ----load_cache---------------------------------------------------------------
data(Severn)
nodes <- Severn$BasinsInfo[, c(
  "gauge_id",
  "downstream_id",
  "distance_downstream",
  "area"
)]
nodes$model <- "RunModel_GR4J"

## ----updated_nodes------------------------------------------------------------
nodes <- rbind(
  nodes,
  data.frame(
    gauge_id = c("Irrigation1", "Irrigation2"),
    downstream_id = c("54001", "54032"),
    distance_downstream = c(35, 10),
    model = NA,
    area = NA
  )
)

nodes

## ----griwm--------------------------------------------------------------------
griwrmV04 <- CreateGRiwrm(
  nodes,
  list(id = "gauge_id", down = "downstream_id", length = "distance_downstream")
)
plot(griwrmV04)

## ----climatic_data------------------------------------------------------------
# Formatting climatic data for CreateInputsModel (See vignette V01_Structure_SD_model for details)
BasinsObs <- Severn$BasinsObs
DatesR <- BasinsObs[[1]]$DatesR
PrecipTot <- cbind(sapply(BasinsObs, function(x) {
  x$precipitation
}))
PotEvapTot <- cbind(sapply(BasinsObs, function(x) {
  x$peti
}))
Qobs <- cbind(sapply(BasinsObs, function(x) {
  x$discharge_spec
}))
Precip <- ConvertMeteoSD(griwrmV04, PrecipTot)
PotEvap <- ConvertMeteoSD(griwrmV04, PotEvapTot)

## -----------------------------------------------------------------------------
irrigationObjective <- matrix(0, nrow = 12, ncol = 4)
colnames(irrigationObjective) <- c("Month", "Irrigation1", "Irrigation2", "total")
irrigationObjective[, 1] <- 1:12
# Irrigation period between March and September
irrigationObjective[3:9, 2] <- 1.2
irrigationObjective[3:9, 3] <- 2.4
irrigationObjective[, 4] <- rowSums(irrigationObjective[, c(2, 3)])
irrigationObjective <- as.data.frame(irrigationObjective)
irrigationObjective

## ----abstraction restriction rule---------------------------------------------
restriction_rule <- data.frame(
  quantile_natural_flow = c(.05, .3, 0.5, 0.7),
  abstraction_rate = c(0.1, 0.15, 0.20, 0.24)
)

## -----------------------------------------------------------------------------
quant_m3s32 <- quantile(
  Qobs[, "54032"] * griwrmV04[griwrmV04$id == "54032", "area"] / 86.4,
  restriction_rule$quantile_natural_flow,
  na.rm = TRUE
)
restriction_rule_m3s <- data.frame(
  threshold_natural_flow = quant_m3s32,
  abstraction_rate = restriction_rule$abstraction_rate
)

matplot(
  restriction_rule$quantile_natural_flow,
  cbind(
    restriction_rule_m3s$threshold_natural_flow,
    restriction_rule$abstraction_rate *
      restriction_rule_m3s$threshold_natural_flow,
    max(irrigationObjective$total)
  ),
  log = "x",
  type = "l",
  main = "Quantiles of flow on the Severn at Saxons Lode (54032)",
  xlab = "quantiles",
  ylab = "Flow (m3/s)",
  lty = 1,
  col = rainbow(3, rev = TRUE)
)
legend(
  "topleft",
  legend = c("Natural flow", "Abstraction limit", "Irrigation max. objective"),
  col = rainbow(3, rev = TRUE),
  lty = 1
)

## -----------------------------------------------------------------------------
# A function to enclose the parameters in the function (See: http://adv-r.had.co.nz/Functional-programming.html#closures)
getAvailableAbstractionEnclosed <- function(restriction_rule_m3s) {
  function(Qnat) {
    approx(
      restriction_rule_m3s$threshold_natural_flow,
      restriction_rule_m3s$abstraction_rate,
      Qnat,
      rule = 2
    )
  }
}
# The function with the parameters inside it :)
getAvailableAbstraction <- getAvailableAbstractionEnclosed(restriction_rule_m3s)
# You can check the storage of the parameters in the function with
as.list(environment(getAvailableAbstraction))

## -----------------------------------------------------------------------------
restriction_rotation <- matrix(
  c(5, 7, 6, 4, 2, 1, 3, 3, 1, 2, 4, 6, 7, 5),
  ncol = 2
)
colnames(restriction_rotation) <- c("Irrigation1", "Irrigation2")
restriction_rotation

## -----------------------------------------------------------------------------
m <- do.call(
  rbind,
  lapply(seq(0, 7), function(x) {
    b <- restriction_rotation <= x
    rowSums(b)
  })
)
# Display the planning of restriction
image(
  1:ncol(m),
  1:nrow(m),
  t(m),
  col = heat.colors(3, rev = TRUE),
  axes = FALSE,
  xlab = "week day",
  ylab = "number of restriction days",
  main = "Number of closed irrigation perimeters"
)
axis(1, 1:ncol(m), unlist(strsplit("SMTWTFS", "")))
axis(2, 1:nrow(m), seq(0, 7))
for (x in 1:ncol(m)) {
  for (y in 1:nrow(m)) {
    text(x, y, m[y, x])
  }
}

## -----------------------------------------------------------------------------
# Flow time series are needed for all direct injection nodes in the network
# even if they may be overwritten after by a controller
QinfIrrig <- data.frame(
  Irrigation1 = rep(0, length(DatesR)),
  Irrigation2 = rep(0, length(DatesR))
)

# Creation of the GRiwrmInputsModel object
IM_Irrig <- CreateInputsModel(griwrmV04, DatesR, Precip, PotEvap, QinfIrrig)

## -----------------------------------------------------------------------------
sv <- CreateSupervisor(IM_Irrig, TimeStep = 7L)

## -----------------------------------------------------------------------------
fIrrigationFactory <- function(
  supervisor,
  irrigationObjective,
  restriction_rule_m3s,
  restriction_rotation
) {
  function(Y) {
    # Y is in m3/day (the last TimeStep simulated flows at node 54032)
    # 1. Calculate the objective of irrigation (m3/s) for each day of the
    # current week, according to the month of this day
    month <- as.numeric(format(supervisor$ts.date, "%m"))
    U <- irrigationObjective[month, c(2, 3)]
    meanU <- mean(rowSums(U))
    if (meanU > 0) {
      # 2. Calculate the naturalized flow (m3/s) from the measured flow and
      # the abstracted flow of the previous week
      lastU <- supervisor$controllers[[supervisor$controller.id]]$U # m3/day
      if (nrow(lastU) == 0) {
        # First supervision: no abstraction yet, the measured flow is the natural flow
        Qnat <- Y / 86400
      } else {
        Qnat <- (Y - rowSums(lastU)) / 86400
      }
      # 3. Calculate the number of restricted days for the week
      # Maximum abstracted flow available under the restriction rule
      Qrestricted <- mean(
        approx(
          restriction_rule_m3s$threshold_natural_flow,
          restriction_rule_m3s$abstraction_rate,
          Qnat,
          rule = 2
        )$y *
          Qnat
      )
      # Total for irrigation
      QIrrig <- min(meanU, Qrestricted)
      n <- floor(7 * (1 - QIrrig / meanU))
      # 4. Return the abstracted flow for the next week taking into account
      # the restriction days: a perimeter is closed on a day when its rank in
      # the rotation matrix is lower than or equal to the number of
      # restricted days
      U[restriction_rotation[seq(nrow(U)), ] <= n] <- 0
    }
    return(-U * 86400) # withdrawal is a negative flow in m3/day on an upstream node
  }
}

## -----------------------------------------------------------------------------
fIrrigation <- fIrrigationFactory(
  supervisor = sv,
  irrigationObjective = irrigationObjective,
  restriction_rule_m3s = restriction_rule_m3s,
  restriction_rotation = restriction_rotation
)

## -----------------------------------------------------------------------------
str(as.list(environment(fIrrigation)))

## -----------------------------------------------------------------------------
CreateController(
  sv,
  ctrl.id = "Irrigation",
  Y = "54032",
  U = c("Irrigation1", "Irrigation2"),
  FUN = fIrrigation
)

## -----------------------------------------------------------------------------
IndPeriod_Run <- seq(
  which(DatesR == (DatesR[1] + 365 * 24 * 60 * 60)), # Set aside warm-up period
  length(DatesR) # Until the end of the time series
)
IndPeriod_WarmUp <- seq(1, IndPeriod_Run[1] - 1)
RunOptions <- CreateRunOptions(
  IM_Irrig,
  IndPeriod_WarmUp = IndPeriod_WarmUp,
  IndPeriod_Run = IndPeriod_Run
)
ParamV02 <- readRDS(system.file(
  "vignettes",
  "ParamV02.RDS",
  package = "airGRiwrm"
))

## -----------------------------------------------------------------------------
OM_Irrig <- RunModel(sv, RunOptions = RunOptions, Param = ParamV02)

## -----------------------------------------------------------------------------
Qm3s <- attr(OM_Irrig, "Qm3s")
Qm3s <- Qm3s[Qm3s$DatesR > "2003-02-25" & Qm3s$DatesR < "2003-10-05", ]
oldpar <- par(mfrow = c(2, 1), mar = c(2.5, 4, 1, 1))
plot(Qm3s[, c("DatesR", "54095", "54001", "54032")], main = "", xlab = "")
plot(
  Qm3s[, c("DatesR", "Irrigation1", "Irrigation2")],
  main = "",
  xlab = "",
  legend.x = "bottomright"
)
par(oldpar)

## ----na_network---------------------------------------------------------------
nodesNA <- data.frame(
  id = c("54095", "Intake", "Other", "54001"),
  down = c("54001", "54001", "54001", NA),
  length = c(42, 10, 10, NA),
  area = c(
    Severn$BasinsInfo$area[Severn$BasinsInfo$gauge_id == "54095"],
    NA,
    NA,
    Severn$BasinsInfo$area[Severn$BasinsInfo$gauge_id == "54001"]
  ),
  model = c("RunModel_GR4J", NA, NA, "RunModel_GR4J")
)
griwrmNA <- CreateGRiwrm(nodesNA)
plot(griwrmNA)

## ----na_inputs----------------------------------------------------------------
PrecipNA <- ConvertMeteoSD(griwrmNA, PrecipTot)
PotEvapNA <- ConvertMeteoSD(griwrmNA, PotEvapTot)

## One year of warm-up and one year of simulation
IndPeriod_RunNA <- seq(length(DatesR) - 365 + 1, length(DatesR))
IndPeriod_WarmUpNA <- seq(IndPeriod_RunNA[1] - 365, IndPeriod_RunNA[1] - 1)
ParamNA <- ParamV02[c("54095", "54001")]

## Withdrawals at the two intakes (m3/s): 0 for the controlled intake
## (overwritten by the controller) and 0.2 m3/s for the uncontrolled one
QinfNA <- data.frame(
  Intake = rep(0, length(DatesR)),
  Other = rep(-0.2, length(DatesR))
)
QinfNA <- QinfNA * 86400 # m3/day

## 7 days of missing data at the uncontrolled intake
QinfNA$Other[seq(IndPeriod_RunNA[1] + 4, IndPeriod_RunNA[1] + 10)] <- NA
IMNA <- CreateInputsModel(griwrmNA, DatesR, PrecipNA, PotEvapNA, Qinf = QinfNA)
RONA <- CreateRunOptions(
  IMNA,
  IndPeriod_WarmUp = IndPeriod_WarmUpNA,
  IndPeriod_Run = IndPeriod_RunNA
)

## ----na_logic-----------------------------------------------------------------
## Control logic without NA handling
fWithdrawalNA <- function(Y) {
  -pmin(0.2 * Y, 0.5 * 86400) # m3/day
}

## Same logic with a fallback order: when the measurement contains NA values,
## the withdrawal is set to zero (no withdrawal when the flow is unknown)
fWithdrawalNAFallback <- function(Y) {
  if (any(is.na(Y))) {
    return(matrix(0, nrow = nrow(Y), ncol = ncol(Y)))
  }
  return(-pmin(0.2 * Y, 0.5 * 86400))
}

## ----na_run-------------------------------------------------------------------
## Reference run without any controller
OMNAref <- RunModel(IMNA, RunOptions = RONA, Param = ParamNA)

## Run with the control logic without NA handling
svNA1 <- CreateSupervisor(IMNA, TimeStep = 1L)
CreateController(
  svNA1,
  "Irrigation",
  Y = "54001",
  U = "Intake",
  FUN = fWithdrawalNA
)
OMNA1 <- RunModel(svNA1, RunOptions = RONA, Param = ParamNA)

## Run with the fallback order
svNA2 <- CreateSupervisor(IMNA, TimeStep = 1L)
CreateController(
  svNA2,
  "Irrigation",
  Y = "54001",
  U = "Intake",
  FUN = fWithdrawalNAFallback
)
OMNA2 <- RunModel(svNA2, RunOptions = RONA, Param = ParamNA)

## ----na_results---------------------------------------------------------------
Qm3sNA1 <- attr(OMNA1, "Qm3s")
Qm3sNA2 <- attr(OMNA2, "Qm3s")

i0 <- seq(1, 15)
tabNA <- data.frame(
  date = format(Qm3sNA2$DatesR[i0], "%Y-%m-%d"),
  Y = Qm3sNA2[["54001"]][i0],
  U_fallback = Qm3sNA2[["Intake"]][i0],
  U_naive = Qm3sNA1[["Intake"]][i0]
)
for (v in names(tabNA)[-1]) {
  tabNA[[v]] <- ifelse(is.na(tabNA[[v]]), "NA", round(tabNA[[v]], 2))
}
knitr::kable(tabNA, row.names = FALSE)

## ----na_count-----------------------------------------------------------------
tabNAcount <- data.frame(
  variable = c(
    "Qsim at 54001, open-loop reference",
    "Qsim at 54001, run with the fallback order",
    "Qsim at 54001, run without fallback",
    "U, without fallback",
    "U, with the fallback order"
  ),
  NA_values = c(
    sum(is.na(OMNAref[["54001"]]$Qsim)),
    sum(is.na(Qm3sNA2[["54001"]])),
    sum(is.na(Qm3sNA1[["54001"]])),
    sum(is.na(Qm3sNA1[["Intake"]])),
    sum(is.na(Qm3sNA2[["Intake"]]))
  )
)
knitr::kable(tabNAcount, row.names = FALSE)

## -----------------------------------------------------------------------------
oldpar <- par(mfrow = c(2, 1), mar = c(2.5, 4, 1, 1))
i30 <- seq(1, 30)
plot(
  i30,
  Qm3sNA2[["54001"]][i30],
  type = "l",
  col = "grey40",
  lwd = 2,
  main = "Flow at 54001 (first 30 days of the run)",
  xlab = "day",
  ylab = "Qsim (m3/s)"
)
abline(v = c(4.5, 12.5), col = "red", lty = 2)
plot(
  i30,
  Qm3sNA2[["Intake"]][i30],
  type = "l",
  col = "blue",
  lwd = 2,
  main = "",
  xlab = "day",
  ylab = "U (m3/s)"
)
lines(i30, Qm3sNA1[["Intake"]][i30], col = "red", lty = 2)
legend(
  "topleft",
  legend = c("with fallback order", "without"),
  col = c("blue", "red"),
  lty = c(1, 2)
)
par(oldpar)

