-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathnormals.R
More file actions
503 lines (434 loc) · 18.3 KB
/
Copy pathnormals.R
File metadata and controls
503 lines (434 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#' Download climate normals from Environment and Climate Change Canada
#'
#' Downloads climate normals from Environment and Climate Change Canada (ECCC)
#' for one or more stations (defined by `climate_id`s). For details and units,
#' see the [`glossary_normals`] data frame or the `glossary_normals` vignette:
#' \code{vignette("glossary_normals", package = "weathercan")}
#'
#' @param climate_ids Character. A vector containing the Climate ID(s) of the
#' station(s) you wish to download data from. See the \code{\link{stations}}
#' 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 `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
#' column headings as normals data from ECCC cannot be directly made into a
#' data frame without doing so.
#' @inheritParams weather_dl
#'
#' @details Climate normals from ECCC include two types of data, averages by
#' month for a variety of measurements as well as data relating to the
#' frost-free period. Because these two data sources are quite different, we
#' return them as nested data so the user can extract them as they wish. See
#' examples for how to use the `unnest()` function from the
#' [`tidyr`](https://tidyr.tidyverse.org/)
#' package to extract the two different datasets.
#'
#' The data also returns a column called `meets_wmo` this reflects whether or
#' not the climate normals for this station met the WMO standards for
#' temperature and precipitation (i.e. both have code >= A). Each measurement
#' column has a corresponding `_code` column which reflects the data quality
#' of that measurement (see the
#' [1991-2020](https://collaboration.cmc.ec.gc.ca/cmc/climate/Normals/Canadian_Climate_Normals_1991_2020_Calculation_Information.pdf),
#' [1981-2010](https://collaboration.cmc.ec.gc.ca/cmc/climate/Normals/Canadian_Climate_Normals_1981_2010_Calculation_Information.pdf), or
#' [1971-2000](https://collaboration.cmc.ec.gc.ca/cmc/climate/Normals/Canadian_Climate_Normals_1971_2000_Calculation_Information.pdf)
#' for more details) ECCC calculation documents.
#'
#' Climate normals are downloaded from the url stored in option
#' `weathercan.urls.normals`. To change this location use:
#' `options(weathercan.urls.normals = "your_new_url")`.
#'
#' @return tibble with nested normals and first/last frost data
#'
#' @examplesIf check_eccc()
#'
#' # Find the climate_id
#' stations_search("Brandon A", normals_years = "current")
#'
#' # Download climate normals 1981-2010
#' n <- normals_dl(climate_ids = "5010480")
#' n
#'
#' # Pull out last frost data *with* station information
#' library(tidyr)
#' f <- unnest(n, frost)
#' f
#'
#' # Pull out normals *with* station information
#' nm <- unnest(n, normals)
#' nm
#'
#' # Download climate normals 1971-2000
#' n <- normals_dl(climate_ids = "5010480", normals_years = "1971-2000")
#' n
#'
#' # Note that some do not have last frost dates
#' n$frost
#'
#' # Download multiple stations for 1981-2010,
#' n <- normals_dl(climate_ids = c("301C3D4", "301FFNJ", "301N49A"))
#' unnest(n, frost)
#'
#'
#' # Note, putting both normals and frost data into the same data set can be done but makes for
#' # a very unweildly dataset (there is lots of repetition)
#' nm <- unnest(n, normals) |>
#' unnest(frost)
#' @export
normals_dl <- function(climate_ids, normals_years = "current",
format = TRUE, stn = NULL,
verbose = FALSE, quiet = FALSE) {
if(!is.null(stn)){
stop("`stn` is defunct, to use an updated stations data frame ",
"use `stations_dl()` to update the internal data, and ",
"`stations_meta()` to check when it was last updated", call. = FALSE)
}
stn <- stations()
check_ids(climate_ids, stn, type = "climate_id")
normals_years <- check_normals(normals_years)
yrs <- paste0("normals_", stringr::str_replace(normals_years, "-", "_"))
n <- dplyr::filter(stn, .data$climate_id %in% climate_ids) %>%
dplyr::select("prov", "station_name", "station_id", "climate_id",
"normals" = dplyr::matches(yrs)) %>%
dplyr::distinct() %>%
dplyr::mutate(climate_id = as.character(.data$climate_id))
# if(nrow(n) == 0) stop("No stations matched these climate ids", call. = FALSE)
if(all(n$normals == FALSE)) {
stop("No stations had climate normals available", call. = FALSE)
} else if(any(n$normals == FALSE)) {
message("Not all stations have climate normals available (climate ids: ",
paste0(n$climate_id[!n$normals], collapse = ", "), ")")
n <- dplyr::filter(n, .data$normals == TRUE) %>%
dplyr::select(-"normals")
}
# Download data
n <- n %>%
dplyr::mutate(
html = purrr::pmap(list(.data$prov, .data$station_id, .data$climate_id),
~normals_html(..1, ..2, ..3, normals_years)),
normals = purrr::map(.data$html, normals_raw),
meets_wmo = purrr::map_lgl(.data$normals, meets_wmo),
normals = purrr::map(.data$normals, normals_extract),
frost = purrr::map(.data$normals, frost_find),
normals = purrr::map2(.data$normals, .data$climate_id,
~ data_extract(.x, climate_id = .y)),
frost = purrr::map2(.data$frost, .data$climate_id,
~ frost_extract(.x, climate_id = .y)),
n_data = purrr::map_dbl(.data$normals, nrow),
n_frost = purrr::map_dbl(.data$frost, nrow))
if(any(no_data <- n$n_data + n$n_frost == 0)) {
message("All climate normals missing for some stations (climate_ids: ",
paste0(n$climate_id[no_data], collapse = ", "), ")")
}
# Format dates etc.
n <- dplyr::mutate(n,
normals_years = !!normals_years,
normals = purrr::map2(.data$normals,
.data$climate_id,
data_format),
frost = purrr::map2(.data$frost,
.data$climate_id,
frost_format))
dplyr::select(n, "prov", "station_name", "climate_id", "normals_years",
"meets_wmo", "normals", "frost")
}
normals_html <- function(prov, station_id, climate_id, normals_years) {
yrs <- stringr::str_extract(normals_years, "^[0-9]{4}")
q <- list(format = "csv", lang = "e", prov = tolower(prov), yr = yrs,
stnID = station_id, climateID = climate_id,
submit = "Download Data")
get_check(url = getOption("weathercan.urls.normals"), query = q,
task = "access climate normals")
}
normals_raw <- function(html, nrows = -1) {
# Extract file
html %>%
httr::content(as = "text", encoding = "latin1") %>%
stringr::str_split(pattern = "\n") %>%
unlist() %>%
# Get rid of all special symbols
stringr::str_remove_all("[^\001-\177]")
}
normals_extract <- function(n, return = "data") {
wmo <- find_line(n, cols = "meets WMO standards")
skip <- find_line(n, cols = c("Jan", "Feb", "Mar"))
if(return == "data") {
if(length(wmo) > 0) {
n <- n[c(wmo, skip:length(n))]
} else {
n <- n[skip:length(n)]
}
# Remove WMO if exists
if(stringr::str_detect(n[1], "WMO standards")) n <- n[-1]
# Remove empty strings if they exist
n <- stringr::str_remove_all(n, "[,]{2,}$")
}
n
}
data_extract <- function(n, climate_id) {
# Remove frost dates
n <- frost_find(n, type = "remove")
readr::local_edition(1)
# Read normals (expect warnings due to header rows, etc.)
suppressWarnings(n <- readr::read_csv(I(n), col_types = readr::cols()))
if(nrow(n) == 0) return(dplyr::tibble())
# Line up names to deal with duplicate variable names
names(n)[1] <- "variable"
n <- dplyr::mutate(n, variable = tolower(.data$variable))
# Mark title variables align variable names accordingly
nn <- dplyr::filter(n_names,
stringr::str_detect(.data$new_var, "title"),
.data$variable %in% n$variable)
# Remove missing groups
nn <- dplyr::filter(n_names, .data$group %in% nn$group)
# Detect missing measurements
missing_data <- dplyr::filter(nn, .data$type == "unique") %>%
dplyr::anti_join(dplyr::select(n, "variable"), by = "variable")
nn <- dplyr::filter(nn, !.data$new_var %in% missing_data$new_var)
# Remove leftover missing subgroups
nn <- dplyr::group_by(nn, .data$subgroup) %>%
dplyr::filter(!all(.data$type == "sub")) %>%
dplyr::ungroup()
# Make subgroups unique
n <- dplyr::left_join(n,
dplyr::filter(nn, .data$type != "sub") %>%
dplyr::select("variable", "subgroup"),
by = "variable")
for(i in seq_len(nrow(n))) {
if(is.na(n[["subgroup"]][i])) n[["subgroup"]][i] <- n[["subgroup"]][i-1]
}
n <- dplyr::mutate(n, variable_sub =
paste0(.data$variable, "_", .data$subgroup))
# Detect extra measurements not expected
missing_names <- dplyr::anti_join(dplyr::select(n, "variable_sub"),
nn, by = "variable_sub")
if(nrow(missing_names) > 0) {
stop("Not all variables for climate station ", climate_id,
" were identified.\nPlease report this here: ",
"https://github.com/ropensci/weathercan/issues", call. = FALSE)
} else if (nrow(nn) != nrow(n)) {
stop("Variables for climate station ", climate_id,
" were misidentified. Please report this here: ",
"https://github.com/ropensci/weathercan/issues", call. = FALSE)
}
# Join new, unique measurement names by sub labels
n_nice <- dplyr::left_join(n, dplyr::select(nn, "new_var", "variable_sub"),
by = "variable_sub")
# Check for problems
if(!all(nn$variable[nn$new_var %in% n_nice$new_var] %in% n$variable)) {
stop("Variable names did not align correctly during formating, ",
"consider using 'format = FALSE' and/or reporting this error.",
call. = FALSE)
}
# Remove titles
nn <- dplyr::filter(nn, !stringr::str_detect(.data$new_var, "title"))
n_nice <- dplyr::filter(n_nice, !stringr::str_detect(.data$new_var, "title"))
# Get codes
codes <- dplyr::select(n_nice, "code" = "Code", "new_var") %>%
dplyr::mutate(new_var = paste0(.data$new_var, "_code")) %>%
tidyr::spread(key = "new_var", value = "code")
# Spread variables
n_nice <- n_nice %>%
dplyr::select(-"Code", -"variable", -"subgroup", -"variable_sub") %>%
tidyr::gather(key = "period", value = "measure", -"new_var") %>%
tidyr::spread(key = "new_var", value = "measure") %>%
# Add Codes
cbind(codes)
# Column order
o <- c(rbind(nn$new_var, paste0(nn$new_var, "_code")))
n_nice <- dplyr::select(n_nice, "period", dplyr::all_of(o))
# Row order
o <- names(n)[!names(n) %in% c("variable", "Code", "subgroup", "variable_sub")]
n_nice %>%
dplyr::mutate(period = factor(.data$period, levels = o)) %>%
dplyr::arrange(.data$period) %>%
dplyr::as_tibble()
}
data_format <- function(n, climate_id) {
fmts <- dplyr::filter(n_formats, .data$new_var %in% names(n))
dates <- dplyr::filter(fmts, .data$format == "date") %>%
dplyr::pull("new_var")
nums <- dplyr::filter(fmts, .data$format == "numeric") %>%
dplyr::pull("new_var")
chars <- dplyr::filter(fmts, .data$format == "character") %>%
dplyr::pull("new_var")
# Prepare dates (if missing, NA)
n_fmt <- n %>%
dplyr::mutate(
dplyr::across(
.cols = dplyr::all_of(dates),
.fns = ~ dplyr::if_else(
condition = . == "",
true = as.character(NA),
false = paste0(., "/", as.numeric(.data$period))
)
)
) %>%
dplyr::mutate(
dplyr::across(
.cols = dplyr::all_of(dates),
.fns = ~ dplyr::if_else(
condition = .data$period == "Year",
true = as.character(NA),
false = .
)
)
)
# In case of warnings
tryCatch(
{
n_fmt <- dplyr::mutate(
.data = n_fmt,
dplyr::across(
.cols = dplyr::all_of(dates),
.fns = ~lubridate::ydm(.)
)
)
},
warning = function(w) stop(climate_id, " has a formating issue with dates", call. = FALSE)
)
tryCatch(
{
n_fmt <- dplyr::mutate(
.data = n_fmt,
dplyr::across(
.cols = dplyr::all_of(nums),
.fns = as.numeric
)
)
},
warning = function(w) stop(climate_id, " has a formating issue with numbers", call. = FALSE)
)
tryCatch(
{
n_fmt <- dplyr::mutate(
.data = n_fmt,
dplyr::across(
.cols = dplyr::all_of(chars),
.fns = ~ dplyr::if_else(
condition = . == "",
true = as.character(NA),
false = as.character(.)
)
)
)
},
warning = function(w) stop(climate_id, " has a formating issue with characters", call. = FALSE))
n_fmt
}
frost_extract <- function(f, climate_id) {
if(all(f == "")) return(dplyr::tibble())
frost_free <- stringr::str_which(f, f_names$match[f_names$group == 1][1])[1]
frost_probs <- stringr::str_which(f, f_names$match[f_names$group == 2][1])[1]
# Frost free days overall
if(any(!is.na(frost_free)) && length(frost_free) > 0) {
if(length(frost_probs) == 0) last <- length(f) else last <- frost_probs - 1
readr::local_edition(1)
f1 <- readr::read_csv(I(f[frost_free:last]),
col_names = c("variable", "value", "frost_code"),
col_types = readr::cols(), progress = FALSE) |>
tidyr::spread(key = "variable", value = "value")
nms <- purrr::map(stats::setNames(f_names$match, f_names$new_var),
\(x) stringr::str_subset(names(f1), x)) |>
unlist()
f1 <- dplyr::rename(f1, !!nms) %>%
dplyr::mutate_at(.vars = dplyr::vars(dplyr::contains("date")),
~lubridate::yday(lubridate::as_date(paste0("1999", .)))) |>
dplyr::mutate(length_frost_free =
stringr::str_extract(.data$length_frost_free, "[0-9]*"),
length_frost_free = as.numeric(.data$length_frost_free))
} else f1 <- na_tibble(f_names$new_var[f_names$group == 1])
# Frost free probabilities
if(any(!is.na(frost_probs)) && length(frost_probs) > 0) {
readr::local_edition(1)
f2 <- readr::read_csv(I(f[frost_probs:length(f)]),
col_names = FALSE, col_types = readr::cols(),
progress = FALSE) |>
dplyr::select(dplyr::where(\(x) !all(is.na(x)))) |>
dplyr::rename_with(
.fn = \(x) "prob",
.cols = dplyr::where(\(x) any(stringr::str_detect(x, "(P|p)robability")))) |>
dplyr::rename_with(
.fn = \(x) "value",
.cols = dplyr::where(\(x) {
any(stringr::str_detect(x, paste0("(", paste0(month.abb, collapse = ")|("), ")")))
})) |>
dplyr::mutate(measure = stringr::str_remove(.data$prob, "\\(\\d{2}%\\)"),
prob = stringr::str_extract(.data$prob, "\\d{2}%")) |>
tidyr::pivot_wider(names_from = "measure", values_from = "value")
nms <- purrr::map(stats::setNames(f_names$match, f_names$new_var),
\(x) stringr::str_subset(names(f2), x)) |>
unlist()
f2 <- dplyr::rename(f2, !!nms)
} else f2 <- na_tibble(f_names$new_var[f_names$group == 2])
if(nrow(f1) == 0 & nrow(f2) == 0) {
r <- cbind(f1, f2)
} else {
r <- dplyr::full_join(
dplyr::mutate(f1, climate_id = .env$climate_id),
dplyr::mutate(f2, climate_id = .env$climate_id),
by = "climate_id", relationship = "many-to-many") |>
dplyr::select(-"climate_id")
}
dplyr::as_tibble(r)
}
frost_find <- function(n, type = "extract") {
frost <- find_line(n, "station data \\(Frost-Free\\)")
# If no frost-free title, look for next measurement
if(length(frost) == 0) {
frost <- purrr::map(f_names$match, \(x) find_line(n, x)) |>
unlist() |>
min_na()
}
if(length(frost) == 1) {
if(type == "extract") r <- n[(frost):length(n)]
if(type == "remove") r <- n[1:(frost-1)]
} else if(length(frost) == 0) {
if(type == "extract") r <- ""
if(type == "remove") r <- n
} else{
stop("Problem identifying frost data in normals\nPlease report this here: ",
"https://github.com/ropensci/weathercan/issues", call. = FALSE)
}
r
}
frost_format <- function(f, climate_id) {
fmts <- dplyr::filter(f_formats, .data$new_var %in% names(f))
dates <- dplyr::filter(fmts, .data$format == "date") %>%
dplyr::pull("new_var")
nums <- dplyr::filter(fmts, .data$format == "numeric") %>%
dplyr::pull("new_var")
chars <- dplyr::filter(fmts, .data$format == "character") %>%
dplyr::pull("new_var")
f_fmt <- dplyr::mutate_at(f, .vars = dates,
~dplyr::if_else(. == "" | is.na(.), as.character(NA),
paste0(., " 1999")))
# In case of warnings
tryCatch({f_fmt <- dplyr::mutate_at(f_fmt, .vars = dates,
~lubridate::yday(lubridate::mdy(.)))},
warning = function(w) stop(climate_id,
" has a formating issue with dates",
call. = FALSE))
tryCatch({f_fmt <- dplyr::mutate_at(f_fmt, .vars = nums,
~as.numeric(as.character(.)))},
warning = function(w) stop(climate_id,
" has a formating issue with numbers",
call. = FALSE))
tryCatch({f_fmt <- dplyr::mutate_at(f_fmt, .vars = chars,
~dplyr::if_else(. == "",
as.character(NA),
as.character(.)))},
warning = function(w) stop(climate_id,
" has a formating issue with characters",
call. = FALSE))
f_fmt
}
meets_wmo <- function(n) {
start <- stringr::str_which(n, "STATION_NAME")
any(stringr::str_detect(n[start:(start+1)], "\\*"))
}