title
Baur
author
KMicha71
date
2 8 2019
output
html_document
pdf_document
default
Monthly Temperature data of Central Europe (~Germany) as taken from
https://de.wikipedia.org/wiki/Zeitreihe_der_Lufttemperatur_in_Deutschland#Messwerte_in_Dekaden
Create plain monthly series
python3 ./source/yearly_monthly.py
tBaur <- read.csv(" https://raw.githubusercontent.com/climdata/baur/master/raw/baur_monthly.csv" , sep = " ," )
tBaurAll <- tBaur [, c(" year" ," month" ," ts" ," time" ," temperature" )]
tBaurAll $ ts <- signif(tBaurAll $ year + (tBaurAll $ month - 0.5 )/ 12 , digits = 6 )
tDwd <- read.csv(" https://raw.githubusercontent.com/climdata/dwdTemperature/master/csv/monthly_temperature_de.csv" , sep = " ," , na = " NA" )
tDwd $ ts <- signif(tDwd $ year + (tDwd $ month - 0.5 )/ 12 , digits = 6 )
tBaurOld <- subset(tBaurAll , tBaurAll $ ts < min(tDwd $ ts ))
tDwdGer <- tDwd [, c(" year" ," month" ," ts" ," time" ," Deutschland" )]
names(tDwdGer )[names(tDwdGer ) == ' Deutschland' ] <- ' temperature'
tAll <- rbind(tBaurOld , tDwdGer )
tAll <- tAll [order(tAll $ ts ),]
write.table(tAll , file = " csv/baur_monthly.csv" , append = FALSE , quote = TRUE , sep = " ," ,
eol = " \n " , na = " NA" , dec = " ." , row.names = FALSE ,
col.names = TRUE , qmethod = " escape" , fileEncoding = " UTF-8" )
# install.packages("ggplot2")
require(" ggplot2" )
## Loading required package: ggplot2
color.temperature <- c(" #0000FF" , " #00CCCC" , " #FFFFFF" , " #EEAA33" , " #FF5555" )
# setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
py <- read.csv(" https://raw.githubusercontent.com/climdata/baur/master/csv/baur_monthly.csv" , sep = " ," )
py2 <- subset(py , ! is.na(py $ temperature ))
mp <- ggplot(py2 , aes(year , month ))
mp + geom_raster(aes(fill = temperature ))+
scale_y_continuous(breaks = c(1 ,6 ,12 ))+
theme(panel.background = element_rect(fill = ' #EEEEEE' , colour = ' white' ), legend.position = " right" , text = element_text(size = 14 ))+
scale_fill_gradientn(colours = color.temperature )
Plot more classical diagram
mp <- ggplot()
jan <- subset(py2 , py2 $ month == 1 )
jul <- subset(py2 , py2 $ month == 7 )
mp + geom_line(aes(y = jan $ temperature , x = jan $ year ), color = " blue" ) +
geom_line(aes(y = jul $ temperature , x = jul $ year ), color = " red" )
# hist(jan$temperature)
# hist(jul$temperature)
mp <- ggplot()
mp + geom_line(aes(y = py2 $ temperature , x = py2 $ ts ))
py7 <- fft(py2 $ temperature , inverse = FALSE )
border1 <- mean(Mod(py7 )) + 1 * sd(Mod(py7 ))
border2 <- 0.3 * sd(Mod(py7 ))
py8 <- py7
py8 <- replace(py8 , Mod(py8 )> border1 , 0.0 )
# py8 <- replace(py8, Mod(py8)<border2, 0.0)
py8 [0 : 2 ] <- py7 [0 : 2 ]
py8 [(length(py8 ) - 3 ): length(py8 )] <- py7 [(length(py7 ) - 3 ): length(py7 )]
# py8[10:14] <- 0.0
# py8[(length(py8) - 14):length(py8)-10] <- 0.0
py8 [520 : (length(py8 ) - 520 )] <- 0.0
# py8[260:(length(py8)-260)] <- 0.0
py8 [130 : (length(py8 )- 130 )] <- 0.0
# 3198/12
py9 <- py2
py9 $ temperature <- Mod(fft(py8 , inverse = TRUE ))/ length(py8 )
py9 <- subset(py9 , py9 $ year > = 1752 )
mp <- ggplot()
mp + geom_line(aes(y = py9 $ temperature , x = py9 $ ts ))
plot.data <- cbind(0 : (length(py7 )- 1 ), Mod(py7 ))
# TODO: why this scaling is necessary?
plot.data [2 : length(py7 ),2 ] <- 2 * plot.data [2 : length(py7 ),2 ]
plot(plot.data , t = " h" , lwd = 2 , main = " " ,
xlab = " Frequency (Hz)" , ylab = " Strength" ,
xlim = c(0 ,length(py7 )), ylim = c(0 ,max(Mod(plot.data [,2 ]))))
py7 <- py8
plot.data <- cbind(0 : (length(py7 )- 1 ), Mod(py7 ))
# TODO: why this scaling is necessary?
plot.data [2 : length(py7 ),2 ] <- 2 * plot.data [2 : length(py7 ),2 ]
plot(plot.data , t = " h" , lwd = 2 , main = " " ,
xlab = " Frequency (Hz)" , ylab = " Strength" ,
xlim = c(0 ,500 ), ylim = c(0 ,5000 ))
mp <- ggplot(py9 , aes(year , month ))
mp + geom_raster(aes(fill = temperature ))+
scale_y_continuous(breaks = c(1 ,6 ,12 ))+
theme(panel.background = element_rect(fill = ' #EEEEEE' , colour = ' white' ), legend.position = " right" , text = element_text(size = 14 ))+
scale_fill_gradientn(colours = color.temperature )
Plot summer and winter of filtered set
mp <- ggplot()
jan <- subset(py9 , py9 $ month == 1 )
jul <- subset(py9 , py9 $ month == 7 )
mp + geom_line(aes(y = jan $ temperature , x = jan $ year ), color = " blue" ) +
geom_line(aes(y = jul $ temperature , x = jul $ year ), color = " red" )
# hist(jan$temperature)
# hist(jul$temperature)
Work with yearly and moving/rolling normalization
# install.packages("data.table")
library(data.table )
# install.packages("zoo")
library(zoo )
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
py3 <- read.csv(" https://raw.githubusercontent.com/climdata/baur/master/raw/baur_yearly.csv" , sep = " ," )
avg <- rollapply(py3 , width = 30 , by = 1 , FUN = mean )
x <- tail(py3 , n = - 30 )
std <- rollapply(py3 , width = 30 , by = 1 , FUN = sd )
py4 <- (x - avg )/ std
py4 $ year <- x $ year
py4 <- tail(py4 , n = - 30 )
tmp <- py4 [,c(" year" ," jan" )]
names(tmp )[names(tmp ) == " jan" ] <- " temperature"
tmp $ month <- rep(1 ,nrow(tmp ))
norm <- tmp
m <- 2
for (month in c(" feb" ," mar" ," apr" ," may" ," jun" ," jul" ," aug" ," sep" ," oct" ," nov" ," dec" )) {
tmp <- py4 [,c(" year" ,month )]
names(tmp )[names(tmp ) == month ] <- " temperature"
tmp $ month <- rep(m ,nrow(tmp ))
m <- m + 1
norm <- rbind(norm , tmp )
}
mp <- ggplot(norm , aes(year , month ))
mp + geom_raster(aes(fill = temperature ))+
scale_y_continuous(breaks = c(1 ,6 ,12 ))+
theme(panel.background = element_rect(fill = ' #EEEEEE' , colour = ' white' ), legend.position = " right" , text = element_text(size = 14 ))+
scale_fill_gradientn(colours = color.temperature )
Work with yearly and fixed normalization
# install.packages("data.table")
library(data.table )
library(zoo )
py3 <- read.csv(" https://raw.githubusercontent.com/climdata/baur/master/raw/baur_yearly.csv" , sep = " ," )
tmp <- py3 [,c(" year" ," jan" )]
names(tmp )[names(tmp ) == " jan" ] <- " temperature"
avg <- mean(tmp $ temperature , na.rm = TRUE )
std <- sd(tmp $ temperature , na.rm = TRUE )
tmp $ temperature <- (tmp $ temperature - avg )/ std
tmp $ month <- rep(1 ,nrow(tmp ))
norm <- tmp
m <- 2
for (month in c(" feb" ," mar" ," apr" ," may" ," jun" ," jul" ," aug" ," sep" ," oct" ," nov" ," dec" )) {
tmp <- py3 [,c(" year" ,month )]
names(tmp )[names(tmp ) == month ] <- " temperature"
avg <- mean(tmp $ temperature , na.rm = TRUE )
std <- sd(tmp $ temperature , na.rm = TRUE )
tmp $ temperature <- (tmp $ temperature - avg )/ std
tmp $ month <- rep(m ,nrow(tmp ))
m <- m + 1
norm <- rbind(norm , tmp )
}
mp <- ggplot(norm , aes(year , month ))
mp + geom_raster(aes(fill = temperature ))+
scale_y_continuous(breaks = c(1 ,6 ,12 ))+
theme(panel.background = element_rect(fill = ' #EEEEEE' , colour = ' white' ), legend.position = " right" , text = element_text(size = 14 ))+
scale_fill_gradientn(colours = color.temperature )