title: 'Tutorial: structuration of a semi-distributive GR model'
author: "David Dorchies"
date: "19 mai 2020"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
library(griwrm)

Semi-distributive network description

List of nodes

seine_nodes <- readr::read_delim(
  file = "https://stratus.irstea.fr/d/0b18e688851a45478f7a/files/?p=/climaware_hydro/griwrm_network.txt&dl=1", 
  delim = ";"
)
seine_nodes

Create the ginet object

# Specify that all nodes are of run-off type
seine_nodes$runoff = TRUE
# Generate the ginet object
ginet <- Ginet(seine_nodes, list(id = "id_sgl", down = "id_aval", length = "distance_aval"))

Create the girop object

# Specify which run-off model to use
seine_nodes$model = "RunModel_GR4J"
# Generate girop object
girop <- Girop(seine_nodes, list(id = "id_sgl", area = "area"))

Load data

Hydrometeorological data on the Seine river basin from the ClimAware project:

urls <- 
  file.path(
    "https://stratus.irstea.fr/d/0b18e688851a45478f7a/files/?p=/climaware_hydro/Q_OBS_NAT", 
    paste0(ginet$id, "_NAT.txt&dl=1")
  )
names(urls) <- ginet$id

load_ts <- function(x) {
  ts <- readr::read_delim(file = x, 
             delim = ";", skip = 16, trim_ws = TRUE)
  ts$Date <- as.POSIXlt(lubridate::ymd(ts$Date))
  ts
}

l <- lapply(urls, load_ts)
gits <- Gits(ginet$id[1], l[[ginet$id[1]]], cols = list(date = "Date", Precip = "Ptot", PotEvap = "ETP", Qobs = "Qnat"))

for(id in ginet$id) {
  l[[id]]$Qnat
  l[[id]]$Qnat <- l[[id]]$Qnat * 86.4 / girop$area[girop$id == id]
  l[[id]]$Qnat[l[[id]]$Qnat < 0] <- NA
  gits <- merge(gits, Gits(id, l[[id]], cols = list(date = "Date", Precip = "Ptot", PotEvap = "ETP", Qobs = "Qnat")))
}

Save data for next vignettes

dir.create("_cache", showWarnings = FALSE)
save(ginet, girop, gits, file = "_cache/seine.RData")