Skip to content

Commit

Permalink
#353 allow for choosing from a couple different base ulrs for ghcnd()…
Browse files Browse the repository at this point in the history
…, bump version
  • Loading branch information
sckott committed Jun 8, 2020
1 parent a2c840d commit 8d77ca9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
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',
tornado data via the 'NOAA' storm prediction center, and more.
Version: 0.9.6.98
Version: 0.9.6.99
License: MIT + file LICENSE
Encoding: UTF-8
Language: en-US
Expand Down
39 changes: 38 additions & 1 deletion R/ghcnd.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
#' one year. In addition to measurements, columns are included for certain
#' flags, which add information on observation sources and quality and are
#' further explained in NOAA's README file for the data.
#'
#' @section Base URL:
#' The base url for data requests can be changed. The allowed urls are:
#' https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/all (default),
#' ftp://ftp.ncei.noaa.gov/pub/data/ghcn/daily/all,
#' ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all
#'
#' You can set the base url using the `RNOAA_GHCND_BASE_URL` environment
#' variable; see example below.
#'
#' The reason for this is that sometimes one base url source is temporarily
#' down, but another base url may work. It doesn't make sense to allow
#' an arbitrary base URL; open an issue if there is another valid
#' base URL for GHNCD data that we should add to the allowed set of base urls.
#'
#' @details This function saves the full set of weather data for the queried
#' site locally in the directory specified by the \code{path} argument.
Expand Down Expand Up @@ -76,6 +90,14 @@
#' # Read in a .dly file you've already downloaded
#' path <- system.file("examples/AGE00147704.dly", package = "rnoaa")
#' ghcnd_read(path)
#'
#' # change the base url for data requests
#' Sys.setenv(RNOAA_GHCND_BASE_URL =
#' "ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all")
#' ghcnd(stations$id[45], verbose = TRUE)
#' ## must be in the allowed set of urls
#' # Sys.setenv(RNOAA_GHCND_BASE_URL = "https://google.com")
#' # ghcnd(stations$id[58], verbose = TRUE)
#' }
ghcnd <- function(stationid, refresh = FALSE, ...) {
csvpath <- ghcnd_local(stationid)
Expand Down Expand Up @@ -215,7 +237,22 @@ ghcnd_GET <- function(stationid, ...){
return(dat)
}

ghcndbase <- function() "ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all"
ghcnd_allowed_urls <- c(
"https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/all",
"ftp://ftp.ncei.noaa.gov/pub/data/ghcn/daily/all",
"ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all"
)
ghcndbase <- function() {
x <- Sys.getenv("RNOAA_GHCND_BASE_URL", "")
if (identical(x, "")) {
x <- "https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/all"
}
if (!x %in% ghcnd_allowed_urls) {
stop("the RNOAA_GHCND_BASE_URL environment variable must be in set:\n",
paste0(ghcnd_allowed_urls, collapse = " \n"))
}
return(x)
}
ghcnd_remote <- function(stationid) {
file.path(ghcndbase(), paste0(stationid, ".dly"))
}
Expand Down
24 changes: 24 additions & 0 deletions man/ghcnd.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-ghcnd.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ test_that("get data", {
expect_lt(NROW(aa), NROW(bb))
expect_lt(NROW(cc), NROW(bb))
})

test_that("alternative base urls", {
expect_equal(Sys.getenv("RNOAA_GHCND_BASE_URL"), "")

Sys.setenv(RNOAA_GHCND_BASE_URL = "https://google.com")
expect_error(ghcnd("ASN00008179"), "must be in set")

Sys.unsetenv("RNOAA_GHCND_BASE_URL")
})

0 comments on commit 8d77ca9

Please sign in to comment.