Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize key query fields in riem_measures() #48

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee51f9e
Parameterize 'data' field in riem_measures()
JElchison Dec 16, 2024
e95c3ec
Add comment linking field documentation
JElchison Dec 16, 2024
2f6a39c
Parameterize 'elev' field in riem_measures()
JElchison Dec 16, 2024
695875d
Parameterize 'latlon' field in riem_measures()
JElchison Dec 16, 2024
ca212f2
Parameterize 'report_type' field in riem_measures(); Skip HFMETAR by …
JElchison Dec 16, 2024
2a5f27e
Choose a safer default date_start
JElchison Dec 16, 2024
3c358b4
Speficy nometa = no, since parsing counts on column names
JElchison Dec 16, 2024
60d8c28
Update self-documentation for riem_measures()
JElchison Dec 16, 2024
d5a7f4c
Update test fixtures files since default endpoint query changed
JElchison Dec 16, 2024
8737611
Document
JElchison Dec 16, 2024
1c8a136
Preserve order of riem_measures() positional arguments; Remove defaul…
JElchison Dec 17, 2024
a75b5b9
Specify the now-required station srgument in test-measures.R
JElchison Dec 17, 2024
3b082f0
Correct copy/paste error
JElchison Dec 17, 2024
12aeea5
Specify the now-required date_start argument in test-measures.R
JElchison Dec 17, 2024
540fb46
Add call to rlang::check_dots_empty() in riem_measures()
JElchison Dec 17, 2024
fe048f4
Add better validation for new riem_measures() args
JElchison Dec 17, 2024
b14e790
Specify the now-required station srgument in test-measures.R
JElchison Dec 17, 2024
3b8bc96
Correct order of args in documentation
JElchison Dec 17, 2024
ae205a3
refactor: use `rlang::is_character()`
maelle Dec 19, 2024
2f0c7f9
refactor: make report_type's input more readable
maelle Dec 19, 2024
b3c5dd4
test: add test for new parameters
maelle Dec 19, 2024
115a373
Address lintr warnings
JElchison Dec 19, 2024
045b229
Add new tests; Address lintr warnings
JElchison Dec 19, 2024
780b9e3
fix: remove duplicate import
maelle Dec 20, 2024
ceddc25
docs: improve documentation of `report_type`
maelle Dec 20, 2024
54b0ac5
refactor: remove unnecessary lambda
maelle Dec 20, 2024
cc05590
docs: update NEWS.md
maelle Dec 20, 2024
6b8995b
formatting
maelle Dec 20, 2024
6202c2f
latlon
maelle Dec 20, 2024
bc561e2
docs: clarify report
maelle Dec 20, 2024
4cc6e72
refactor: validate data
maelle Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Authors@R: c(
comment = "The research leading to these results has received funding from the European Research Council under the ERC Grant Agreement number 336167– the CHAI Project"),
person("rOpenSci", role = "fnd",
comment = "https://ropensci.org/"),
person("Daryl", "Herzmann", , "[email protected]", role = "ctb")
person("Daryl", "Herzmann", , "[email protected]", role = "ctb"),
person("Jonathan", "Elchison", email = "[email protected]", role = c("aut"),
comment = c(ORCID = "0009-0004-0787-3426"))
)
Description: Allows to get weather data from Automated Surface Observing
System (ASOS) stations (airports) in the whole world thanks to the
Expand Down Expand Up @@ -40,4 +42,4 @@ Suggests:
xts
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# riem (development version)

* Breaking change: `date_start` and `station` no longer have default values.

* New arguments `data`, `latlon`, `report_type`, `elev` (#49, @JElchison).

# riem 0.3.2

* Remove last usage of vcr as the choice was made to use httptest2 instead.
Expand Down
55 changes: 49 additions & 6 deletions R/measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
#'
#'
#' @param station station ID, see riem_stations()
#' @param date_start date of start of the desired data, e.g. "2000-01-01"
#' @param date_start date of start of the desired data, e.g. "2016-01-01"
#' @inheritParams rlang::args_dots_empty
#' @param date_end date of end of the desired data, e.g. "2016-04-22"
#' @param data The data columns to return. The available options are: all, tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, vsby, gust, skyc1, skyc2, skyc3, skyc4, skyl1, skyl2, skyl3, skyl4, wxcodes, ice_accretion_1hr, ice_accretion_3hr, ice_accretion_6hr, peak_wind_gust, peak_wind_drct, peak_wind_time, feel, metar, snowdepth # nolint: line_length_linter
#' @param elev If TRUE, the elevation (m) of the station will be included in the output. # nolint: line_length_linter
#' @param latlon If TRUE, the latitude and longitude of the station will be included in the output. # nolint: line_length_linter
#' @param report_type A vector of strings, representing report types to query. The available options are `"hfmetar"`, `"routine"`, `"specials"`. Default value is `c("routine", "specials")`. # nolint: line_length_linter
#'
#' @return a data.frame (tibble tibble) with measures,
#' the number of columns can vary from station to station,
Expand Down Expand Up @@ -63,28 +68,66 @@
#' )
#' }
riem_measures <- function(
station = "VOHY",
date_start = "2014-01-01",
maelle marked this conversation as resolved.
Show resolved Hide resolved
date_end = as.character(Sys.Date())) {
station,
date_start,
...,
date_end = as.character(Sys.Date()),
data = "all",
elev = FALSE,
latlon = TRUE,
report_type = NULL) {
if (!rlang::is_character(station, n = 1L)) {
cli::cli_abort("{.arg station} must be a string.")
}
date_start <- format_and_check_date(date_start, "date_start")
rlang::check_dots_empty()
date_end <- format_and_check_date(date_end, "date_end")
if (date_end < date_start) {
cli::cli_abort("{.arg date_end} must be bigger than {.arg date_start}.")
}
if (!rlang::is_character(data, n = 1L)) {
cli::cli_abort("{.arg data} must be a string.")
}
if (!is.logical(elev)) {
cli::cli_abort("{.arg elev} must be a logical (TRUE/FALSE)") # nolint: nonportable_path_linter
}
if (!is.logical(latlon)) {
cli::cli_abort("{.arg latlon} must be a logical (TRUE/FALSE)") # nolint: nonportable_path_linter
}

report_type <- report_type %||% c("routine", "specials")
report_type <- tolower(report_type) # not case-sensitive
report_type <- rlang::arg_match(
maelle marked this conversation as resolved.
Show resolved Hide resolved
report_type,
values = c("hfmetar", "routine", "specials"),
multiple = TRUE
)
report_type <- purrr::map_int(
report_type,
switch,
hfmetar = 1L,
routine = 3L,
specials = 4L
)
report_type <- paste(report_type, collapse = ",")

resp <- perform_riem_request(
path = "cgi-bin/request/asos.py/", # nolint: nonportable_path_linter
# query fields per https://mesonet.agron.iastate.edu/cgi-bin/request/asos.py?help # nolint: line_length_linter
query = list(
station = station,
data = "all",
data = data,
elev = ifelse(elev, "yes", "no"),
latlon = ifelse(latlon, "yes", "no"),
year1 = lubridate::year(date_start),
month1 = lubridate::month(date_start),
day1 = lubridate::day(date_start),
year2 = lubridate::year(date_end),
month2 = lubridate::month(date_end),
day2 = lubridate::day(date_end),
report_type = report_type,
format = "tdf",
latlon = "yes",
nometa = "no",
tz = "UTC"
)
)
Expand Down
23 changes: 19 additions & 4 deletions man/riem_measures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 47 additions & 3 deletions tests/testthat/_snaps/measures.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
# riem_measures checks dates
# riem_measures fails if required 'station' param is absent

argument "station" is missing, with no default

# riem_measures fails if required 'date_start' param is absent

argument "date_start" is missing, with no default

# riem_measures validates 'station' param

`station` must be a string.

---

HTTP 422 Unprocessable Entity.

# riem_measures validates dates

x Invalid `date_start`: somethingelse.
i Correct format is YYYY-MM-DD.

---

x Invalid `date_end`: somethingelse.
x Invalid `date_start`: 2015 31 01.
i Correct format is YYYY-MM-DD.

---

x Invalid `date_start`: 2015 31 01.
x Invalid `date_end`: somethingelse.
i Correct format is YYYY-MM-DD.

---
Expand All @@ -22,3 +38,31 @@

`date_end` must be bigger than `date_start`.

# riem_measures validates 'elev' param

`elev` must be a logical (TRUE/FALSE)

---

`elev` must be a logical (TRUE/FALSE)

# riem_measures validates 'latlon' param

`latlon` must be a logical (TRUE/FALSE)

---

`latlon` must be a logical (TRUE/FALSE)

# riem_measures validates 'report_type' param

`report_type` must be one of "hfmetar", "routine", or "specials", not "11111".

---

`report_type` must be one of "hfmetar", "routine", or "specials", not "zzzzz".

---

`report_type` must be one of "hfmetar", "routine", or "specials", not "zzzzz".

Loading
Loading