library(tidyverse)
library(lubridate)
library(extrafont)
library(hrbrthemes)
extrafont::loadfonts()
library(patchwork)


wdr <- getwd()

#link https://www.ncdc.noaa.gov/cdo-web/datasets/GSOY/locations/CITY:AU000006/detail
Vienna <- readr::read_csv(file=paste0(wdr, "/data/", "Global Summary of the Year Location Details-Vienna-1877315.csv"),
                          col_types = cols(
                            .default = col_double(),
                            STATION = col_character(),
                            NAME = col_character())) %>% 
  janitor::clean_names()


fn_f_to_cels <- function(degree_in_f)  {
  (degree_in_f - 32)*5/9
}

Vienna <- Vienna %>% 
  mutate_at(vars(starts_with("t")), .funs=list(celsius= ~map_dbl(., fn_f_to_cels)))


# data cleaning (duplicates) ----------------------------------------------

Vienna %>% 
  group_by(date) %>% 
  summarise(n_obs=n()) %>% 
  filter(n_obs>1) %>% 
  pull(date) -> year_duplicates


Vienna %>% 
  filter(date %in% year_duplicates) %>% 
  arrange(date) #two different stations from 1949 to 1954
## # A tibble: 12 x 10
##    station name   date  dx90  tavg  tmax  tmin tavg_celsius tmax_celsius
##    <chr>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl>        <dbl>        <dbl>
##  1 AUW000~ VIEN~  1949     3  49.9  58.8  40.9         9.94         14.9
##  2 AU0000~ WIEN~  1949     0  50.9  57.5  44.3        10.5          14.2
##  3 AUW000~ VIEN~  1950     6  49.7  58.7  40.8         9.83         14.8
##  4 AU0000~ WIEN~  1950     4  50.7  57.3  44.1        10.4          14.1
##  5 AUW000~ VIEN~  1951     1  50.1  58.6  41.4        10.1          14.8
##  6 AU0000~ WIEN~  1951     0  51.2  57.4  45          10.7          14.1
##  7 AUW000~ VIEN~  1952    10  48.3  56.8  39.7         9.06         13.8
##  8 AU0000~ WIEN~  1952     3  49.7  56    43.4         9.83         13.3
##  9 AUW000~ VIEN~  1953     2  49.4  58.7  40.2         9.67         14.8
## 10 AU0000~ WIEN~  1953     1  51    58.4  43.5        10.6          14.7
## 11 AUW000~ VIEN~  1954    NA  NA    NA    NA          NA            NA  
## 12 AU0000~ WIEN~  1954     0  48.7  55.6  41.7         9.28         13.1
## # ... with 1 more variable: tmin_celsius <dbl>
Vienna %>% 
  group_by(station, date) %>% 
  summarise(n_obs=n()) %>% 
  ggplot()+
  geom_line(aes(x=date,
                y=n_obs,
                color=station))

#AU000005901 station continuous over entire period;
#AUW00034165 only brief period; remove

Vienna <- Vienna %>% 
  filter(station=="AU000005901")

# graphs ------------------------------------------------------------------

# > Number days with maximum temperature > 90 F (32.2C) -------------------

plot_line_days <- Vienna %>% 
  ggplot()+
  geom_line(aes(x=date,
                y=dx90),
            color="orange")+
  labs(title="Number of days with maximum temperature > 32.2C per year")+
  scale_x_continuous(breaks=seq(1850,2000, 50))+
  hrbrthemes::theme_ft_rc()+
  theme(panel.grid.minor.y = element_blank(),
        panel.spacing = unit(0, "cm"),
        plot.title = element_text(colour="#929299"),
        plot.margin = unit(c(1,0,0,0), "cm"),
        panel.grid.minor.x = element_blank(),
        axis.title = element_blank())

# average temperature per year ---------------------------------------------

# > barplot ---------------------------------------------------------------

plot_bar_temp<- Vienna %>% 
  ggplot()+
  labs(title="Average temperature per year (heatmap)")+
  geom_bar(aes(x=date,
               y=1,
               fill=tavg_celsius),
           color="transparent",
           stat="identity")+
  scale_fill_gradient2(low="darkblue", mid="white", high="red",
                       midpoint = mean(Vienna$tavg_celsius, na.rm = T),
                       na.value = "grey30", name="°C")+
  hrbrthemes::theme_ft_rc()+
  theme(panel.grid=element_blank(),
        axis.text.y = element_blank(),
        axis.title = element_blank(),
        legend.position="right",
        plot.title = element_text(colour="#929299"),
        panel.spacing = unit(0, "cm"),
        axis.line.x = element_line(colour="#464950",  size = 0.2),
        plot.margin = unit(c(1,0,0,0), "cm"),
        legend.justification="top")+
  guides(fill=guide_colorbar(barwidth=unit(.2, "cm")))



# > line plot -------------------------------------------------------------

plot_line_temp <- Vienna %>% 
  ggplot()+
  labs(title="Average temperature per year")+
  geom_line(aes(x=date,
               y=tavg_celsius))+
  geom_smooth(aes(x=date,
                  y=tavg_celsius))+
  hrbrthemes::theme_ft_rc()+
  scale_x_continuous(breaks=seq(1850,2000, 50))+
  scale_y_continuous(labels=function(x) paste(x, "C"))+
  theme(axis.title = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.spacing = unit(0, "cm"),
        plot.title = element_text(colour="#929299"),
        plot.margin = unit(c(1,0,0,0), "cm"))


# topical nights ----------------------------------------------------------


# > get data --------------------------------------------------------------


Vienna_daily <- readr::read_csv(file=paste0(wdr, "/data/", "NOAA_Temps_Vienna_daily.csv"),
                          col_types = cols(
                            .default = col_double(),
                            DATE=col_date(),
                            STATION = col_character(),
                            NAME = col_character())) %>% 
  janitor::clean_names()



# > data cleaning -----------------------------------------------------------


#check for duplicates
duplicate_years <- Vienna_daily %>% 
  group_by(date) %>% 
  summarise(n_obs=n()) %>% 
  filter(n_obs>1) %>% 
  mutate(year=lubridate::year(date)) %>% 
  distinct(year) #years in which more than 1 observatio per year; same issue as above


Vienna_daily %>% 
  filter(lubridate::year(date) %in% duplicate_years$year) %>% 
  distinct(station) #again two stations; remove station which operates only for a few years
## # A tibble: 2 x 1
##   station    
##   <chr>      
## 1 AUW00034165
## 2 AU000005901
Vienna_daily <- Vienna_daily  %>% 
  filter(station=="AU000005901") #remove station causing additional observations

Vienna_daily %>% 
  ggplot()+
  geom_bar(aes(x=date)) #check = 1 observation for each day

# > analysis --------------------------------------------------------------

#define tropical days = min temp not below 20 °C
Vienna_daily <- Vienna_daily %>% 
  select(station, date, starts_with("t")) %>% 
  mutate_at(vars(starts_with("t")), .funs=list(cels=fn_f_to_cels)) %>% 
  mutate(tropical=case_when(tmin_cels>=20 ~ TRUE,
                            TRUE ~ as.logical(FALSE)))

skimr::skim(Vienna_daily$tropical)
## 
## Skim summary statistics
## 
## -- Variable type:logical -------------------------------------------------------------------------------------------------------
##               variable missing complete     n   mean
##  Vienna_daily$tropical       0    59995 59995 0.0076
##                        count
##  FAL: 59537, TRU: 458, NA: 0
plot_tropical <- Vienna_daily %>% 
  mutate(year=lubridate::year(date)) %>% 
  group_by(year) %>% 
  summarise(n_tropical=sum(tropical[tropical==TRUE])) %>% 
  ggplot()+
  geom_line(aes(x=year,
                y=n_tropical),
            color="orange")+
  labs(title="Number of 'tropical' nights per year",
       subtitle="In a tropical night temperature does not fall below 20 °C.")+
  hrbrthemes::theme_ft_rc()+
  scale_x_continuous(breaks=seq(1850, 2000, 50))+
  theme(axis.title = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.spacing = unit(0, "cm"),
        plot.title = element_text(colour="#929299"),
        plot.margin = unit(c(1,0,0,0), "cm"))



# combine plots -----------------------------------------------------------

combined_plot <- patchwork::wrap_plots(plot_bar_temp, 
                                       plot_line_temp, 
                                       plot_tropical)+
  plot_layout(nrow = 3)+
  plot_annotation(title="Temperature dynamics in Vienna, 1850-2017",
                  caption=paste("source: Global Summary of the Year Location Details, NOAA;\ngraph: Roland Schmidt | @zoowalk | werk.statt.codes"),
                  theme=hrbrthemes::theme_ft_rc()+theme(plot.title=element_text(color="#929299", size = 24)))

combined_plot