Last active
July 3, 2019 04:32
-
-
Save adamhsparks/23d6266f4a2b5f19f9edef6970f0364a to your computer and use it in GitHub Desktop.
Generate a climogram using bomrang to download station data for 2018/19 summer growing season in Australia
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
library(bomrang) | |
library(tidyverse) | |
library(theme.usq) | |
# Find stations ---------------------------------------------------------------- | |
# Find nearest stations for each area | |
# search https://www.latlong.net/ for the lat lon values of each location | |
# clermont | |
head(capella <- | |
sweep_for_stations(latlon = c(-23.083333, 148.0166666))) | |
head(clermont <- | |
sweep_for_stations(latlon = c(-22.824930, 147.639380))) | |
# SQ | |
head(dalby <- | |
sweep_for_stations(latlon = c(-27.183270, 151.263733))) | |
head(toowoomba <- | |
sweep_for_stations(latlon = c(-27.563830, 151.953970))) | |
head(millmerran <- | |
sweep_for_stations(latlon = c(-27.873250, 151.271110))) | |
# NNSW | |
head(inverell <- | |
sweep_for_stations(latlon = c(-29.776279, 151.113434))) | |
head(mullalley <- | |
sweep_for_stations(latlon = c(-31.099090, 149.909210))) | |
head(gunnedah <- | |
sweep_for_stations(latlon = c(-30.976900, 150.251820))) | |
# for Capella and Clermont we can use the Clermont Airport station | |
# for the Darling Downs we will just use the Dalby Airport | |
# for NNSW we can use both the Inverell Research Centre and Gunnedah Airport | |
# Download --------------------------------------------------------------------- | |
clermont_rain <- | |
get_historical(stationid = clermont[1, 1], type = "rain")[, -c(1, 8)] | |
clermont_max <- | |
get_historical(stationid = clermont[1, 1], type = "max")[, -c(1, 8)] | |
clermont_min <- | |
get_historical(stationid = clermont[1, 1], type = "min")[, -c(1, 8)] | |
dalby_rain <- | |
get_historical(stationid = dalby[1, 1], type = "rain")[, -c(1, 8)] | |
dalby_max <- | |
get_historical(stationid = dalby[1, 1], type = "max")[, -c(1, 8)] | |
dalby_min <- | |
get_historical(stationid = dalby[1, 1], type = "min")[, -c(1, 8)] | |
inverell_rain <- | |
get_historical(stationid = inverell[1, 1], type = "rain")[, -c(1, 8)] | |
inverell_max <- | |
get_historical(stationid = inverell[1, 1], type = "max")[, -c(1, 8)] | |
inverell_min <- | |
get_historical(stationid = inverell[1, 1], type = "min")[, -c(1, 8)] | |
gunnedah_rain <- | |
get_historical(stationid = gunnedah[1, 1], type = "rain")[, -c(1, 8)] | |
gunnedah_max <- | |
get_historical(stationid = gunnedah[1, 1], type = "max")[, -c(1, 8)] | |
gunnedah_min <- | |
get_historical(stationid = gunnedah[1, 1], type = "min")[, -c(1, 8)] | |
# Munge for summary statistics for reporting 2018/19 season -------------------- | |
clermont_avgs <- left_join(clermont_rain, clermont_max) %>% | |
left_join(., clermont_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2019-02-28" & | |
YYYYMMDD <= "2019-05-01") %>% | |
summarise( | |
min = min(min_temperature, na.rm = TRUE), | |
max = max(max_temperature, na.rm = TRUE), | |
avg_tmp = round(mean( | |
c(min_temperature, max_temperature), na.rm = TRUE | |
), 2), | |
rainfall = sum(rainfall, na.rm = TRUE), | |
start = min(YYYYMMDD), | |
end = max(YYYYMMDD) | |
) %>% | |
add_column(Location = "Clermont") %>% | |
select(c("Location", "min", "max", "avg_tmp", "rainfall", "start", "end")) | |
dalby_avgs <- left_join(dalby_rain, dalby_max) %>% | |
left_join(., dalby_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-12-31" & | |
YYYYMMDD <= "2019-03-01") %>% | |
mutate( | |
tmp = mean(c(min_temperature, max_temperature), na.rm = TRUE), | |
tmp = na_if(min_temperature, NA), | |
tmp = na_if(max_temperature, NA) | |
) %>% | |
summarise( | |
min = min(min_temperature, na.rm = TRUE), | |
max = max(max_temperature, na.rm = TRUE), | |
avg_tmp = round(mean( | |
c(min_temperature, max_temperature), na.rm = TRUE | |
), 2), | |
rainfall = sum(rainfall, na.rm = TRUE), | |
start = min(YYYYMMDD), | |
end = max(YYYYMMDD) | |
) %>% | |
add_column(Location = "Dalby") %>% | |
select(c("Location", "min", "max", "avg_tmp", "rainfall", "start", "end")) | |
inverell_avgs <- left_join(inverell_rain, inverell_max) %>% | |
left_join(., inverell_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-12-31" & | |
YYYYMMDD <= "2019-03-01") %>% | |
summarise( | |
min = min(min_temperature, na.rm = TRUE), | |
max = max(max_temperature, na.rm = TRUE), | |
avg_tmp = round(mean( | |
c(min_temperature, max_temperature), na.rm = TRUE | |
), 2), | |
rainfall = sum(rainfall, na.rm = TRUE), | |
start = min(YYYYMMDD), | |
end = max(YYYYMMDD) | |
) %>% | |
add_column(Location = "Inverell") %>% | |
select(c("Location", "min", "max", "avg_tmp", "rainfall", "start", "end")) | |
gunnedah_avgs <- left_join(gunnedah_rain, gunnedah_max) %>% | |
left_join(., gunnedah_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-12-31" & | |
YYYYMMDD <= "2019-03-01") %>% | |
summarise( | |
min = min(min_temperature, na.rm = TRUE), | |
max = max(max_temperature, na.rm = TRUE), | |
avg_tmp = round(mean( | |
c(min_temperature, max_temperature), na.rm = TRUE | |
), 2), | |
rainfall = sum(rainfall, na.rm = TRUE), | |
start = min(YYYYMMDD), | |
end = max(YYYYMMDD) | |
) %>% | |
add_column(Location = "Gunnedah") %>% | |
select(c("Location", "min", "max", "avg_tmp", "rainfall", "start", "end")) | |
# Averages and summary for growing season -------------------------------------- | |
(bind_rows(clermont_avgs, dalby_avgs, inverell_avgs, gunnedah_avgs)) | |
# Munge for graph -------------------------------------------------------------- | |
clermont <- left_join(clermont_rain, clermont_max) %>% | |
left_join(., clermont_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-12-01" & | |
YYYYMMDD <= "2019-05-31") %>% | |
mutate( | |
tmp = mean(c(min_temperature, max_temperature), na.rm = TRUE), | |
tmp = na_if(min_temperature, NA), | |
tmp = na_if(max_temperature, NA) | |
) %>% | |
add_column(Location = "Clermont") %>% | |
select(c("Location", "YYYYMMDD", "tmp", "rainfall")) | |
dalby <- left_join(dalby_rain, dalby_max) %>% | |
left_join(., dalby_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-09-01" & | |
YYYYMMDD <= "2019-04-30") %>% | |
mutate( | |
tmp = mean(c(min_temperature, max_temperature), na.rm = TRUE), | |
tmp = na_if(min_temperature, NA), | |
tmp = na_if(max_temperature, NA) | |
) %>% | |
add_column(Location = "Dalby") %>% | |
select(c("Location", "YYYYMMDD", "tmp", "rainfall")) | |
inverell <- left_join(inverell_rain, inverell_max) %>% | |
left_join(., inverell_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-09-01" & | |
YYYYMMDD <= "2019-03-31") %>% | |
mutate( | |
tmp = mean(c(min_temperature, max_temperature), na.rm = TRUE), | |
tmp = na_if(min_temperature, NA), | |
tmp = na_if(max_temperature, NA) | |
) %>% | |
add_column(Location = "Inverell") %>% | |
select(c("Location", "YYYYMMDD", "tmp", "rainfall")) | |
gunnedah <- left_join(gunnedah_rain, gunnedah_max) %>% | |
left_join(., gunnedah_min) %>% | |
mutate(YYYYMMDD = ISOdate(year, month, day)) %>% | |
filter(YYYYMMDD >= "2018-09-01" & | |
YYYYMMDD <= "2019-03-31") %>% | |
mutate( | |
tmp = mean(c(min_temperature, max_temperature), na.rm = TRUE), | |
tmp = na_if(min_temperature, NA), | |
tmp = na_if(max_temperature, NA) | |
) %>% | |
add_column(Location = "Gunnedah") %>% | |
select(c("Location", "YYYYMMDD", "tmp", "rainfall")) | |
# nnsw <- left_join(inverell, gunnedah, by = "YYYYMMDD") %>% | |
# mutate(tmp = mean(c(tmp.x, tmp.y), na.rm = TRUE)) %>% | |
# mutate(rainfall = mean(c(rainfall.x, rainfall.y), na.rm = TRUE)) %>% | |
# add_column(Location = "NNSW") %>% | |
# select(c("Location", "YYYYMMDD", "tmp", "rainfall")) | |
weather <- bind_rows(clermont, dalby, inverell, gunnedah) | |
weather$Location <- factor(weather$Location, | |
levels = c("Clermont", | |
"Dalby", | |
"Inverell", | |
"Gunnedah")) | |
# Graph ------------------------------------------------------------------------ | |
# max rainfall set from max clermont rainfall in Apr 2019 | |
ylim.prim <- c(0, 160) | |
ylim.sec <- c(10, 50) | |
b <- diff(ylim.prim) / diff(ylim.sec) | |
a <- b * (ylim.prim[1] - ylim.sec[1]) | |
png(filename = "Climograms/Image_1.png", width = 1024, height = 557, units = "px") | |
ggplot(weather, aes(x = as.Date(YYYYMMDD), y = rainfall)) + | |
geom_col() + | |
geom_line(aes(y = a + tmp * b), | |
color = usq_cols("support red"), | |
size = 1.25) + | |
scale_y_continuous("Precipitation (mm)", | |
sec.axis = sec_axis( ~ (. - a) / b, | |
name = "Mean Temperature (˚C)")) + | |
labs( | |
title = paste0("Climogram for ", bYYYY, "-", eYYYY, " Summer Cropping Season"), | |
subtitle = "Daily data sourced from Australia Bureau of Meteorology (BOM)", | |
caption = "Clermont data is from Clermont Airport, Dalby data is from the Dalby Airport, | |
Inverell data is from the Inverell Research Centre and Gunnedah is from the Gunnedah Airport AWS" | |
) + | |
scale_x_date(date_breaks = "1 month", date_labels = "%b\n%Y") + | |
xlab("Date") + | |
theme_usq() + | |
theme( | |
axis.ticks.y.right = element_line(color = usq_cols("support red")), | |
axis.text.y.right = element_text(color = usq_cols("support red")), | |
axis.title.y.right = element_text(color = usq_cols("support red")) | |
) + | |
facet_grid(Location ~ .) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment