Skip to content

Commit 5fbc307

Browse files
Merge pull request #179 from ropensci/dev
2 parents 78a899f + 0bd2a22 commit 5fbc307

8 files changed

Lines changed: 72 additions & 62 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: weathercan
22
Type: Package
33
Title: Download Weather Data from Environment and Climate Change Canada
4-
Version: 0.7.5
4+
Version: 0.7.6
55
Authors@R: c(
66
person("Steffi", "LaZerte", email = "sel@steffilazerte.ca", role = c("aut","cre"), comment = c(ORCID = "0000-0002-7690-8360")),
77
person("Sam", "Albers", email = "sam.albers@gmail.com", role = c("ctb"), comment = c(ORCID = "0000-0002-9270-7884")),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# weathercan 0.7.6
2+
- Standardize normals year range and checks
3+
- Fix bug in `stations_search()` that precluded doing normals and years searches at the same time
4+
- Change `stations_search()` output to include interval, start and end years
5+
16
# weathercan 0.7.5
27
- Fix province names for stations data frame (#175)
38
- Don't allow start times earlier than 1840 (earliest API will return; #174)

R/normals.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#' data frame or the \code{\link{stations_search}} function to find Climate
1111
#' IDs.
1212
#' @param normals_years Character. The year range for which you want climate
13-
#' normals. Default "1981-2010". One of "1971-2000", "1981-2010", "1991-2020".
14-
#' Note: Some "1991-2020" are available online, but are not yet downloadable
13+
#' normals. Default `1981-2010`. One of `current`,
14+
#' `1981-2010`, or `1971-2000`. `current` returns only stations
15+
#' from the most recent *complete* normals year range (i.e. `1981-2010`).
16+
#' Note: Some `1991-2020` are available online, but are not yet downloadable
1517
#' via weathercan.
1618
#' @param format Logical. If TRUE (default) formats measurements to numeric and
1719
#' date accordingly. Unlike `weather_dl()`, `normals_dl()` will always format
@@ -79,7 +81,7 @@
7981
#' unnest(frost)
8082
#' @export
8183

82-
normals_dl <- function(climate_ids, normals_years = "1981-2010",
84+
normals_dl <- function(climate_ids, normals_years = "current",
8385
format = TRUE, stn = NULL,
8486
verbose = FALSE, quiet = FALSE) {
8587

@@ -91,13 +93,8 @@ normals_dl <- function(climate_ids, normals_years = "1981-2010",
9193
}
9294
stn <- stations()
9395

94-
if(normals_years == "1991-2020") {
95-
stop("The new normals for 1991-2020 are not yet available via weathercan",
96-
call. = FALSE)
97-
}
98-
9996
check_ids(climate_ids, stn, type = "climate_id")
100-
check_normals(normals_years)
97+
normals_years <- check_normals(normals_years)
10198

10299
yrs <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
103100

R/stations.R

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -423,47 +423,35 @@ stations_search <- function(name = NULL,
423423
}
424424

425425
check_int(interval)
426-
427426
stn <- dplyr::filter(stations(),
428427
.data$interval %in% !! interval, !is.na(.data$start))
429428

429+
normals_years <- check_normals(normals_years, null_ok = TRUE)
430430
if(!is.null(normals_years)) {
431-
432-
if(normals_years == "1991-2020") {
433-
message(
434-
"You can find out which stations have normals for 1991-2020, ",
435-
"but be aware that they are not yet available for download via weathercan")
436-
}
437-
438-
if(normals_years == "current") {
439-
yr <- "normals_1981_2010" # Currently set Normals
440-
} else {
441-
yr <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
442-
}
431+
yr <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
443432
stn <- dplyr::filter(stn, .data[[yr]])
444-
} else {
445-
446-
if (!is.null(starts_latest)){
447-
suppressWarnings({
448-
starts_latest <- try(as.numeric(as.character(starts_latest)),
449-
silent = TRUE)
450-
})
451-
if (is.na(starts_latest) | inherits(starts_latest, "try-error")){
452-
stop("'starts_latest' needs to be coercible into numeric", call. = FALSE)
453-
}
454-
stn <- dplyr::filter(stn, .data$start <= starts_latest)
433+
}
434+
435+
if (!is.null(starts_latest)){
436+
suppressWarnings({
437+
starts_latest <- try(as.numeric(as.character(starts_latest)),
438+
silent = TRUE)
439+
})
440+
if (is.na(starts_latest) | inherits(starts_latest, "try-error")){
441+
stop("'starts_latest' needs to be a year (YYYY)", call. = FALSE)
455442
}
456-
457-
if (!is.null(ends_earliest)){
458-
suppressWarnings({
459-
ends_earliest <- try(as.numeric(as.character(ends_earliest)),
460-
silent = TRUE)
461-
})
462-
if (is.na(ends_earliest) | inherits(ends_earliest, "try-error")){
463-
stop("'ends_earliest' needs to be coercible into numeric", call. = FALSE)
464-
}
465-
stn <- dplyr::filter(stn, .data$end >= ends_earliest)
443+
stn <- dplyr::filter(stn, .data$start <= starts_latest)
444+
}
445+
446+
if (!is.null(ends_earliest)){
447+
suppressWarnings({
448+
ends_earliest <- try(as.numeric(as.character(ends_earliest)),
449+
silent = TRUE)
450+
})
451+
if (is.na(ends_earliest) | inherits(ends_earliest, "try-error")){
452+
stop("'ends_earliest' needs to be a year (YYYY)", call. = FALSE)
466453
}
454+
stn <- dplyr::filter(stn, .data$end >= ends_earliest)
467455
}
468456

469457
if(!is.null(name)) {
@@ -519,10 +507,7 @@ stations_search <- function(name = NULL,
519507
.data$station_name,
520508
.data$station_id,
521509
.data$interval)
522-
if(!is.null(normals_years)) {
523-
stn <- dplyr::select(stn, -"interval", -"start", -"end") %>%
524-
dplyr::distinct()
525-
}
510+
526511
stn
527512
}
528513

R/utils.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,31 @@ check_ids <- function(ids, stn, type){
3232
}
3333
}
3434

35-
check_normals <- function(normals_years) {
36-
if(!is.character(normals_years) ||
37-
!stringr::str_detect(normals_years, "^[0-9]{4}-[0-9]{4}$")) {
38-
stop("'normals_years' must be a text string in the format YYYY-YYYY e.g., '1981-2010'",
39-
call. = FALSE)
40-
}
35+
check_normals <- function(normals_years, null_ok = FALSE) {
36+
37+
if(null_ok && is.null(normals_years)) return(normals_years)
38+
39+
if(normals_years == "current") {
40+
message("The most current normals available for download by weathercan are '1981-2010'")
41+
normals_years <- "1981-2010"
42+
}
43+
44+
if(!null_ok) new_message <- stop else new_message <- message
45+
46+
if(normals_years == "1991-2020") {
47+
new_message(
48+
"You use `stations_search()` to see which stations have normals for 1991-2020, ",
49+
"but be aware that they are not yet available for download via weathercan")
50+
}
51+
52+
if(is.null(normals_years) ||
53+
!is.character(normals_years) ||
54+
!stringr::str_detect(normals_years, "^[0-9]{4}-[0-9]{4}$")
55+
) {
56+
stop("'normals_years' must be either 'current' or a text string in the format YYYY-YYYY e.g., '1981-2010'",
57+
call. = FALSE)
58+
}
59+
normals_years
4160
}
4261

4362
find_line <- function(headings, cols) {

man/normals_dl.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_03_station_dl.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ test_that("stations_search 'starts_latest' and 'ends_earliest' together", {
256256
test_that("stations_search returns normals only", {
257257
expect_warning(s <- stations_search("Brandon", normals_only = TRUE),
258258
"`normals_only` is deprecated")
259-
expect_silent(s <- stations_search("Brandon", normals_years = "current"))
259+
expect_message(
260+
s <- stations_search("Brandon", normals_years = "current"),
261+
"The most current normals available for download by weathercan are"
262+
)
260263
expect_gt(nrow(stations()), nrow(s))
261264
expect_true(all(s$normals))
262-
expect_equal(unique(s$station_id), s$station_id)
263265

264266
expect_silent(s1 <- stations_search("Brandon", normals_years = "1981-2010"))
265267
expect_gt(nrow(stations()), nrow(s1))

tests/testthat/test_08_normals.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,14 @@ test_that("normals_dl() gets extreme wind chill correctly", {
309309
skip_on_cran()
310310
skip_if_offline()
311311

312-
expect_silent(nd <- normals_dl(climate_id = "2100517")) %>%
313-
expect_s3_class("tbl_df")
312+
expect_message(nd <- normals_dl(climate_id = "2100517"), "current normals")
313+
expect_s3_class(nd, "tbl_df")
314314
})
315315

316316
test_that("normals_dl() multiple weird stations", {
317317
skip_on_cran()
318318
skip_if_offline()
319-
expect_silent(nd <- normals_dl(climate_ids = c("301C3D4", "301FFNJ", "301N49A")))
319+
expect_message(nd <- normals_dl(climate_ids = c("301C3D4", "301FFNJ", "301N49A")), "current normals")
320320

321321
expect_snapshot_value(nd, style = "json2", tolerance = 0.001)
322322
})

0 commit comments

Comments
 (0)