-
Notifications
You must be signed in to change notification settings - Fork 0
/
microclimate_summary.R
317 lines (275 loc) · 12.2 KB
/
microclimate_summary.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#install.packages("XYZ")
require(dplyr)
require(Thermimage)
require(ggplot2)
require(xts)
require(lubridate)
require(timetk)
require(tidyverse)
require(chron)
require(dplyr)
require(ggthemes)
require(ggplot2)
require(plotrix)
require(plotfunctions)
require(raster)
require(fANCOVA)
require(rgeos)
require(dplyr)
#######################################
####DAILY MEAN MAX MIN TEMPERATURES####
#######################################
# CHANGE PLOT NAME
setwd("FILEPATH")
plot_name = "las_17"
# ADAPT FLEXIBLY BASED ON FILE COLUMN NAMES
header <- c("egal", "date", "egal2", "t_soil", "t_surface", "t_air", "egal3", "egal4", "egal5")
# CHANGE FILE NAME
microclimate_las1 <- read.csv(paste("FILEPATH/",plot_name,"/data_94193801_0.csv", sep=""),
header = FALSE,
col.names = header,
sep = ";")
microclimate_las1 <- microclimate_las1[,c("date", "t_soil", "t_surface", "t_air")]
microclimate_las1$time <- strptime(microclimate_las1$date, "%Y.%m.%d %H:%M")
microclimate_las1$date <- as.Date(microclimate_las1$time)
microclimate_las1$hour = times(strftime(microclimate_las1$time, format="%T"))
#write.csv(microclimate_las1,
#paste("E:/Microclimate/Agroecology_data/", plot_name, "/", plot_name, "_microclimate.csv", sep = ""))
#microclimate_las1$hour <- as.numeric(microclimate_las1$hour)
daily_microclimate <- microclimate_las1 %>%
group_by(date) %>%
summarize(tmean_soil = mean(t_soil),
tmax_soil = max(t_soil),
tmin_soil = min(t_soil),
tsd_soil = sd(t_soil),
tmean_surface = mean(t_surface),
tmax_surface = max(t_surface),
tmin_surface = min(t_surface),
tsd_surface = sd(t_surface),
tmean_air = mean(t_air),
tmax_air = max(t_air),
tmin_air = min(t_air),
tsd_air = sd(t_air))
daily_microclimate["las_name"] <- plot_name
daily_microclimate <- daily_microclimate[-c(266:271), ]
# EXPORT
write.csv(daily_microclimate,
paste("FILEPATH", plot_name, "_daily_summary.csv", sep = ""))
# COMBINE ALL PLOTS
# create a list of all summary metric files
my_files <- list.files("FILEPATH/daily_summary", pattern = "\\.csv$")
# adapt wd for lapply
setwd("FILEPATH/daily_summary")
# read all files
sum_metric_list <- lapply(my_files, read.csv)
# combine all plots to a single file
all_sum_metrics <- bind_rows(sum_metric_list)
all_sum_metrics <- all_sum_metrics %>% select(-X)
write.csv(all_sum_metrics, "FILEPATH/daily_summary/all_plots_daily_summary.csv")
#######################################
####PLOTTING####
#######################################
# READ IN
stats_plus_micro <- read.csv("FILEPATH/temp_vs_metric/stats_plus_micro.csv")
# FORMAT AS DATETIME FOR PLOT
stats_plus_micro$date <- as.Date(stats_plus_micro$date, "%Y-%m-%d")
scale = c("#a1dab4", "#41b6c4", "#41b6c4", "#2c7fb8", "#FF6666")
# PLOT
ggplot(data = stats_plus_micro, aes(x=date, y=tmean_air, color = pad_med, group = las_name)) +
geom_line(size = 0.7)+
#scale_color_manual(values = scale)+
theme_bw()+
theme(legend.position = "bottom", axis.text.x=element_text(angle=50,hjust=0.5,vjust=0.5))+
labs(title = "Mean Air Temperature in *C",
subtitle = "",
caption = "",
fill = "")+
geom_vline(xintercept = "")+
scale_x_date("Date", date_breaks = "1 month", date_labels = "%b %d")+
scale_y_continuous("Temperature in *C")
scale_fill_manual(values=c("#0C7BDC", "#FFC20A"))
#######################################
####DATA PREPARATION: SUPERFLUOUS BECAUSE EXPORT####
#######################################
# READ IN PAD DATA
total.pad.stat <- read.csv("FILEPATH/temp_vs_metric/total.pad.stat.csv")
# SUMMARIZE PER PLOT INSTEAD OF HEIGHT
plots.pad.stat <- total.pad.stat %>%
group_by(plot_name) %>%
summarize(las_name = first(las_name),
pad_mean = mean(mean),
pad_med = median(med),
pad_max = max(max),
pad_min = min(min),
pad_sd = sd(sd),
pad_n = sum(n),
cc_percent = first(cc_percent.x),
cc_class = first(cc_class),
als_elevation = first(als_elevation),
gps_elevation = first(gps_elevation),
slope = first(slope),
aspect = first(aspect_class),
veg_type = first(veg_type),
veg_class = first(class)
)
# EXPORT
#write.csv(plots.pad.stat, "E:/figures/temp_vs_metric/total.pad.stat.summarized.csv")
# read other metrics
all_sums_by_rows <- read.csv("FILEPATH/summary_metrics_by_row/all_summ_by_rows.csv")
#filter out mean, remove other rows and rename
#repeat for most important metrics
mean_metrics <- all_sums_by_rows %>% filter(metric == "Mean")
mean_metrics[ , c(1, 2, 12:14)] <- list(NULL)
mean_metrics <- rename(mean_metrics,
FHD_mean = FHD,
CR_mean = CR,
pai_mean = pai,
RH25_mean = RH25,
RH50_mean = RH50,
RH75_mean = RH75,
RH95_mean = RH95,
RH98_mean = RH98,
iris_name = plot_name)
median_metrics <- all_sums_by_rows %>% filter(metric == "Median")
median_metrics[ , c(1, 2, 12:14)] <- list(NULL)
median_metrics <- rename(median_metrics,
FHD_med = FHD,
CR_med = CR,
pai_med = pai,
RH25_med = RH25,
RH50_med = RH50,
RH75_med = RH75,
RH95_med = RH95,
RH98_med = RH98,
iris_name = plot_name)
sd_metrics <- all_sums_by_rows %>% filter(metric == "Std. Deviation")
sd_metrics[ , c(1, 2, 12:14)] <- list(NULL)
sd_metrics <- rename(sd_metrics,
FHD_sd = FHD,
CR_sd = CR,
pai_sd = pai,
RH25_sd = RH25,
RH50_sd = RH50,
RH75_sd = RH75,
RH95_sd = RH95,
RH98_sd = RH98,
iris_name = plot_name)
min_metrics <- all_sums_by_rows %>% filter(metric == "Min")
min_metrics[ , c(1, 2, 12:14)] <- list(NULL)
min_metrics <- rename(min_metrics,
FHD_min = FHD,
CR_min = CR,
pai_min = pai,
RH25_min = RH25,
RH50_min = RH50,
RH75_min = RH75,
RH95_min = RH95,
RH98_min = RH98,
iris_name = plot_name)
max_metrics <- all_sums_by_rows %>% filter(metric == "Max")
max_metrics[ , c(1, 2, 12:14)] <- list(NULL)
max_metrics <- rename(max_metrics,
FHD_max = FHD,
CR_max = CR,
pai_max = pai,
RH25_max = RH25,
RH50_max = RH50,
RH75_max = RH75,
RH95_max = RH95,
RH98_max = RH98,
iris_name = plot_name)
# MERGE
df_list <- list(mean_metrics, median_metrics, sd_metrics, min_metrics, max_metrics)
merged_df <- Reduce(function(x, y) merge(x, y, all=TRUE, by = "iris_name"), df_list)
# EXPORT
# write.csv(merged_df, "E:/figures/temp_vs_metric/other_metrics_sorted.csv")
# finishing touches, deleting index columns, renaming, merging and exporting
all_sum_metrics <- read.csv("FILEPATH/daily_summary/all_plots_daily_summary.csv")
total_pad_stat <- read.csv("FILEPATH/temp_vs_metric/total.pad.stat.summarized.csv")
other_metrics_sorted <- read.csv("FILEPATH/temp_vs_metric/other_metrics_sorted.csv")
other_metrics_sorted <- rename(other_metrics_sorted, las_name = iris_name )
other_metrics_sorted <- other_metrics_sorted[,-c(1)]
merged_stats <- merge(total_pad_stat, other_metrics_sorted, all=TRUE, by = "las_name")
merged_stats <- merged_stats[,-c(2)]
stats_plus_micro <- merge(all_sum_metrics, merged_stats, all=TRUE, by = "las_name")
stats_plus_micro <- stats_plus_micro[,-c(2)]
#FINAL EXPORT
#write.csv(stats_plus_micro, "FILEPATH/temp_vs_metric/stats_plus_micro.csv")
###########################################################
#######HOURLY AIR (!) TEMPERATURES WITHOUT SUMMARY#########
###########################################################
setwd("E://")
plot_name = "las_1"
header <- c("egal", "date", "egal2", "t_soil", "t_surface", "t_air", "egal3", "egal4", "egal5")
# CHANGE FILE NAME
microclimate <- read.csv(paste("FILEPATH/Microclimate/Agroecology_data/",plot_name,"/data_94193801_0.csv", sep=""),
header = FALSE,
col.names = header,
sep = ";")
# DEFINE FUNCTION TO SUMMARIZE HOURLY TEMPERATURE OUT OF 15 MINS INTERVALS
mean_hourly <- function(microclimate, plot_name) {
microclimate$time <- strptime(microclimate$date, "%Y.%m.%d %H:%M")
microclimate$date <- as.POSIXct(microclimate$time)
aggr_microclimate <- aggregate(list(t_air = microclimate$t_air),
list(hourofday = cut(microclimate$date, "1 hour")),
mean)
aggr_microclimate$plot <- plot_name
return(aggr_microclimate)
}
# NESTED FUNCTION TO APPLY WHOLE WORKFLOW, NEEDS PRIOR FUNCTION TO WORK
apply_workflow <- function(microclimate){
microclimate <- microclimate[,c("date", "t_soil", "t_surface", "t_air")]
hourly_microclimate <- mean_hourly(microclimate, plot_name)
write.csv(hourly_microclimate,
paste("E:/Microclimate/hourly_summary/", plot_name, "_hourly_summary.csv", sep = ""))
}
# APPLY WORKFLOW FUNCTION
apply_workflow(microclimate)
# LIST ALL SUMMARY METRIC FILES
my_files <- list.files("FILEPATH/hourly_summary", pattern = "\\.csv$")
# CHANGE WD FOR LAPPLY
setwd("FILEPATH/hourly_summary")
# READ ALL FILES
sum_metric_list <- lapply(my_files, read.csv)
# COMBINE FILES
all_sum_metrics <- bind_rows(sum_metric_list)
all_sum_metrics <- all_sum_metrics[,-1]
write.csv(all_sum_metrics, "FILEPATH/hourly_summary/all_plots_hourly_summary.csv")
#################################################################################
#######HOURLY TEMPERATURES WITHOUT SUMMARY (ALL TEMPERATURES NOT JUST AIR########
#################################################################################
setwd("FILEPATH")
plot_name = "las_17"
# header <- c("index", "date", "t_soil", "t_surface", "t_air", "time", "hour")
microclimate <- read.csv(paste("FILEPATH/Agroecology_data/",plot_name, "/", plot_name, "_microclimate.csv", sep=""),
header = T,
sep = ",")
mean_hourly <- function(microclimate, plot_name) {
microclimate$time <- as.POSIXct(microclimate$time)
aggr_microclimate <- aggregate(list(t_soil = microclimate$t_soil,
t_surface = microclimate$t_surface,
t_air = microclimate$t_air),
list(hourofday = cut(microclimate$time, "1 hour")),
mean)
aggr_microclimate$plot <- plot_name
return(aggr_microclimate)
}
apply_workflow <- function(microclimate){
microclimate <- microclimate[,c("time", "t_soil", "t_surface", "t_air")]
hourly_microclimate <- mean_hourly(microclimate, plot_name)
write.csv(hourly_microclimate,
paste("FILEPATH/hourly_summary_all_temps/", plot_name, "_hourly_summary_all_temps.csv", sep = ""),
row.names = F)
}
apply_workflow(microclimate)
### NOW WE MERGE THEM ALL TO A SINGLE FILE
# LIST ALL SUMMARY METRIC FILES
my_files <- list.files("FILEPATH/hourly_summary_all_temps", pattern = "\\.csv$")
# CHANGE WD FOR LAPPLY
setwd("FILEPATH/hourly_summary_all_temps")
# READ ALL FILES
sum_metric_list <- lapply(my_files, read.csv)
# COMBINE FILES
all_sum_metrics <- bind_rows(sum_metric_list)
all_sum_metrics <- all_sum_metrics[,-1]
write.csv(all_sum_metrics, "FILEPATH/hourly_summary_all_temps/all_plots_hourly_summary_all_temps.csv", row.names = F)