-
Notifications
You must be signed in to change notification settings - Fork 5
/
get_dpird_summaries.R
809 lines (743 loc) · 23.4 KB
/
get_dpird_summaries.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#' Get DPIRD Weather Data in Summarised Formats
#'
#' Fetch nicely formatted individual station weather summaries from the
#' \acronym{DPIRD} Weather 2.0 \acronym{API}.
#'
#' # Start Dates
#'
#' The earliest available data start from August of 2000 for Vasse, \dQuote{VA}.
#'
#' # Column Name Details
#'
#' Column names are converted from the default returns of the \acronym{API} to
#' be snake_case formatted and where appropriate, the names of the values
#' that are analogous between \acronym{SILO} and \acronym{DPIRD} data are
#' named using the same name for ease of interoperability, _e.g._, using
#' `rbind()` to create a `data.table` that contains data from both APIs.
#' However, use with caution and don't mix datasets of different time-steps,
#' _i.e._, this function gets many summary values not just \dQuote{daily}
#' time-step data. The functions that access the \acronym{SILO}
#' \acronym{API} only provide access to daily data, so don't mix (sub)hourly,
#' monthly or yearly data from \acronym{DPIRD} with \acronym{SILO}.
#'
#' @param station_code A `character` string or `factor` from
#' [get_stations_metadata()] of the \acronym{BOM} station code for the station
#' of interest.
#' @param start_date A `character` string or `Date` object representing the
#' beginning of the range to query in the format \dQuote{yyyy-mm-dd}
#' (ISO8601). Data returned is inclusive of this date.
#' @param end_date A `character` string or `Date` object representing the end of
#' the range query in the format \dQuote{yyyy-mm-dd} (ISO8601). Data
#' returned is inclusive of this date. Defaults to the current system date.
#' @param interval A `character` string that indicates the time interval to
#` summarise over. Default is `daily`; others are `15min`, `30min`, `hourly`,
#' `monthly` or `yearly`. For intervals shorter than 1 day, the time period
#' covered will be midnight to midnight, with the end_date time interval being
#' before midnight - hour/minute values are for the end of the time period.
#' Data for shorter intervals (`15min`, `30min`) are available from January of
#' the previous year.
#' @param values A `character` string with the type of summarised weather
#' to return. See **Available Values** for a full list of valid values.
#' Defaults to `all` with all available values being returned.
#' @param api_key A `character` string containing your \acronym{API} key from
#' \acronym{DPIRD}, <https://www.agric.wa.gov.au/web-apis>, for the
#' \acronym{DPIRD} Weather 2.0 \acronym{API}.
#'
#' @section Available Values:
#'
#' * all (which will return all of the following values),
#' * airTemperature,
#' * airTemperatureAvg,
#' * airTemperatureMax,
#' * airTemperatureMaxTime,
#' * airTemperatureMin,
#' * airTemperatureMinTime,
#' * apparentAirTemperature,
#' * apparentAirTemperatureAvg,
#' * apparentAirTemperatureMax,
#' * apparentAirTemperatureMaxTime,
#' * apparentAirTemperatureMin,
#' * apparentAirTemperatureMinTime,
#' * barometricPressure,
#' * barometricPressureAvg,
#' * barometricPressureMax,
#' * barometricPressureMaxTime,
#' * barometricPressureMin,
#' * barometricPressureMinTime,
#' * battery,
#' * batteryMinVoltage,
#' * batteryMinVoltageDateTime,
#' * chillHours,
#' * deltaT,
#' * deltaTAvg,
#' * deltaTMax,
#' * deltaTMaxTime,
#' * deltaTMin,
#' * deltaTMinTime,
#' * dewPoint,
#' * dewPointAvg,
#' * dewPointMax,
#' * dewPointMaxTime,
#' * dewPointMin,
#' * dewPointMinTime,
#' * erosionCondition,
#' * erosionConditionMinutes,
#' * erosionConditionStartTime,
#' * errors,
#' * evapotranspiration,
#' * evapotranspirationShortCrop,
#' * evapotranspirationTallCrop,
#' * frostCondition,
#' * frostConditionMinutes,
#' * frostConditionStartTime,
#' * heatCondition,
#' * heatConditionMinutes,
#' * heatConditionStartTime,
#' * observations,
#' * observationsCount,
#' * observationsPercentage,
#' * panEvaporation,
#' * panEvaporation12AM,
#' * rainfall,
#' * relativeHumidity,
#' * relativeHumidityAvg,
#' * relativeHumidityMax,
#' * relativeHumidityMaxTime,
#' * relativeHumidityMin,
#' * relativeHumidityMinTime,
#' * richardsonUnits,
#' * soilTemperature,
#' * soilTemperatureAvg,
#' * soilTemperatureMax,
#' * soilTemperatureMaxTime,
#' * soilTemperatureMin,
#' * soilTemperatureMinTime,
#' * solarExposure,
#' * wetBulb,
#' * wetBulbAvg,
#' * wetBulbMax,
#' * wetBulbMaxTime,
#' * wetBulbMin,
#' * wetBulbMinTime,
#' * wind,
#' * windAvgSpeed, and
#' * windMaxSpeed
#'
#' @return a [data.table] with `station_code` and the date interval
#' queried together with the requested weather variables in alphabetical
#' order. The first ten columns will always be:
#'
#' * `station_code`,
#' * `station_name`,
#' * `longitude`,
#' * `latitude`,
#' * `year`,
#' * `month`,
#' * `day`,
#' * `hour`,
#' * `minute`, and if `month` or finer is present,
#' * `date` (a combination of year, month, day, hour, minute as appropriate).
#'
#' @note Please note this function converts date-time columns from Coordinated
#' Universal Time \sQuote{UTC} to Australian Western Standard Time
#' \sQuote{AWST}.
#'
#' @family DPIRD
#' @family data fetching
#'
#' @author Adam H. Sparks, \email{adamhsparks@@gmail.com}, and Rodrigo
#' Pires, \email{rodrigo.pires@@dpird.wa.gov.au}
#'
#' @examples
#' \dontrun{
#' # Note that you need to supply your own API key
#' # Use default for end date (current system date) to get rainfall
#'
#' wd <- get_dpird_summaries(
#' station_code = "CL001",
#' start_date = "20171028",
#' api_key = "your_api_key",
#' interval = "yearly",
#' values = "rainfall"
#' )
#'
#' # Only for wind and erosion conditions for daily time interval
#'
#' wd <- get_dpird_summaries(
#' station_code = "BI",
#' start_date = "20220501",
#' end_date = "20220502",
#' api_key = "your_api_key",
#' interval = "daily",
#' values = c(
#' "wind",
#' "erosionCondition",
#' "erosionConditionMinutes",
#' "erosionConditionStartTime"
#' )
#' )
#' }
#' @autoglobal
#' @export
get_dpird_summaries <- function(station_code,
start_date,
end_date = Sys.Date(),
interval = c("daily", "15min", "30min", "hourly", "monthly", "yearly"),
values = "all",
api_key) {
# this section is necessary to double check availability dates of DPIRD
# stations to give users a better experience. It slows down the first query
# but avoids confusion when no values are returned for a station that exists
# but the date requested pre-dates the station data
if (Sys.info()['sysname'] == "Windows") {
metadata_file <- file.path(tempdir(), "dpird_metadata.Rda", fsep = "\\")
if (!file.exists(metadata_file)) {
saveRDS(
get_stations_metadata(
which_api = "dpird",
api_key = api_key,
include_closed = TRUE
),
file = metadata_file,
compress = FALSE
)
}
} else {
metadata_file <- file.path(tempdir(), "dpird_metadata.Rda")
if (!file.exists(metadata_file)) {
saveRDS(
get_stations_metadata(
which_api = "dpird",
api_key = api_key,
include_closed = TRUE
),
file = metadata_file,
compress = FALSE
)
}
}
# simplify using the metadata to fetch weather data by converting factors to
# numeric values
if (inherits(x = station_code, what = "factor")) {
station_code <- as.character(station_code)
}
if (missing(station_code) | !is.character(station_code)) {
stop(call. = FALSE, "Please supply a valid `station_code`.")
}
if (missing(start_date))
stop(call. = FALSE, "Please supply a valid start date as `start_date`.")
if (any(values == "all")) {
values <-
c("stationCode",
"stationName",
"period",
weatherOz::dpird_summary_values)
} else {
if (any(values %notin% weatherOz::dpird_summary_values)) {
stop(call. = FALSE, "You have specified invalid weather values.")
}
values <-
c("stationCode", "stationName", "period", values)
}
# validate user provided dates
start_date <- .check_date(start_date)
end_date <- .check_date(end_date)
.check_date_order(start_date, end_date)
.check_earliest_available_dpird(station_code, start_date, metadata_file)
# if interval is not set, default to "daily", else check input to be sure
approved_intervals <- c("daily", "15min", "30min", "hourly", "monthly", "yearly")
if (identical(interval, approved_intervals)) {
interval <- "daily"
}
likely_interval <- agrep(pattern = interval, x = approved_intervals)
# Match time interval query to user requests
checked_interval <-
try(match.arg(approved_intervals[likely_interval],
approved_intervals,
several.ok = FALSE),
silent = TRUE)
# Error if summary interval is not available. API only allows for daily,
# 15 min, 30 min, hourly, monthly or yearly
if (methods::is(checked_interval, "try-error")) {
stop(call. = FALSE, "\"", interval, "\" is not a supported time interval")
}
request_interval <- lubridate::interval(start_date, end_date, tzone = "Australia/Perth")
# Stop if query is for 15 and 30 min intervals and date is more than one
# year in the past
this_year <- lubridate::year(lubridate::today())
if (checked_interval %in% c("15min", "30min") &
lubridate::year(start_date) <
this_year - 1 |
checked_interval %in% c("15min", "30min") &
lubridate::year(end_date) <
this_year - 1) {
stop(
call. = FALSE,
"Start date is too early. Data in 15 and 30 min intervals are only ",
"available from the the 1st day of ",
this_year - 1,
"."
)
}
# determine how many records are being requested. Default here is 'daily' as
# with default user arguments
total_records_req <- data.table::fcase(
checked_interval == "yearly",
floor(lubridate::time_length(request_interval, unit = "year") + 1),
checked_interval == "monthly",
floor(lubridate::time_length(request_interval, unit = "month") + 1),
checked_interval == "hourly",
floor(lubridate::time_length(request_interval, unit = "hour") + 1),
checked_interval == "30min",
floor((
lubridate::time_length(request_interval, unit = "hour")
) + 1) * 2,
checked_interval == "15min",
floor((
lubridate::time_length(request_interval, unit = "hour")
) + 1) * 4,
default = floor(lubridate::time_length(request_interval, unit = "day") + 1)
)
if (total_records_req < 1) {
stop(
call. = FALSE,
"You have submitted a query with 0 total records.\n",
"Please extend the dates requested."
)
}
# TODO: When Phil gets lat/lon values added to the summary results from the
# API, remove this bit here and add lat/lon to the list of queried values
if (Sys.info()['sysname'] == "Windows") {
metadata_file <- file.path(tempdir(), "dpird_metadata.Rda", fsep = "\\")
if (!file.exists(metadata_file)) {
saveRDS(
get_stations_metadata(which_api = "dpird", api_key = api_key),
file = metadata_file,
compress = FALSE
)
}
} else {
metadata_file <- file.path(tempdir(), "dpird_metadata.Rda")
if (!file.exists(metadata_file)) {
saveRDS(
get_stations_metadata(which_api = "dpird", api_key = api_key),
file = metadata_file,
compress = FALSE
)
}
}
# END chunk to remove
query_list <- .build_query(
station_code = station_code,
start_date_time = start_date,
end_date_time = end_date,
interval = checked_interval,
values = values,
include_closed = TRUE,
api_key = api_key,
limit = total_records_req
)
# set base URL according to interval
end_point <- data.table::fcase(
checked_interval == "15min",
"summaries/15min",
checked_interval == "30min",
"summaries/30min",
checked_interval == "hourly",
"summaries/hourly",
checked_interval == "daily",
"summaries/daily",
checked_interval == "monthly",
"summaries/monthly",
default = "summaries/yearly"
)
out <-
.parse_summary(
.ret_list = .query_dpird_api(
.end_point = end_point,
.query_list = query_list,
.limit = total_records_req
),
.values = values
)
out[, period.from := NULL]
out[, period.to := NULL]
.set_snake_case_names(out)
# provide some standard names between DPIRD and SILO for easy merging where
# data values are shared
# not all columns are renamed, but almost all are listed for clarity
data.table::setnames(
out,
old = c(
"station_code",
"station_name",
"year",
"month",
"day",
"date",
"air_temperature_avg",
"air_temperature_max",
"air_temperature_max_time",
"air_temperature_min",
"air_temperature_min_time",
"apparent_air_temperature_avg",
"apparent_air_temperature_max",
"apparent_air_temperature_max_time",
"apparent_air_temperature_min",
"apparent_air_temperature_min_time",
"barometric_pressure",
"battery_min_voltage",
"battery_min_voltage_date_time",
"chill_hours",
"delta_t_avg",
"delta_t_max",
"delta_t_max_time",
"delta_t_min",
"delta_t_min_time",
"dew_point_avg",
"dew_point_max",
"dew_point_max_time",
"dew_point_min",
"dew_point_min_time",
"erosion_condition_minutes",
"erosion_condition_start_time",
"errors",
"evapotranspiration",
"evapotranspiration_short_crop",
"evapotranspiration_tall_crop",
"frost_condition_minutes",
"frost_condition_start_time",
"heat_condition_minutes",
"heat_condition_start_time",
"observations_count",
"observations_percentage",
"pan_evaporation",
"pan_evaporation_12am",
"rainfall",
"relative_humidity_avg",
"relative_humidity_max",
"relative_humidity_max_time",
"relative_humidity_min",
"relative_humidity_min_time",
"richardson_units",
"soil_temperature",
"solar_exposure",
"wet_bulb_avg",
"wet_bulb_max",
"wet_bulb_max_time",
"wet_bulb_min",
"wet_bulb_min_time",
"wind_avg_speed",
"wind_height",
"wind_max_direction_compass_point",
"wind_max_direction_degrees",
"wind_max_speed",
"wind_max_time"
),
new = c(
"station_code",
"station_name",
"year",
"month",
"day",
"date",
"air_tavg",
"air_tmax",
"air_tmax_time",
"air_tmin",
"air_tmin_time",
"apparent_air_tavg",
"apparent_air_tmax",
"apparent_air_tmax_time",
"apparent_air_tmin",
"apparent_air_tmin_time",
"barometric_pressure",
"battery_min_voltage",
"battery_min_voltage_date_time",
"chill_hours",
"delta_tavg",
"delta_tmax",
"delta_tmax_time",
"delta_tmin",
"delta_tmin_time",
"dew_point_avg",
"dew_point_max",
"dew_point_max_time",
"dew_point_min",
"dew_point_min_time",
"erosion_condition_minutes",
"erosion_condition_start_time",
"errors",
"et",
"et_short_crop",
"et_tall_crop",
"frost_condition_minutes",
"frost_condition_start_time",
"heat_condition_minutes",
"heat_condition_start_time",
"observations_count",
"observations_percentage",
"pan_evaporation",
"pan_evaporation_12am",
"rainfall",
"rh_avg",
"rh_tmax",
"rh_tmax_time",
"rh_tmin",
"rh_tmin_time",
"richardson_units",
"soil_temperature",
"radiation",
"wet_bulb_avg",
"wet_bulb_tmax",
"wet_bulb_tmax_time",
"wet_bulb_tmin",
"wet_bulb_tmin_time",
"wind_avg_speed",
"wind_height",
"wind_max_direction_compass_point",
"wind_max_direction_degrees",
"wind_max_speed",
"wind_max_time"
),
skip_absent = TRUE
)
# TODO: When Phil gets lat/lon values added to the summary results from the
# API, remove this bit here and add lat/lon to the list of queried values
out <- merge(
x = out,
y = readRDS(file = metadata_file)[, c(1:2, 5:6)],
by.x = c("station_code", "station_name"),
by.y = c("station_code", "station_name")
)
# END chunk to remove
data.table::setcolorder(out, order(names(out)))
# this function contains several `if` and `if else` branches, so it's in its
# own function to simplify this parent function and keep it easier to maintain
# you can find it at the bottom of this file, so you can see what it's doing
out <-
.set_col_orders(.out = out, .checked_interval = checked_interval)
if (any(grep("time", colnames(out)))) {
out[, grep("time", colnames(out)) := suppressMessages(lapply(
.SD,
lubridate::ymd_hms,
truncated = 3,
tz = "Australia/West"
)), .SDcols = grep("time", colnames(out))]
}
data.table::setnames(out, gsub("period_", "", names(out)))
out[, station_code := as.factor(station_code)]
data.table::setkey(x = out, cols = station_code)
return(out[])
}
#' Parse DPIRD API summary data
#'
#' Internal function that parses and tidy up data as returned by
#' `.query_dpird_api()`
#'
#' @param .ret_list a list with the DPIRD weather API response
#' @param .values a character vector with the variables to query. See the
#' `get_dpird_summaries()` for further details.
#'
#' @return a tidy `data.table` with station id and requested weather summaries
#'
#' @noRd
#' @autoglobal
#' @keywords Internal
#'
.parse_summary <- function(.ret_list, .values) {
x <- vector(mode = "list", length = length(.ret_list))
for (i in seq_len(length(.ret_list))) {
y <- jsonlite::fromJSON(.ret_list[[i]]$parse("UTF8"))
if ("summaries" %in% names(y$collection)) {
nested_list_objects <-
data.table::as.data.table(y$collection$summaries)
# insert `station_name` and `station_code` into the nested_list_objects df
nested_list_objects[, station_code := y$collection$stationCode]
nested_list_objects[, station_name := y$collection$stationName]
x[[i]] <- nested_list_objects
}
}
nested_list_objects <- data.table::rbindlist(x)
# get the nested list columns and convert them to data.table objects
col_classes <-
vapply(nested_list_objects, class, FUN.VALUE = character(1))
col_lists <- which(col_classes == "list")
if (length(col_lists) > 0L) {
new_df_list <- vector(mode = "list", length = length(col_lists))
names(new_df_list) <- names(col_lists)
j <- 1
for (i in col_lists) {
new_df_list[[j]] <-
data.table::rbindlist(lapply(X = nested_list_objects[[i]], FUN = data.table::as.data.table))
# drop the list column from the org data.table
nested_list_objects[, names(new_df_list[j]) := NULL]
j <- j + 1
}
x <-
data.table::setorder(x = data.table::as.data.table(do.call(what = cbind, args = new_df_list)))
return(cbind(nested_list_objects, x))
}
return(nested_list_objects)
}
#' Check user inputs for earliest available DPIRD weather data
#'
#' @param .start_date A date object passed from another function
#'
#' @return invisible `NULL`, called for its side-effects
#' @autoglobal
#' @noRd
.check_earliest_available_dpird <- function(.station_code, .start_date, .f) {
y <- readRDS(file = .f)[, c(1, 3)]
y <- y[y$station_code %in% .station_code]
if (.start_date < y$start) {
stop(
call. = FALSE,
"You have requested weather data prior to the establishment of this ",
"DPIRD weather station's establishment. You might try the SILO data ",
"for data from the BOM station network instead."
)
}
return(invisible(NULL))
}
#' Sets the Column Orders Based on Time Step in the Data
#'
#' Sets column orders and adds date columns as necessary to match the data's
#' time-step values.
#'
#' @param out A `data.table` containing results from the DPIRD Weather 2.0 API
#' @param checked_interval a time-step interval value that has been checked for
#' validity
#'
#' @return a `data.table` with ordered columns
#' @autoglobal
#' @noRd
.set_col_orders <- function(.out, .checked_interval) {
if (.checked_interval == "monthly") {
.out[, date := lubridate::ym(sprintf("%s-%s", .out$period_year, .out$period_month))]
data.table::setcolorder(
.out,
c(
"station_code",
"station_name",
"longitude",
"latitude",
"period_year",
"period_month",
"date"
)
)
.out[, period_day := NULL]
.out[, period_hour := NULL]
.out[, period_minute := NULL]
data.table::setorder(x = .out, cols = "period_year", "period_month")
} else if (.checked_interval == "daily") {
.out[, date := lubridate::ymd(sprintf(
"%s-%s-%s",
.out$period_year,
.out$period_month,
.out$period_day
))]
data.table::setcolorder(
.out,
c(
"station_code",
"station_name",
"longitude",
"latitude",
"period_year",
"period_month",
"period_day",
"date"
)
)
.out[, period_minute := NULL]
.out[, period_hour := NULL]
data.table::setorder(x = .out,
cols = "period_year",
"period_month",
"period_day")
} else if (.checked_interval == "hourly") {
.out[, date := lubridate::ymd_h(
sprintf(
"%s-%s-%s-%s",
.out$period_year,
.out$period_month,
.out$period_day,
.out$period_hour
),
tz = "Australia/West"
)]
data.table::setcolorder(
.out,
c(
"station_code",
"station_name",
"longitude",
"latitude",
"period_year",
"period_month",
"period_day",
"period_hour",
"date"
)
)
.out[, period_minute := NULL]
data.table::setorder(x = .out,
cols = "period_year",
"period_month",
"period_day",
"period_hour")
} else if (.checked_interval == "30min" ||
.checked_interval == "15min") {
.out[, date := lubridate::ymd_hm(
sprintf(
"%s-%s-%s-%s-%s",
.out$period_year,
.out$period_month,
.out$period_day,
.out$period_hour,
.out$period_minute
),
tz = "Australia/West"
)]
data.table::setcolorder(
.out,
c(
"station_code",
"station_name",
"longitude",
"latitude",
"period_year",
"period_month",
"period_day",
"period_hour",
"period_minute",
"date"
)
)
data.table::setorder(
x = .out,
cols = "period_year",
"period_month",
"period_day",
"period_hour",
"period_minute"
)
} else {
data.table::setcolorder(.out,
c(
"station_code",
"station_name",
"longitude",
"latitude",
"period_year"
))
.out[, period_month := NULL]
.out[, period_day := NULL]
.out[, period_hour := NULL]
.out[, period_minute := NULL]
data.table::setorder(x = .out, cols = "period_year")
}
return(.out)
}