Skip to content

Commit

Permalink
added ersst function, fix #96
Browse files Browse the repository at this point in the history
bumped patch version
returning ncdf4 object from ersst fxn for now
  • Loading branch information
sckott committed Dec 1, 2015
1 parent 2bc6a2e commit 5b71ec5
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Description: Client for many 'NOAA' data sources including the 'NCDC' climate
for 'NOAA' sea ice data, the 'NOAA' severe weather inventory, 'NOAA' Historical
Observing 'Metadata' Repository ('HOMR') data, 'NOAA' storm data via 'IBTrACS',
and tornado data via the 'NOAA' storm prediction center.
Version: 0.4.4.9400
Version: 0.4.8.9000
License: MIT + file LICENSE
Authors@R: c(
person("Hart", "Edmund", role = "ctb", email = "Edmund.m.hart@gmail.com"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(erddap_grid)
export(erddap_info)
export(erddap_search)
export(erddap_table)
export(ersst)
export(gefs)
export(gefs_dimension_values)
export(gefs_dimensions)
Expand Down
79 changes: 79 additions & 0 deletions R/ersst.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' NOAA Extended Reconstructed Sea Surface Temperature (ERSST) data
#'
#' @export
#' @param year (numeric) A year. Must be > 1853. The max value is whatever
#' the current year is. Required
#' @param month A month, character or numeric. If single digit (e.g. 8), we
#' add a zero in front (e.g., 08). Required
#' @param ... Curl options passed on to \code{\link[httr]{GET}}. Optional
#' @return An \code{ncdf4} object for now, may change output later to
#' perhaps a data.frame. See \pkg{ncdf4} for parsing the output.
#' @references
#' \url{https://www.ncdc.noaa.gov/data-access/marineocean-data/extended-reconstructed-sea-surface-temperature-ersst-v4}
#' @details NetCDF files are downloaded, stored, and read from the directory given in the
#' \code{path} parameter. The default is \code{~/.rnoaa/ersst}, and files are
#' saved within whatever that directory is.
#'
#' Files are quite small, so we don't worry about reading in cached data to
#' save time, as we do in other functions in this package.
#' @examples \dontrun{
#' # October, 2015
#' ersst(year = 2015, month = 10)
#'
#' # May, 2015
#' ersst(year = 2015, month = 5)
#' ersst(year = 2015, month = "05")
#'
#' # February, 1890
#' ersst(year = 1890, month = 2)
#'
#' # Process data
#' library("ncdf4")
#' res <- ersst(year = 1890, month = 2)
#' ## varibles
#' names(res$var)
#' ## get a variable
#' ncdf4::ncvar_get(res, "lon_bnds")
#'
#' # curl debugging
#' library('httr')
#' ersst(year = 2015, month = 10, config=verbose())
#' }
ersst <- function(year, month, path = "~/.rnoaa/ersst", overwrite = TRUE, ...) {
check4pkg("ncdf4")
ff <- ersst_local(path, year, month)
dpath <- ersst_GET(make_ersst(year, month), path = ff, overwrite, ...)
ncdf4::nc_open(dpath)
}

make_ersst <- function(year, month) {
check_year(year)
month <- check_month(month)
paste0(".", year, month, ".nc")
}

ersst_base <- function(ver = "v4") {
sprintf("http://www1.ncdc.noaa.gov/pub/data/cmb/ersst/%s/netcdf/ersst.%s", ver, ver)
}

check_year <- function(x) {
if (nchar(x) != 4) stop("year must be a length 4 numeric", call. = FALSE)
if (as.numeric(x) < 1854) stop("year must be > 1853", call. = FALSE)
}

check_month <- function(x) {
if (!nchar(x) %in% c(1, 2)) stop("month must be a length 1 or 2 month value", call. = FALSE)
if (as.numeric(x) < 1 || as.numeric(x) > 12) stop("month must be a number between 1 and 12", call. = FALSE)
if (nchar(x) == 1) paste0("0", x) else x
}

ersst_GET <- function(dat, path, overwrite, ...) {
dir.create(dirname(path), showWarnings = FALSE, recursive = TRUE)
res <- GET(paste0(ersst_base(), dat), write_disk(path, overwrite = overwrite), ...)
stop_for_status(res)
res$request$output$path
}

ersst_local <- function(path, year, month) {
file.path(path, sprintf("%s%s.nc", year, month))
}
61 changes: 61 additions & 0 deletions man/ersst.Rd

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

0 comments on commit 5b71ec5

Please sign in to comment.