Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from shamindras/iss-03-ghcn-readme
ENH: #3 Update noaa ghcnd extract code
- Loading branch information
Showing
9 changed files
with
188 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' This is a simply wrapper function to extract \code{noaa ghcn} | ||
#' station, states, and countries metadata as individual tibbles. | ||
#' This uses the \code{rnoaa} package directly to do this. | ||
#' | ||
#' @return This will be list with 3 separate tibbles named | ||
#' \code{ghcnd_stations, ghcnd_states, ghcnd_countries} | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' ghcnd_stations_metadata <- backburner::noaa_ghcn_stations_extract() | ||
#' } | ||
#' | ||
#' @export | ||
noaa_ghcn_stations_extract <- function(){ | ||
|
||
# Download the ghcn stations, states, countries table directly | ||
ghcnd_stations <- rnoaa::ghcnd_stations() %>% tibble::as_tibble(x = .) | ||
ghcnd_states <- rnoaa::ghcnd_states() %>% tibble::as_tibble(x = .) | ||
ghcnd_countries <- rnoaa::ghcnd_countries() %>% tibble::as_tibble(x = .) | ||
|
||
# Do renaming of columns for ghcn country table | ||
ghcnd_countries <- ghcnd_countries %>% | ||
dplyr::rename(id_fips_code = code, | ||
country_name = name) | ||
|
||
# Do renaming of columns for ghcn state table | ||
ghcnd_states <- ghcnd_states %>% | ||
dplyr::rename(state_code = code, | ||
state_name = name) | ||
|
||
# Return output as a list of tibbles | ||
out_ghcnd <- list("ghcnd_stations" = ghcnd_stations, | ||
"ghcnd_states" = ghcnd_states, | ||
"ghcnd_countries" = ghcnd_countries) | ||
|
||
base::return(out_ghcnd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Transformed GHCN-Daily Stations file. This is metadata for the GHCN-daily | ||
#' file. It contains additional info on US/Canada states and country | ||
#' information for each station | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' ghcnd_stations_tform <- backburner::get_transform_ghcnd_stations() | ||
#' } | ||
#' | ||
#' @return (tibble) : Transformed tibble of GHCN stations metadata | ||
#' @export | ||
get_transform_ghcnd_stations <- function(){ | ||
|
||
# First extract the station, states, and countries metadata | ||
ghcnd_stations_metadata <- backburner::noaa_ghcn_stations_extract() | ||
|
||
# Download the ghcn stations, states, countries table | ||
ghcnd_stations <- ghcnd_stations_metadata[["ghcnd_stations"]] | ||
ghcnd_states <- ghcnd_stations_metadata[["ghcnd_states"]] | ||
ghcnd_countries <- ghcnd_stations_metadata[["ghcnd_countries"]] | ||
|
||
# Do cleaning of ghcn station table to US/Canada state and country tables | ||
ghcnd_stations <- ghcnd_stations %>% | ||
dplyr::mutate(id_fips_code = | ||
stringr::str_sub(string = id, start = 1, | ||
end = 2), | ||
id_network_code = | ||
stringr::str_sub(string = id, start = 3, | ||
end = 3), | ||
id_station_numsys = | ||
stringr::str_sub(string = id, start = 4, | ||
end = 12)) %>% | ||
dplyr::rename(state_code = state) %>% | ||
dplyr::left_join(x = ., y = ghcnd_states, | ||
by = c("state_code")) %>% | ||
dplyr::left_join(x = ., y = ghcnd_countries, | ||
by = c("id_fips_code")) %>% | ||
dplyr::select(dplyr::starts_with("id"), | ||
latitude, longitude, elevation, | ||
state_code, state_name, country_name, | ||
name, | ||
dplyr::everything()) %>% | ||
janitor::clean_names() | ||
|
||
base::return(ghcnd_stations) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: "How to Extract NOAA GHCND stations metadata" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{How to Extract NOAA GHCND stations metadata} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
We can easily get nicely transformed [NOAA Global Historical Climatology Network Daily (GHCN-D) station metadata](https://docs.opendata.aws/noaa-ghcn-pds/readme.html). This is | ||
particularly useful for US/Canada related weather data. | ||
|
||
We can do this by simply running the following code: | ||
```{r setup, eval=FALSE} | ||
library(backburner) | ||
ghcnd_stations <- backburner::get_transform_ghcnd_stations() | ||
``` | ||
|
||
You can then use the `ghcnd_stations` tibble as required to get geospatial | ||
metadata on the `GHCN-D` climate features. |