Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce coops results to numeric, account for data results not named "data", add examples #129

Merged
merged 4 commits into from
Jan 2, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions R/coops.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
#' \item STND - Station datum
#' }
#'
#' @references \url{http://co-ops.nos.noaa.gov/api/}
#' @return List, of length two.
#' @references
#' \url{http://co-ops.nos.noaa.gov/api/}
#'
#' \url{http://tidesandcurrents.noaa.gov/map/}
#'
#' @return List, of length one or two.
#' \itemize{
#' \item metadata A list of metadata with slots id, name, lat, lon
#' \item data A data.frame with data
Expand All @@ -60,17 +64,65 @@
#' coops_search(station_name = 8723970, begin_date = 19820301, end_date = 20141001,
#' datum = "stnd", product = "monthly_mean")
#'
#' # Get verified water level data at Vaca Key (2723970)
#' # Get verified water level data at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' datum = "stnd", product = "water_level")
#'
#' # Get air temperature at Vaca Key (2723970)
#' # Get daily mean water level data at Fairport, OH (9063053)
#' coops_search(station_name = 9063053, begin_date = 20150927, end_date = 20150928,
#' product = "daily_mean", datum = "stnd"), time_zone = "lst")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an extra parentheses here datum = "stnd"), time_zone = "lst")

#'
#' # Get air temperature at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' product = "air_temperature")
#'
#' # Get water temperature at Vaca Key (2723970)
#' # Get water temperature at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' product = "water_temperature")
#'
#' # Get air pressure at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' product = "air_pressure")
#'
#' # Get conductivity at Eugene Island, LA (8764314)
#' coops_search(station_name = 8764314, begin_date = 20151223, end_date = 20151224,
#' product = "conductivity")
#'
#' # Get salinity at Eugene Island, LA (8764314)
#' coops_search(station_name = 8764314, begin_date = 20150927, end_date = 20150928,
#' product = "salinity")
#'
#' # Get humidity at Eugene Island, LA (8764314)
#' coops_search(station_name = 8764314, begin_date = 20150927, end_date = 20150928,
#' product = "humidity")
#'
#' # Get wind at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' product = "wind")
#'
#' # Get hourly water level height at Key West (8724580)
#' coops_search(station_name = 8724580, begin_date = 20140927, end_date = 20140928,
#' product = "hourly_height", datum = "stnd")
#'
#' # Get high-low water level at Key West (8724580)
#' coops_search(station_name = 8724580, begin_date = 20140927, end_date = 20140928,
#' product = "high_low", datum = "stnd")
#'
#' # Get currents data at Pascagoula Harbor (ps0401)
#' coops_search(station_name = "ps0401", begin_date = 20151221, end_date = 20151222,
#' product = "currents")
#'
#' # Get one-minute water level at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140927, end_date = 20140928,
#' datum = "stnd", product = "one_minute_water_level")
#'
#' # Get datums at Fort Myers, FL (8725520)
#' coops_search(station_name = 8725520, product = "datums")
#'
#' # Get water level predictions at Vaca Key (8723970)
#' coops_search(station_name = 8723970, begin_date = 20140928, end_date = 20140929,
#' datum = "stnd", product = "predictions")
#'
#' }
coops_search <- function(begin_date = NULL, end_date = NULL, station_name = NULL,
product, datum = NULL, units = "metric", time_zone = "gmt",
Expand All @@ -88,10 +140,21 @@ coops_search <- function(begin_date = NULL, end_date = NULL, station_name = NULL

res <- coops_GET(coops_base(), args, ...)

if (is.null(nrow(res$data))) {
if (is.null(nrow(res$data)) & is.null(nrow(res$predictions))
& is.null(nrow(res$datums))) {
stop("No data was found", call. = FALSE)
}

if(!(product %in% c("monthly_mean", "datums"))){
res[[length(res)]][,1] <- as.POSIXct(res[[length(res)]][,1])
}

if(product == "monthly_mean"){
res[[length(res)]] <- data.frame(apply(res[[length(res)]], 2, as.numeric))
}

res[[length(res)]][,2] <- as.numeric(res[[length(res)]][,2])

res
}

Expand Down
58 changes: 54 additions & 4 deletions man/coops.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.