Skip to content

Commit

Permalink
add the historical maximum temperature data
Browse files Browse the repository at this point in the history
  • Loading branch information
huizezhang-sherry committed Jun 26, 2023
1 parent 9519ace commit 78cff5c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 81 deletions.
17 changes: 12 additions & 5 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#' Australia climate data
#'
#' Daily measure on precipitation (\code{prcp}), maximum temperature
#' (\code{tmax}), and minimum temperature (\code{tmin}) in 2020 for
#' 639 stations.
#' \code{climate_aus}: daily measure on precipitation (\code{prcp}),
#' maximum temperature (\code{tmax}), and minimum temperature (\code{tmin})
#' in 2020 for 639 stations. \code{historical_tmax}: daily maximum temperature
#' (\code{tmax}) for 75 stations in Victoria and New South Wales for two
#' periods: 1971-1975 and 2016-2020.
#'
#' @details
#' \describe{
#' \item{id}{station ID, "ASN000" are international paddings, the next
Expand All @@ -16,13 +19,17 @@
#' \item{elev}{elevation of the stations}
#' \item{name}{station name}
#' \item{wmo_id}{the world meteorological organisation (WMO) station number}
#' \item{ts}{a list-column that nests all the temporal variables:
#' date, prcp, tmax, and tmin}
#' \item{ts}{For \code{climate_aus}: date, prcp, tmax, and tmin, for
#' \code{historical_tmax}: date and tmax}
#' }
#' @rdname climate-data
#' @examples
#' climate_aus %>% face_temporal() %>% face_spatial()
"climate_aus"

#' @rdname climate-data
"historical_tmax"

#' Toy climate data
#'
#' Daily measure (2020-01-01 to 2020-01-10) on precipitation (prcp),
Expand Down
36 changes: 0 additions & 36 deletions data-raw/prcp_aus.R

This file was deleted.

65 changes: 33 additions & 32 deletions data-raw/tmax-hist.R
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
## code to prepare `DATASET` dataset goes here
library(rnoaa)
library(tidyverse)
library(cubble)
all_stations <- ghcnd_stations() %>%
filter(str_starts(id, "ASN")) %>%
filter(last_year >= 2020) %>%
mutate(wmo_id = as.numeric(wmo_id),
name = str_to_lower(name)) %>%
select(-state, -gsn_flag)
all_stations <- rnoaa::ghcnd_stations() |>
filter(str_starts(id, "ASN")) |> # Australian stations start wiht "ASN"
filter(last_year >= 2020) |>
mutate(wmo_id = as.numeric(wmo_id), name = str_to_lower(name)) |>
select(-state, -gsn_flag) |>
select(id, longitude, latitude, elevation, name,
wmo_id, element, first_year, last_year) |>
rename(long = longitude, lat = latitude, elev = elevation)

cand <- all_stations %>%
filter(element == "TMAX",
first_year < 1970,
!is.na(wmo_id))
tmax_stations <- all_stations |>
filter(element == "TMAX", first_year < 1970, !is.na(wmo_id)) %>%
filter(between(as.numeric(stringr::str_sub(id, 7, 8)), 46, 90)) %>%
filter(!id %in% c("ASN00047048", "ASN00085279", "ASN00085298")) %>%
select(-c(element:last_year))

historical_tmax <- cand %>%
rowwise() %>%
mutate(ts = list(meteo_pull_monitors(
monitors = id, var = "TMAX",
date_min = glue::glue("{first_year}-01-01"),
date_max = glue::glue("{last_year}-12-31")) |>
select(-id))) %>%
as_cubble(index = date, key = id, coords = c(longitude, latitude))
tmax71 <- rnoaa::meteo_pull_monitors(
monitors = tmax_stations$id, var = "TMAX",
date_min = glue::glue("1971-01-01"),
date_max = glue::glue("1975-12-31")
)

tmax_hist <- historical_tmax |>
face_temporal() |>
mutate(tmax = tmax / 10) |>
filter(between(lubridate::year(date), 1970, 1975) |
lubridate::year(date) > 2015) |>
mutate(yearmonth = tsibble::yearmonth(date)) |>
group_by(yearmonth) |>
summarise(tmax = mean(tmax, na.rm = TRUE)) |>
face_spatial() |>
select(-c(element:last_year)) |>
filter(stringr::str_sub(id, 7, 8) >= 76)
tmax16 <- rnoaa::meteo_pull_monitors(
monitors = tmax_stations$id, var = "TMAX",
date_min = glue::glue("2016-01-01"),
date_max = glue::glue("2020-12-31")
)

usethis::use_data(tmax_hist, overwrite = TRUE, compress = "gzip")
tmax <- tmax71 %>%
bind_rows(tmax16) %>%
arrange(id, date) %>%
mutate(tmax = tmax /10)

historical_tmax <- make_cubble(
spatial = tmax_stations, temporal = tmax,
key = id, index = date, coords = c(long, lat))

usethis::use_data(historical_tmax, overwrite = TRUE)
Binary file added data/historical_tmax.rda
Binary file not shown.
17 changes: 12 additions & 5 deletions man/climate_aus.Rd → man/climate-data.Rd

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

3 changes: 0 additions & 3 deletions man/dplyr.Rd

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

0 comments on commit 78cff5c

Please sign in to comment.