Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: weathercan
Type: Package
Title: Download Weather Data from Environment and Climate Change Canada
Version: 0.7.5
Version: 0.7.6
Authors@R: c(
person("Steffi", "LaZerte", email = "sel@steffilazerte.ca", role = c("aut","cre"), comment = c(ORCID = "0000-0002-7690-8360")),
person("Sam", "Albers", email = "sam.albers@gmail.com", role = c("ctb"), comment = c(ORCID = "0000-0002-9270-7884")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# weathercan 0.7.6
- Standardize normals year range and checks
- Fix bug in `stations_search()` that precluded doing normals and years searches at the same time
- Change `stations_search()` output to include interval, start and end years

# weathercan 0.7.5
- Fix province names for stations data frame (#175)
- Don't allow start times earlier than 1840 (earliest API will return; #174)
Expand Down
15 changes: 6 additions & 9 deletions R/normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#' data frame or the \code{\link{stations_search}} function to find Climate
#' IDs.
#' @param normals_years Character. The year range for which you want climate
#' normals. Default "1981-2010". One of "1971-2000", "1981-2010", "1991-2020".
#' Note: Some "1991-2020" are available online, but are not yet downloadable
#' normals. Default `1981-2010`. One of `current`,
#' `1981-2010`, or `1971-2000`. `current` returns only stations
#' from the most recent *complete* normals year range (i.e. `1981-2010`).
#' Note: Some `1991-2020` are available online, but are not yet downloadable
#' via weathercan.
#' @param format Logical. If TRUE (default) formats measurements to numeric and
#' date accordingly. Unlike `weather_dl()`, `normals_dl()` will always format
Expand Down Expand Up @@ -79,7 +81,7 @@
#' unnest(frost)
#' @export

normals_dl <- function(climate_ids, normals_years = "1981-2010",
normals_dl <- function(climate_ids, normals_years = "current",
format = TRUE, stn = NULL,
verbose = FALSE, quiet = FALSE) {

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

if(normals_years == "1991-2020") {
stop("The new normals for 1991-2020 are not yet available via weathercan",
call. = FALSE)
}

check_ids(climate_ids, stn, type = "climate_id")
check_normals(normals_years)
normals_years <- check_normals(normals_years)

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

Expand Down
61 changes: 23 additions & 38 deletions R/stations.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,47 +423,35 @@ stations_search <- function(name = NULL,
}

check_int(interval)

stn <- dplyr::filter(stations(),
.data$interval %in% !! interval, !is.na(.data$start))

normals_years <- check_normals(normals_years, null_ok = TRUE)
if(!is.null(normals_years)) {

if(normals_years == "1991-2020") {
message(
"You can find out which stations have normals for 1991-2020, ",
"but be aware that they are not yet available for download via weathercan")
}

if(normals_years == "current") {
yr <- "normals_1981_2010" # Currently set Normals
} else {
yr <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
}
yr <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
stn <- dplyr::filter(stn, .data[[yr]])
} else {

if (!is.null(starts_latest)){
suppressWarnings({
starts_latest <- try(as.numeric(as.character(starts_latest)),
silent = TRUE)
})
if (is.na(starts_latest) | inherits(starts_latest, "try-error")){
stop("'starts_latest' needs to be coercible into numeric", call. = FALSE)
}
stn <- dplyr::filter(stn, .data$start <= starts_latest)
}

if (!is.null(starts_latest)){
suppressWarnings({
starts_latest <- try(as.numeric(as.character(starts_latest)),
silent = TRUE)
})
if (is.na(starts_latest) | inherits(starts_latest, "try-error")){
stop("'starts_latest' needs to be a year (YYYY)", call. = FALSE)
}

if (!is.null(ends_earliest)){
suppressWarnings({
ends_earliest <- try(as.numeric(as.character(ends_earliest)),
silent = TRUE)
})
if (is.na(ends_earliest) | inherits(ends_earliest, "try-error")){
stop("'ends_earliest' needs to be coercible into numeric", call. = FALSE)
}
stn <- dplyr::filter(stn, .data$end >= ends_earliest)
stn <- dplyr::filter(stn, .data$start <= starts_latest)
}

if (!is.null(ends_earliest)){
suppressWarnings({
ends_earliest <- try(as.numeric(as.character(ends_earliest)),
silent = TRUE)
})
if (is.na(ends_earliest) | inherits(ends_earliest, "try-error")){
stop("'ends_earliest' needs to be a year (YYYY)", call. = FALSE)
}
stn <- dplyr::filter(stn, .data$end >= ends_earliest)
}

if(!is.null(name)) {
Expand Down Expand Up @@ -519,10 +507,7 @@ stations_search <- function(name = NULL,
.data$station_name,
.data$station_id,
.data$interval)
if(!is.null(normals_years)) {
stn <- dplyr::select(stn, -"interval", -"start", -"end") %>%
dplyr::distinct()
}

stn
}

Expand Down
31 changes: 25 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,31 @@ check_ids <- function(ids, stn, type){
}
}

check_normals <- function(normals_years) {
if(!is.character(normals_years) ||
!stringr::str_detect(normals_years, "^[0-9]{4}-[0-9]{4}$")) {
stop("'normals_years' must be a text string in the format YYYY-YYYY e.g., '1981-2010'",
call. = FALSE)
}
check_normals <- function(normals_years, null_ok = FALSE) {

if(null_ok && is.null(normals_years)) return(normals_years)

if(normals_years == "current") {
message("The most current normals available for download by weathercan are '1981-2010'")
normals_years <- "1981-2010"
}

if(!null_ok) new_message <- stop else new_message <- message

if(normals_years == "1991-2020") {
new_message(
"You use `stations_search()` to see which stations have normals for 1991-2020, ",
"but be aware that they are not yet available for download via weathercan")
}

if(is.null(normals_years) ||
!is.character(normals_years) ||
!stringr::str_detect(normals_years, "^[0-9]{4}-[0-9]{4}$")
) {
stop("'normals_years' must be either 'current' or a text string in the format YYYY-YYYY e.g., '1981-2010'",
call. = FALSE)
}
normals_years
}

find_line <- function(headings, cols) {
Expand Down
8 changes: 5 additions & 3 deletions man/normals_dl.Rd

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

6 changes: 4 additions & 2 deletions tests/testthat/test_03_station_dl.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,12 @@ test_that("stations_search 'starts_latest' and 'ends_earliest' together", {
test_that("stations_search returns normals only", {
expect_warning(s <- stations_search("Brandon", normals_only = TRUE),
"`normals_only` is deprecated")
expect_silent(s <- stations_search("Brandon", normals_years = "current"))
expect_message(
s <- stations_search("Brandon", normals_years = "current"),
"The most current normals available for download by weathercan are"
)
expect_gt(nrow(stations()), nrow(s))
expect_true(all(s$normals))
expect_equal(unique(s$station_id), s$station_id)

expect_silent(s1 <- stations_search("Brandon", normals_years = "1981-2010"))
expect_gt(nrow(stations()), nrow(s1))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_08_normals.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ test_that("normals_dl() gets extreme wind chill correctly", {
skip_on_cran()
skip_if_offline()

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

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

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