-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.R
executable file
·141 lines (100 loc) · 3.19 KB
/
analyze.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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: TopoCliF
# Script purpose: create presets from DEM tiles and Randolph SHP
# Date: Mo Nov 26, 2018
# Author: Arne Thiemann
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ---- pre ------------
options(stringsAsFactors = F)
lapply(c("rgdal", "raster", "ggplot2", "tidyr", "dplyr", "gridExtra", "e1071"
), require, character.only = T)
source("analysis_parameters.R")
if(plateau_detection) require(igraph)
# set raster plot colors
colors_elevation <- colorRampPalette(
c(
"darkgreen", "forestgreen", "chartreuse", "khaki", "yellow", "peru",
"sienna4"
)
)
colors_slope <- colorRampPalette(colors_slope)
# ---- load index ------------
index <- read.csv(
#file.choose(),
index_input_path,
sep = ";"
)
# ---- start loop for figures ------------
# Write out info about the processing that is started
if(create_figures) writeLines("Creating figures")
if(calculate_metrics) writeLines("Calculating metrics")
writeLines(
ifelse(
plateau_detection,
"including plateau detection",
"without plateau detection"
)
)
# create empty cols in the index table for the metrics computation, if set
if(calculate_metrics) {
index[, c(
"m_area_absolute_glacier",
"m_elevation_min",
"m_elevation_max",
"m_elevation_range",
"m_elevation_mean",
"m_elevation_sd",
"m_ela_calculated",
"m_glacier_skewness"
)] <- NA
}
# loop through glaciers listed in the index table
pb <- txtProgressBar(0, nrow(index), style = 3) # progress bar
for (i in seq(nrow(index))) {
##### ToDo: include annulation/reset here, in case one glacier doesnt work
temp_ras <- stack(index$raster_path[i])
names(temp_ras) <- c("elev_absolute", "elev_relative", "slope", "aspect")
temp_ras_data <- na.omit(as.data.frame(temp_ras))
colnames(temp_ras_data) <- c("elev_absolute", "elev_relative", "slope",
"aspect")
# calculate ELA height from defined assumption
ela_calculated <- minValue(temp_ras[["elev_absolute"]]) +
(maxValue(temp_ras[["elev_absolute"]]) -
minValue(temp_ras[["elev_absolute"]])) * ela_assumed
# will create figures for each glacier
if(create_figures) source("analysis_figures.R")
# runs the plateau detection
if(plateau_detection) source("analysis_plateaus.R")
# will create metrics for each glacier
if(calculate_metrics) source
setTxtProgressBar(pb, i)
}
writeLines("\n") # more space
# save metrics as a table
if(calculate_metrics) {
write.table(
index,
file = index_metrics_output_file,
sep = ";",
row.names = F
)
}
# ---- analyze metrics ------------
if(calculate_metrics){
# create long format
index_longformat <- index %>%
select(-name, -comment, -RGI_alias, -raster_path) %>%
gather(., key = "parameter", value = "value", -RGI_ID, -type)
png(
filename = metrics_histogram_figure_path,
width = 1920,
height = 1200,
units = "px",
res = 120
)
print(
ggplot(index_longformat) +
geom_histogram(aes(x = value, fill = type)) +
facet_wrap(~ parameter, scales = "free"))
dev.off()
}