-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
2,073 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
>>>>>>> 79146b21e9e5f977d8ddab23bba1f84504844c48 | ||
^doc$ | ||
^Meta$ | ||
^Rdev$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.