From ed52b90478558f4e0055b1c9e9c0a9c5d745c46f Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@irstea.fr>
Date: Fri, 7 Aug 2020 14:16:30 +0200
Subject: [PATCH] doc: Add influenced flow calibration case

Refs #11
---
 DESCRIPTION                                   |   2 +-
 vignettes/V02_First_run.Rmd                   |   1 -
 vignettes/V03_First_Calibration.Rmd           |   2 +-
 vignettes/V04_Open-loop_influenced_flow.Rmd   |  24 +++-
 ..._Open-loop_influenced_flow_calibration.Rmd | 124 ++++++++++++++++++
 5 files changed, 146 insertions(+), 7 deletions(-)
 create mode 100644 vignettes/V05_Open-loop_influenced_flow_calibration.Rmd

diff --git a/DESCRIPTION b/DESCRIPTION
index f14242f..7166157 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,6 +1,6 @@
 Package: griwrm
 Title: GR based Integrated Water Resource Management
-Version: 0.3.0
+Version: 0.3.1
 Authors@R:
     person(given = "David",
            family = "Dorchies",
diff --git a/vignettes/V02_First_run.Rmd b/vignettes/V02_First_run.Rmd
index 91712d0..01e3de4 100644
--- a/vignettes/V02_First_run.Rmd
+++ b/vignettes/V02_First_run.Rmd
@@ -78,7 +78,6 @@ The user must at least define the following arguments:
 
 
 ```{r}
-library(lubridate)
 IndPeriod_Run <- seq(
   which(InputsModel[[1]]$DatesR == (InputsModel[[1]]$DatesR[1] + months(12))), # Set aside warm-up period
   length(InputsModel[[1]]$DatesR) # Until the end of the time series
diff --git a/vignettes/V03_First_Calibration.Rmd b/vignettes/V03_First_Calibration.Rmd
index 276f432..82796a0 100644
--- a/vignettes/V03_First_Calibration.Rmd
+++ b/vignettes/V03_First_Calibration.Rmd
@@ -1,5 +1,5 @@
 ---
-title: "03_First_Calibration"
+title: "First calibration of the model"
 author: "David Dorchies"
 vignette: >
   %\VignetteIndexEntry{Calibration of naturalised semi-distributive model}
diff --git a/vignettes/V04_Open-loop_influenced_flow.Rmd b/vignettes/V04_Open-loop_influenced_flow.Rmd
index 9b2a396..de3aa51 100644
--- a/vignettes/V04_Open-loop_influenced_flow.Rmd
+++ b/vignettes/V04_Open-loop_influenced_flow.Rmd
@@ -1,5 +1,6 @@
 ---
 title: "Running open-loop influenced semi-distributed flow model"
+author: "David Dorchies"
 output: rmarkdown::html_vignette
 vignette: >
   %\VignetteIndexEntry{Running open-loop influenced semi-distributed flow model}
@@ -182,9 +183,17 @@ DiagramGriwrm(griwrm2)
 
 ## Loading reservoir observation time series
 
+Description of the files, the columns and the type of connection (inlet / outlet) are defined in the list below:
+
 ```{r}
 
 lCfgReservoirs <- jsonlite::read_json(system.file("seine_data", "config_reservoirs.json", package = "griwrm"))
+str(lCfgReservoirs)
+```
+
+Then, we load observation data for each reservoir and each connection:
+
+```{r}
 
 for(reservoir_id in names(lCfgReservoirs)) {
   df <- readr::read_delim(
@@ -297,14 +306,13 @@ for(id in ids) {
   ts$Q <- ts$Q * 86.4 / griwrm$area[griwrm$id == id] / 1000
   # Setting data gaps to NA
   ts$Q[ts$Q <= 0] <- NA
-  # Qnat column is merged into Qobs dataframe
+  # Column is merged into Qobs dataframe
   Qobs2 <- MergeTS(Qobs2, id, ts[,c("Date", "Q")])
 }
 ```
 
-### Comparison with simulated flows
-
-```{r, fig.height = 5, fig.width = 8}
+## Save data for next vignettes
+```{r}
 ReduceOutputsModel <- function(OutputsModels, IndPeriod) {
   items <- names(OutputsModels)
   OutputsModelsOut <- sapply(items, function(x) {OutputsModels[[x]] <- OutputsModels[[x]][IndPeriod]})
@@ -313,6 +321,13 @@ ReduceOutputsModel <- function(OutputsModels, IndPeriod) {
   return(OutputsModelsOut)
 }
 
+QobsReservoirs <- Qobs[Qobs$DatesR < "2008-08-01",c("DatesR", reservoir_connections$id)]
+save(QobsReservoirs, Qobs2, griwrm2, ReduceOutputsModel, file = "_cache/V04.RData")
+```
+
+### Comparison with simulated flows
+
+```{r, fig.height = 5, fig.width = 8}
 htmltools::tagList(lapply(
   ids,
   function(x) {
@@ -325,3 +340,4 @@ htmltools::tagList(lapply(
 ```
 
 
+
diff --git a/vignettes/V05_Open-loop_influenced_flow_calibration.Rmd b/vignettes/V05_Open-loop_influenced_flow_calibration.Rmd
new file mode 100644
index 0000000..ced3831
--- /dev/null
+++ b/vignettes/V05_Open-loop_influenced_flow_calibration.Rmd
@@ -0,0 +1,124 @@
+---
+title: "Calibration of an open-loop influenced flow model"
+author: "David Dorchies"
+output: rmarkdown::html_vignette
+vignette: >
+  %\VignetteIndexEntry{Calibration of an open-loop influenced flow model}
+  %\VignetteEngine{knitr::rmarkdown}
+  %\VignetteEncoding{UTF-8}
+---
+
+```{r, include = FALSE}
+knitr::opts_chunk$set(
+  collapse = TRUE,
+  comment = "#>"
+)
+```
+
+```{r setup}
+library(griwrm)
+```
+
+This vignette proposes an example of calibration of influenced flow with the Marne reservoir. It will used influenced observation flows directly measured at gauging stations and flows recorded at reservoir inlets and outlet.
+
+## Set the data
+
+Loading naturalised data and influenced flow configuration:
+
+```{r}
+load("_cache/V01b.RData")
+load("_cache/V04.RData")
+```
+
+Remove extra items from global configuration
+
+```{r}
+selectedNodes <- c("MARNE_P23", "STDIZ_04", "LOUVE_19", "VITRY_25", "MARNE_P28", "MARNE_R25", "CHALO_21", "MONTR_18", "NOISI_17")
+griwrm3 <- griwrm2[griwrm2$id %in% selectedNodes,]
+griwrm3[griwrm3$id == "NOISI_17", c("down", "length")] = NA # Downstream station instead of PARIS_05
+DiagramGriwrm(griwrm3)
+```
+
+Generate `GriwrmInputsModel` object
+
+```{r}
+Qobs3 <- cbind(Qobs2, QobsReservoirs)
+iEnd <-which(DatesR == lubridate::ymd("2008-07-31"))
+InputsModel3 <- CreateInputsModel(griwrm3, DatesR[1:iEnd], Precip[1:iEnd,], PotEvap[1:iEnd,], Qobs3)
+```
+
+## GriwmRunOptions object
+
+
+```{r}
+IndPeriod_Run <- seq(
+  which(InputsModel3[[1]]$DatesR == (InputsModel3[[1]]$DatesR[1] + months(12))), # Set aside warm-up period
+  length(InputsModel3[[1]]$DatesR) # Until the end of the time series
+)
+```
+
+The warmup period could also be defined as is:
+
+```{r}
+IndPeriod_WarmUp = seq(1,IndPeriod_Run[1]-1)
+```
+
+
+
+```{r}
+RunOptions <- CreateRunOptions(
+  InputsModel = InputsModel3,
+  IndPeriod_WarmUp = IndPeriod_WarmUp,
+  IndPeriod_Run = IndPeriod_Run
+)
+```
+
+## InputsCrit object
+
+```{r}
+InputsCrit <- CreateInputsCrit(
+  InputsModel = InputsModel3,
+  FUN_CRIT = airGR::ErrorCrit_KGE2,
+  RunOptions = RunOptions, Qobs = Qobs3
+)
+```
+
+## GriwrmCalibOptions object
+
+```{r}
+CalibOptions <- CreateCalibOptions(InputsModel3)
+str(CalibOptions)
+```
+
+## Calibration
+
+```{r warning = FALSE}
+OutputsCalib <- Calibration(InputsModel3, RunOptions, InputsCrit, CalibOptions)
+```
+
+## Run model with Michel calibration
+
+```{r}
+ParamMichel <- sapply(griwrm3$id, function(x) {OutputsCalib[[x]]$Param})
+
+OutputsModels3 <- RunModel(
+  InputsModel = InputsModel3,
+  RunOptions = RunOptions,
+  Param = ParamMichel
+)
+```
+
+### Comparison with simulated flows
+
+```{r, fig.height = 5, fig.width = 8}
+htmltools::tagList(lapply(
+  griwrm3$id[!is.na(griwrm3$model)],
+  function(x) {
+    Q3 <- Qobs3[RunOptions[[1]]$IndPeriod_Run,x]
+    iQ3 <- which(!is.na(Q3))
+    IndPeriod_Obs <- iQ3[1]:tail(iQ3,1)
+    OutputsModels <- ReduceOutputsModel(OutputsModels3[[x]], IndPeriod_Obs)
+    plot(OutputsModels, Qobs = Q3[IndPeriod_Obs] , main = x)
+  }
+))
+```
-- 
GitLab