Skip to content

Commit

Permalink
Merge pull request #129 from jsta/master
Browse files Browse the repository at this point in the history
Coerce coops results to numeric, account for data results not named "data", add examples
  • Loading branch information
sckott committed Jan 2, 2016
2 parents 0b145f1 + 2bc3043 commit c47a75d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 10 deletions.
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")
#'
#' # 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.

0 comments on commit c47a75d

Please sign in to comment.