From 9f87cacbe16dda8c11a1954cd05e25aa080da572 Mon Sep 17 00:00:00 2001 From: de Lavenne Alban <alban.de-lavenne@irstea.fr> Date: Thu, 6 Oct 2022 20:31:27 +0200 Subject: [PATCH] fix: to pass CRAN checks on vignettes --- DESCRIPTION | 2 +- R/as_transfr.R | 6 ++--- vignettes/V01_get_started.Rmd | 4 ++-- vignettes/V02_inputs_preparation_stars.Rmd | 23 ++++++++++++------- vignettes/V03_inputs_preparation_whitebox.Rmd | 23 +++++++++++++++---- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d855ad9..362791d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: transfR Type: Package Title: Transfer of Hydrograph from Gauged to Ungauged Catchments Version: 1.0.0 -Date: 2022-05-11 +Date: 2022-10-04 Authors@R: c( 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"), diff --git a/R/as_transfr.R b/R/as_transfr.R index 293cd3e..b1de935 100644 --- a/R/as_transfr.R +++ b/R/as_transfr.R @@ -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") #--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){ - deltat <- units::set_units(deltat,"min") + deltat <- units::set_units(deltat, "min") 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 if("Qobs"%in%names(st)) st[["Qobs"]] <- units::set_units(st[["Qobs"]],"m^3/s") diff --git a/vignettes/V01_get_started.Rmd b/vignettes/V01_get_started.Rmd index 2b1bc0f..881f89c 100644 --- a/vignettes/V01_get_started.Rmd +++ b/vignettes/V01_get_started.Rmd @@ -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: -* 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). -* 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). +* 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/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. diff --git a/vignettes/V02_inputs_preparation_stars.Rmd b/vignettes/V02_inputs_preparation_stars.Rmd index 84c701b..a603e7b 100644 --- a/vignettes/V02_inputs_preparation_stars.Rmd +++ b/vignettes/V02_inputs_preparation_stars.Rmd @@ -27,17 +27,18 @@ library(transfR) data(Oudon) wd <- tempdir(check = TRUE) -st_write(st_sf(ID = paste0("ID",1:6), geom = st_geometry(Oudon$obs)), - dsn = file.path(wd,"catchments.shp"), delete_layer = T) -write.table(data.frame(DateTime = st_get_dimension_values(Oudon$obs,1), +st_write(st_sf(ID = paste0("ID", 1:6), geom = st_geometry(Oudon$obs)), + dsn = file.path(wd, "catchments.shp"), delete_layer = TRUE) +write.table(data.frame(DateTime = format(st_get_dimension_values(Oudon$obs,1), + "%Y-%m-%d %H:%M:%S"), ID1 = Oudon$obs$Qobs[,1], ID2 = Oudon$obs$Qobs[,2], ID3 = Oudon$obs$Qobs[,3], ID4 = Oudon$obs$Qobs[,4], ID5 = Oudon$obs$Qobs[,5], ID6 = Oudon$obs$Qobs[,6]), - file = file.path(wd,"discharge.txt"), - col.names = T, row.names = F, sep = "\t", quote = F) + file = file.path(wd, "discharge.txt"), + col.names = TRUE, row.names = FALSE, sep = ";", quote = FALSE) ``` ## 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 ```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE} 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 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 ```{r, echo=TRUE, message=FALSE, results='hide', eval=TRUE} 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 <- set_units(Qmatrix,"m^3/s") +Qmatrix <- set_units(Qmatrix, "m^3/s") ``` ## 3. Creating a stars object @@ -99,3 +101,8 @@ sim$st ## References <div id="refs"></div> + +```{r, echo=FALSE, message=FALSE, warning=FALSE, eval=TRUE, results='hide'} +# Cleaning temporary directory +unlink(wd, recursive = TRUE) +``` diff --git a/vignettes/V03_inputs_preparation_whitebox.Rmd b/vignettes/V03_inputs_preparation_whitebox.Rmd index d603e75..d59c1b9 100644 --- a/vignettes/V03_inputs_preparation_whitebox.Rmd +++ b/vignettes/V03_inputs_preparation_whitebox.Rmd @@ -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: @@ -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). +```{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'} library(whitebox) @@ -82,7 +95,7 @@ CoursEau_Topage2019 <- st_read(paste0("https://services.sandre.eaufrance.fr/geo/ # Change projection and write files 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) whitebox::wbt_rasterize_streams("network_topage.shp", base = "dem_100m.tif", @@ -230,8 +243,8 @@ names(hl_region) <- "hl" # Crop hydraulic length for each catchment hl <- list() for(id in catchments$id){ - crop <- st_crop(hl_region,catchments[catchments$id==id,]) - crop <- crop-min(crop$hl,na.rm=TRUE) + crop <- st_crop(hl_region, catchments[catchments$id==id,]) + crop <- crop-min(crop$hl, na.rm = TRUE) crop$hl <- units::set_units(crop$hl, "m") hl[[id]] <- crop } @@ -251,7 +264,7 @@ plot(network, col = "white", lwd = 1.5, add = TRUE) ## 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'} obs_st <- st_as_stars(list(Qobs = Blavet$obs$Qobs), -- GitLab