This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathget_historical_weather.R
414 lines (382 loc) · 14.2 KB
/
get_historical_weather.R
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
#' Obtain historical BOM data
#'
#' Retrieves daily observations for a given station.
#'
#' @note Methods \code{get_historical_weather} and \code{get_historical} are
#' equivalent. No preference is given to the use of either.
#'
#' @param stationid \acronym{BOM} station \sQuote{ID}. See Details.
#' @param latlon Length-2 numeric vector of Latitude/Longitude. See Details.
#' @param radius Numeric value, distance (km) from \var{latlon}, must be
#' numeric.
#' @param type Measurement type for \code{daily}, either daily \dQuote{rain},
#' \dQuote{min} (temp), \dQuote{max} (temp), or \dQuote{solar} (exposure).
#' Partial matching is performed. If not specified returns the first
#' matching type in the order listed. Ignored if \code{hourly} or
#' \code{minute} are selected for \code{tscale}.
#' @return A \code{bomrang_tbl} object (extension of a
#' \code{\link[base]{data.frame}}) of historical observations for the selected
#' station/product type, with some subset of the following columns:
#'
#' \tabular{rl}{
#' **product_code**:\tab BOM internal code.\cr
#' **station_number**:\tab BOM station ID.\cr
#' **year**:\tab Year of observation (YYYY).\cr
#' **month**:\tab Month of observation (1-12).\cr
#' **day**:\tab Day of observation (1-31).\cr
#' **min_temperature**:\tab Minimum daily recorded temperature (degrees C).\cr
#' **max_temperature**:\tab Maximum daily recorded temperature (degrees C).\cr
#' **accum_days_min**:\tab Accumulated number of days of minimum
#' temperature.\cr
#' **accum_days_max**:\tab Accumulated number of days of maximum
#' temperature.\cr
#' **rainfall**:\tab Daily recorded rainfall in mm.\cr
#' **period**:\tab Period over which rainfall was measured.\cr
#' **solar_exposure**:\tab Daily global solar exposure in MJ/m^2.\cr
#' **quality**:\tab Y, N, or missing. Data which have not yet completed the\cr
#' \tab routine quality control process are marked accordingly.
#' }
#'
#' The following attributes are set on the data, and these are used to
#' generate the header:
#'
#' \tabular{rl}{
#' **site**:\tab BOM station ID.\cr
#' **name**:\tab BOM station name.\cr
#' **lat**:\tab Latitude in decimal degrees.\cr
#' **lon**:\tab Longitude in decimal degrees.\cr
#' **start**:\tab Date observations start.\cr
#' **end**:\tab Date observations end.\cr
#' **years**:\tab Available number of years data.\cr
#' **percent**:\tab Percent complete.\cr
#' **AWS**:\tab Automated weather station?\cr
#' **type**:\tab Measurement types available for the station.\cr
#' }
#'
#' @section Caution:
#' Temperature data prior to 1910 should be used with extreme caution as many
#' stations prior to that date were exposed in non-standard shelters. Some
#' of which give readings which are several degrees warmer or cooler than
#' those measured according to post-1910 standards.
#'
#' Daily maximum temperatures usually occur in the afternoon and daily minimum
#' temperatures overnight or near dawn. Occasionally, however, the lowest
#' temperature in the 24 hours to prior to 9 AM can occur around 9 AM the
#' previous day if the night was particularly warm.
#'
#' Either \code{stationid} or \code{latlon} must be provided, but if both are,
#' then \code{stationid} will be used as it is more reliable.
#'
#' In some cases data is available back to the 1800s, so tens-of-thousands of
#' daily records will be returned. Other stations will be newer and will
#' return fewer observations.
#'
#' @section \CRANpkg{dplyr} Compatibility: The \code{bomrang_tbl} class is
#' compatible with \code{\link[dplyr:dplyr-package]{dplyr}} as long as the
#' \code{bomrang} package is on the search path. Common functions
#' (\code{\link[dplyr]{filter}}, \code{\link[dplyr]{select}},
#' \code{\link[dplyr]{arrange}}, \code{\link[dplyr]{mutate}},
#' \code{\link[dplyr:select]{rename}}, \code{\link[dplyr]{arrange}},
#' \code{\link[dplyr]{slice}}, \code{\link[dplyr]{group_by}}) are provided
#' which mask the \CRANpkg{dplyr} versions (but use those internally,
#' maintaining attributes).
#'
#' @seealso \link{get_current_weather}
#'
#' @author Jonathan Carroll, \email{rpkg@@jcarroll.com.au}
#'
#' @examples
#' \donttest{
#' get_historical_weather(stationid = "023000",
#' type = "max") ## ~48,000+ daily records
#' get_historical_weather(latlon = c(-35.2809, 149.1300),
#' type = "min") ## 3,500+ daily records
#' }
#' @rdname get_historical_weather
#' @export get_historical_weather
get_historical_weather <- get_historical <-
function(stationid = NULL,
latlon = NULL,
radius = NULL,
type = c("rain", "min", "max", "solar")) {
site <- ncc_obs_code <- NULL #nocov
if (is.null(stationid) & is.null(latlon))
stop("\nstationid or latlon must be provided\n.",
call. = FALSE)
if (!is.null(stationid) & !is.null(latlon)) {
warning("\nOnly one of `stationid` or `latlon` may be provided. ",
"\nUsing `stationid`\n.")
}
if (is.null(stationid)) {
if (!identical(length(latlon), 2L) || !is.numeric(latlon))
stop("\n`latlon` must be a 2-element numeric vector.",
call. = FALSE)
stationdetails <-
sweep_for_stations(latlon = latlon)[1, , drop = TRUE]
message("\nClosest station: ",
stationdetails$site,
" (",
stationdetails$name,
")\n")
stationid <- stationdetails$site
}
## ensure station is known
ncc_list <- .get_ncc()
if (suppressWarnings(all(
is.na(as.numeric(stationid)) |
as.numeric(stationid) %notin% ncc_list$site
)))
stop("\nStation not recognised.",
call. = FALSE)
type <- match.arg(type)
obscode <- switch(
type,
rain = 136,
min = 123,
max = 122,
solar = 193
)
ncc_list <-
dplyr::filter(ncc_list, c(site == as.numeric(stationid) &
ncc_obs_code == obscode))
if (obscode %notin% ncc_list$ncc_obs_code)
stop(call. = FALSE,
"\n`type` ",
type,
" is not available for `stationid` ",
stationid,
"\n")
zipurl <- .get_zip_url(stationid, obscode)
dat <- .get_zip_and_load(zipurl)
names(dat) <- c("product_code",
"station_number",
"year",
"month",
"day",
switch(
type,
min = c("min_temperature",
"accum_days_min",
"quality"),
max = c("max_temperature",
"accum_days_max",
"quality"),
rain = c("rainfall",
"period",
"quality"),
solar = c("solar_exposure")
))
dat[["station_number"]] <- sprintf("%06d",
as.integer(dat[["station_number"]]))
return(
structure(
dat,
class = union("bomrang_tbl", class(dat)),
station = stationid,
type = type,
origin = "historical",
location = ncc_list$name,
lat = ncc_list$lat,
lon = ncc_list$lon,
start = ncc_list$start,
end = ncc_list$end,
count = ncc_list$years,
units = "years",
ncc_list = ncc_list
)
)
}
# export `get_historical()` ----
#' @rdname get_historical_weather
#' @examples
#' \donttest{
#' get_historical(stationid = "023000",
#' type = "max") ## ~48,000+ daily records
#' get_historical(latlon = c(-35.2809, 149.1300),
#' type = "min") ## 3,500+ daily records
#' }
#' @export
get_historical <- get_historical_weather
#' Get latest historical station metadata
#'
#' Fetches BOM metadata for checking historical record availability. Also can be
#' used to return the metadata if user desires.
#'
#' @md
#'
#' @return A data frame of metadata for BOM historical records
#' @keywords internal
#' @author Jonathan Carroll, \email{rpkg@@jcarroll.com.au} and James Goldie
#' \email{me@@rensa.co}.
#' @noRd
.get_ncc <- function() {
# set a custom user-agent, restore original settings on exit
# required for #130 - BOM returns 403 for RStudio
op <- options()
on.exit(options(op))
options(HTTPUserAgent = paste0("{bomrang} R package (",
utils::packageVersion("bomrang"),
") https://github.com/ropensci/bomrang"))
# CRAN NOTE avoidance
site <- name <- lat <- lon <- start_month <- #nocov start
start_year <-
end_month <- end_year <- years <- percent <- AWS <-
start <- end <- ncc_obs_code <- site <- NULL #nocov end
base_url <- "http://www.bom.gov.au/climate/data/lists_by_element/"
rain <- paste0(base_url, "alphaAUS_136.txt")
tmax <- paste0(base_url, "alphaAUS_122.txt")
tmin <- paste0(base_url, "alphaAUS_123.txt")
solar <- paste0(base_url, "alphaAUS_193.txt")
weather <- c(rain, tmax, tmin, solar)
names(weather) <- c("rain", "tmax", "tmin", "solar")
ncc_codes <- vector(mode = "list", length = length(weather))
names(ncc_codes) <- names(weather)
for (i in seq_along(weather)) {
ncc_obs_code <- substr(weather[i],
nchar(weather[i]) - 6,
nchar(weather[i]) - 4)
# read the station list in as a vector first so that we can
# detect and remove the header and footer...
ncc <- readLines(weather[i])
header_start <- grep("^\\-+$", ncc) + 1L
footer_start <- grep("^[0-9]+ stations", ncc) - 1L
if (length(header_start > 0) && length(footer_start > 0)) {
# ... then process it as a data frame
ncc <-
readr::read_table(
ncc[header_start:footer_start],
skip = 0,
col_names = c(
"site",
"name",
"lat",
"lon",
"start_month",
"start_year",
"end_month",
"end_year",
"years",
"percent",
"AWS"
),
col_types = c(
site = readr::col_integer(),
name = readr::col_character(),
lat = readr::col_double(),
lon = readr::col_double(),
start_month = readr::col_character(),
start_year = readr::col_character(),
end_month = readr::col_character(),
end_year = readr::col_character(),
years = readr::col_double(),
percent = readr::col_integer(),
AWS = readr::col_character()
),
na = ""
)
# unite month and year, convert to a date and add ncc_obs_code
ncc <-
ncc %>%
tidyr::unite(start, start_month, start_year, sep = "-") %>%
tidyr::unite(end, end_month, end_year, sep = "-") %>%
dplyr::mutate(start = lubridate::dmy(paste0("01-", start))) %>%
dplyr::mutate(end = lubridate::dmy(paste0("01-", end))) %>%
dplyr::mutate(ncc_obs_code = ncc_obs_code)
# finally, store in a list
ncc_codes[[i]] <- ncc
} else {
warning(
call. = FALSE,
"The list of available stations for `type = ",
names(weather)[i],
"` is currently empty.\n",
"This is likely a temporary error in the Bureau of Meteorology\'s\n",
"database and may cause requests for ",
names(weather)[i],
" station data to fail."
)
}
}
dplyr::bind_rows(ncc_codes)
}
#' Identify URL of historical observations resources
#'
#' BOM data is available via URL endpoints but the arguments are not (well)
#' documented. This function first obtains an auxilliary data file for the given
#' station/measurement type which contains the remaining value `p_c`. It then
#' constructs the approriate resource URL.
#'
#' @md
#' @param site site ID.
#' @param code measurement type. See internals of [get_historical_weather].
#' @importFrom httr GET content
#'
#' @return URL of the historical observation resource
#' @keywords internal
#' @author Jonathan Carroll, \email{rpkg@@jcarroll.com.au}
#' @noRd
.get_zip_url <- function(site, code = 122) {
base_url <- "http://www.bom.gov.au/jsp/ncc/cdio/weatherData/"
url1 <-
paste0(
base_url,
"av?p_stn_num=",
site,
"&p_display_type=availableYears&p_nccObsCode=",
code
)
raw <- httr::content(httr::GET(url1), "text")
if (grepl("Error code: 1001", raw))
stop("Error in retrieving resource identifiers.")
pc <- sub("^.*:", "", raw)
url2 <-
paste0(
base_url,
"av?p_display_type=dailyZippedDataFile&p_stn_num=",
site,
"&p_c=",
pc,
"&p_nccObsCode=",
code
)
url2
}
#' Download a BOM Data .zip file and load into session
#'
#' @param url URL of zip file to be downloaded/extracted/loaded.
#'
#' @return data loaded from the zip file
#' @keywords internal
#' @author Jonathan Carroll, \email{rpkg@@jcarroll.com.au}
#' @noRd
.get_zip_and_load <- function(url) {
USERAGENT <- paste0("{bomrang} R package (",
utils::packageVersion("bomrang"),
") https://github.com/ropensci/bomrang")
# set a custom user-agent, restore original settings on exit
# required for #130 - BOM returns 403 for RStudio
op <- options()
on.exit(options(op))
options(HTTPUserAgent = USERAGENT)
# BOM's FTP server can timeout too quickly
# Also, BOM's http server sometimes sends a http response of 200, "all good",
# but then will not actually serve the requested file, so we want to set a max
# time limit for the complete process to complete as well.
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
FTP_RESPONSE_TIMEOUT = 60L,
CONNECTTIMEOUT = 60L,
TIMEOUT = 120L,
USERAGENT = USERAGENT
)
tmp <- tempfile(fileext = ".zip")
curl::curl_download(url, tmp, mode = "wb", quiet = TRUE, handle = h)
zipped <- utils::unzip(tmp, exdir = dirname(tmp))
unlink(tmp)
datfile <- grep("Data.csv", zipped, value = TRUE)
message("Data saved as ", datfile)
dat <-
utils::read.csv(datfile, header = TRUE, stringsAsFactors = TRUE)
dat
}