Skip to content

Commit

Permalink
Merge wiDButil
Browse files Browse the repository at this point in the history
  • Loading branch information
bumbanian committed Mar 11, 2021
1 parent 7bd0a44 commit 514257e
Show file tree
Hide file tree
Showing 22 changed files with 2,073 additions and 261 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
>>>>>>> 79146b21e9e5f977d8ddab23bba1f84504844c48
^doc$
^Meta$
^Rdev$
39 changes: 37 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
.Rproj.user
# History files
.Rhistory
.Rapp.history

# Session Data files
.RData
.Ruserdata

# User-specific files
.Ruserdata

# Example code in package build process
*-Ex.R

# 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

# Downloads
/downloads/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ language: R
cache: packages
before_install:
- sudo apt-get install libgdal-dev
- R -e 'install.packages("rgdal", repos=c("http://R-Forge.R-project.org", "http://cran.rstudio.com"))'

r_packages:
- covr
Expand Down
18 changes: 12 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
Package: isoWater
Type: Package
Title: Infer source of water using stable isotope data
Version: 0.2.0.9000
Author: Gabe Bowen
Maintainer: Gabe Bowen <gabe.bowen@utah.edu>
Description: Bayesian inference of the source and source isotope composition of water samples that may have experienced evaporation. Original algorithms presented in Bowen et al. (2018) <doi:10.1007/s00442-018-4192-5>.
Imports: R2jags, parallel, abind, R2WinBUGS, doParallel, foreach
Title: Discovery, Retrieval, and Analysis of Water Isotope Data
Version: 0.2.1
Authors@R: person("Gabriel", "Bowen", email = "gabe.bowen@utah.edu",
role = c("aut", "cre"))
Description: wiDB_ functions provide interface to the public API of
the wiDB: build, check and submit queries, and receive and
unpack responses. Data analysis functions support Bayesian
inference of the source and source isotope composition of water
samples that may have experienced evaporation. Algorithms
adapted from Bowen et al. (2018) <doi:10.1007/s00442-018-4192-5>.
Imports: R2jags, parallel, abind, R2WinBUGS, doParallel, foreach,
httr, jsonlite
Depends: R (>= 3.5)
Suggests:
knitr,
Expand Down
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ export(mwlSource)
export(mixSource)
export(mwl)
export(iso)
export(wiDB_sites)
export(wiDB_data)
export(wiDB_values)
importFrom(httr, GET, content)
importFrom(jsonlite, fromJSON)
importFrom(doParallel, registerDoParallel, stopImplicitCluster)
importFrom(foreach, foreach, "%dopar%")
importFrom(R2jags, jags)
importFrom(R2WinBUGS, monitor)
importFrom(stats, sd, var)
importFrom(graphics, abline, lines, par, points)
importFrom(abind, abind)
importFrom(abind, abind)
importFrom(utils, read.csv, unzip)
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# isoWater news

## isoWater 0.2.0.9000
* Updated documentation
## isoWater 0.2.1
* Merged functions and documentation from wiDButil package
* Updated documentation and vignette


## isoWater 0.2.0
* Reformatted code to create package
Expand Down
4 changes: 4 additions & 0 deletions R/constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ mwl = function(HO, plot = TRUE){
stop("Non-numeric values in HO")
}
ns = nrow(HO)
HO = HO[!(is.na(HO[,1]) | is.na(HO[,2])),]
if(nrow(HO) != ns){
warning("Missing values removed from HO")
}
if(ns < 3){
stop("At least 3 sample values required")
}
Expand Down
2 changes: 1 addition & 1 deletion R/watercomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mwlSource = function(obs, MWL=c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672),
#takes values of observed and hypothesized endmember source waters (each type 'iso'),hypothesized EL slope,
#prior (as relative contribution of each source to mixture), and number of parameter draws
mixSource = function(obs, sources, slope, prior=rep(1,nrow(sources)),
shp=2, ngens=1e5, ncores = 1){
shp=1, ngens=1e5, ncores = 1){

if(class(obs)[2] != "iso"){
warning("Expecting iso object for obs, this argument may be
Expand Down
223 changes: 223 additions & 0 deletions R/wiDBfunctions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
#####
#Validate query input
#####
wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev,
minDate, maxDate, countries, states, types, projects){

qStr = ""

if(!is.null(minLat)){
if(class(minLat) != "numeric"){stop("minLat must be numeric")}
qStr = paste0(qStr, "&minLat=", minLat)
}
if(!is.null(maxLat)){
if(class(maxLat) != "numeric"){stop("maxLat must be numeric")}
qStr = paste0(qStr, "&maxLat=", maxLat)
}
if(!is.null(minLong)){
if(class(minLong) != "numeric"){stop("minLong must be numeric")}
qStr = paste0(qStr, "&minLong=", minLong)
}
if(!is.null(maxLong)){
if(class(maxLong) != "numeric"){stop("maxLong must be numeric")}
qStr = paste0(qStr, "&maxLong=", maxLong)
}
if(!is.null(minElev)){
if(class(minElev) != "numeric"){stop("minElev must be numeric")}
qStr = paste0(qStr, "&minElev=", minElev)
}
if(!is.null(maxElev)){
if(class(maxElev) != "numeric"){stop("maxElev must be numeric")}
qStr = paste0(qStr, "&maxElev=", maxElev)
}
if(!is.null(minDate)){
if(class(minDate) != "character"){stop("minDate must be string")}
td = c(as.numeric(substr(minDate, 1, 4)), as.numeric(substr(minDate, 6, 7)),
as.numeric(substr(minDate, 9, 10)))
if(NA %in% td){stop("minDate format must be YYYY-MM-DD")}
qStr = paste0(qStr, "&minDate=", minDate)
}
if(!is.null(maxDate)){
if(class(maxDate) != "character"){stop("maxDate must be string")}
td = c(as.numeric(substr(maxDate, 1, 4)), as.numeric(substr(maxDate, 6, 7)),
as.numeric(substr(maxDate, 9, 10)))
if(NA %in% td){stop("maxDate format must be YYYY-MM-DD")}
qStr = paste0(qStr, "&maxDate=", maxDate)
}
if(!is.null(countries)){
if(class(countries) != "character"){stop("countries must be string")}
countries = gsub(" ", "", countries)
countries = gsub(" ", "", countries)
if(length(countries > 1)){
countries = paste0(countries, collapse = ",")
}
qStr = paste0(qStr, "&countries=", countries)
}
if(!is.null(states)){
if(class(states) != "character"){stop("states must be string")}
states = gsub(" ", "", states)
states = gsub(" ", "", states)
if(length(states > 1)){
states = paste0(states, collapse = ",")
}
qStr = paste0(qStr, "&states=", states)
}
if(!is.null(types)){
if(class(types) != "character"){stop("types must be string")}
types = gsub(" ", "", types)
types = gsub(" ", "", types)
if(length(types > 1)){
types = paste0(types, collapse = ",")
}
qStr = paste0(qStr, "&types=", types)
}
if(!is.null(projects)){
if(class(projects) != "character"){stop("projects must be string")}
projects = gsub(" ", "", projects)
projects = gsub(" ", "", projects)
if(length(types > 1)){
types = paste0(types, collapse = ",")
}
qStr = paste0(qStr, "&projects=", projects)
}

if(nchar(qStr) == 0){stop("No query arguments provided")}

qStr = paste0("?", substr(qStr, 2, nchar(qStr)))

return(qStr)
}

#####
#Find sites
#####
wiDB_sites = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NULL,
minElev = NULL, maxElev = NULL, minDate = NULL, maxDate = NULL,
countries = NULL, states = NULL, types = NULL, projects = NULL){

qStr = wiDB_validate(minLat, maxLat, minLong, maxLong, minElev, maxElev,
minDate, maxDate, countries, states, types, projects)

baseStr = "https://wateriso.utah.edu/api/v1/sites.php"
q = paste0(baseStr, qStr)
d = GET(q)

if(d$status_code != 200){stop(paste("Request returned error code", d$status_code))}

resp = fromJSON(content(d, as = "text", encoding = "UTF-8"))

if(length(resp$sites) == 0){
warning("No sites returned")
return(NULL)
}

return(resp$sites)
}

#####
#Obtain data
#####
wiDB_data = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NULL,
minElev = NULL, maxElev = NULL, minDate = NULL, maxDate = NULL,
countries = NULL, states = NULL, types = NULL, projects = NULL,
fields = NULL, tmpdir = tempdir(), clean = TRUE){

qStr = wiDB_validate(minLat, maxLat, minLong, maxLong, minElev, maxElev,
minDate, maxDate, countries, states, types, projects)

if(!dir.exists(tmpdir)){
warning("Directory doesn't exist, trying to create")
dir.create(tmpdir)
if(!dir.exists(tmpdir)){stop("Unable to create directory")}
}

if(class(clean) != "logical"){stop("clean must be TRUE/FALSE")}

flist = c("Site_ID", "Site_Name", "Latitude", "Longitude", "Elevation",
"Sample_ID", "Type", "Start_Date", "Start_Time_Zone",
"Collection_Date", "Collection_Time_Zone", "Phase",
"Depth_meters", "Sample_Comments", "d2H", "d18O",
"d2H_Analytical_SD", "d18O_Analytical_SD", "WI_Analysis_Source",
"Project_ID")

if(!is.null(fields)){
if(class(fields) != "character"){stop("fields must be a string")}
fields = gsub(" ", "", fields)
fields = gsub(" ", "", fields)
fels = strsplit(fields, ",")
fels = fels[[1]]
for(i in 1:length(fels)){
if(!(fels[i] %in% flist)){stop(paste("Value", i, "in fields is not a valid field name"))}
}
qStr = paste0(qStr, "&return=", fields)
}

baseStr = "https://wateriso.utah.edu/api/v1/download.php"
q = paste0(baseStr, qStr)
g = GET(q)

if(g$status_code != 200){stop(paste("Request returned error code", g$status_code))}

fn = g$headers$`content-disposition`
fn = strsplit(fn, "=")[[1]][2]
writeBin(g$content, paste0(tmpdir, "/", fn))

#unzip and output .csv
unzip(paste0(tmpdir, "/", fn), exdir = paste0(tmpdir, "/downloads"))

#get and order file list
froot = strsplit(fn, "-")[[1]][1]
df = paste0(tmpdir, "/downloads/", froot, "-data.csv")
pf = paste0(tmpdir, "/downloads/", froot, "-project.csv")

if(file.size(df) == 0){
file.remove(c(paste0(tmpdir, "/", fn), df, pf))
warning("No records returned")
return(NULL)
}

#read in data
d = read.csv(df)

#read in projects
p = read.csv(pf)

file.remove(paste0(tmpdir, "/", fn))

if(clean){
file.remove(c(df, pf))
}

return(list("data" = d, "projects" = p))
}

#####
#Get field values
#####
wiDB_values = function(fields){

if(!is.character(fields)){
stop("fields values must be character strings")
}
for(i in 1:length(fields)){
if(!(fields[i] %in% c("countries", "states", "types", "projects",
"Site_ID", "Sample_ID", "WI_Analysis_ID",
"Climate_ID"))){
stop("One or more fields values not supported")
}
}

baseStr = "https://wateriso.utah.edu/api/v1/values.php?fields="
q = baseStr
for(i in fields){
q = paste0(q, i, ",")
}
q = substr(q, 1, nchar(q) - 1)
d = GET(q)

if(d$status_code != 200){stop(paste("Request returned error code", d$status_code))}

resp = fromJSON(content(d, as = "text", encoding = "UTF-8"))

return(resp)
}
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# isoWater

An R package for inferring the source of waters based on their isotopic composition and allowing for isotope effects associated with partial evaporation. Install from GitHub using the devtools package:
An R package for obtaining and analyzing water isotope data. Includes interfaces to the Waterisotopes Database and tools for inferring the source of waters based on their isotopic composition accounting for isotope effects associated with partial evaporation. Install the latest release from GitHub using the devtools package:

```
library(devtools)
install_GitHub("SPATIAL-Lab/isoWater")
install_GitHub("SPATIAL-Lab/isoWater@*release")
library(isoWater)
```

References:

Bowen, G. J., Putman, A., Brooks, J. R., Bowling, D. R., Oerter, E. J., & Good, S. P. (2018). Inferring the source of evaporated waters using stable H and O isotopes. Oecologia, 187(4), 1025-1039.
Bowen, G. J., Putman, A. L., Brooks, J. R., Bowling, D. R., Oerter, E. J., & Good, S. P. (2018) Inferring the source of evaporated waters using stable H and O isotopes. Oecologia, 187(4), 1025-1039.

Putman, A. L., & Bowen, G. J. (2019) A global database of the stable isotopic ratios of meteoric and terrestrial waters. Hydrology and Earth System Sciences 23(10), 4389-4396.
Loading

0 comments on commit 514257e

Please sign in to comment.