# From https://mesonet.agron.iastate.edu/request/download.phtml?network=NY_ASOS
get_asos <- function(station, year, csv_output = NULL, cache = tempfile()) {
url <- "http://mesonet.agron.iastate.edu/cgi-bin/request/asos.py?"
dst <- file.path(cache, paste0(station, ".csv"))
if (!file.exists(dst)) {
query <- list(
station = station, data = "all",
year1 = as.character(year), month1 = "1", day1 = "1",
year2 = as.character(year), month2 = "12", day2 = "31", tz = "GMT",
format = "comma", latlon = "no", direct = "yes")
dir.create(cache, showWarnings = FALSE, recursive = TRUE)
r <- GET(url, query = query, write_disk(dst))
stop_for_status(r)
message("Saved file ", dst)
}
}
download_weather <- function(data_dir, raw_dir, year, stations) {
cache <- raw_dir
dst <- file.path(cache, "weather")
dir.create(dst, recursive = TRUE, showWarnings = FALSE)
lapply(stations, get_asos, year = year, cache = dst)
paths <- dir(dst, full.names = TRUE, pattern = ".*\\.csv")
col_types <- cols(
.default = col_double(),
station = col_character(),
valid = col_datetime(format = ""),
skyc1 = col_character(),
skyc2 = col_character(),
skyc3 = col_character(),
skyc4 = col_character(),
presentwx = col_character(),
metar = col_character()
)
all <- lapply(paths, read_csv, comment = "#", na = "M",
col_names = TRUE, col_types = col_types)
raw <- bind_rows(all)
names(raw) <- c("station", "valid", "tmpf", "dwpf", "relh", "drct", "sknt",
"p01i", "alti", "mslp", "vsby", "gust",
"skyc1", "skyc2", "skyc3", "skyc4",
"skyl1", "skyl2", "skyl3", "skyl4", "presentwx", "metar")
weather <-
raw %>%
rename_(time = ~valid) %>%
select_(
~station, ~time, temp = ~tmpf, dewp = ~dwpf, humid = ~relh,
wind_dir = ~drct, wind_speed = ~sknt, wind_gust = ~gust,
precip = ~p01i, pressure = ~mslp, visib = ~vsby
) %>%
mutate_(
time = ~as.POSIXct(strptime(time, "%Y-%m-%d %H:%M")),
wind_speed = ~as.numeric(wind_speed) * 1.15078, # convert to mpg
wind_gust = ~as.numeric(wind_speed) * 1.15078,
year = ~year,
month = ~lubridate::month(time),
day = ~lubridate::mday(time),
hour = ~lubridate::hour(time)) %>%
group_by_(~station, ~month, ~day, ~hour) %>%
filter_(~ row_number() == 1) %>%
select_(~station, ~year:hour, ~temp:visib) %>%
ungroup() %>%
filter_(~!is.na(month)) %>%
mutate_(
time_hour = ~ISOdatetime(year, month, day, hour, 0, 0)
)
save_csv(weather, file.path(raw_dir, "weather.csv"))
save_rda(weather, file = file.path(data_dir, "weather.rda"),
compress = "bzip2")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.