-
Notifications
You must be signed in to change notification settings - Fork 0
/
station_attributes.R
266 lines (225 loc) · 9.12 KB
/
station_attributes.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#NA functions
mean.na <- function(x,...){if (all(is.na(x))){NA} else {mean(x, na.rm = TRUE)}}
sum.na <- function(x,...){if (all(is.na(x))){NA} else {sum(x, na.rm = TRUE)}}
min.na <- function(x,...){if (all(is.na(x))){NA} else {min(x, na.rm = TRUE)}}
max.na <- function(x,...){if (all(is.na(x))){NA} else {max(x, na.rm = TRUE)}}
which.max.na <- function(x,...){if (all(is.na(x))){NA} else {which.max(x)}}
which.min.na <- function(x,...){if (all(is.na(x))){NA} else {which.min(x)}}
quantile.na <- function(x, probs = seq(0, 1, 0.25)){
if (all(is.na(x))){rep(NA, length(probs))} else {quantile(x, probs = probs,
type = 1, na.rm = TRUE)}}
library(dplyr)
library(lubridate)
# calculate station averages
path_p <- "D:/R/Climate_sensitivity/data/"
path_t <- "D:/R/Climate_sensitivity/data_temp/"
path <- "D:/R/Climate_sensitivity/Abflussdaten/"
files <- read.csv(paste(path,"Klima_Abfluss_Vergleich.csv", sep =""), header=TRUE)
nr <- length(files$Q_Nr)
results <- files # Temperature sensitivity
results$Tm <- array(NA, nr) # mean T
results$Pm <- array(NA, nr) # mean P
results$snowfrac <- array(NA, nr) # snow fraction
results$Tm_weekly <- matrix(NA, nr, 53) # mean weekly T
results$Tm_weekly_min <- matrix(NA, nr, 53) # min weekly T
# Main Loop
for (ii in 1:nr) # nr
{
loc = files$C_Nr[ii] # Klima Daten file
# read precip data
FILE <- paste(path_p,"order_17129_",loc,"_rhs150d0_1_data.txt", sep ="")
data <- read.table(FILE, header = FALSE, skip=3, colClasses = "character")
date <- as.Date((data[,2]),"%Y%m%d")
data <- data[,3]
NAvalues <- (data == "-")
data[NAvalues] <- NA
data <- as.numeric(data)
a1 <- data.frame(date = date,P_obs = data)
a1$year <- as.numeric(format(a1$date, "%Y"))
a1 <- subset(a1, year >= 1900 & year <= 2014)
a1 <- data.frame(date = date,P_obs = data)
results$Pm[ii] <- mean.na(a1$P_obs)*365
# read temp data
FILE <- paste(path_t,"order_17130_",loc,"_ths200d0_1_data.txt", sep ="")
data <- read.table(FILE, header = FALSE, skip=3, colClasses = "character")
date <- as.Date((data[,2]),"%Y%m%d")
data <- data[,3]
NAvalues <- (data == "-")
data[NAvalues] <- NA
data <- as.numeric(data)
a2 <- data.frame(date = date,T_obs = data)
a2$year <- as.numeric(format(a2$date, "%Y"))
a2 <- subset(a2, year >= 1900 & year <= 2014)
a2 <- data.frame(date = date,T_obs = data)
results$Tm[ii] <- mean.na(a2$T_obs)
# weekly temperature
# TODO calc weakly mean first...
#a2 <- a2 %>%
# mutate(week = week(date))
#Tm_weekly <- a2 %>%
# group_by(week) %>%
# summarise(Tm_weekly = min(T_obs, na.rm = TRUE)) # na.rm = TRUE to handle missing values
#results$Tm_weekly[ii,] <- Tm_weekly$Tm_weekly
###
a2 <- a2 %>%
mutate(week = week(date), year = year(date)) # add year to distinguish weeks across years
# calculate weekly averages
Tm_weekly_year <- a2 %>%
group_by(year, week) %>% #group by both year and week to avoid overlap across years
summarise(Tm_weekly_year = mean(T_obs, na.rm = TRUE), .groups = 'drop')
# calculate something using the weekly averages, e.g. mean
Tm_weekly_avg <- Tm_weekly_year %>%
group_by(week) %>%
summarise(Tm_weekly_year = mean(Tm_weekly_year, na.rm = TRUE))
results$Tm_weekly[ii,] <- Tm_weekly_avg$Tm_weekly_year
# or min
Tm_weekly_min <- Tm_weekly_year %>%
group_by(week) %>%
summarise(Tm_weekly_year = min(Tm_weekly_year, na.rm = TRUE))
results$Tm_weekly_min[ii,] <- Tm_weekly_min$Tm_weekly_year
# snow frac
P_snow <- array(0, length(a2$T_obs))
P_snow[a2$T_obs < 0 & !is.na(a2$T_obs)] <- a1$P_obs[a2$T_obs < 0 & !is.na(a2$T_obs)]
results$snowfrac[ii] <- sum.na(P_snow) / sum.na(a1$P_obs)
#a <- merge(a1, a2, by ="date")
}
### elevation and temperature
pdf("D:/R/Climate_sensitivity/temp_vs_elevation.pdf", width = 4, height = 4.5)
# plot
plot(results$Tm, results$elev,
xlab = "Temperature [°C]",
ylab = "Elevation [m a.s.l.]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$elev ~ results$Tm)
abline(model, col = "black", lwd = 2)
# correlation
correlation <- cor(results$Tm, results$elev)
print(round(correlation,3))
# predict catchment info
model <- lm(results$Tm ~ results$elev)
results$temp_ezg <- coef(model)[1] + coef(model)[2] * results$elev_ezg
dev.off()
### elevation and snow fraction
pdf("D:/R/Climate_sensitivity/snowfraction_vs_elevation.pdf", width = 4, height = 4.5)
# plot
plot(results$snowfrac, results$elev,
xlab = "Snow fraction [-]",
ylab = "Elevation [m a.s.l.]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$elev ~ results$snowfrac)
abline(model, col = "black", lwd = 2)
# correlation
correlation <- cor(results$snowfrac, results$elev)
print(round(correlation,3))
# predict catchment info
model <- lm(results$snowfrac ~ results$elev)
results$snowfrac_ezg <- coef(model)[1] + coef(model)[2] * results$elev_ezg
dev.off()
### elevation and precipitation
pdf("D:/R/Climate_sensitivity/precipitation_vs_elevation.pdf", width = 4, height = 4.5)
# plot
plot(results$Pm, results$elev,
xlab = "Precipitation [mm/y]",
ylab = "Elevation [m a.s.l.]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$elev ~ results$Pm)
abline(model, col = "black", lwd = 2)
# correlation
correlation <- cor(results$Pm, results$elev)
print(round(correlation,3))
# predict catchment info
model <- lm(results$Pm ~ results$elev)
results$precip_ezg <- coef(model)[1] + coef(model)[2] * results$elev_ezg
dev.off()
### glacier cover and temperature
pdf("D:/R/Climate_sensitivity/temp_vs_glaciers.pdf", width = 4, height = 4.5)
# plot
plot(results$temp_ezg[results$Glacier>0], results$Glacier[results$Glacier>0],
xlab = "Temperature [°C]",
ylab = "Glacier cover [%]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$Glacier[results$Glacier>0] ~ results$temp_ezg[results$Glacier>0])
abline(model, col = "black", lwd = 2)
# correlation
correlation <- cor(results$temp_ezg[results$Glacier>0], results$Glacier[results$Glacier>0])
print(round(correlation,3))
dev.off()
### glacier cover and temperature
pdf("D:/R/Climate_sensitivity/snowfrac_vs_glaciers.pdf", width = 4, height = 4.5)
# plot
plot(results$snowfrac_ezg[results$Glacier>0], results$Glacier[results$Glacier>0],
xlab = "Snow fraction [-]",
ylab = "Glacier cover [%]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$Glacier[results$Glacier>0] ~ results$snowfrac_ezg[results$Glacier>0])
abline(model, col = "black", lwd = 2)
# correlation
correlation <- cor(results$snowfrac_ezg[results$Glacier>0], results$Glacier[results$Glacier>0])
print(round(correlation,3))
dev.off()
### temperature for specified elevation bands
elev <- seq(500,3500,500)
model <- lm(results$Tm ~ results$elev)
temp_predict <- coef(model)[1] + coef(model)[2] * elev
print(round(temp_predict,1))
temperature <- seq(-6,8,2)
model <- lm(results$elev ~ results$Tm)
elev_predict <- coef(model)[1] + coef(model)[2] * temperature
print(round(elev_predict,0))
### temperature for specified glacier covers
glaciers <- seq(0,60,10)
model <- lm(results$temp_ezg[results$Glacier>0] ~ results$Glacier[results$Glacier>0])
temp_predict <- coef(model)[1] + coef(model)[2] * glaciers
print(round(temp_predict,1))
temperature <- seq(-6,2,1)
model <- lm(results$Glacier[results$Glacier>0] ~ results$temp_ezg[results$Glacier>0])
glacier_predict <- coef(model)[1] + coef(model)[2] * temperature
print(round(glacier_predict,0))
### elevation and temperature for each week
pdf("D:/R/Climate_sensitivity/weekly_temp.pdf", width = 4, height = 4.5)
weekly_isotherm <- matrix(NA, 53, 2)
temperature <- 0 #c(0,1,2)
for (w in 1:53)
{
plot(results$Tm_weekly[,w], results$elev,
xlab = "Temperature [°C]",
ylab = "Elevation [m a.s.l.]",
pch = 19,
col = "black",
cex = 1.0)
model <- lm(results$elev ~ results$Tm_weekly[,w])
abline(model, col = "black", lwd = 2)
elev_predict <- coef(model)[1] + coef(model)[2] * temperature
print(round(elev_predict,0))
weekly_isotherm[w,1] <- elev_predict
# correlation
correlation <- cor(results$Tm_weekly[,w], results$elev)
print(round(correlation,3))
# weekly minima
model <- lm(results$elev ~ results$Tm_weekly_min[,w])
abline(model, col = "black", lwd = 2)
elev_predict <- coef(model)[1] + coef(model)[2] * temperature
print(round(elev_predict,0))
weekly_isotherm[w,2] <- elev_predict
}
dev.off()
out_isotherm <- data.frame(week = seq(1, 53, 1), isotherm = weekly_isotherm)
pdf("D:/R/Climate_sensitivity/weekly_temp_regime.pdf", width = 4, height = 4.5)
plot(out_isotherm$week, out_isotherm$isotherm.1,
xlab = "Week [-]",
ylab = "Elevation [m a.s.l.]",
pch = 19,
col = "black",
cex = 1.0)
dev.off()
write.table(out_isotherm,file=paste(path,"0deg_isotherm_regime.txt", sep ="" ))