Skip to content

Commit

Permalink
Added import_remote function, tweak to ecometab function to format al…
Browse files Browse the repository at this point in the history
…l columns as numeric
  • Loading branch information
fawda123 committed Mar 23, 2015
1 parent 13824df commit 425f69f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: SWMPr
Type: Package
Title: SWMPr package for estuarine monitoring data
Version: 1.8.1
Date: 2015-3-20
Version: 1.9.0
Date: 2015-3-23
Author: Marcus Beck
Maintainer: Marcus Beck <[email protected]>
Description: This packages provides functions for retrieving, organizing, and
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(decomp_cj.swmpr)
export(ecometab)
export(hist.swmpr)
export(import_local)
export(import_remote)
export(lines.swmpr)
export(map_reserve)
export(metab_day)
Expand Down
9 changes: 8 additions & 1 deletion R/swmpr_analyze.R
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,13 @@ ecometab.swmpr <- function(swmpr_in, depth_val = NULL, metab_units = 'mmol', tra
'wspd', 'bp')
dat <- dat[,names(dat) %in% to_keep]

# all vals as numeric
dat[, 2:ncol(dat)] <- apply(
dat[, 2:ncol(dat), drop = FALSE],
2,
function(x) suppressWarnings(as.numeric(x))
)

#convert do from mg/L to mmol/m3
dat$do <- dat[, 'do_mgl'] / 32 * 1000

Expand All @@ -1077,7 +1084,7 @@ ecometab.swmpr <- function(swmpr_in, depth_val = NULL, metab_units = 'mmol', tra
# take diff of each column, divide by 2, add original value
datetimestamp <- diff(dat$datetimestamp)/2 + dat$datetimestamp[-c(nrow(dat))]
dat <- apply(
dat[,2:ncol(dat)],
dat[,2:ncol(dat), drop = FALSE],
2,
function(x) diff(x)/2 + x[1:(length(x) -1)]
)
Expand Down
57 changes: 56 additions & 1 deletion R/swmpr_retrieval.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,59 @@ import_local <- function(path, station_code, trace = FALSE){
# return data frame
return(out)

}
}



######
#' Import SWMP data from Amazon web services
#'
#' Import SWMP data that are stored on Amazon web services as .RData files. Data include almost all available data from 1994 to 2014 for every SWMP station.
#'
#' @param station_code chr string of station to import, i.e., 7 or 8 characters indicating the reserve, site, and data type. Do not include years.
#'
#' @export
#'
#' @return Returns a swmpr object.
#'
#' @details
#' This function allows quick retrieval of .RData files for all data at a single SwMP site. It differs from the other data retrieval functions in that the raw data are downloaded from an independent remote server. Retrieval time is much faster because the files are in binary format for quick import. However, the data are only available up to December 2014 and may not be regularly updated. Always use the CDMO for current data. The data have also been pre-processed using the \code{\link{qaqc}} and setstep functions.
#'
#' The files are available here: \url{https://s3.amazonaws.com/swmpalldata/}. Files can be obtained using the function or by copying the URL to a web browser with the station name appended to the address, including the .RData file extenssion. For example, \url{https://s3.amazonaws.com/swmpalldata/acebbnut.RData}.
#'
#' @seealso \code{\link{import_local}}
#'
#' @examples
#' ## see the available files on the server
#' library(XML)
#' library(httr)
#' files_s3 <- GET('https://s3.amazonaws.com/swmpalldata/')$content
#' files_s3 <- rawToChar(files_s3)
#' files_s3 <- htmlTreeParse(files_s3, useInternalNodes = T)
#' files_s3 <- xpathSApply(files_s3, '//contents//key', xmlValue)
#'
#' ## import a file
#' dat <- import_remote('acebbnut')
#'
#' head(dat)
#'
#' attributes(dat)
import_remote <- function(station_code){

# download
raw_content <- paste0(
'https://s3.amazonaws.com/swmpalldata/',
station_code,
'.RData'
)
raw_content <- httr::GET(raw_content)$content
connect <- rawConnection(raw_content)
load(connect)
wq <- get(station_code)
close(connect)

# return
return(wq)

}

2 changes: 1 addition & 1 deletion man/ecometab.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Input data include both water quality and weather time series, which are typical

The open-water method is a common approach to quantify net ecosystem metabolism using a mass balance equation that describes the change in dissolved oxygen over time from the balance between photosynthetic and respiration processes, corrected using an empirically constrained air-sea gas diffusion model (see Ro and Hunt 2006, Thebault et al. 2008). The diffusion-corrected DO flux estimates are averaged separately over each day and night of the time series. The nighttime average DO flux is used to estimate respiration rates, while the daytime DO flux is used to estimate net primary production. To generate daily integrated rates, respiration rates are assumed constant such that hourly night time DO flux rates are multiplied by 24. Similarly, the daytime DO flux rates are multiplied by the number of daylight hours, which varies with location and time of year, to yield net daytime primary production. Respiration rates are subtracted from daily net production estimates to yield gross production rates. The metabolic day is considered the 24 hour period between sunsets on two adjacent calendar days.

Aereal rates for gross production and total respiration are based on volumetric rates normalized to the depth of the water column at the sampling location, which is assumed to be well-mixed, such that the DO sensor is reflecting the integrated processes in the entire water column (including the benthos). Water column depth is calculated as the mean value of the depth variable across the time series in the \code{\link{swmpr}} object. Depth values are floored at one meter for very shallow stations and 0.5 meters is also added to reflect the practice of placing sensors slightly off of the bottom. Additionally, the air-sea gas exchange model is calibrated with wind data either collected at, or adjusted to, wind speed at 10 m above the surface. The metadata should be consulted for exact height. The value can be changed manually using a \code{height} argument, which is passed to \code{\link{calckl}}.
Areal rates for gross production and total respiration are based on volumetric rates normalized to the depth of the water column at the sampling location, which is assumed to be well-mixed, such that the DO sensor is reflecting the integrated processes in the entire water column (including the benthos). Water column depth is calculated as the mean value of the depth variable across the time series in the \code{\link{swmpr}} object. Depth values are floored at one meter for very shallow stations and 0.5 meters is also added to reflect the practice of placing sensors slightly off of the bottom. Additionally, the air-sea gas exchange model is calibrated with wind data either collected at, or adjusted to, wind speed at 10 m above the surface. The metadata should be consulted for exact height. The value can be changed manually using a \code{height} argument, which is passed to \code{\link{calckl}}.

A minimum of three records are required for both day and night periods to calculate daily metabolism estimates. Occasional missing values for air temperature, barometric pressure, and wind speed are replaced with the climatological means (hourly means by month) for the period of record using adjacent data within the same month as the missing data.

Expand Down
42 changes: 42 additions & 0 deletions man/import_remote.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/swmpr_retrieval.R
\name{import_remote}
\alias{import_remote}
\title{Import SWMP data from Amazon web services}
\usage{
import_remote(station_code)
}
\arguments{
\item{station_code}{chr string of station to import, i.e., 7 or 8 characters indicating the reserve, site, and data type. Do not include years.}
}
\value{
Returns a swmpr object.
}
\description{
Import SWMP data that are stored on Amazon web services as .RData files. Data include almost all available data from 1994 to 2014 for every SWMP station.
}
\details{
This function allows quick retrieval of .RData files for all data at a single SwMP site. It differs from the other data retrieval functions in that the raw data are downloaded from an independent remote server. Retrieval time is much faster because the files are in binary format for quick import. However, the data are only available up to December 2014 and may not be regularly updated. Always use the CDMO for current data. The data have also been pre-processed using the \code{\link{qaqc}} and setstep functions.

The files are available here: \url{https://s3.amazonaws.com/swmpalldata/}. Files can be obtained using the function or by copying the URL to a web browser with the station name appended to the address, including the .RData file extenssion. For example, \url{https://s3.amazonaws.com/swmpalldata/acebbnut.RData}.
}
\examples{
## see the available files on the server
library(XML)
library(httr)
files_s3 <- GET('https://s3.amazonaws.com/swmpalldata/')$content
files_s3 <- rawToChar(files_s3)
files_s3 <- htmlTreeParse(files_s3, useInternalNodes = T)
files_s3 <- xpathSApply(files_s3, '//contents//key', xmlValue)

## import a file
dat <- import_remote('acebbnut')

head(dat)

attributes(dat)
}
\seealso{
\code{\link{import_local}}
}

2 changes: 1 addition & 1 deletion man/plot_summary.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data(apacpnut)
dat <- qaqc(apacpnut)

## plot
plot_summary(dat, param = 'chla_n')
plot_summary(dat, param = 'chla_n', years = c(2007, 2013))
}
\seealso{
\code{\link[ggplot2]{ggplot}}
Expand Down

0 comments on commit 425f69f

Please sign in to comment.