1
|
# R code to plot latitudinal profiles of mean slope, along with both
|
2
|
# RMSE and correlation coefficients comparing fused layers with both the
|
3
|
# raw ASTER and with the Canada DEM
|
4
|
#
|
5
|
# Jim Regetz
|
6
|
# NCEAS
|
7
|
# Created on 08-Jun-2011
|
8
|
|
9
|
library(raster)
|
10
|
|
11
|
datadir <- "/home/regetz/media/temp/terrain/slope"
|
12
|
|
13
|
# load slope rasters
|
14
|
s.aster <- raster(file.path(datadir, "aster_300straddle_s.tif"))
|
15
|
s.srtm <- raster(file.path(datadir, "srtm_150below_s.tif"))
|
16
|
s.uncor <- raster(file.path(datadir, "fused_300straddle_s.tif"))
|
17
|
s.eramp <- raster(file.path(datadir, "fused_300straddle_rampexp_s.tif"))
|
18
|
s.bg <- raster(file.path(datadir, "fused_300straddle_blendgau_s.tif"))
|
19
|
s.can <- raster(file.path(datadir, "cdem_300straddle_s.tif"))
|
20
|
|
21
|
# extract raster latitudes for later
|
22
|
lats300 <- yFromRow(s.aster, 1:nrow(s.aster))
|
23
|
lats150 <- yFromRow(s.srtm, 1:nrow(s.srtm))
|
24
|
|
25
|
# initialize output pdf device driver
|
26
|
pdf("slope-assessment.pdf", height=8, width=11.5)
|
27
|
|
28
|
|
29
|
#
|
30
|
# plot latitudinal profiles of mean slope
|
31
|
#
|
32
|
|
33
|
par(mfrow=c(2,2), omi=c(1,1,1,1))
|
34
|
ylim <- c(1, 6)
|
35
|
|
36
|
plot(lats300, rowMeans(as.matrix(s.uncor), na.rm=TRUE), type="l",
|
37
|
xlab="Latitude", ylab="Mean slope", ylim=ylim)
|
38
|
text(min(lats300), max(ylim)-0.5, pos=4, font=3, labels="uncorrected")
|
39
|
abline(v=60, col="red", lty=2)
|
40
|
mtext(expression(paste("Latitudinal profiles of mean slope (",
|
41
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
42
|
|
43
|
plot(lats300, rowMeans(as.matrix(s.can), na.rm=TRUE), type="l",
|
44
|
xlab="Latitude", ylab="Mean slope", ylim=ylim)
|
45
|
text(min(lats300), max(ylim)-0.5, pos=4, font=3, labels="Canada DEM")
|
46
|
abline(v=60, col="red", lty=2)
|
47
|
|
48
|
plot(lats300, rowMeans(as.matrix(s.eramp), na.rm=TRUE), type="l",
|
49
|
xlab="Latitude", ylab="Mean slope", ylim=ylim)
|
50
|
text(min(lats300), max(ylim)-0.5, pos=4, font=3, labels="exponential ramp")
|
51
|
abline(v=60, col="red", lty=2)
|
52
|
|
53
|
plot(lats300, rowMeans(as.matrix(s.bg), na.rm=TRUE), type="l",
|
54
|
xlab="Latitude", ylab="Mean slope", ylim=ylim)
|
55
|
text(min(lats300), max(ylim)-0.5, pos=4, font=3, labels="gaussian blend")
|
56
|
abline(v=60, col="red", lty=2)
|
57
|
|
58
|
|
59
|
#
|
60
|
# plot latitudinal profiles of RMSE
|
61
|
#
|
62
|
|
63
|
# simple helper function to calculate row-wise RMSEs
|
64
|
rmse <- function(r1, r2, na.rm=TRUE) {
|
65
|
sqrt(rowMeans(as.matrix((r1 - r2)^2), na.rm=na.rm))
|
66
|
}
|
67
|
|
68
|
par(mfrow=c(2,3), omi=c(1,1,1,1))
|
69
|
|
70
|
# ...with respect to ASTER
|
71
|
plot(lats300, rmse(s.uncor, s.aster), type="l", xlab="Latitude",
|
72
|
ylab="RMSE", ylim=c(0, 5))
|
73
|
lines(lats150, rmse(crop(s.uncor, extent(s.srtm)), s.srtm), col="blue")
|
74
|
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
75
|
lty=c(1, 1), bty="n")
|
76
|
text(min(lats300), 4.5, pos=4, font=3, labels="uncorrected")
|
77
|
abline(v=60, col="red", lty=2)
|
78
|
mtext(expression(paste(
|
79
|
"Slope discrepancies with respect to separate ASTER/SRTM components (",
|
80
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
81
|
|
82
|
plot(lats300, rmse(s.eramp, s.aster), type="l", xlab="Latitude",
|
83
|
ylab="RMSE", ylim=c(0, 5))
|
84
|
lines(lats150, rmse(crop(s.eramp, extent(s.srtm)), s.srtm), col="blue")
|
85
|
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
86
|
lty=c(1, 1), bty="n")
|
87
|
text(min(lats300), 4.5, pos=4, font=3, labels="exponential ramp")
|
88
|
abline(v=60, col="red", lty=2)
|
89
|
|
90
|
plot(lats300, rmse(s.bg, s.aster), type="l", xlab="Latitude",
|
91
|
ylab="RMSE", ylim=c(0, 5))
|
92
|
lines(lats150, rmse(crop(s.bg, extent(s.srtm)), s.srtm), col="blue")
|
93
|
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
94
|
lty=c(1, 1), bty="n")
|
95
|
text(min(lats300), 4.5, pos=4, font=3, labels="gaussian blend")
|
96
|
abline(v=60, col="red", lty=2)
|
97
|
|
98
|
# ...with respect to CDEM
|
99
|
plot(lats300, rmse(s.uncor, s.can), type="l", xlab="Latitude",
|
100
|
ylab="RMSE", ylim=c(0, 5))
|
101
|
text(min(lats300), 4.5, pos=4, font=3, labels="uncorrected")
|
102
|
abline(v=60, col="red", lty=2)
|
103
|
mtext(expression(paste(
|
104
|
"Slope discrepancies with respect to Canada DEM (",
|
105
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
106
|
|
107
|
plot(lats300, rmse(s.eramp, s.can), type="l", xlab="Latitude",
|
108
|
ylab="RMSE", ylim=c(0, 5))
|
109
|
text(min(lats300), 4.5, pos=4, font=3, labels="exponential ramp")
|
110
|
abline(v=60, col="red", lty=2)
|
111
|
|
112
|
plot(lats300, rmse(s.bg, s.can), type="l", xlab="Latitude",
|
113
|
ylab="RMSE", ylim=c(0, 5))
|
114
|
text(min(lats300), 4.5, pos=4, font=3, labels="gaussian blend")
|
115
|
abline(v=60, col="red", lty=2)
|
116
|
|
117
|
|
118
|
#
|
119
|
# plot latitudinal profiles of correlation coefficients
|
120
|
#
|
121
|
|
122
|
# simple helper function to calculate row-wise correlation coefficients
|
123
|
corByLat <- function(r1, r2, rows) {
|
124
|
if (missing(rows)) {
|
125
|
rows <- 1:nrow(r1)
|
126
|
}
|
127
|
m1 <- as.matrix(r1)
|
128
|
m2 <- as.matrix(r2)
|
129
|
sapply(rows, function(row) cor(m1[row,], m2[row,],
|
130
|
use="pairwise.complete.obs"))
|
131
|
}
|
132
|
|
133
|
par(mfrow=c(2,3), omi=c(1,1,1,1))
|
134
|
|
135
|
ylim <- c(0.65, 1)
|
136
|
|
137
|
# ...with respect to ASTER
|
138
|
plot(lats300, corByLat(s.uncor, s.aster), type="l", xlab="Latitude",
|
139
|
ylab="Correlation", ylim=ylim)
|
140
|
lines(lats150, corByLat(crop(s.uncor, extent(s.srtm)), s.srtm), col="blue")
|
141
|
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
142
|
lty=c(1, 1), bty="n")
|
143
|
text(min(lats300), min(ylim), pos=4, font=3, labels="uncorrected")
|
144
|
abline(v=60, col="red", lty=2)
|
145
|
mtext(expression(paste(
|
146
|
"Slope correlations with respect to separate ASTER/SRTM components (",
|
147
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
148
|
|
149
|
plot(lats300, corByLat(s.eramp, s.aster), type="l", xlab="Latitude",
|
150
|
ylab="Correlation", ylim=ylim)
|
151
|
lines(lats150, corByLat(crop(s.eramp, extent(s.srtm)), s.srtm), col="blue")
|
152
|
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
153
|
lty=c(1, 1), bty="n")
|
154
|
text(min(lats300), min(ylim), pos=4, font=3, labels="exponential ramp")
|
155
|
abline(v=60, col="red", lty=2)
|
156
|
|
157
|
plot(lats300, corByLat(s.bg, s.aster), type="l", xlab="Latitude",
|
158
|
ylab="Correlation", ylim=ylim)
|
159
|
lines(lats150, corByLat(crop(s.bg, extent(s.srtm)), s.srtm), col="blue")
|
160
|
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
161
|
lty=c(1, 1), bty="n")
|
162
|
text(min(lats300), min(ylim), pos=4, font=3, labels="gaussian blend")
|
163
|
abline(v=60, col="red", lty=2)
|
164
|
|
165
|
# ...with respect to CDEM
|
166
|
plot(lats300, corByLat(s.uncor, s.can), type="l", xlab="Latitude",
|
167
|
ylab="Correlation", ylim=ylim)
|
168
|
text(min(lats300), min(ylim), pos=4, font=3, labels="uncorrected")
|
169
|
abline(v=60, col="red", lty=2)
|
170
|
mtext(expression(paste(
|
171
|
"Slope correlations with respect to Canada DEM (",
|
172
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
173
|
|
174
|
plot(lats300, corByLat(s.eramp, s.can), type="l", xlab="Latitude",
|
175
|
ylab="Correlation", ylim=ylim)
|
176
|
text(min(lats300), min(ylim), pos=4, font=3, labels="exponential ramp")
|
177
|
abline(v=60, col="red", lty=2)
|
178
|
|
179
|
plot(lats300, corByLat(s.bg, s.can), type="l", xlab="Latitude",
|
180
|
ylab="Correlation", ylim=ylim)
|
181
|
text(min(lats300), min(ylim), pos=4, font=3, labels="gaussian blend")
|
182
|
abline(v=60, col="red", lty=2)
|
183
|
|
184
|
# close pdf device driver
|
185
|
dev.off()
|