Commit 9f87cacb authored by de Lavenne Alban's avatar de Lavenne Alban
Browse files

fix: to pass CRAN checks on vignettes

No related merge requests found
Showing with 39 additions and 19 deletions
+39 -19
...@@ -2,7 +2,7 @@ Package: transfR ...@@ -2,7 +2,7 @@ Package: transfR
Type: Package Type: Package
Title: Transfer of Hydrograph from Gauged to Ungauged Catchments Title: Transfer of Hydrograph from Gauged to Ungauged Catchments
Version: 1.0.0 Version: 1.0.0
Date: 2022-05-11 Date: 2022-10-04
Authors@R: c( Authors@R: c(
person("Alban", "de Lavenne", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9448-3490"), email = "alban.delavenne@inrae.fr"), person("Alban", "de Lavenne", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9448-3490"), email = "alban.delavenne@inrae.fr"),
person("Christophe", "Cudennec", role = c("ths"), comment = c(ORCID = "0000-0002-1707-8926"), email = "christophe.cudennec@agrocampus-ouest.fr"), person("Christophe", "Cudennec", role = c("ths"), comment = c(ORCID = "0000-0002-1707-8926"), email = "christophe.cudennec@agrocampus-ouest.fr"),
......
...@@ -64,11 +64,11 @@ as_transfr <- function(object, st, uc, lagtime, surface, delineation, outlet, ce ...@@ -64,11 +64,11 @@ as_transfr <- function(object, st, uc, lagtime, surface, delineation, outlet, ce
# if("Qobs"%in%names(st)) st[[which(names(st)=="Qobs")]] <- units::set_units(st[[which(names(st)=="Qobs")]],"m^3/s") # if("Qobs"%in%names(st)) st[[which(names(st)=="Qobs")]] <- units::set_units(st[[which(names(st)=="Qobs")]],"m^3/s")
#--Deducing time step #--Deducing time step
deltat <- unique(difftime(st_get_dimension_values(st,1)[-1],st_get_dimension_values(st,1)[-dim(st)[1]],units="mins")) deltat <- unique(difftime(st_get_dimension_values(st,1)[-1], st_get_dimension_values(st,1)[-dim(st)[1]], units = "mins"))
if(length(deltat)==1){ if(length(deltat)==1){
deltat <- units::set_units(deltat,"min") deltat <- units::set_units(deltat, "min")
object$deltat <- deltat object$deltat <- deltat
}else{stop("Time step must be steady.")} }else{stop(paste0("Time step must be steady. Detected time steps: ", paste0(deltat, "min", collapse = ", "), "."))}
#--Set up units #--Set up units
if("Qobs"%in%names(st)) st[["Qobs"]] <- units::set_units(st[["Qobs"]],"m^3/s") if("Qobs"%in%names(st)) st[["Qobs"]] <- units::set_units(st[["Qobs"]],"m^3/s")
......
...@@ -25,8 +25,8 @@ This package aims to estimate discharge time series of ungauged catchments (non- ...@@ -25,8 +25,8 @@ This package aims to estimate discharge time series of ungauged catchments (non-
An object of class transfR needs to be created first with the function `as_transfr()`. It will also be used to gather all the catchment attributes and intermediary results from the different steps. This object needs to be created from two users inputs: An object of class transfR needs to be created first with the function `as_transfr()`. It will also be used to gather all the catchment attributes and intermediary results from the different steps. This object needs to be created from two users inputs:
* a spatio-temporal object (stars object) that is describing both discharge and the corresponding spatial support (outlet, centroid or catchment boundary); see vignette [Preparation of input data: creation of a stars object](https://cran.r-project.org/web/packages/transfR/vignettes/V02_inputs_preparation_stars.html). * a spatio-temporal object (stars object) that is describing both discharge and the corresponding spatial support (outlet, centroid or catchment boundary); see vignette [Preparation of input data: creation of a stars object](https://cran.r-project.org/package=transfR/vignettes/V02_inputs_preparation_stars.html).
* raster maps of hydraulic length (stars or matrix object) for each catchment describing the flow path length from each pixel to the outlet within the river network [@Cudennec2004; @Aouissi2013]; see vignette [Preparation of input data: geomorphological analysis with whitebox](https://cran.r-project.org/web/packages/transfR/vignettes/V03_inputs_preparation_whitebox.html). * raster maps of hydraulic length (stars or matrix object) for each catchment describing the flow path length from each pixel to the outlet within the river network [@Cudennec2004; @Aouissi2013]; see vignette [Preparation of input data: geomorphological analysis with whitebox](https://cran.r-project.org/package=transfR/vignettes/V03_inputs_preparation_whitebox.html).
This package does not provide functions to create these two inputs. It needs to be prepared beforehand by the user. Several GIS softwares offer possibilities to extract them from a digital elevation model such as GRASS toolkits [@Jasiewicz2011], Whitebox GAT (see @Lindsay2016 or [WhiteboxTools](https://github.com/jblindsay/whitebox-tools)), TauDEM (D. Tarboton, Utah State University) or online services (@Squividant2015 for catchment delineation only). The vignettes mentioned above give some guidance on the preparation of input data. This package does not provide functions to create these two inputs. It needs to be prepared beforehand by the user. Several GIS softwares offer possibilities to extract them from a digital elevation model such as GRASS toolkits [@Jasiewicz2011], Whitebox GAT (see @Lindsay2016 or [WhiteboxTools](https://github.com/jblindsay/whitebox-tools)), TauDEM (D. Tarboton, Utah State University) or online services (@Squividant2015 for catchment delineation only). The vignettes mentioned above give some guidance on the preparation of input data.
......
...@@ -27,17 +27,18 @@ library(transfR) ...@@ -27,17 +27,18 @@ library(transfR)
data(Oudon) data(Oudon)
wd <- tempdir(check = TRUE) wd <- tempdir(check = TRUE)
st_write(st_sf(ID = paste0("ID",1:6), geom = st_geometry(Oudon$obs)), st_write(st_sf(ID = paste0("ID", 1:6), geom = st_geometry(Oudon$obs)),
dsn = file.path(wd,"catchments.shp"), delete_layer = T) dsn = file.path(wd, "catchments.shp"), delete_layer = TRUE)
write.table(data.frame(DateTime = st_get_dimension_values(Oudon$obs,1), write.table(data.frame(DateTime = format(st_get_dimension_values(Oudon$obs,1),
"%Y-%m-%d %H:%M:%S"),
ID1 = Oudon$obs$Qobs[,1], ID1 = Oudon$obs$Qobs[,1],
ID2 = Oudon$obs$Qobs[,2], ID2 = Oudon$obs$Qobs[,2],
ID3 = Oudon$obs$Qobs[,3], ID3 = Oudon$obs$Qobs[,3],
ID4 = Oudon$obs$Qobs[,4], ID4 = Oudon$obs$Qobs[,4],
ID5 = Oudon$obs$Qobs[,5], ID5 = Oudon$obs$Qobs[,5],
ID6 = Oudon$obs$Qobs[,6]), ID6 = Oudon$obs$Qobs[,6]),
file = file.path(wd,"discharge.txt"), file = file.path(wd, "discharge.txt"),
col.names = T, row.names = F, sep = "\t", quote = F) col.names = TRUE, row.names = FALSE, sep = ";", quote = FALSE)
``` ```
## 1. Reading a vector layer with sf ## 1. Reading a vector layer with sf
...@@ -48,7 +49,7 @@ It is advised to use the [sf](https://cran.r-project.org/package=sf) package to ...@@ -48,7 +49,7 @@ It is advised to use the [sf](https://cran.r-project.org/package=sf) package to
```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE} ```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE}
library(sf) library(sf)
catchments <- st_read(file.path(wd,"catchments.shp"), "catchments", stringsAsFactors = F) catchments <- st_read(file.path(wd, "catchments.shp"), "catchments", stringsAsFactors = FALSE)
obs_sf <- catchments[1:5,] # Gauged catchments obs_sf <- catchments[1:5,] # Gauged catchments
sim_sf <- catchments[6,] # Ungauged catchments sim_sf <- catchments[6,] # Ungauged catchments
``` ```
...@@ -59,9 +60,10 @@ It is advised to provide the units of your discharge time series using the [unit ...@@ -59,9 +60,10 @@ It is advised to provide the units of your discharge time series using the [unit
```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE} ```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE}
library(units) library(units)
Q <- read.table(file.path(wd,"discharge.txt"), header = T, sep = "\t") Q <- read.table(file.path(wd, "discharge.txt"), header = TRUE, sep = ";",
colClasses = c("character", rep("numeric", 6)))
Qmatrix <- as.matrix(Q[,-1]) Qmatrix <- as.matrix(Q[,-1])
Qmatrix <- set_units(Qmatrix,"m^3/s") Qmatrix <- set_units(Qmatrix, "m^3/s")
``` ```
## 3. Creating a stars object ## 3. Creating a stars object
...@@ -99,3 +101,8 @@ sim$st ...@@ -99,3 +101,8 @@ sim$st
## References ## References
<div id="refs"></div> <div id="refs"></div>
```{r, echo=FALSE, message=FALSE, warning=FALSE, eval=TRUE, results='hide'}
# Cleaning temporary directory
unlink(wd, recursive = TRUE)
```
...@@ -20,7 +20,7 @@ knitr::opts_chunk$set( ...@@ -20,7 +20,7 @@ knitr::opts_chunk$set(
) )
``` ```
The hydrological modelling of the `transfR` package is based on a geomorphological analysis of the studied catchments. In this vignette, we give some guidance on how to perform this geomorphological analysis. More specifically, we extract the catchment delineation and hydraulic length maps from a digital elevation model (DEM). This analysis is one of the two inputs needed (together with the time series of flow observations) to build a `transfR` object and start using the `transfR` package (see the [Get started with transfR](https://cran.r-project.org/web/packages/transfR/vignettes/V01_get_started.html) vignette). The hydrological modelling of the `transfR` package is based on a geomorphological analysis of the studied catchments. In this vignette, we give some guidance on how to perform this geomorphological analysis. More specifically, we extract the catchment delineation and hydraulic length maps from a digital elevation model (DEM). This analysis is one of the two inputs needed (together with the time series of flow observations) to build a `transfR` object and start using the `transfR` package (see the [Get started with transfR](https://cran.r-project.org/package=transfR/vignettes/V01_get_started.html) vignette).
Hydraulic length is defined as the distance within the river network along an identified flow path to the outlet. It can be extracted from a DEM in many different ways, such as with the GRASS toolkits [@Jasiewicz2011], Whitebox GAT (see @Lindsay2016 or [WhiteboxTools](https://github.com/jblindsay/whitebox-tools)), TauDEM (D. Tarboton, Utah State University) or online services (@Squividant2015 for catchment delineation only). This vignette presents one possible workflow by making use of two main R packages: Hydraulic length is defined as the distance within the river network along an identified flow path to the outlet. It can be extracted from a DEM in many different ways, such as with the GRASS toolkits [@Jasiewicz2011], Whitebox GAT (see @Lindsay2016 or [WhiteboxTools](https://github.com/jblindsay/whitebox-tools)), TauDEM (D. Tarboton, Utah State University) or online services (@Squividant2015 for catchment delineation only). This vignette presents one possible workflow by making use of two main R packages:
...@@ -69,6 +69,19 @@ write_stars(dem_100m["warp"], file.path(wbt_wd,"dem_100m.tif")) ...@@ -69,6 +69,19 @@ write_stars(dem_100m["warp"], file.path(wbt_wd,"dem_100m.tif"))
The hydrological modelling distinguish the hillslope from the river network. Both will have very different transfer dynamics, and the `transfR` package aims to describe the transfer function of the river network only. Defining where this river network begins and the flow path it takes is a key, and non-trivial, issue for hydrogeomorphologists. The easiest way to draw a drainage network from a DEM is usually to define a minimum drainage area threshold at which the drainage network is assumed to start. However, defining this threshold can be difficult as it may vary spatially, especially with the geology of the region. It may therefore be better to use a known river network and force the flow paths to follow it. Here we will use the [French TOPAGE river network](https://bdtopage.eaufrance.fr/) as a reference and use a stream burning technique into the DEM following @Lindsay2016a and using the function [`whitebox::wbt_burn_streams_at_roads()`](https://www.whiteboxgeo.com/manual/wbt_book/available_tools/hydrological_analysis.html#BurnStreamsAtRoads). The hydrological modelling distinguish the hillslope from the river network. Both will have very different transfer dynamics, and the `transfR` package aims to describe the transfer function of the river network only. Defining where this river network begins and the flow path it takes is a key, and non-trivial, issue for hydrogeomorphologists. The easiest way to draw a drainage network from a DEM is usually to define a minimum drainage area threshold at which the drainage network is assumed to start. However, defining this threshold can be difficult as it may vary spatially, especially with the geology of the region. It may therefore be better to use a known river network and force the flow paths to follow it. Here we will use the [French TOPAGE river network](https://bdtopage.eaufrance.fr/) as a reference and use a stream burning technique into the DEM following @Lindsay2016a and using the function [`whitebox::wbt_burn_streams_at_roads()`](https://www.whiteboxgeo.com/manual/wbt_book/available_tools/hydrological_analysis.html#BurnStreamsAtRoads).
```{r, echo=FALSE, message=FALSE, warning=FALSE, eval=TRUE, results='hide'}
# If WhiteboxTools executable are not present, install it in the temporary directory
library(whitebox)
if(!wbt_init()){
install_whitebox(pkg_dir = wbt_wd)
exe_path <- file.path(wbt_wd, "WBT", "whitebox_tools") # Unix
if(!file.exists(exe_path)) exe_path <- paste0(exe_path,".exe") # Windows
if(!file.exists(exe_path)) stop("WhiteboxTools executable not found")
wbt_options(exe_path = exe_path,
wd = wbt_wd)
}
```
```{r, echo=TRUE, message=FALSE, warning=FALSE, results='hide'} ```{r, echo=TRUE, message=FALSE, warning=FALSE, results='hide'}
library(whitebox) library(whitebox)
...@@ -82,7 +95,7 @@ CoursEau_Topage2019 <- st_read(paste0("https://services.sandre.eaufrance.fr/geo/ ...@@ -82,7 +95,7 @@ CoursEau_Topage2019 <- st_read(paste0("https://services.sandre.eaufrance.fr/geo/
# Change projection and write files # Change projection and write files
network_topage <- st_transform(CoursEau_Topage2019, EPSG) network_topage <- st_transform(CoursEau_Topage2019, EPSG)
st_write(network_topage, file.path(wbt_wd,"network_topage.shp"), st_write(network_topage, file.path(wbt_wd, "network_topage.shp"),
delete_layer = TRUE, quiet = TRUE) delete_layer = TRUE, quiet = TRUE)
whitebox::wbt_rasterize_streams("network_topage.shp", whitebox::wbt_rasterize_streams("network_topage.shp",
base = "dem_100m.tif", base = "dem_100m.tif",
...@@ -230,8 +243,8 @@ names(hl_region) <- "hl" ...@@ -230,8 +243,8 @@ names(hl_region) <- "hl"
# Crop hydraulic length for each catchment # Crop hydraulic length for each catchment
hl <- list() hl <- list()
for(id in catchments$id){ for(id in catchments$id){
crop <- st_crop(hl_region,catchments[catchments$id==id,]) crop <- st_crop(hl_region, catchments[catchments$id==id,])
crop <- crop-min(crop$hl,na.rm=TRUE) crop <- crop-min(crop$hl, na.rm = TRUE)
crop$hl <- units::set_units(crop$hl, "m") crop$hl <- units::set_units(crop$hl, "m")
hl[[id]] <- crop hl[[id]] <- crop
} }
...@@ -251,7 +264,7 @@ plot(network, col = "white", lwd = 1.5, add = TRUE) ...@@ -251,7 +264,7 @@ plot(network, col = "white", lwd = 1.5, add = TRUE)
## 5. Creating a transfR object and running a simulation ## 5. Creating a transfR object and running a simulation
Catchment delineations can be used as the spatial dimension of `stars` objects to georeference the observed flow time series of gauged catchments and locate ungauged catchments (see vignette [Preparation of input data: creation of a stars object](https://cran.r-project.org/web/packages/transfR/vignettes/V02_inputs_preparation_stars.html) for details). Here we will create a `stars` object using the observed discharge of the `Blavet` dataset and the delineations that we just computed with `whitebox`. Catchment delineations can be used as the spatial dimension of `stars` objects to georeference the observed flow time series of gauged catchments and locate ungauged catchments (see vignette [Preparation of input data: creation of a stars object](https://cran.r-project.org/package=transfR/vignettes/V02_inputs_preparation_stars.html) for details). Here we will create a `stars` object using the observed discharge of the `Blavet` dataset and the delineations that we just computed with `whitebox`.
```{r, echo=TRUE, message=FALSE, warning=FALSE, results='hide'} ```{r, echo=TRUE, message=FALSE, warning=FALSE, results='hide'}
obs_st <- st_as_stars(list(Qobs = Blavet$obs$Qobs), obs_st <- st_as_stars(list(Qobs = Blavet$obs$Qobs),
......
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