Skip to content

Commit

Permalink
refactor: use lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Apr 4, 2024
1 parent 1e84f47 commit 45d87da
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 44 deletions.
5 changes: 2 additions & 3 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
linters:with_defaults(camel_case_linter = NULL,
object_length_linter(50))
exclusions: list("tests/testthat.R", "tests/testthat/test-measures.R", "tests/testthat/test-networks.R", "tests/testthat/test-stations.R")
linters: lintr::linters_with_tags(tags = NULL)
encoding: "UTF-8"
48 changes: 31 additions & 17 deletions R/measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' @param date_start date of start of the desired data, e.g. "2000-01-01"
#' @param date_end date of end of the desired data, e.g. "2016-04-22"
#'
#' @return a data.frame (tibble tibble) with measures, the number of columns can vary from station to station,
#' @return a data.frame (tibble tibble) with measures,
#' the number of columns can vary from station to station,
#' but possible variables are
#' \itemize{
#' \item station: three or four character site identifier
Expand All @@ -15,7 +16,12 @@
#' \item relh: Relative Humidity in %
#' \item drct: Wind Direction in degrees from north
#' \item sknt: Wind Speed in knots
#' \item p01i: One hour precipitation for the period from the observation time to the time of the previous hourly precipitation reset. This varies slightly by site. Values are in inches. This value may or may not contain frozen precipitation melted by some device on the sensor or estimated by some other means. Unfortunately, we do not know of an authoritative database denoting which station has which sensor.
#' \item p01i: One hour precipitation for the period from the observation time
#' to the time of the previous hourly precipitation reset.
#' This varies slightly by site. Values are in inches.
#' This value may or may not contain frozen precipitation melted by some device
#' on the sensor or estimated by some other means. Unfortunately, we do not know
#' of an authoritative database denoting which station has which sensor.
#' \item alti: Pressure altimeter in inches
#' \item mslp: Sea Level Pressure in millibar
#' \item vsby: Visibility in miles
Expand All @@ -29,24 +35,32 @@
#' \item skyl3: Sky Level 3 Altitude in feet
#' \item skyl4: Sky Level 4 Altitude in feet
#' \item presentwx: Present Weather Codes (space seperated),
#' see e.g. Chapter 8 of [this manual](https://www.ofcm.gov/publications/fmh/FMH1/FMH1.pdf) for further explanations.
#' see e.g. Chapter 8 of [this manual](https://www.ofcm.gov/publications/fmh/FMH1/FMH1.pdf) for further explanations.# nolint: line_length_linter
#' \item feel: Apparent Temperature (Wind Chill or Heat Index) in degF
#' \item ice_accretion_1hr: Ice Accretion over 1 Hour in inch
#' \item ice_accretion_3hr: Ice Accretion over 3 Hour in inch
#' \item ice_accretion_6hr: Ice Accretion over 6 Hour in inch
#' \item relh: Relative Humidity in %
#' \item metar: unprocessed reported observation in METAR format
#' \item peak_wind_gust: Wind gust in knots from the METAR PK WND remark, this value may be different than the value found in the gust field. The gust field is derived from the standard METAR wind report.
#' \item peak_wind_drct: The wind direction in degrees North denoted in the METAR PK WND remark.
#' \item peak_wind_time: The timestamp of the PK WND value in the same timezone as the valid field and controlled by the tz parameter.
#' \item peak_wind_gust: Wind gust in knots from the METAR PK WND remark,
#' this value may be different than the value found in the gust field.
#' The gust field is derived from the standard METAR wind report.
#' \item peak_wind_drct: The wind direction in degrees North denoted
#' in the METAR PK WND remark.
#' \item peak_wind_time: The timestamp of the PK WND value in the same timezone
#' as the valid field and controlled by the tz parameter.

#' }
#' @details The data is queried through \url{https://mesonet.agron.iastate.edu/request/download.phtml}.
#' @details The data is queried through \url{https://mesonet.agron.iastate.edu/request/download.phtml}.# nolint: line_length_linter
#' @export
#'
#' @examples
#' \dontrun{
#' riem_measures(station = "VOHY", date_start = "2016-01-01", date_end = "2016-04-22")
#' riem_measures(
#' station = "VOHY",
#' date_start = "2016-01-01",
#' date_end = "2016-04-22"
#' )
#' }
riem_measures <- function(
station = "VOHY",
Expand All @@ -59,7 +73,7 @@ riem_measures <- function(
}

resp <- perform_riem_request(
path = "cgi-bin/request/asos.py/",
path = "cgi-bin/request/asos.py/", # nolint: nonportable_path_linter
query = list(
station = station,
data = "all",
Expand All @@ -79,34 +93,34 @@ riem_measures <- function(

content <- httr2::resp_body_string(resp)

col.names <- read.table(
col_names <- read.table(
text = content,
skip = 5,
nrows = 1,
skip = 5L,
nrows = 1L,
na.strings = c("", "NA", "M"),
sep = "\t",
stringsAsFactors = FALSE
) %>%
t() %>%
as.character()
col.names <- gsub(" ", "", col.names)
col_names <- gsub(" ", "", col_names, fixed = TRUE)

result <- read.table(
text = content,
skip = 6,
col.names = col.names,
skip = 6L,
col.names = col_names,
na.strings = c("", "NA", "M"),
sep = "\t",
stringsAsFactors = FALSE,
fill = TRUE
)

if (nrow(result) == 0) {
if (nrow(result) == 0L) {
cli::cli_warn("No results for this query.")
return(NULL)
}

result$valid <- lubridate::ymd_hm(result$valid)
result$valid <- lubridate::ymd_hm(result$valid) # nolint: extraction_operator_linter

tibble::as_tibble(result)
}
Expand Down
7 changes: 4 additions & 3 deletions R/networks.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#' Get ASOS and AWOS networks
#'
#' @return a data.frame (tibble tibble) with the names and codes of available networks.
#' @return a data.frame (tibble tibble) with the names and codes of
#' available networks.
#' @export
#'
#' @examples
#' \dontrun{
#' riem_networks()
#' }
riem_networks <- function() {
resp <- perform_riem_request(path = "api/1/networks.json")
resp <- perform_riem_request(path = "api/1/networks.json") # nolint: nonportable_path_linter

httr2::resp_check_status(resp)

Expand All @@ -19,7 +20,7 @@ riem_networks <- function() {
name = purrr::map_chr(content[["data"]], "name")
)

is_asos_or_awos <- grepl("A[SW]OS", networks_data$code)
is_asos_or_awos <- grepl("A[SW]OS", networks_data[["code"]])

networks_data[is_asos_or_awos, ]
}
19 changes: 11 additions & 8 deletions R/stations.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#' Get stations of an ASOS network
#'
#'
#' @param network A single network code, see riem_networks() for finding the code corresponding to a name.
#' @return a data.frame (tibble tibble) with the id, name, longitude (lon) and latitude (lat) of each station in the network.
#' @details You can see a map of stations in a network at \url{https://mesonet.agron.iastate.edu/request/download.phtml}.
#' @param network A single network code, see riem_networks() for finding
#' the code corresponding to a name.
#' @return a data.frame (tibble tibble) with the id, name, longitude (lon) and
#' latitude (lat) of each station in the network.
#' @details You can see a map of stations in a network at
#' \url{https://mesonet.agron.iastate.edu/request/download.phtml}.
#' @export
#'
#' @examples
#' \dontrun{
#' riem_stations(network = "IN__ASOS")
#' }
riem_stations <- function(network) {
valid_network_code <- (network %in% riem_networks()$code)
valid_network_code <- (network %in% riem_networks()$code) # nolint: extraction_operator_linter
if (!valid_network_code) error_invalid_network(network)

resp <- perform_riem_request(
path = sprintf("api/1/network/%s.json", network)
path = sprintf("api/1/network/%s.json", network) # nolint: nonportable_path_linter
)

httr2::resp_check_status(resp)
Expand All @@ -27,16 +30,16 @@ riem_stations <- function(network) {


results <- results[, !names(results) == "combo"]
results$lon <- as.numeric(results$longitude)
results$lat <- as.numeric(results$latitude)
results$lon <- as.numeric(results$longitude) # nolint: extraction_operator_linter
results$lat <- as.numeric(results$latitude) # nolint: extraction_operator_linter

return(results)
}

error_invalid_network <- function(network) {
cli::cli_abort(
c(
x = sprintf("{.arg network} ({.value {network}}) is an invalid network code."),
x = sprintf("{.arg network} ({.value {network}}) is an invalid network code."), # nolint: line_length_linter
i = "See {.fun riem_networks} for valid codes."
)
)
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ perform_riem_request <- function(path, query = NULL) {
httr2::req_url_path_append(path) %>%
httr2::req_url_query(!!!query) %>%
httr2::req_user_agent("riem (https://docs.ropensci.org/riem)") %>%
httr2::req_retry(max_tries = 3, max_seconds = 120) %>%
httr2::req_retry(max_tries = 3L, max_seconds = 120L) %>%
httr2::req_perform()
}
30 changes: 22 additions & 8 deletions man/riem_measures.Rd

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

3 changes: 2 additions & 1 deletion man/riem_networks.Rd

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

9 changes: 6 additions & 3 deletions man/riem_stations.Rd

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

0 comments on commit 45d87da

Please sign in to comment.