-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bumped patch version returning ncdf4 object from ersst fxn for now
- Loading branch information
Showing
4 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.