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 pathinternal_functions.R
372 lines (334 loc) · 9.39 KB
/
internal_functions.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
"%||%" <- function(a, b) if (!is.null(a)) a else b
`%notin%` <- function(x, table) {
match(x, table, nomatch = 0L) == 0L
}
.force_double <- function(v) {
suppressWarnings(as.double(v))
}
#' Get response from a BOM URL
#'
#' Gets response from a BOM URL, checks the server for response first, then
#' tries to fetch the file or returns an informative message.
#'
#' @param remote_file file resource being requested from BOM
#'
#' @details Original execution came from
#' <https://community.rstudio.com/t/internet-resources-should-fail-gracefully/49199/12>
#'
#' @author Adam H. Sparks, adamhsparks@@gmail.com
#' @noRd
#'
.get_url <- function(remote_file) {
# define custom useragent and handle for communicating with BOM servers
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
)
try_GET <- function(x, ...) {
tryCatch({
response = curl::curl_fetch_memory(url = x, handle = h)
},
error = function(e)
conditionMessage(e),
warning = function(w)
conditionMessage(w))
}
# a proper response will return a list class object
# otherwise a timeout will just be a character string
is_response <- function(x) {
inherits(x, "list")
}
# First check Internet connection
if (!curl::has_internet()) {
stop(call. = FALSE,
"No Internet connection.")
}
resp <- try_GET(x = remote_file)
# check for possible timeout message and stop if that's the case
if (!is_response(resp)) {
stop(call. = FALSE,
resp) # return char string value server provides
}
# Then stop if status indicates file not found
if (as.integer(resp$status_code) == 404 |
as.integer(resp$status_code) == 550) {
stop(
call. = FALSE,
"\nA file or station was matched. However, a corresponding file was not ",
"found at bom.gov.au.\n"
)
}
if (tools::file_ext(remote_file) == "xml") {
xml_out <- xml2::read_xml(rawToChar(resp$content))
return(xml_out)
}
if (tools::file_ext(remote_file) == "json") {
json_out <-
jsonlite::fromJSON(rawToChar(resp$content))
return(json_out)
}
if (grepl(pattern = "dailyZippedDataFile", x = remote_file)) {
csv_out <-
data.table::fread(input = remote_file,
header = TRUE,
stringsAsFactors = TRUE)
return(csv_out)
}
}
# Distance over a great circle. Reasonable approximation.
.haversine_distance <- function(lat1, lon1, lat2, lon2) {
# to radians
lat1 <- lat1 * 0.01745
lat2 <- lat2 * 0.01745
lon1 <- lon1 * 0.01745
lon2 <- lon2 * 0.01745
delta_lat <- abs(lat1 - lat2)
delta_lon <- abs(lon1 - lon2)
# radius of earth
12742 * asin(sqrt(`+`(
(sin(delta_lat / 2)) ^ 2,
cos(lat1) * cos(lat2) * (sin(delta_lon / 2)) ^ 2
)))
}
#' @noRd
# Check if user enables caching. If so use cache directory, else use tempdir()
.set_cache <- function(cache) {
if (isTRUE(cache)) {
if (!dir.exists(manage_cache$cache_path_get())) {
manage_cache$mkdir()
}
cache_dir <- manage_cache$cache_path_get()
} else {
cache_dir <- tempdir()
}
return(cache_dir)
}
#' @noRd
# Check states for précis and ag bulletin, use fuzzy matching
.check_states <- function(state) {
state <- toupper(state)
states <- c(
"ACT",
"NSW",
"NT",
"QLD",
"SA",
"TAS",
"VIC",
"WA",
"CANBERRA",
"NEW SOUTH WALES",
"NORTHERN TERRITORY",
"QUEENSLAND",
"SOUTH AUSTRALIA",
"TASMANIA",
"VICTORIA",
"WESTERN AUSTRALIA",
"AUSTRALIA",
"AU",
"AUS",
"OZ"
)
if (state %in% states) {
the_state <- state
return(the_state)
} else {
likely_states <- agrep(pattern = state,
x = states,
value = TRUE)
if (length(likely_states) == 1) {
the_state <- toupper(likely_states)
message(
paste0(
"\nUsing state = ",
likely_states,
".\n",
"If this is not what you intended, please check your entry."
)
)
return(the_state)
} else if (length(likely_states) == 0) {
stop(
"\nA state or territory matching what you entered was not found. ",
"Please check and try again.\n"
)
}
}
if (length(likely_states) > 1) {
message(
"Multiple states match state.",
"'\ndid you mean:\n\tstate = '",
paste(likely_states[1],
"or",
likely_states[2],
"or",
likely_states[3]),
"'?"
)
}
}
#' convert_state
#'
#' Convert state to standard abbreviation
#' @noRd
.convert_state <- function(state) {
state <- gsub(" ", "", state)
state <-
substring(gsub("[[:punct:]]", "", tolower(state)), 1, 2)
state_code <- c(
"AUS",
"AUS",
"AUS",
"NSW",
"NSW",
"VIC",
"VIC",
"QLD",
"QLD",
"QLD",
"WA",
"WA",
"WA",
"SA",
"SA",
"SA",
"TAS",
"TAS",
"ACT",
"NT",
"NT"
)
state_names <- c(
"au",
"oz",
"as",
"ne",
"ns",
"vi",
"v",
"ql",
"qe",
"q",
"wa",
"we",
"w",
"s",
"sa",
"so",
"ta",
"t",
"ac",
"no",
"nt"
)
state <- state_code[pmatch(state, state_names)]
if (any(is.na(state)))
stop("Unable to determine state")
return(state)
}
#' splits time cols and removes extra chars for forecast XML objects
#'
#' @param x an object containing a BOM forecast object parsed from XML
#'
#' @return cleaned data.table cols of date and time
#' @keywords internal
#' @author Adam H. Sparks, \email{adamhsparks@@gmail.com}
#' @importFrom data.table ":="
#' @noRd
.split_time_cols <- function(x) {
.SD <- start_time_local <- end_time_local <- NULL
x[, c("start_time_local",
"UTC_offset_drop") := data.table::tstrsplit(start_time_local,
"+",
fixed = TRUE)]
x[, c("end_time_local",
"utc_offset") := data.table::tstrsplit(end_time_local,
"+",
fixed = TRUE)]
x[, "UTC_offset_drop" := NULL]
# remove the "T" from time cols
x[, c("start_time_local",
"end_time_local",
"start_time_utc",
"end_time_utc") := lapply(.SD, gsub, pattern = "T",
replacement = " "),
.SDcols = c("start_time_local",
"end_time_local",
"start_time_utc",
"end_time_utc")]
# remove the "Z" from UTC cols
x[, c("start_time_utc", "end_time_utc") := lapply(.SD, gsub, pattern = "Z",
replacement = ""),
.SDcols = c("start_time_utc", "end_time_utc")]
return(x)
}
#' Validate user entered filepath value or return BOM URL
#'
#' @param filepath User provided value for checking
#'
#' @noRd
.validate_filepath <- function(filepath) {
if (is.null(filepath)) {
location <- "ftp://ftp.bom.gov.au/anon/gen/fwo"
return(location)
} else {
location <- trimws(filepath)
if (!file.exists(location)) {
stop("\nDirectory does not exist: ", filepath,
call. = FALSE)
} else if (tolower(tools::file_ext(location)) == "xml") {
stop("\nYou have provided a file, not a directory containing a file.",
call. = FALSE)
}
return(location)
}
}
#' Create the base URL/file location of BOM files for all XML functions
#'
#' Takes the XML file name and creates the full file path or URL
#'
#' @param AUS_XML a vector of XML file names for BOM products
#' @param the_state user provided state argument for requested data
#'
#' @noRd
.create_bom_file <- function(AUS_XML, .the_state, .file_loc) {
if (.the_state != "AUS") {
xml_url <-
dplyr::case_when(
.the_state == "ACT" |
.the_state == "CANBERRA" ~ paste0(.file_loc, "/", AUS_XML[1]),
.the_state == "NSW" |
.the_state == "NEW SOUTH WALES" ~ paste0(.file_loc, "/", AUS_XML[1]),
.the_state == "NT" |
.the_state == "NORTHERN TERRITORY" ~ paste0(.file_loc,
"/", AUS_XML[2]),
.the_state == "QLD" |
.the_state == "QUEENSLAND" ~ paste0(.file_loc, "/", AUS_XML[3]),
.the_state == "SA" |
.the_state == "SOUTH AUSTRALIA" ~ paste0(.file_loc, "/", AUS_XML[4]),
.the_state == "TAS" |
.the_state == "TASMANIA" ~ paste0(.file_loc, "/", AUS_XML[5]),
.the_state == "VIC" |
.the_state == "VICTORIA" ~ paste0(.file_loc, "/", AUS_XML[6]),
.the_state == "WA" |
.the_state == "WESTERN AUSTRALIA" ~ paste0(.file_loc, "/", AUS_XML[7])
)
}
return(xml_url)
}