Skip to content

Commit e6d5235

Browse files
committed
Add dex
1 parent 7b4df17 commit e6d5235

File tree

19 files changed

+161
-35
lines changed

19 files changed

+161
-35
lines changed

CRAN-RELEASE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This package was submitted to CRAN on 2021-06-09.
2+
Once it is accepted, delete this file and tag the release (commit c39aa1c).

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ License: GPL-3
2626
Language: en-US
2727
Encoding: UTF-8
2828
RoxygenNote: 7.1.1
29+
LazyData: true

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export(iso)
55
export(wiDB_sites)
66
export(wiDB_data)
77
export(wiDB_values)
8+
export(dex)
89
importFrom(httr, GET, content, user_agent)
910
importFrom(jsonlite, fromJSON)
1011
importFrom(doParallel, registerDoParallel, stopImplicitCluster)
@@ -14,4 +15,4 @@ importFrom(R2WinBUGS, monitor)
1415
importFrom(stats, sd, var)
1516
importFrom(graphics, abline, lines, par, points)
1617
importFrom(abind, abind)
17-
importFrom(utils, read.csv, unzip)
18+
importFrom(utils, read.csv, unzip, data)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# isoWater news
22

33
## isoWater 1.0.1.9000
4+
* Add dex function
5+
* Bug and spelling fixes
46

57
## isoWater 1.0.1
68
* Bug fixes

R/constructors.R

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ iso = function(H, O, Hsd, Osd, HOc = 0){
1818
#####
1919

2020
mwl = function(HO, plot = TRUE){
21-
if(class(HO)[1] != "data.frame"){
21+
if(!inherits(HO, "data.frame")){
2222
stop("HO must be a data.frame")
2323
}
2424
if(!is.numeric(HO[,1]) | !is.numeric(HO[,2])){
@@ -69,3 +69,41 @@ mwl = function(HO, plot = TRUE){
6969
class(mwl) = "mwl"
7070
return(mwl)
7171
}
72+
73+
#####
74+
#--- deuterium excess ----
75+
#calculate dex for one or more samples
76+
#####
77+
78+
dex = function(HO, form = "dex", MWL = NULL){
79+
if(!inherits(HO, "data.frame")){
80+
stop("HO must be a data.frame")
81+
}
82+
if(!is.numeric(HO[,1]) | !is.numeric(HO[,2])){
83+
stop("Non-numeric values in HO")
84+
}
85+
86+
if(is.null(MWL)){
87+
data("GMWL", envir = environment())
88+
GMWL = GMWL
89+
MWL = GMWL
90+
}
91+
92+
if(!is.numeric(MWL) | length(MWL) < 2){
93+
stop("MWL must be mwl object or numeric vector with slope, intercept")
94+
}
95+
96+
if(!(form %in% c("dex", "lcex", "both"))){
97+
stop("form must be dex, lcex, or both")
98+
}
99+
100+
if(form %in% c("dex", "both")){
101+
HO$dex = HO[,1] - 8 * HO[,2]
102+
}
103+
104+
if(form %in% c("lcex", "both")){
105+
HO$lcex = HO[,1] - MWL[1] * HO[,2] - MWL[2]
106+
}
107+
108+
return(HO)
109+
}

R/watercomp.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
#takes values of observed water (type 'iso'), MWL (see below), hypothesized EL slope value
66
#and number of parameter draws
7-
mwlSource = function(obs, MWL=c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672),
8-
slope, stype = 1, ngens=1e4, ncores = 1){
7+
mwlSource = function(obs, MWL = NULL, slope, stype = 1, ngens=1e4, ncores = 1){
98

9+
if(is.null(MWL)){
10+
data("GMWL", envir = environment())
11+
GMWL = GMWL
12+
MWL = GMWL
13+
}
14+
1015
if(MWL[6] == 80672 & stype == 2){
1116
warning("Using stype=2 and GMWL is inappropriate for most applications; see man")
1217
}
1318

14-
if(length(class(obs)) < 2 | class(obs)[2] != "iso"){
19+
if(!inherits(obs, "iso")){
1520
warning("Expecting iso object for obs, this argument may be formatted incorrectly")
1621
}
1722
if(!(stype %in% c(1, 2))){
@@ -101,7 +106,7 @@ mwlSource = function(obs, MWL=c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672),
101106
mixSource = function(obs, sources, slope, prior=rep(1,nrow(sources)),
102107
shp=1, ngens=1e5, ncores = 1){
103108

104-
if(length(class(obs)) < 2 | class(obs)[2] != "iso"){
109+
if(!inherits(obs, "iso")){
105110
warning("Expecting iso object for obs, this argument may be
106111
formatted incorrectly")
107112
}
@@ -118,7 +123,7 @@ mixSource = function(obs, sources, slope, prior=rep(1,nrow(sources)),
118123
obs.vcov[2 + (i - 1) * 2, 2] = obs[i, 4] ^ 2
119124
}
120125

121-
if(length(class(sources)) < 2 | class(sources)[2] != "iso"){
126+
if(!inherits(sources, "iso")){
122127
warning("Expecting iso object for sources, this argument may be
123128
formatted incorrectly")
124129
}

R/wiDBfunctions.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,45 @@ wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev,
77
qStr = ""
88

99
if(!is.null(minLat)){
10-
if(class(minLat) != "numeric"){stop("minLat must be numeric")}
10+
if(!inherits(minLat, "numeric")){stop("minLat must be numeric")}
1111
qStr = paste0(qStr, "&minLat=", minLat)
1212
}
1313
if(!is.null(maxLat)){
14-
if(class(maxLat) != "numeric"){stop("maxLat must be numeric")}
14+
if(!inherits(maxLat, "numeric")){stop("maxLat must be numeric")}
1515
qStr = paste0(qStr, "&maxLat=", maxLat)
1616
}
1717
if(!is.null(minLong)){
18-
if(class(minLong) != "numeric"){stop("minLong must be numeric")}
18+
if(!inherits(minLong, "numeric")){stop("minLong must be numeric")}
1919
qStr = paste0(qStr, "&minLong=", minLong)
2020
}
2121
if(!is.null(maxLong)){
22-
if(class(maxLong) != "numeric"){stop("maxLong must be numeric")}
22+
if(!inherits(maxLong, "numeric")){stop("maxLong must be numeric")}
2323
qStr = paste0(qStr, "&maxLong=", maxLong)
2424
}
2525
if(!is.null(minElev)){
26-
if(class(minElev) != "numeric"){stop("minElev must be numeric")}
26+
if(!inherits(minElev, "numeric")){stop("minElev must be numeric")}
2727
qStr = paste0(qStr, "&minElev=", minElev)
2828
}
2929
if(!is.null(maxElev)){
30-
if(class(maxElev) != "numeric"){stop("maxElev must be numeric")}
30+
if(!inherits(maxElev, "numeric")){stop("maxElev must be numeric")}
3131
qStr = paste0(qStr, "&maxElev=", maxElev)
3232
}
3333
if(!is.null(minDate)){
34-
if(class(minDate) != "character"){stop("minDate must be string")}
34+
if(!inherits(minDate, "character")){stop("minDate must be string")}
3535
td = c(as.numeric(substr(minDate, 1, 4)), as.numeric(substr(minDate, 6, 7)),
3636
as.numeric(substr(minDate, 9, 10)))
3737
if(NA %in% td){stop("minDate format must be YYYY-MM-DD")}
3838
qStr = paste0(qStr, "&minDate=", minDate)
3939
}
4040
if(!is.null(maxDate)){
41-
if(class(maxDate) != "character"){stop("maxDate must be string")}
41+
if(!inherits(maxDate, "character")){stop("maxDate must be string")}
4242
td = c(as.numeric(substr(maxDate, 1, 4)), as.numeric(substr(maxDate, 6, 7)),
4343
as.numeric(substr(maxDate, 9, 10)))
4444
if(NA %in% td){stop("maxDate format must be YYYY-MM-DD")}
4545
qStr = paste0(qStr, "&maxDate=", maxDate)
4646
}
4747
if(!is.null(countries)){
48-
if(class(countries) != "character"){stop("countries must be string")}
48+
if(!inherits(countries, "character")){stop("countries must be string")}
4949
countries = gsub(" ", "", countries)
5050
countries = gsub(" ", "", countries)
5151
if(length(countries > 1)){
@@ -54,7 +54,7 @@ wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev,
5454
qStr = paste0(qStr, "&countries=", countries)
5555
}
5656
if(!is.null(states)){
57-
if(class(states) != "character"){stop("states must be string")}
57+
if(!inherits(states, "character")){stop("states must be string")}
5858
states = gsub(" ", "", states)
5959
states = gsub(" ", "", states)
6060
if(length(states > 1)){
@@ -63,7 +63,7 @@ wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev,
6363
qStr = paste0(qStr, "&states=", states)
6464
}
6565
if(!is.null(types)){
66-
if(class(types) != "character"){stop("types must be string")}
66+
if(!inherits(types, "character")){stop("types must be string")}
6767
types = gsub(" ", "", types)
6868
types = gsub(" ", "", types)
6969
if(length(types > 1)){
@@ -72,7 +72,7 @@ wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev,
7272
qStr = paste0(qStr, "&types=", types)
7373
}
7474
if(!is.null(projects)){
75-
if(class(projects) != "character"){stop("projects must be string")}
75+
if(!inherits(projects, "character")){stop("projects must be string")}
7676
projects = gsub(" ", "", projects)
7777
projects = gsub(" ", "", projects)
7878
if(length(projects > 1)){
@@ -148,7 +148,7 @@ wiDB_data = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NUL
148148
if(!dir.exists(tmpdir)){stop("Unable to create directory")}
149149
}
150150

151-
if(class(clean) != "logical"){stop("clean must be TRUE/FALSE")}
151+
if(!inherits(clean, "logical")){stop("clean must be TRUE/FALSE")}
152152

153153
flist = c("Site_ID", "Site_Name", "Latitude", "Longitude", "Elevation",
154154
"Sample_ID", "Type", "Start_Date", "Start_Time_Zone",
@@ -158,7 +158,7 @@ wiDB_data = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NUL
158158
"Project_ID")
159159

160160
if(!is.null(fields)){
161-
if(class(fields) != "character"){stop("fields must be a string")}
161+
if(!inherits(fields, "character")){stop("fields must be a string")}
162162
fields = gsub(" ", "", fields)
163163
fields = gsub(" ", "", fields)
164164
fels = strsplit(fields, ",")

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Putman, A. L., & Bowen, G. J. (2019) A global database of the stable isotopic ra
1616

1717
<!-- badges: start -->
1818
[![Build status](https://github.com/SPATIAL-Lab/isoWater/actions/workflows/r.yml/badge.svg)](https://github.com/SPATIAL-Lab/isoWater/actions)
19-
[![codecov](https://codecov.io/gh/SPATIAL-Lab/isoWater/branch/master/graph/badge.svg)](https://codecov.io/gh/SPATIAL-Lab/isoWater)
19+
[![codecov](https://codecov.io/gh/SPATIAL-Lab/isoWater/branch/master/graph/badge.svg)](https://app.codecov.io/gh/SPATIAL-Lab/isoWater)
2020
<!-- badges: end -->

data-raw/prepData.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ mixModel = "model{
5353
}"
5454

5555
use_data(mwlModel, mixModel, internal = TRUE, overwrite = TRUE)
56+
57+
GMWL = c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672)
58+
use_data(GMWL, overwrite = TRUE)

data/GMWL.rda

138 Bytes
Binary file not shown.

man/GMWL.Rd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
\name{GMWL}
2+
\alias{GMWL}
3+
\docType{data}
4+
\title{
5+
Global Meteoric Water Line
6+
}
7+
\description{
8+
Parameters for the Global Meteoric Water Line fit to a global precipitation compilation in Bowen, et al. (2019).
9+
}
10+
\usage{data("GMWL")}
11+
\format{
12+
The format is:
13+
num [1:6] slope, intercept, average d18O, sum of squares in d18O, root mean square error, number of samples
14+
}
15+
\source{
16+
Bowen et al. (2019) Isotopes in the water cycle: Regional- to global-Scale patterns and applications. \emph{Annual Review of Earth and Planetary Sciences} \strong{47} 453--479. \doi{10.1146/annurev-earth-053018-060220}.
17+
}
18+
\examples{
19+
data(GMWL)
20+
}
21+
\keyword{datasets}

man/dex.Rd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
\name{dex}
2+
3+
\alias{dex}
4+
5+
\title{
6+
Deuterium excess
7+
}
8+
9+
\description{
10+
Calculates deuterium excess or line-conditioned excess.
11+
}
12+
13+
\usage{
14+
dex(HO, form = "dex", MWL = NULL)
15+
}
16+
17+
\arguments{
18+
\item{HO}{
19+
data.frame. Hydrogen (column 1) and oxygen (column 2) isotope values for 1 or more water samples.
20+
}
21+
\item{form}{character. Calculate deuterium excess (\dQuote{dex}), line-conditioned excess (\dQuote{lcex}), or \dQuote{both}.}
22+
\item{MWL}{
23+
numeric. Vector the first two elements of which contain the meteoric water line slope and intercept (e.g., as created by \code{\link{mwl}}). The default value (if \code{MWL = NULL}) reflects the Global Meteoric Water Line estimated from a global precipitation compilation in Bowen, et al. (2019). Ignored for \code{form = "dex"}.
24+
}
25+
}
26+
27+
\value{
28+
Returns a copy of \code{HO} with an added field(s) \dQuote{dex} and/or \dQuote{lcex} containing the calculated values. Deuterium excess is calculated following Dansgaard (1964) as: \emph{dex = \eqn{\delta}2H - 8 * \eqn{\delta}18O}, and lc-excess following Landwehr & Coplen (2006) as \emph{lcex = \eqn{\delta}2H - m * \eqn{\delta}18O - b}, where \emph{m} and \emph{b} are the slope and intercept of \code{MWL}, respectively.
29+
}
30+
31+
\references{
32+
Bowen et al. (2019) Isotopes in the water cycle: Regional- to global-Scale patterns and applications. \emph{Annual Review of Earth and Planetary Sciences} \strong{47} 453--479. \doi{10.1146/annurev-earth-053018-060220}.
33+
34+
Dansgaard (1964) Stable isotopes in precipitation. \emph{Tellus} \strong{16} 436--468. \doi{10.1111/j.2153-3490.1964.tb00181.x}.
35+
36+
Landwehr & Coplen (2006) Line-conditioned excess: A new method for characterizing stable hydrogen and oxygen isotope ratios in hydrologic systems. In \emph{Isotopes in Environmental Studies}, International Atomic Energy Agency, 132--135. \url{http://www-pub.iaea.org/MTCD/publications/PDF/CSP_26_web.pdf}.
37+
}
38+
39+
\examples{
40+
O = runif(10, -15, -2)
41+
H = O * 8 + 10 + rnorm(10, 0, 6)
42+
d = dex(data.frame(H, O), form = "both")
43+
print(d)
44+
}

man/iso.Rd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ iso(H, O, Hsd, Osd, HOc = 0)
2222
numeric. Oxygen isotope value or vector of oxygen isotope values.
2323
}
2424
\item{Hsd}{
25-
numeric. 1 standard devation uncertainty of \code{H} (value or vector of values).
25+
numeric. 1 standard deviation uncertainty of \code{H} (value or vector of values).
2626
}
2727
\item{Osd}{
28-
numeric. 1 standard devation uncertainty of \code{O} (value or vector of values).
28+
numeric. 1 standard deviation uncertainty of \code{O} (value or vector of values).
2929
}
3030
\item{HOc}{
3131
numeric. Covariance of \code{H} and \code{O} uncertainties.

man/mwl.Rd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ mwl(HO, plot = TRUE)
2424
}
2525

2626
\details{
27-
\code{mwl} will return an error if fewer than 3 sample values are provided and a warning if fewer than 10 samples are provided or if the correlation coefficient betwen H and O values is less than 0.7. Sample values should span a broad enough range of isotope values to strongly constrain the MWL.
27+
\code{mwl} will return an error if fewer than 3 sample values are provided and a warning if fewer than 10 samples are provided or if the correlation coefficient between H and O values is less than 0.7. Sample values should span a broad enough range of isotope values to strongly constrain the MWL.
2828

29-
Model II (reduced major axis) regression is used to accomodate errors on both isotope values.
29+
Model II (reduced major axis) regression is used to accommodate errors on both isotope values.
3030
}
3131

3232
\value{

man/mwlSource.Rd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ Given parameters describing a meteoric water line in H-O isotope space, generate
1111
}
1212

1313
\usage{
14-
mwlSource(obs, MWL=c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672),
15-
slope, stype = 1, ngens=1e4, ncores = 1)
14+
mwlSource(obs, MWL = NULL, slope, stype = 1, ngens=1e4, ncores = 1)
1615
}
1716

1817
\arguments{
@@ -37,7 +36,7 @@ mwlSource(obs, MWL=c(8.01, 9.57, -8.096, 2564532.2, 5.76, 80672),
3736
}
3837

3938
\details{
40-
The prior distribution of source values is constrained by \code{MWL}, which contains the parameters: slope, intercept, average d18O, sum of squares in d18O, root mean square error, and number of samples for an emperically-determined meteoric water line. This object can be created from a H and O isotope dataset using the fuction \code{\link{mwl}}. The default values reflect the Global Meteoric Water Line estimated from a global precipitation compilation in Bowen, et al. (2019). \code{stype} determines how the source uncertainty about the MWL is calculated; the default (1, confidence interval) is appropriate if the source is best represented as an integrated mixture of the samples defining the MWL, whereas option 2 (prediction interval) is appropriate if the source is best represented as a single sample.
39+
The prior distribution of source values is constrained by \code{MWL}, which contains the parameters: slope, intercept, average d18O, sum of squares in d18O, root mean square error, and number of samples for an empirically-determined meteoric water line. This object can be created from a H and O isotope dataset using the function \code{\link{mwl}}. The default value (if \code{MWL = NULL}) reflects the Global Meteoric Water Line estimated from a global precipitation compilation in Bowen, et al. (2019). \code{stype} determines how the source uncertainty about the MWL is calculated; the default (1, confidence interval) is appropriate if the source is best represented as an integrated mixture of the samples defining the MWL, whereas option 2 (prediction interval) is appropriate if the source is best represented as a single sample.
4140

4241
If \code{ncores} = 1, three chains will be run on a single core. If \code{ncores} > 1, \code{ncores} chains will be run in parallel on \code{ncores} cores.
4342
}

man/wiDB_data.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ wiDB_data(minLat = NULL, maxLat = NULL, minLong = NULL,
4949
character. Vector of one or more two-letter state or province codes for query.
5050
}
5151
\item{types}{
52-
character. Vector of one or more sample types for query. See vocabulary in the \href{https://wateriso.utah.edu/waterisotopes/pages/spatial_db/contribute.html}{wiDB tempate}.
52+
character. Vector of one or more sample types for query. See vocabulary in the \href{https://wateriso.utah.edu/waterisotopes/pages/spatial_db/contribute.html}{wiDB template}.
5353
}
5454
\item{projects}{
5555
character. Vector of one or more project codes for query.

man/wiDB_sites.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ wiDB_sites(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NULL,
4848
character. Vector of one or more two-letter state or province codes for query.
4949
}
5050
\item{types}{
51-
character. Vector of one or more sample types for query. See vocabulary in the \href{https://wateriso.utah.edu/waterisotopes/pages/spatial_db/contribute.html}{wiDB tempate}.
51+
character. Vector of one or more sample types for query. See vocabulary in the \href{https://wateriso.utah.edu/waterisotopes/pages/spatial_db/contribute.html}{wiDB template}.
5252
}
5353
\item{projects}{
5454
character. Vector of one or more project codes for query.

tests/testthat/test-mwlSource.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
#Prep MWL
22
O = runif(10, -15, -2)
33
H = O * 8 + 10 + rnorm(10, 0, 6)
4+
HO = data.frame(H, O)
45

5-
MWL = mwl(data.frame(H, O), plot = FALSE)
6+
MWL = mwl(HO, plot = FALSE)
67
test_that("mwl works", {
78
expect_length(MWL, 6)
89
})
910

11+
d = dex(HO, form = "both")
12+
test_that("dex works", {
13+
expect_s3_class(d, "data.frame")
14+
expect_equal(d[,3], H - 8 * O)
15+
expect_error(dex(H))
16+
expect_error(dex(HO, "lcex", 12))
17+
expect_error(dex(HO, "jim"))
18+
})
19+
1020
obs = iso(-60, -6, 0.5, 0.1, 0)
1121
slope = c(5, 0.3)
1222

0 commit comments

Comments
 (0)