data-raw/climate_full.R

library(rnoaa)
library(tidyverse)
library(cubble)

# all the Australian stations that are still active in 2020 and have all of
# the three PRCP, TMAX, and TMIN recorded
aus_stations <- all_stations %>%
  filter(element %in% c("PRCP", "TMAX", "TMIN")) %>%
  nest(element: last_year) %>%
  rowwise() %>%
  filter(nrow(data) == 3) %>%
  select(-data)

# query the climate data for the selected station (may take a while if run for
# the first time)
aus_climate_raw <- aus_stations %>%
  rowwise() %>%
  mutate(ts = list(
    meteo_pull_monitors(
      monitors = id,
      var = c("PRCP", "TMAX", "TMIN"),
      date_min = "2016-01-01",
      date_max = "2020-12-31"
      ) %>%
      select(-id)
    )
  )

climate_full <- aus_climate_raw %>%
  unnest(ts) %>%
  mutate(tmax = tmax/10, tmin = tmin/10) %>%
  tsibble::as_tsibble(key = id, index = date) %>%
  fill_gaps() %>%
  tidyr::fill(long, lat, elev, name, wmo_id) %>%
  cubble::as_cubble(key = id, index = date, coords = c(long, lat))

usethis::use_data(climate_full, overwrite = TRUE)
huizezhang-sherry/weatherdata documentation built on June 15, 2022, 6:40 p.m.