Skip to content

Commit

Permalink
feat: stations_elevation() using read_fwf
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio-berti committed Jan 5, 2025
1 parent 7b4ad57 commit c772735
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GHCNr
Title: Download Weather Station Data from GHCN
Version: 1.2.0
Version: 1.3.0
Authors@R:
person("Emilio", "Berti", , "emilio.berti@idiv.de", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9286-011X"))
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(as_daily)
export(coverage)
export(daily)
export(download_inventory)
export(elevation_stations)
export(filter_stations)
export(get_countries)
export(get_country)
Expand Down Expand Up @@ -71,6 +72,8 @@ importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(httr2,resp_status)
importFrom(httr2,resp_status_desc)
importFrom(readr,fwf_positions)
importFrom(readr,read_fwf)
importFrom(readr,read_table)
importFrom(rlang,.data)
importFrom(stats,interaction.plot)
Expand Down
11 changes: 0 additions & 11 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,3 @@
#' }
#' @source <https://www.countrycallingcodes.com/iso-country-codes/europe-codes.php>
"USC00010655"

#' Elevation of the GHCNd Stations
#'
#' @format ## `europe_codes`
#' A table 128,024 x 2:
#' \describe{
#' \item{station}{Station name}
#' \item{elevation}{Elevation in meters}
#' }
#' @source <https://www.ncei.noaa.gov/pub/data/ghcn/daily/ghcnd-stations.csv>
"stations_elevation"
38 changes: 38 additions & 0 deletions R/elevation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' @title The GHCNd Station URL with Elevation
#' @return The URL of the GHCNd stations.
.elevation_url <- function() {
return("https://www.ncei.noaa.gov/pub/data/ghcn/daily/ghcnd-stations.txt")
}

#' @title Get GHCNd Station Elevation
#'
#' @importFrom readr read_fwf fwf_positions
#' @importFrom curl has_internet
#'
#' @export
#'
#' @return The table with the elevation of GHCNd stations.
#'
#' @examples
#' \dontrun{
#' el <- elevation_stations()
#' }
elevation_stations <- function() {
if (!has_internet()) stop("You do not have access to the internet. Check you connection and try again.")

ans <- tryCatch(
read_fwf(.elevation_url(),
fwf_positions(
start = c(1, 32),
end = c(11, 38),
col_names = c("station", "elevation")
),
col_types = "cd"
),
error = function(e) message(errorCondition(e))
)
if (is.null(ans)) stop(.elevation_url(), " not found.")

return(ans)

}
Binary file removed data/stations_elevation.rda
Binary file not shown.
14 changes: 14 additions & 0 deletions man/dot-elevation_url.Rd

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

19 changes: 19 additions & 0 deletions man/elevation_stations.Rd

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

26 changes: 0 additions & 26 deletions man/stations_elevation.Rd

This file was deleted.

0 comments on commit c772735

Please sign in to comment.