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_current_weather.R
315 lines (283 loc) · 10.5 KB
/
get_current_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
#' Get current weather observations of a BOM station
#'
#' @param station_name The name of the weather station. Fuzzy string matching
#' via \code{\link[base]{agrep}} is done.
#' @param strict (logical) If \code{TRUE}, \var{station_name} must match the
#' station name exactly, except that \var{station_name} need not be upper case.
#' Note this may be different to \code{full_name} in the response. See
#' \strong{Details}.
#' @param latlon A length-2 numeric vector giving the decimal degree
#' latitude and longitude (in that order), \emph{e.g.}, \code{latlon =
#' c(-34, 151)} for Sydney. When given instead of \code{station_name}, the
#' nearest station (in this package) is used, with a message indicating the
#' nearest such station. (See also \code{\link{sweep_for_stations}}.) Ignored if
#' used in combination with \var{station_name}, with a warning.
#' @param emit_latlon_msg Logical. If \code{TRUE} (the default), and
#' \code{latlon} is selected, a message is emitted before the table is returned
#' indicating which station was actually used (\emph{i.e.}, which station was
#' found to be nearest to the given coordinate).
#'
#' @details
#' Station names are not consistently named within the Bureau, so
#' the response may contain a different \code{full_name} to the one
#' matched, even if \var{strict = TRUE}. For example, \cr
#' \code{get_current_weather("CASTLEMAINE PRISON")[["full_name"]][1]} \cr
#' is \code{Castlemaine}, not \code{Castlemaine Prison}.
#'
#' Note that the column \code{local_date_time_full} is set to a
#' \code{POSIXct} object in the local time of the \strong{user}.
#' For more details see \dQuote{Appendix 1 - Output from get_current_weather()}
#' in the \CRANpkg{bomrang} vignette \cr
#' \code{vignette("bomrang", package = "bomrang")}\cr
#' for a complete list of fields and units.
#'
#' @return A \code{bomrang_tbl} object (extension of a
#' \code{\link[base]{data.frame}}) of requested BOM station's current and prior
#' 72hr data. For full details of fields and units returned, see Appendix 1
#' in the \CRANpkg{bomrang} vignette, use \cr
#' \code{vignette("bomrang", package = "bomrang")} to view.
#' @examples
#' \donttest{
#' # warning
#' Melbourne_weather <- get_current_weather("Melbourne")
#'
#' # no warning
#' Melbourne_weather <- get_current_weather("Melbourne (Olympic Park)")
#'
#' # Get weather by latitude and longitude:
#' get_current_weather(latlon = c(-34, 151))
#' }
#' @references
#' Weather data observations are retrieved from:
#' Australian Bureau of Meteorology (\acronym{BOM}) Weather Data Services,
#' Observations - individual stations:
#' \url{http://www.bom.gov.au/catalogue/data-feeds.shtml}
#'
#' Station location and other metadata are sourced from the Australian Bureau of
#' Meteorology (\acronym{BOM}) webpage, Bureau of Meteorology Site Numbers:
#' \url{http://www.bom.gov.au/climate/cdo/about/site-num.shtml}
#'
#' @seealso \link{get_historical_weather}
#'
#' @author Hugh Parsonage, \email{hugh.parsonage@@gmail.com}
#' @importFrom magrittr use_series
#' @importFrom magrittr %$%
#' @importFrom data.table :=
#' @importFrom data.table %chin%
#' @importFrom data.table setnames
#' @rdname get_current_weather
#' @export get_current_weather
get_current_weather <-
function(station_name,
strict = FALSE,
latlon = NULL,
emit_latlon_msg = TRUE) {
JSONurl_site_list <- name <- .SD <- NULL
# Load JSON URL list
load(system.file("extdata", "JSONurl_site_list.rda", # nocov start
package = "bomrang")) # nocov end
if (missing(station_name) && is.null(latlon)) {
stop("One of 'station_name' or 'latlon' must be provided.")
}
if (!missing(station_name)) {
if (!is.null(latlon)) {
latlon <- NULL
warning("Both station_name and latlon provided. Ignoring latlon.")
}
stopifnot(is.character(station_name),
length(station_name) == 1)
station_name <- toupper(station_name)
# If there's an exact match, use it; else, attempt partial match.
if (station_name %in% JSONurl_site_list[["name"]]) {
the_station_name <- station_name
} else {
likely_stations <-
# Present those with common prefixes first
c(
grep(
pattern = paste0("^", station_name),
x = JSONurl_site_list[["name"]],
ignore.case = TRUE,
value = TRUE
),
agrep(
pattern = station_name,
x = JSONurl_site_list[["name"]],
value = TRUE
)
) %>%
unique
if (length(likely_stations) == 0) {
stop("No station found.")
}
the_station_name <- likely_stations[1]
if (length(likely_stations) > 1) {
# Likely common use case
# (otherwise defaults to KURNELL RADAR,
# which does not provide observations)
if (toupper(station_name) == "SYDNEY" &&
"SYDNEY (OBSERVATORY HILL)" %in% likely_stations) {
likely_stations <- c(
"SYDNEY (OBSERVATORY HILL)",
setdiff(likely_stations,
"SYDNEY (OBSERVATORY HILL)")
)
the_station_name <- "SYDNEY (OBSERVATORY HILL)"
}
# If not strict, warn; otherwise, later code will error on its own.
if (!strict) {
warning(
"Multiple stations match station_name. ",
"Using\n\tstation_name = '",
the_station_name,
"'\nDid you mean any of the following?\n",
paste0(
"\tstation_name = '",
likely_stations[-1],
"'",
collapse = "\n"
)
)
}
}
if (strict) {
if (length(likely_stations) == 1) {
stop(
"strict = TRUE but station name not exactly matched.",
"\nDid you mean the following?\n\t",
"station_name = '",
the_station_name,
"'"
)
} else {
stop(
"strict = TRUE but station name not exactly matched.\n",
"Multiple stations match station_name. ",
"\n\nDid you mean any of the following?\n",
paste0("\tstation_name = '",
likely_stations,
"'",
collapse = "\n")
)
}
}
}
json_url <-
JSONurl_site_list[name == the_station_name][["url"]]
full_lat <-
JSONurl_site_list[name == the_station_name][["lat"]]
full_lon <-
JSONurl_site_list[name == the_station_name][["lon"]]
} else {
# We have established latlon is not NULL
if (length(latlon) != 2 || !is.numeric(latlon)) {
stop("latlon must be a length-2 numeric vector.")
}
Lat <- latlon[1]
Lon <- latlon[2]
# CRAN NOTE avoidance: names of JSONurl_site_list
lat <- lon <- NULL # nocov
station_nrst_latlon <-
JSONurl_site_list %>%
# Lat Lon are in JSON
.[which.min(.haversine_distance(Lat, Lon, lat, lon))]
if (emit_latlon_msg) {
distance <-
station_nrst_latlon %$%
.haversine_distance(Lat, Lon, lat, lon) %>%
signif(digits = 3)
on.exit(
message(
"Using station_name = '",
station_nrst_latlon$name,
"', at latitude = ",
station_nrst_latlon$lat,
", ",
"longitude = ",
station_nrst_latlon$lon,
" (",
distance,
" km away)."
)
)
}
json_url <- station_nrst_latlon[["url"]]
full_lat <- station_nrst_latlon[["lat"]]
full_lon <- station_nrst_latlon[["lon"]]
}
observations.json <- .get_url(remote_file = json_url)
if (is.null(observations.json)) {
return(invisible(NULL))
}
if ("observations" %notin% names(observations.json) ||
"data" %notin% names(observations.json$observations) ||
length(observations.json$observations$data) == 0) {
id_code <- gsub("http://www.bom.gov.au/fwo/", "", json_url)
id_code <- gsub(".json", "", id_code)
web_url <-
paste0("http://www.bom.gov.au/products/", id_code, ".shtml")
stop(
call. = FALSE,
"\nA station was matched.",
"\nA JSON file was found, however it was not in expected form. ",
"You may wish to check the BOM website for the station for further ",
"possible information on the station's status, ",
web_url
)
}
out <-
observations.json %>%
use_series("observations") %>%
use_series("data")
data.table::setDT(out)
# replaced rounded values from .json with full values from internal db
data.table::set(out, j = "lat", value = full_lat)
data.table::set(out, j = "lon", value = full_lon)
# BOM raw JSON uses `name`, which is ambiguous (see #27)
if ("name" %in% names(out)) {
setnames(out, "name", "full_name")
}
# CRAN NOTE avoidance
out[, "local_date_time_full" := lapply(.SD, function(x)
as.POSIXct(x,
origin = "1970-1-1",
format = "%Y%m%d%H%M%OS")),
.SDcols = "local_date_time_full"]
out[, "aifstime_utc" := lapply(.SD, function(x)
as.POSIXct(
x,
origin = "1970-1-1",
format = "%Y%m%d%H%M%OS",
tz = "GMT"
)),
.SDcols = "aifstime_utc"]
# Columns which are meant to be numeric
double_cols <-
c("lat",
"lon",
"apparent_t",
"cloud_base_m",
"cloud_oktas",
"rain_trace")
for (j in which(names(out) %chin% double_cols)) {
data.table::set(out, j = j, value = .force_double(out[[j]]))
}
station_meta <- subset(JSONurl_site_list, url %in% json_url)
return(
structure(
out,
class = union("bomrang_tbl", class(out)),
station = station_meta$site,
type = "All",
origin = "Current",
location = station_meta$name,
lat = station_meta$lat,
lon = station_meta$lon,
start = station_meta$start,
end = station_meta$end,
count = station_meta$end - station_meta$start,
units = "years",
ncc_list = station_meta
)
)
}