Skip to content

Instantly share code, notes, and snippets.

@yeedle
Created October 24, 2017 13:50
Show Gist options
  • Save yeedle/a64f63a2b55c08c462c8e94ba22c9e3b to your computer and use it in GitHub Desktop.
Save yeedle/a64f63a2b55c08c462c8e94ba22c9e3b to your computer and use it in GitHub Desktop.
Temperature of every hour in London since 1973
library(tidyverse)
library(R.utils)
library(glue)
library(padr)
library(ggExtra)
library(lubridate)
library(scales)
library(hrbrthemes)
#london
USAF <- '037720'
WBAN <- '99999'
url <- 'ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-lite/{yr}/{usaf}-{wban}-{yr}.gz'
urls <- glue(url, yr = 1973:2017, usaf = USAF, wban = WBAN)
fetch <- safely(function(url, .pb = NULL) {
if ((!is.null(.pb)) && inherits(.pb, "Progress") && (.pb$i < .pb$n)) .pb$tick()$print()
url %>%
downloadFile() %>%
gunzip(tempfile()) %>%
read_fwf(
col_positions = fwf_cols(
year = c(1, 4),
month = c(6, 7),
day = c(9, 11),
hour = c(12, 13),
temp = c(14, 19)
),
col_types = "iiiid"
)
})
pb <- progress_estimated(length(urls))
weather_data <- map(urls, fetch, .pb = pb)
data <- weather_data %>%
map_df(~.x$result) %>%
filter(temp != -9999) %>%
mutate(temp = temp/10) %>% # downloaded data is scaled by a factor of 10
mutate(timestamp = make_datetime(year, month, day, hour)) %>%
# add and fill some missing hours here and there
pad() %>%
mutate(
year = year(timestamp),
month = month(timestamp),
day = mday(timestamp),
hour = hour(timestamp)
) %>%
fill(temp)
data %>%
mutate(idx = (yday(timestamp)-1)*24 + hour) %>% # creates hourly index
# the following block removes leap dates and readjusts hourly index:
mutate(
idx = if_else(
leap_year(year) & idx > 1416,
idx - 24,
idx
)
) %>%
filter(!(month == 2 & day == 29)) %>%
ggplot(aes(x = idx, y = year, fill = temp)) +
geom_tile(height = .9) +
scale_y_reverse(
expand_scale(add = c(1, 0)),
breaks = 1973:2017
) +
scale_x_continuous(
expand = c(0,0),
breaks = c(0, 1094, 2189, 3284, 4379, 5474, 6569, 7664, 8759),
labels = c(
"1 Jan, 12:00",
"15 Feb, 14:00",
"2 Apr, 5:00",
"17 May, 20:00",
"2 Jul, 11:00",
"17 Aug, 2:00",
"1 Oct, 17:00",
"16 Nov, 8:00",
"31 Dec, 23:00"
)
) +
scale_fill_viridis_c(
breaks = c(-11, 1, 13, 25, 37),
labels = c("-11℃", "1℃", "13℃", "25℃", "37℃"), # data range is -11 - 37.3
option = "C"
) +
theme_ipsum() +
labs(
x = "Hour of the year",
y = "Year",
fill = "Temp",
title = "Hourly Temperature in London",
subtitle = "1973-2017",
caption = "Source: NCEI/NOAA, www.ncdc.noaa.gov"
)
ggsave("~/Downloads/weather/weather-london-hi-res.png", scale = 9, limitsize = F)
ggsave("~/Downloads/weather/weather-london.png", scale = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment