Skip to content

Commit

Permalink
add rnoaa_options function for handlling pkg level options
Browse files Browse the repository at this point in the history
within rnoaa_options added cache_messages param setting to toggle messages
added a test for rnoaa_options
aaded message suppression handling in the four functions that currently use hoardr caching: cpc, arc2, lcd, bsw
  • Loading branch information
sckott committed Mar 23, 2020
1 parent deff8b9 commit 32d5f53
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 14 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.5.96
Version: 0.9.5.97
License: MIT + file LICENSE
Encoding: UTF-8
Language: en-US
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export(noaa_plot)
export(noaa_seaice)
export(noaa_stations)
export(readshpfile)
export(rnoaa_options)
export(se_data)
export(se_files)
export(sea_ice)
Expand Down
4 changes: 3 additions & 1 deletion R/arc2.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' - precip - precipitation
#'
#' @examples \dontrun{
#' arc2(date = "1983-01-01")
#' x = arc2(date = "1983-01-01")
#' arc2(date = "2017-02-14")
#' }
arc2 <- function(date, ...) {
Expand All @@ -36,6 +36,8 @@ arc2_get <- function(year, month, day, cache = TRUE, overwrite = FALSE, ...) {
res <- suppressMessages(
arc2_GET_write(sub("/$", "", key), file, overwrite, ...))
file <- res$content
} else {
cache_mssg(file)
}
return(file)
}
Expand Down
2 changes: 2 additions & 0 deletions R/bsw.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ bsw_get <- function(year, month, day, uv_stress, resolution,
)
if (!file.exists(file)) {
suppressMessages(bsw_GET_write(key, file, overwrite, ...))
} else {
cache_mssg(file)
}
return(file)
}
Expand Down
4 changes: 3 additions & 1 deletion R/cpc.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' you can easily do it yourself by e.g., `subset(x, precip >= 0)`
#'
#' @examples \dontrun{
#' cpc_prcp(date = "2017-01-15")
#' x = cpc_prcp(date = "2017-01-15")
#' cpc_prcp(date = "2015-06-05")
#' cpc_prcp(date = "2017-01-15")
#' cpc_prcp(date = "2005-07-09")
Expand Down Expand Up @@ -73,6 +73,8 @@ cpc_get <- function(year, month, day, us, cache = TRUE, overwrite = FALSE, ...)
if (!file.exists(file)) {
res <- suppressMessages(cpc_GET_write(sub("/$", "", key), file, overwrite, ...))
file <- res$content
} else {
cache_mssg(file)
}
return(file)
}
Expand Down
2 changes: 1 addition & 1 deletion R/isd.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ isdbase <- function() 'https://www1.ncdc.noaa.gov/pub/data/noaa'
read_isd <- function(x, cleanup, force, additional, parallel, cores, progress) {
path_rds <- x
if (file.exists(path_rds) && !force) {
message("found in cache")
cache_mssg(path_rds)
df <- readRDS(path_rds)
} else {
df <- isdparser::isd_parse(
Expand Down
10 changes: 6 additions & 4 deletions R/lcd.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
#' column name
#'
#' @examples \dontrun{
#' lcd(station = "01338099999", year = 2017, verbose = TRUE)
#' lcd(station = "01338099999", year = 2015, verbose = TRUE)
#' lcd(station = "02413099999", year = 2009, verbose = TRUE)
#' lcd(station = "02413099999", year = 2001, verbose = TRUE)
#' x = lcd(station = "01338099999", year = 2017)
#' lcd(station = "01338099999", year = 2015)
#' lcd(station = "02413099999", year = 2009)
#' lcd(station = "02413099999", year = 2001)
#'
#' # pass curl options
#' lcd(station = "02413099999", year = 2002, verbose = TRUE)
Expand All @@ -65,6 +65,8 @@ lcd_get <- function(station, year, overwrite = FALSE, ...) {
sprintf("%s_%s.csv", year, station))
if (!file.exists(file)) {
suppressMessages(lcd_GET_write(key, file, overwrite, ...))
} else {
cache_mssg(file)
}
return(file)
}
Expand Down
30 changes: 30 additions & 0 deletions R/rnoaa_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
roenv <- new.env()
roenv$cache_messages <- TRUE

#' rnoaa options
#' @export
#' @param cache_messages (logical) whether to emit messages with information
#' on caching status for function calls that can cache data. default: `TRUE`
#' @details rnoaa package level options; stored in an internal
#' package environment `roenv`
#' @examples \dontrun{
#' rnoaa_options(cache_messages = FALSE)
#' }
rnoaa_options <- function(cache_messages = TRUE) {
roenv$cache_messages <- cache_messages
return(NULL)
}

stract <- function(str, pattern) regmatches(str, regexpr(pattern, str))

cache_mssg <- function(file) {
if (roenv$cache_messages) {
fi <- file.info(file)
size <- round(fi$size/1000000, 3)
chaftdec <- nchar(stract(as.character(size), '^[0-9]+'))
if (chaftdec > 1) size <- round(size, 1)
message("using cached file: ", file)
message(
sprintf("date created/size(mb): %s / %s", fi$ctime, size))
}
}
2 changes: 1 addition & 1 deletion man/arc2.Rd

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

2 changes: 1 addition & 1 deletion man/cpc_prcp.Rd

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

8 changes: 4 additions & 4 deletions man/lcd.Rd

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

24 changes: 24 additions & 0 deletions man/rnoaa_options.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-rnoaa_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# delete any cached files
arc2_cache$delete_all()

test_that("rnoaa_options", {
skip_on_cran()

expect_is(rnoaa_options, "function")
expect_null(rnoaa_options())

# after setting param its in the env var
expect_true(roenv$cache_messages)
rnoaa_options(FALSE)
expect_false(roenv$cache_messages)

# reset options
rnoaa_options()
})

0 comments on commit 32d5f53

Please sign in to comment.