Project

General

Profile

Download (7.3 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
a.aster <- raster(file.path(datadir, "aster_300straddle_a.tif"))
15
a.srtm <- raster(file.path(datadir, "srtm_150below_a.tif"))
16
a.uncor <- raster(file.path(datadir, "fused_300straddle_a.tif"))
17
a.enblend <- raster(file.path(datadir, "fused_300straddle_enblend_a.tif"))
18
a.bg <- raster(file.path(datadir, "fused_300straddle_blendgau_a.tif"))
19
a.can <- raster(file.path(datadir, "cdem_300straddle_a.tif"))
20

    
21
# extract raster latitudes for later
22
lats300 <- yFromRow(a.aster, 1:nrow(a.aster))
23
lats150 <- yFromRow(a.srtm, 1:nrow(a.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(a.can), 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="Original DEMs")
40
lines(lats300, rowMeans(as.matrix(a.aster), na.rm=TRUE), col="blue")
41
lines(lats150, rowMeans(as.matrix(a.srtm), na.rm=TRUE), col="red")
42
legend("bottomright", legend=c("ASTER", "SRTM", "CDED"), col=c("blue",
43
    "red", "black"), lty=c(1, 1), bty="n")
44
abline(v=60, col="red", lty=2)
45
mtext(expression(paste("Latitudinal profiles of mean aspect (",
46
    136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
47

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

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

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

    
63

    
64
#
65
# plot latitudinal profiles of RMSE
66
#
67

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

    
78
par(mfrow=c(2,3), omi=c(1,1,1,1))
79

    
80
ylim <- c(0, 100)
81

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

    
94
plot(lats300, rmse(a.enblend, a.aster), type="l", xlab="Latitude",
95
    ylab="RMSE", ylim=ylim)
96
lines(lats150, rmse(crop(a.enblend, extent(a.srtm)), a.srtm), col="blue")
97
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
98
    lty=c(1, 1), bty="n")
99
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="multires spline")
100
abline(v=60, col="red", lty=2)
101

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

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

    
119
plot(lats300, rmse(a.enblend, a.can), type="l", xlab="Latitude",
120
    ylab="RMSE", ylim=ylim)
121
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="multires spline")
122
abline(v=60, col="red", lty=2)
123

    
124
plot(lats300, rmse(a.bg, a.can), type="l", xlab="Latitude",
125
    ylab="RMSE", ylim=ylim)
126
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="gaussian blend")
127
abline(v=60, col="red", lty=2)
128

    
129
# close pdf device driver
130
dev.off()
131

    
132
stop("not doing correlations")
133

    
134
#
135
# plot latitudinal profiles of correlation coefficients
136
#
137

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

    
149
par(mfrow=c(2,3), omi=c(1,1,1,1))
150

    
151
ylim <- c(0, 1)
152

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

    
165
plot(lats300, corByLat(a.enblend, a.aster), type="l", xlab="Latitude",
166
    ylab="Correlation", ylim=ylim)
167
lines(lats150, corByLat(crop(a.enblend, extent(a.srtm)), a.srtm), col="blue")
168
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
169
    lty=c(1, 1), bty="n")
170
text(min(lats300), min(ylim), pos=4, font=3, labels="multires spline")
171
abline(v=60, col="red", lty=2)
172

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

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

    
190
plot(lats300, corByLat(a.enblend, a.can), type="l", xlab="Latitude",
191
    ylab="Correlation", ylim=ylim)
192
text(min(lats300), min(ylim), pos=4, font=3, labels="multires spline")
193
abline(v=60, col="red", lty=2)
194

    
195
plot(lats300, corByLat(a.bg, a.can), type="l", xlab="Latitude",
196
    ylab="Correlation", ylim=ylim)
197
text(min(lats300), min(ylim), pos=4, font=3, labels="gaussian blend")
198
abline(v=60, col="red", lty=2)
    (1-1/1)