Project

General

Profile

Download (7.04 KB) Statistics
| Branch: | Revision:
1
# R code to plot latitudinal profiles of mean aspect, 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/aspect"
12

    
13
# load aspect rasters
14
s.aster <- raster(file.path(datadir, "aster_300straddle_a.tif"))
15
s.srtm <- raster(file.path(datadir, "srtm_150below_a.tif"))
16
s.uncor <- raster(file.path(datadir, "fused_300straddle_a.tif"))
17
s.eramp <- raster(file.path(datadir, "fused_300straddle_rampexp_a.tif"))
18
s.bg <- raster(file.path(datadir, "fused_300straddle_blendgau_a.tif"))
19
s.can <- raster(file.path(datadir, "cdem_300straddle_a.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("aspect-assessment.pdf", height=8, width=11.5)
27

    
28

    
29
#
30
# plot latitudinal profiles of mean aspect
31
#
32

    
33
par(mfrow=c(2,2), omi=c(1,1,1,1))
34

    
35
ylim <- c(160, 180)
36

    
37
plot(lats300, rowMeans(as.matrix(s.uncor), na.rm=TRUE), type="l",
38
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
39
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="uncorrected")
40
abline(v=60, col="red", lty=2)
41
mtext(expression(paste("Latitudinal profiles of mean aspect (",
42
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
43

    
44
plot(lats300, rowMeans(as.matrix(s.can), na.rm=TRUE), type="l",
45
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
46
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="Canada DEM")
47
abline(v=60, col="red", lty=2)
48

    
49
plot(lats300, rowMeans(as.matrix(s.eramp), na.rm=TRUE), type="l",
50
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
51
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="exponential ramp")
52
abline(v=60, col="red", lty=2)
53

    
54
plot(lats300, rowMeans(as.matrix(s.bg), na.rm=TRUE), type="l",
55
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
56
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="gaussian blend")
57
abline(v=60, col="red", lty=2)
58

    
59

    
60
#
61
# plot latitudinal profiles of RMSE
62
#
63

    
64
# simple helper function to calculate row-wise RMSEs, accounting for the
65
# fact that aspect values are circular (0-360), so the difference
66
# between e.g. 5 and 355 should only be 10
67
rmse <- function(r1, r2, na.rm=TRUE, use) {
68
    diffs <- abs(as.matrix(r1) - as.matrix(r2))
69
    if (!missing(use)) diffs[!use] <- NA
70
    diffs[] <- ifelse(diffs>180, 360-diffs, diffs)
71
    sqrt(rowMeans(diffs^2, na.rm=na.rm))
72
}
73

    
74
par(mfrow=c(2,3), omi=c(1,1,1,1))
75

    
76
ylim <- c(0, 100)
77

    
78
# ...with respect to ASTER
79
plot(lats300, rmse(s.uncor, s.aster), type="l", xlab="Latitude",
80
    ylab="RMSE", ylim=ylim)
81
lines(lats150, rmse(crop(s.uncor, extent(s.srtm)), s.srtm), col="blue")
82
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
83
    lty=c(1, 1), bty="n")
84
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="uncorrected")
85
abline(v=60, col="red", lty=2)
86
mtext(expression(paste(
87
    "Aspect discrepancies with respect to separate ASTER/SRTM components (",
88
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
89

    
90
plot(lats300, rmse(s.eramp, s.aster), type="l", xlab="Latitude",
91
    ylab="RMSE", ylim=ylim)
92
lines(lats150, rmse(crop(s.eramp, 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), max(ylim)-5, pos=4, font=3, labels="exponential ramp")
96
abline(v=60, col="red", lty=2)
97

    
98
plot(lats300, rmse(s.bg, s.aster), type="l", xlab="Latitude",
99
    ylab="RMSE", ylim=ylim)
100
lines(lats150, rmse(crop(s.bg, extent(s.srtm)), s.srtm), col="blue")
101
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
102
    lty=c(1, 1), bty="n")
103
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="gaussian blend")
104
abline(v=60, col="red", lty=2)
105

    
106
# ...with respect to CDEM
107
plot(lats300, rmse(s.uncor, s.can), type="l", xlab="Latitude",
108
    ylab="RMSE", ylim=ylim)
109
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="uncorrected")
110
abline(v=60, col="red", lty=2)
111
mtext(expression(paste(
112
    "Aspect discrepancies with respect to Canada DEM (",
113
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
114

    
115
plot(lats300, rmse(s.eramp, s.can), type="l", xlab="Latitude",
116
    ylab="RMSE", ylim=ylim)
117
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="exponential ramp")
118
abline(v=60, col="red", lty=2)
119

    
120
plot(lats300, rmse(s.bg, s.can), type="l", xlab="Latitude",
121
    ylab="RMSE", ylim=ylim)
122
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="gaussian blend")
123
abline(v=60, col="red", lty=2)
124

    
125
# close pdf device driver
126
dev.off()
127

    
128
stop("not doing correlations")
129

    
130
#
131
# plot latitudinal profiles of correlation coefficients
132
#
133

    
134
# simple helper function to calculate row-wise correlation coefficients
135
corByLat <- function(r1, r2, rows) {
136
    if (missing(rows)) {
137
        rows <- 1:nrow(r1)
138
    }
139
    m1 <- as.matrix(r1)
140
    m2 <- as.matrix(r2)
141
    sapply(rows, function(row) cor(m1[row,], m2[row,],
142
        use="pairwise.complete.obs"))
143
}
144

    
145
par(mfrow=c(2,3), omi=c(1,1,1,1))
146

    
147
ylim <- c(0, 1)
148

    
149
# ...with respect to ASTER
150
plot(lats300, corByLat(s.uncor, s.aster), type="l", xlab="Latitude",
151
    ylab="Correlation", ylim=ylim)
152
lines(lats150, corByLat(crop(s.uncor, extent(s.srtm)), s.srtm), col="blue")
153
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
154
    lty=c(1, 1), bty="n")
155
text(min(lats300), min(ylim), pos=4, font=3, labels="uncorrected")
156
abline(v=60, col="red", lty=2)
157
mtext(expression(paste(
158
    "Aspect correlations with respect to separate ASTER/SRTM components (",
159
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
160

    
161
plot(lats300, corByLat(s.eramp, s.aster), type="l", xlab="Latitude",
162
    ylab="Correlation", ylim=ylim)
163
lines(lats150, corByLat(crop(s.eramp, extent(s.srtm)), s.srtm), col="blue")
164
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
165
    lty=c(1, 1), bty="n")
166
text(min(lats300), min(ylim), pos=4, font=3, labels="exponential ramp")
167
abline(v=60, col="red", lty=2)
168

    
169
plot(lats300, corByLat(s.bg, s.aster), type="l", xlab="Latitude",
170
    ylab="Correlation", ylim=ylim)
171
lines(lats150, corByLat(crop(s.bg, extent(s.srtm)), s.srtm), col="blue")
172
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
173
    lty=c(1, 1), bty="n")
174
text(min(lats300), min(ylim), pos=4, font=3, labels="gaussian blend")
175
abline(v=60, col="red", lty=2)
176

    
177
# ...with respect to CDEM
178
plot(lats300, corByLat(s.uncor, s.can), type="l", xlab="Latitude",
179
    ylab="Correlation", ylim=ylim)
180
text(min(lats300), min(ylim), pos=4, font=3, labels="uncorrected")
181
abline(v=60, col="red", lty=2)
182
mtext(expression(paste(
183
    "Aspect correlations with respect to Canada DEM (",
184
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
185

    
186
plot(lats300, corByLat(s.eramp, s.can), type="l", xlab="Latitude",
187
    ylab="Correlation", ylim=ylim)
188
text(min(lats300), min(ylim), pos=4, font=3, labels="exponential ramp")
189
abline(v=60, col="red", lty=2)
190

    
191
plot(lats300, corByLat(s.bg, s.can), type="l", xlab="Latitude",
192
    ylab="Correlation", ylim=ylim)
193
text(min(lats300), min(ylim), pos=4, font=3, labels="gaussian blend")
194
abline(v=60, col="red", lty=2)
    (1-1/1)