diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..52f88fa --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,4 @@ +^renv$ +^renv\.lock$ +^LICENSE\.md$ +.vscode diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97ee4fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# History files +.Rhistory +.Rapp.history + +# Session Data files +.RData +.RDataTmp + +# User-specific files +.Ruserdata + +# Output files from R CMD build +/*.tar.gz + +# Output files from R CMD check +/*.Rcheck/ + +# RStudio files +.Rproj.user/ + +# produced vignettes +vignettes/*.html +vignettes/*.pdf + +# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 +.httr-oauth + +# knitr and R markdown default cache directories +*_cache/ +/cache/ + +# Temporary files created by R markdown +*.utf8.md +*.knit.md + +# R Environment Variables +.Renviron + +# Rcpp artifacts +*.o +*.so +*.dll + +# ignore renv files; if people want to init renv from DESCRIPTION, they should be able to +renv* +.Rprofile diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..708ec8b --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,37 @@ +{ + // Setup derived from https://github.com/renkun-ken/vscode-rcpp-demo/ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${userHome}/.cache/R/renv/library/${workspaceFolderBasename}*/**/include", + "${userHome}/R/x86_64-pc-linux-gnu-library/4.2/Rcpp/include", + "/usr/share/R/include" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c11", + "cppStandard": "c++17" + }, + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**", + "${userHome}/Library/Caches/org.R-project.R/R/renv/library/${workspaceFolderBasename}*/**/include", + "/Library/Frameworks/R.framework/Resources/include", + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", + "/opt/homebrew/include" + ], + "defines": [], + "macFrameworkPath": [ + "/System/Library/Frameworks", + "/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17" + } + ], + "version": 4 +} diff --git a/DESCRIPTION b/DESCRIPTION index 3063add..6117511 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,12 +2,27 @@ Package: rsplash Type: Package Title: Simple Process-led Algorithms for Simulating Habitats v2.0 Version: 1.0 +Encoding: UTF-8 Date: 2022-11-30 Author: David Sandoval Maintainer: David Sandoval Description: Implementaion of the SPLASH v2.0 Algorithms for calibration-free calculations of water and energy fluxes. License: AFL (>= 3) -Imports: Rcpp, raster, xts, parallel, topmodel +Imports: + doParallel, + fastmatch, + lubridate, + parallel, + raster, + Rcpp, + topmodel, + xts +Suggests: + spatial.tools, + splashTools +Remotes: + github::dsval/splashTools@d103f33 ByteCompile: TRUE LinkingTo: Rcpp -RoxygenNote: 7.1.1 +RoxygenNote: 7.3.2 +Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 18f57a4..7bc0984 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,14 +1,13 @@ -useDynLib(rsplash, .registration=TRUE) -exportPattern("^[[:alpha:]]+") -import(Rcpp) -export(splash.point) +# Generated by roxygen2: do not edit by hand + export(splash.grid) +export(splash.point) export(unSWC) export(unSWC.grid) export(upslope_area) +import(Rcpp) import(raster) import(topmodel) import(xts) - - - +importFrom(Rcpp,sourceCpp) +useDynLib(rsplash, .registration = TRUE) diff --git a/R/data.R b/R/data.R new file mode 100644 index 0000000..f974bc8 --- /dev/null +++ b/R/data.R @@ -0,0 +1,38 @@ +#' Sample Data +#' +#' @format +#' A list containing +#' - `forcing` as an [xts::xts()] object, and +#' - `soil` as a named double vector. +"ATNeu_example" + +#' Bourne SNOTEL station sample data +#' +#' @source +#' +#' @format +#' A list containing +#' - `forcing` as an [xts::xts()] object, and +#' - `max_depth_sm` numeric vector length 1 +#' - `soil` as a named double vector. +#' - `md` a metadata [data.frame()] with 1 row and 28 columns +"Bourne" + +#' South American sample gridded data +#' +#' Used to construct a [splash.grid()] call. The CRU in the name likely stands for the +#' [Climatic Research +#' Unit](https://research-portal.uea.ac.uk/en/organisations/climatic-research-unit) at +#' the University of East Anglia. +#' +#' @source +#' +#' @format +#' A list containing +#' - `tc` a [RasterBrick][raster::Raster-class] of monthly temperature data (mean deg C) +#' - `pn` a [RasterBrick][raster::Raster-class] of monthly precipitation data (mean mm/d or mm/month?) +#' - `sw_in` a [RasterBrick][raster::Raster-class] of monthly precipitation data (mean W m^-2) +#' - `elev` a [RasterLayer][raster::Raster-class] representing a DEM over South America (presumably in average meters above sealevel) +#' - `soil` a [RasterBrick][raster::Raster-class] representing the soil fractions +#' - ⚠️ sometimes adding up to less, sometimes to more than 100% ⚠️ +"SA_cru" diff --git a/R/splash.grid.R b/R/splash.grid.R index fbd0dc3..ed54c39 100644 --- a/R/splash.grid.R +++ b/R/splash.grid.R @@ -3,13 +3,16 @@ #' R/C++ implementation of the SPLASH v.2.0 algorithm (Davis et al., 2017; Sandoval et al., in prep.). #' #' @param sw_in Incoming shortwave solar radiation (W m-2), Raster* object of monthly or daily averages with z time dimension. -#' @param tc Air temperature (�C), same dimensions as sw_in +#' @param tc Air temperature (°C), same dimensions as sw_in #' @param pn Precipitation (mm), same dimensions as sw_in #' @param elev Elevation (m.a.s.l) #' @param soil Raster* object with the layers organized as sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3) and depth(m) #' @param outdir (optional) directory path where the results will be saved, the working directory by default #' @param tmpdir (optional) directory path where the temporary files will be saved -#' @param sim.control (optional) list including options to control the output: output.mode="monthly" by default, "daily" also available, inmem=FALSE by default write all the results to the disk by chunks to save RAM, sacrificing speed. +#' @param sim.control (optional) list including options to control the output: +#' - monthly_out = TRUE by default; daily if FALSE. +#' - inmem = FALSE by default; writes all the results to the disk by chunks to save RAM, +#' sacrificing speed. #' @return a list of rasterBricks objects with z time dimension, all of them saved to outdir as netcdf files: #' \itemize{ #' \item \eqn{wn}: Soil water content (mm) within the first 2 m of depth. @@ -24,7 +27,21 @@ #' @keywords splash, evapotranspiration, soil moisture #' @export #' @examples -#' splash.grid(sw_in=200, tc=15, pn=10, lat=44,elev=1800,slop=10,asp=270,soil_data=c(sand=44,clay=2,OM=6,fgravel=12)) +#' \dontrun{data(SA_cru) +#' splash.grid( +#' sw_in = SA_cru$sw_in, +#' tc = SA_cru$tc, +#' pn = SA_cru$pn, +#' elev = SA_cru$elev, +#' soil = SA_cru$soil, +#' outdir = getwd(), +#' tmpdir = dirname(raster::rasterTmpFile()), +#' sim.control = list( +#' monthly_out = FALSE, +#' inmem = TRUE +#' ) +#' )} + splash.grid<-function(sw_in, tc, pn, elev, soil, outdir=getwd(),tmpdir=dirname(rasterTmpFile()),sim.control=list(monthly_out=TRUE,inmem=FALSE)){ ########################################################################### # 00. Check if parallel computation is required by the user and if the dimensions of the raster objects match @@ -267,7 +284,7 @@ splash.grid<-function(sw_in, tc, pn, elev, soil, outdir=getwd(),tmpdir=dirname(r bs <- blockSize(sw_in, minblocks=nodes*10) } ########################### export the vaiables to the nodes - parallel:::clusterExport(cl, c("sw_in","tc","pn","elev","lat","terraines",'soil','resolution','Au','ztime','bs','splash.point','sim.control'),envir=environment()) + parallel::clusterExport(cl, c("sw_in","tc","pn","elev","lat","terraines",'soil','resolution','Au','ztime','bs','splash.point','sim.control'),envir=environment()) pb <- pbCreate(bs$n) pb <- txtProgressBar(min=1,max = max(bs$n,2), style = 3) #cat("computing...","\n") @@ -358,7 +375,7 @@ splash.grid<-function(sw_in, tc, pn, elev, soil, outdir=getwd(),tmpdir=dirname(r ############################################################################################### for (i in 1:bs$n) { - d <- parallel:::recvOneData(cl) + d <- parallel::recvOneData(cl) # error? if (! d$value$success) { stop('cluster error:',"\n",d$value$value) diff --git a/R/splash.point.R b/R/splash.point.R index fa12ffb..2a76045 100644 --- a/R/splash.point.R +++ b/R/splash.point.R @@ -3,11 +3,11 @@ #' R/C++ implementation of the SPLASH v.2.0 algorithm (Davis et al., 2017; Sandoval et al., in prep.). #' #' @param sw_in Incoming shortwave solar radiation (W m-2), timeseries object of monthly or daily averages. -#' @param tc Air temperature (�C), same timestep as sw_in +#' @param tc Air temperature (°C), same timestep as sw_in #' @param pn Precipitation (mm), same timestep as sw_in #' @param elev Elevation (m.a.s.l) -#' @param slop Terrain feature: slope inclination (�) -#' @param asp Terrain feature: slope orientation (�), standard clockwise from 0.0� North +#' @param slop Terrain feature: slope inclination (°) +#' @param asp Terrain feature: slope orientation (°), standard clockwise from 0.0° North #' @param soil_data Soil data organized as a vector in the way: c(sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3)) #' @return a time series matrix including: #' \itemize{ @@ -25,7 +25,20 @@ #' @keywords splash, evapotranspiration, soil moisture #' @export #' @examples -#' splash.point(sw_in=200, tc=15, pn=10, lat=44,elev=1800,slop=10,asp=270,soil_data=c(sand=44,clay=2,OM=6,fgravel=12)) +#' data(Bourne) +#' splash.point( +#' sw_in = Bourne$forcing$sw_in, +#' tc = Bourne$forcing$Ta, +#' pn = Bourne$forcing$P, +#' lat = Bourne$md$latitude, +#' elev = Bourne$md$elev_m, +#' slop = Bourne$md$slop_250m, +#' asp = Bourne$md$asp_250m, +#' soil_data = Bourne$soil, +#' Au = Bourne$md$Aups_250m, +#' resolution = 250.0 +#' ) + splash.point<-function(sw_in, tc, pn, lat,elev,slop=0,asp=0,soil_data,Au=0,resolution=250,time_index=NULL,monthly_out=FALSE,ts_out=TRUE,verbose=TRUE){ ########################################################################### # 010. Check time info @@ -252,7 +265,7 @@ soil_hydro<-function(sand, clay, OM, fgravel=0,bd=NA, ...) { # by Texture and Organic Matter for Hydrologic Solutions. # Soil Sci. Soc. Am. J. 70, 1569. doi:10.2136/sssaj2005.0117 # Balland, V., Pollacco, J.A.P., Arp, P.A., 2008. Modeling soil hydraulic properties for - # a wide range of soil conditions. Ecol. Modell. 219, 300�316. doi:10.1016/j.ecolmodel.2008.07.009 + # a wide range of soil conditions. Ecol. Modell. 219, 300-316. doi:10.1016/j.ecolmodel.2008.07.009 # ************************************************************************ results<-list() ######################################################################################## @@ -524,7 +537,7 @@ frain_func<-function(tc,Tt,Tr,time_index){ # Inputs: # tc ..... double, air daily temperature C # Tt... double, parameter threshold temperature,where 50% of precipitation falls as rain - # Tr... double, TR is range of temperatures where both rainfall and snowfall can occur, in �C, typically around 13 �C + # Tr... double, TR is range of temperatures where both rainfall and snowfall can occur, in °C, typically around 13 °C # y ....... year # Returns: fraction of the precipitaion falling as rain # Features: calculates the fraction of the precipitaion falling as rain, accounting for monthly variations of the parameters @@ -568,7 +581,7 @@ snowfall_prob<-function(tc,lat,elev){ # Features: calculates the snowfall occurrence probability # Ref: Jennings, K.S., Winchell, T.S., Livneh, B., Molotch, N.P., 2018. Spatial variation of the - # rain-snow temperature threshold across the Northern Hemisphere. Nat. Commun. 9, 1�9. doi:10.1038/s41467-018-03629-7 + # rain-snow temperature threshold across the Northern Hemisphere. Nat. Commun. 9, 1-9. doi:10.1038/s41467-018-03629-7 # ************************************************************************ # calibration set #p_snow<-1/(1+exp(-0.5827+1.319*as.numeric(tc)-as.numeric(elev)*4.18E-4-abs(as.numeric(lat))*1.140E-2)) diff --git a/R/unsSWC.R b/R/unsSWC.R index 3e8c5f4..604879f 100644 --- a/R/unsSWC.R +++ b/R/unsSWC.R @@ -1,15 +1,13 @@ #' unsSWC #' #' Estimates water content using an analytca integral of the profile described by Brooks and Corey 1964 and calculates depth to water table if there is any -#' @param soil_data, vector as retrieved from splashTools::getSoilNRCS() or splashTools::getSoilSite() +#' @param soil_data, vector as retrieved from [splashTools::getSoilNRCS()] or [splashTools::getSoilSite()] #' @param uns_depth, maximum depth to calculate the accumulated water content in (m) #' @param wn, xts object, total water content of the profile from surface to bedrock (m) #' @param ouputdir, directory where to save the files #' @import xts #' @keywords splashTools #' @export -#' @examples -#' unSWC() unSWC<-function(soil_data,uns_depth,wn){ # test diff --git a/R/unsSWC.grid.R b/R/unsSWC.grid.R index ca54a1e..e086d22 100644 --- a/R/unsSWC.grid.R +++ b/R/unsSWC.grid.R @@ -8,8 +8,6 @@ #' @import raster #' @keywords splashTools #' @export -#' @examples -#' unSWC() unSWC.grid<-function(soil_data,uns_depth,wn,d_name ,outdir=getwd()){ ######################################################################### diff --git a/R/upslope_area.R b/R/upslope_area.R index e15c339..41068f5 100644 --- a/R/upslope_area.R +++ b/R/upslope_area.R @@ -1,14 +1,12 @@ #' upslope_area #' #' Computes n cells draining in/out and the contributing area per pixel in m2 using topmodel for small areas or Taudem for large, corrections acording to latitude done by raster::area -#' @param dem +#' @param dem a [RasterLayer][raster::Raster-class] Digital Elevation Model model passed to [topmodel::sinkfill()] #' @return a RasterStack with the contributing area per pixel, number of cells draining in, and number of cells draining out #' @import topmodel #' @import raster #' @keywords contributing area #' @export -#' @examples -#' splash.grid() upslope_area<-function(dem,resolution){ resolution<-cellStats(resolution, stat='mean', na.rm=TRUE) diff --git a/R/zzz.R b/R/zzz.R index 0161caf..20571ef 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,16 +1,9 @@ - -## Up until R 2.15.0, the require("methods") is needed but (now) -## triggers an warning from R CMD check -#.onLoad <- function(libname, pkgname){ -# #require("methods") ## needed with R <= 2.15.0 -# loadRcppModules() -#} - - -## For R 2.15.1 and later this also works. Note that calling loadModule() triggers -## a load action, so this does not have to be placed in .onLoad() or evalqOnLoad(). -loadModule("NumEx", TRUE) -loadModule("yada", TRUE) -loadModule("stdVector", TRUE) - - +## usethis namespace: start +#' @importFrom Rcpp sourceCpp +## usethis namespace: end +NULL + +## usethis namespace: start +#' @useDynLib rsplash, .registration = TRUE +## usethis namespace: end +NULL diff --git a/README.md b/README.md index 1b62868..bf6c5de 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,7 @@ addLegend(legend.loc = "topright", legend.names =c('SWC obs.','SWC sim.'),col=c( Sandoval, D., Prentice, I. C. and Nóbrega R. (in progress). Simple process-led algorithms for simulating habitats (SPLASH v.2.0): calibration-free calculations of water and energy fluxes Davis, T.W., Prentice, I.C., Stocker, B.D., Thomas, R.T., Whitley, R.J., Wang, H., Evans, B.J., Gallego-Sala, A. V., Sykes, M.T., Cramer, W., 2017. Simple process-led algorithms for simulating habitats (SPLASH v.1.0): robust indices of radiation, evapotranspiration and plant-available moisture. Geosci. Model Dev. 10, 689–708. doi:10.5194/gmd-10-689-2017 + +## Development Setup + +If you want to work on this codebase using Visual Studio Code, some setup files are included. Assuming you install the extensions `REditorSupport.r` and `ms-vscode.cpptools-extension-pack`, you should get a functioning environment set up and running by executing `renv::init()` and choosing the "explicit" option, i.e., reading package dependencies from the package `DESCRIPTION`. diff --git a/man/ATNeu_example.Rd b/man/ATNeu_example.Rd new file mode 100644 index 0000000..880c5aa --- /dev/null +++ b/man/ATNeu_example.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{ATNeu_example} +\alias{ATNeu_example} +\title{Sample Data} +\format{ +A list containing +\itemize{ +\item \code{forcing} as an \code{\link[xts:xts]{xts::xts()}} object, and +\item \code{soil} as a named double vector. +} +} +\usage{ +ATNeu_example +} +\description{ +Sample Data +} +\keyword{datasets} diff --git a/man/Bourne.Rd b/man/Bourne.Rd new file mode 100644 index 0000000..1bfc29e --- /dev/null +++ b/man/Bourne.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{Bourne} +\alias{Bourne} +\title{Bourne SNOTEL station sample data} +\format{ +A list containing +\itemize{ +\item \code{forcing} as an \code{\link[xts:xts]{xts::xts()}} object, and +\item \code{max_depth_sm} numeric vector length 1 +\item \code{soil} as a named double vector. +\item \code{md} a metadata \code{\link[=data.frame]{data.frame()}} with 1 row and 28 columns +} +} +\source{ +\url{https://wcc.sc.egov.usda.gov/nwcc/site?sitenum=361} +} +\usage{ +Bourne +} +\description{ +Bourne SNOTEL station sample data +} +\keyword{datasets} diff --git a/man/Rcpp_modules_examples.Rd b/man/Rcpp_modules_examples.Rd deleted file mode 100644 index a8ea240..0000000 --- a/man/Rcpp_modules_examples.Rd +++ /dev/null @@ -1,24 +0,0 @@ -\name{Rcpp Modules Examples} -\alias{Num} -\alias{World} -\alias{bar} -\alias{bla} -\alias{bla1} -\alias{bla2} -\alias{foo} -\alias{vec} -\alias{hello} -\alias{Rcpp_Num-class} -\alias{Rcpp_World-class} -\alias{Rcpp_vec-class} -\alias{C++Object-class} -\title{ - Functions and Objects created by Rcpp Modules Example -} -\description{ - These function and objects are accessible from R via the Rcpp Modules mechanism - which creates them based on the declaration in the C++ file. -} -\seealso{ - The Rcpp Modules vignette. -} \ No newline at end of file diff --git a/man/SA_cru.Rd b/man/SA_cru.Rd new file mode 100644 index 0000000..e87d0e7 --- /dev/null +++ b/man/SA_cru.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{SA_cru} +\alias{SA_cru} +\title{South American sample gridded data} +\format{ +A list containing +\itemize{ +\item \code{tc} a \link[raster:Raster-classes]{RasterBrick} of monthly temperature data (mean deg C) +\item \code{pn} a \link[raster:Raster-classes]{RasterBrick} of monthly precipitation data (mean mm/d or mm/month?) +\item \code{sw_in} a \link[raster:Raster-classes]{RasterBrick} of monthly precipitation data (mean W m^-2) +\item \code{elev} a \link[raster:Raster-classes]{RasterLayer} representing a DEM over South America (presumably in average meters above sealevel) +\item \code{soil} a \link[raster:Raster-classes]{RasterBrick} representing the soil fractions +\itemize{ +\item ⚠️ sometimes adding up to less, sometimes to more than 100\% ⚠️ +} +} +} +\source{ +\url{https://crudata.uea.ac.uk/cru/data/hrg/} +} +\usage{ +SA_cru +} +\description{ +Used to construct a \code{\link[=splash.grid]{splash.grid()}} call. The CRU in the name probably stands for the +\href{https://research-portal.uea.ac.uk/en/organisations/climatic-research-unit}{Climatic Research Unit} at +the University of East Anglia. The soil data's +} +\keyword{datasets} diff --git a/man/rsplash-package.Rd b/man/rsplash-package.Rd deleted file mode 100644 index 9028caf..0000000 --- a/man/rsplash-package.Rd +++ /dev/null @@ -1,32 +0,0 @@ -\name{rsplash-package} -\alias{rsplash-package} -\alias{rsplash} -\docType{package} -\title{\packageTitle{rsplash}} -\description{\packageDescription{rsplash}} -\details{ - The DESCRIPTION file: \packageDESCRIPTION{rsplash} - \packageIndices{rsplash} - - This section should provide a more detailed overview of how to use the - package, including the most important functions. -} -\author{ - \packageAuthor{rsplash} - - Maintainer: \packageMaintainer{rsplash} -} -\references{ - This optional section can contain literature or other references for - background information. -} -% Optionally other standard keywords, one per line, -% from the file KEYWORDS in the R documentation. -\keyword{package} -\seealso{ - Optional links to other man pages -} -\examples{ - ## Optional simple examples of the most important functions - ## Use \dontrun{} around code to be shown but not executed -} diff --git a/man/rsplashtest-package.Rd b/man/rsplashtest-package.Rd deleted file mode 100644 index 80921e8..0000000 --- a/man/rsplashtest-package.Rd +++ /dev/null @@ -1,34 +0,0 @@ -\name{rsplashtest-package} -\alias{rsplashtest-package} -\alias{rsplashtest} -\docType{package} -\title{ - A short title line describing what the package does -} -\description{ - A more detailed description of what the package does. A length - of about one to five lines is recommended. -} -\details{ - This section should provide a more detailed overview of how to use the - package, including the most important functions. -} -\author{ -Your Name, email optional. - -Maintainer: Your Name -} -\references{ - This optional section can contain literature or other references for - background information. -} -\keyword{ package } -\seealso{ - Optional links to other man pages -} -\examples{ - \dontrun{ - ## Optional simple examples of the most important functions - ## These can be in \dontrun{} and \donttest{} blocks. - } -} diff --git a/man/splash.grid.Rd b/man/splash.grid.Rd index 4ac50d1..e2ea4ad 100644 --- a/man/splash.grid.Rd +++ b/man/splash.grid.Rd @@ -4,43 +4,68 @@ \alias{splash.grid} \title{Simple process-led algorithms for simulating habitats (SPLASH v.2.0)} \usage{ -splash.grid(sw_in, tc, pn, elev, soil, outdir = getwd(), - tmpdir = dirname(rasterTmpFile()), sim.control = list(output.mode = - "monthly", inmem = FALSE)) +splash.grid( + sw_in, + tc, + pn, + elev, + soil, + outdir = getwd(), + tmpdir = dirname(rasterTmpFile()), + sim.control = list(monthly_out = TRUE, inmem = FALSE) +) } \arguments{ \item{sw_in}{Incoming shortwave solar radiation (W m-2), Raster* object of monthly or daily averages with z time dimension.} -\item{tc}{Air temperature (戼㸰C), same dimensions as sw_in} +\item{tc}{Air temperature (°C), same dimensions as sw_in} \item{pn}{Precipitation (mm), same dimensions as sw_in} \item{elev}{Elevation (m.a.s.l)} -\item{soil}{Raster* object with the layers organized as sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3)} +\item{soil}{Raster* object with the layers organized as sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3) and depth(m)} -\item{outdir}{(optional) directory path where the results will be saved, working directory by default} +\item{outdir}{(optional) directory path where the results will be saved, the working directory by default} -\item{tmpdir}{(optional) directory path where the temporary files will be saved, default temporary directory by default} +\item{tmpdir}{(optional) directory path where the temporary files will be saved} -\item{sim.control}{(optional) list including options to control the output: output.mode="monthly" by default, "daily" also available, inmem=FALSE by default write all the results to the disk by chunks to save RAM, sacrificing speed.} +\item{sim.control}{(optional) list including options to control the output: +\itemize{ +\item monthly_out = TRUE by default; daily if FALSE. +\item inmem = FALSE by default; writes all the results to the disk by chunks to save RAM, +sacrificing speed. +}} } \value{ a list of rasterBricks objects with z time dimension, all of them saved to outdir as netcdf files: \itemize{ - \item \eqn{W_n}: Soil water content (mm) within the first 2 m of depth. - \item \eqn{ro}: Runoff (mm d-1). - \item \eqn{pet}: Potential evapotranspiration (mm d-1). - \item \eqn{aet}: Actual evapotranspiration (mm d-1). - \item \eqn{snow}: Snow water equivalent (mm). - \item \eqn{bflow}: Lateral flow (mm d-1). +\item \eqn{wn}: Soil water content (mm) within the first 2 m of depth. +\item \eqn{ro}: Runoff (mm d-1). +\item \eqn{pet}: Potential evapotranspiration (mm d-1). +\item \eqn{aet}: Actual evapotranspiration (mm d-1). +\item \eqn{snow}: Snow water equivalent (mm). +\item \eqn{bflow}: Lateral flow (mm d-1). } } \description{ R/C++ implementation of the SPLASH v.2.0 algorithm (Davis et al., 2017; Sandoval et al., in prep.). } \examples{ -splash.grid(sw_in=200, tc=15, pn=10, lat=44,elev=1800,slop=10,asp=270,soil_data=c(sand=44,clay=2,OM=6,fgravel=12)) +\dontrun{data(SA_cru) +splash.grid( + sw_in = SA_cru$sw_in, + tc = SA_cru$tc, + pn = SA_cru$pn, + elev = SA_cru$elev, + soil = SA_cru$soil, + outdir = getwd(), + tmpdir = dirname(raster::rasterTmpFile()), + sim.control = list( + monthly_out = FALSE, + inmem = TRUE + ) +)} } \keyword{evapotranspiration,} \keyword{moisture} diff --git a/man/splash.point.Rd b/man/splash.point.Rd index dded320..5ae83a5 100644 --- a/man/splash.point.Rd +++ b/man/splash.point.Rd @@ -14,42 +14,58 @@ splash.point( asp = 0, soil_data, Au = 0, - resolution = 250 + resolution = 250, + time_index = NULL, + monthly_out = FALSE, + ts_out = TRUE, + verbose = TRUE ) } \arguments{ \item{sw_in}{Incoming shortwave solar radiation (W m-2), timeseries object of monthly or daily averages.} -\item{tc}{Air temperature (戼㸰C), same timestep as sw_in} +\item{tc}{Air temperature (°C), same timestep as sw_in} \item{pn}{Precipitation (mm), same timestep as sw_in} \item{elev}{Elevation (m.a.s.l)} -\item{slop}{Terrain feature: slope inclination (戼㸰)} +\item{slop}{Terrain feature: slope inclination (°)} -\item{asp}{Terrain feature: slope orientation (戼㸰), standard clockwise from 0.0戼㸰 North} +\item{asp}{Terrain feature: slope orientation (°), standard clockwise from 0.0° North} \item{soil_data}{Soil data organized as a vector in the way: c(sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3))} } \value{ a time series matrix including: \itemize{ - \item \eqn{wn}: Soil water content within the first 2m of depth (mm). - \item \eqn{ro}: Runoff (mm d-1). - \item \eqn{pet}: Potential evapotranspiration (mm d-1). - \item \eqn{aet}: Actual evapotranspiration (mm d-1). - \item \eqn{snow}: Snow water equivalent (mm). - \item \eqn{cond}: Condensation (mm d-1). - \item \eqn{bflow}: Lateral flow (mm d-1). - \item \eqn{netr}: Daytime net radiation (MJ d-1). +\item \eqn{wn}: Soil water content within the first 2m of depth (mm). +\item \eqn{ro}: Runoff (mm d-1). +\item \eqn{pet}: Potential evapotranspiration (mm d-1). +\item \eqn{aet}: Actual evapotranspiration (mm d-1). +\item \eqn{snow}: Snow water equivalent (mm). +\item \eqn{cond}: Condensation (mm d-1). +\item \eqn{bflow}: Lateral flow (mm d-1). +\item \eqn{netr}: Daytime net radiation (MJ d-1). } } \description{ R/C++ implementation of the SPLASH v.2.0 algorithm (Davis et al., 2017; Sandoval et al., in prep.). } \examples{ -splash.point(sw_in=200, tc=15, pn=10, lat=44,elev=1800,slop=10,asp=270,soil_data=c(sand=44,clay=2,OM=6,fgravel=12)) +data(Bourne) +splash.point( + sw_in = Bourne$forcing$sw_in, + tc = Bourne$forcing$Ta, + pn = Bourne$forcing$P, + lat = Bourne$md$latitude, + elev = Bourne$md$elev_m, + slop = Bourne$md$slop_250m, + asp = Bourne$md$asp_250m, + soil_data = Bourne$soil, + Au = Bourne$md$Aups_250m, + resolution = 250.0 +) } \keyword{evapotranspiration,} \keyword{moisture} diff --git a/man/unSWC.Rd b/man/unSWC.Rd index d94ae5c..b3369eb 100644 --- a/man/unSWC.Rd +++ b/man/unSWC.Rd @@ -4,10 +4,10 @@ \alias{unSWC} \title{unsSWC} \usage{ -unSWC(soil_data, uns_depth, wn, units = "mm") +unSWC(soil_data, uns_depth, wn) } \arguments{ -\item{soil_data, }{vector as retrieved from splashTools::getSoilNRCS() or splashTools::getSoilSite()} +\item{soil_data, }{vector as retrieved from \code{\link[splashTools:getSoilNRCS]{splashTools::getSoilNRCS()}} or \code{\link[splashTools:getSoilSite]{splashTools::getSoilSite()}}} \item{uns_depth, }{maximum depth to calculate the accumulated water content in (m)} @@ -18,7 +18,4 @@ unSWC(soil_data, uns_depth, wn, units = "mm") \description{ Estimates water content using an analytca integral of the profile described by Brooks and Corey 1964 and calculates depth to water table if there is any } -\examples{ -unSWC() -} \keyword{splashTools} diff --git a/man/unSWC.grid.Rd b/man/unSWC.grid.Rd index ddb0d8b..29602d1 100644 --- a/man/unSWC.grid.Rd +++ b/man/unSWC.grid.Rd @@ -4,7 +4,7 @@ \alias{unSWC.grid} \title{unsSWC.grid} \usage{ -unSWC.grid(soil_data, uns_depth, wn) +unSWC.grid(soil_data, uns_depth, wn, d_name, outdir = getwd()) } \arguments{ \item{soil_data, }{Raster* object with the layers organized as sand(perc),clay(perc),organic matter(perc),coarse-fragments-fraction(perc), bulk density(g cm-3)} @@ -13,12 +13,9 @@ unSWC.grid(soil_data, uns_depth, wn) \item{wn, }{Raster* object, total water content of the profile from surface to bedrock (m)} -\item{ouputdir, }{directory where to save the files} +\item{outdir, }{directory where to save the files} } \description{ -Estimates water content using an analytca integral of the profile described by Brooks and Corey 1964 and calculates depth to water table if there is any -} -\examples{ -unSWC() +Estimates water content in the unsaturated part of the profile using an analytical integral of the eqn. described by Brooks and Corey 1964 and calculates depth to water table if there is any (*experimental) } \keyword{splashTools} diff --git a/man/upslope_area.Rd b/man/upslope_area.Rd index eb39634..d7fd9b0 100644 --- a/man/upslope_area.Rd +++ b/man/upslope_area.Rd @@ -4,19 +4,16 @@ \alias{upslope_area} \title{upslope_area} \usage{ -upslope_area(dem) +upslope_area(dem, resolution) } \arguments{ -\item{dem}{} +\item{dem}{a \link[raster:Raster-classes]{RasterLayer} Digital Elevation Model model passed to \code{\link[topmodel:sinkfill]{topmodel::sinkfill()}}} } \value{ a RasterStack with the contributing area per pixel, number of cells draining in, and number of cells draining out } \description{ -Computes the upslope area in m2 using topmodel for small areas or Taudem for large, corrections acording to latitude done by raster::area -} -\examples{ -splash.grid() +Computes n cells draining in/out and the contributing area per pixel in m2 using topmodel for small areas or Taudem for large, corrections acording to latitude done by raster::area } \keyword{area} \keyword{contributing} diff --git a/src/.vscode/settings.json b/src/.vscode/settings.json deleted file mode 100644 index 1be854f..0000000 --- a/src/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "cmath": "cpp" - } -} \ No newline at end of file diff --git a/src/Num.cpp b/src/Num.cpp deleted file mode 100644 index 3bfb722..0000000 --- a/src/Num.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- -// -// Num.cpp: Rcpp R/C++ interface class library -- Rcpp Module example -// -// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois -// -// This file is part of Rcpp. -// -// Rcpp is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 2 of the License, or -// (at your option) any later version. -// -// Rcpp is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Rcpp. If not, see . - -#include - -class Num { // simple class with two private variables -public: // which have a getter/setter and getter - Num() : x(0.0), y(0){} ; - - double getX() { return x ; } - void setX(double value){ x = value ; } - - int getY() { return y ; } - -private: - double x ; - int y ; -}; - -RCPP_MODULE(NumEx){ - using namespace Rcpp ; - - class_( "Num" ) - - .default_constructor() - - // read and write property - .property( "x", &Num::getX, &Num::setX ) - - // read-only property - .property( "y", &Num::getY ) - ; -} diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 2e4f61b..5583bbd 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -5,17 +5,16 @@ using namespace Rcpp; +#ifdef RCPP_USE_GLOBAL_ROSTREAM +Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); +Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); +#endif + -RcppExport SEXP _rcpp_module_boot_NumEx(); RcppExport SEXP _rcpp_module_boot_splash_module(); -RcppExport SEXP _rcpp_module_boot_yada(); -RcppExport SEXP _rcpp_module_boot_stdVector(); static const R_CallMethodDef CallEntries[] = { - {"_rcpp_module_boot_NumEx", (DL_FUNC) &_rcpp_module_boot_NumEx, 0}, {"_rcpp_module_boot_splash_module", (DL_FUNC) &_rcpp_module_boot_splash_module, 0}, - {"_rcpp_module_boot_yada", (DL_FUNC) &_rcpp_module_boot_yada, 0}, - {"_rcpp_module_boot_stdVector", (DL_FUNC) &_rcpp_module_boot_stdVector, 0}, {NULL, NULL, 0} }; diff --git a/src/rcpp_module.cpp b/src/rcpp_module.cpp deleted file mode 100644 index 426e375..0000000 --- a/src/rcpp_module.cpp +++ /dev/null @@ -1,85 +0,0 @@ -// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- -// -// rcpp_module.cpp: Rcpp R/C++ interface class library -- Rcpp Module examples -// -// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois -// -// This file is part of Rcpp. -// -// Rcpp is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 2 of the License, or -// (at your option) any later version. -// -// Rcpp is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Rcpp. If not, see . - -#include - -std::string hello() { - throw std::range_error( "boom" ) ; -} - -int bar(int x) { - return x*2; -} - -double foo(int x, double y) { - return x * y; -} - -void bla() { - Rprintf("hello\\n"); -} - -void bla1(int x) { - Rprintf("hello (x = %d)\\n", x); -} - -void bla2( int x, double y) { - Rprintf("hello (x = %d, y = %5.2f)\\n", x, y); -} - -class World { -public: - World() : msg("hello") {} - void set(std::string msg) { this->msg = msg; } - std::string greet() { return msg; } - -private: - std::string msg; -}; - - - -RCPP_MODULE(yada){ - using namespace Rcpp ; - - function("hello" , &hello , "documentation for hello "); - function("bla" , &bla , "documentation for bla "); - function("bla1" , &bla1 , "documentation for bla1 "); - function("bla2" , &bla2 , "documentation for bla2 "); - - // with formal arguments specification - function("bar" , &bar , - List::create( _["x"] = 0.0), - "documentation for bar "); - function("foo" , &foo , - List::create( _["x"] = 1, _["y"] = 1.0), - "documentation for foo "); - - class_("World") - // expose the default constructor - .constructor() - - .method("greet", &World::greet , "get the message") - .method("set", &World::set , "set the message") - ; -} - - diff --git a/src/stdVector.cpp b/src/stdVector.cpp deleted file mode 100644 index 9149379..0000000 --- a/src/stdVector.cpp +++ /dev/null @@ -1,90 +0,0 @@ -// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- -// -// stdVector.cpp: Rcpp R/C++ interface class library -- Rcpp Module class example -// -// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois -// -// This file is part of Rcpp. -// -// Rcpp is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 2 of the License, or -// (at your option) any later version. -// -// Rcpp is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Rcpp. If not, see . - -#include // need to include the main Rcpp header file only - -// convenience typedef -typedef std::vector vec; - -// helpers -void vec_assign( vec* obj, Rcpp::NumericVector data) { - obj->assign( data.begin(), data.end() ) ; -} - -void vec_insert( vec* obj, int position, Rcpp::NumericVector data) { - vec::iterator it = obj->begin() + position; - obj->insert( it, data.begin(), data.end() ); -} - -Rcpp::NumericVector vec_asR( vec* obj) { - return Rcpp::wrap( *obj ); -} - -void vec_set( vec* obj, int i, double value) { - obj->at( i ) = value; -} - -void vec_resize( vec* obj, int n) { obj->resize( n ); } -void vec_push_back( vec* obj, double x ) { obj->push_back( x ); } - -// Wrappers for member functions that return a reference -// Required on Solaris -double vec_back(vec *obj){ return obj->back() ; } -double vec_front(vec *obj){ return obj->front() ; } -double vec_at(vec *obj, int i){ return obj->at(i) ; } - -RCPP_MODULE(stdVector){ - using namespace Rcpp ; - - // we expose the class std::vector as "vec" on the R side - class_("vec") - - // exposing the default constructor - .constructor() - - // exposing member functions -- taken directly from std::vector - .method( "size", &vec::size) - .method( "max_size", &vec::max_size) - .method( "capacity", &vec::capacity) - .method( "empty", &vec::empty) - .method( "reserve", &vec::reserve) - .method( "pop_back", &vec::pop_back ) - .method( "clear", &vec::clear ) - - // specifically exposing const member functions defined above - .method( "back", &vec_back ) - .method( "front", &vec_front ) - .method( "at", &vec_at ) - - // exposing free functions taking a std::vector* - // as their first argument - .method( "assign", &vec_assign ) - .method( "insert", &vec_insert ) - .method( "as.vector",&vec_asR ) - .method( "push_back",&vec_push_back ) - .method( "resize", &vec_resize) - - // special methods for indexing - .method( "[[", &vec_at ) - .method( "[[<-", &vec_set ) - - ; -}