Project

General

Profile

Download (7.3 KB) Statistics
| Branch: | Revision:
1 4df82d84 Jim Regetz
# 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 7cf747b0 Jim Regetz
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 4df82d84 Jim Regetz
21
# extract raster latitudes for later
22 7cf747b0 Jim Regetz
lats300 <- yFromRow(a.aster, 1:nrow(a.aster))
23
lats150 <- yFromRow(a.srtm, 1:nrow(a.srtm))
24 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, rowMeans(as.matrix(a.can), na.rm=TRUE), type="l",
38 4df82d84 Jim Regetz
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
39 7cf747b0 Jim Regetz
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 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, rowMeans(as.matrix(a.uncor), na.rm=TRUE), type="l",
49 4df82d84 Jim Regetz
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
50 7cf747b0 Jim Regetz
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="simple fused")
51 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
52
53 7cf747b0 Jim Regetz
plot(lats300, rowMeans(as.matrix(a.enblend), na.rm=TRUE), type="l",
54 4df82d84 Jim Regetz
    xlab="Latitude", ylab="Mean aspect", ylim=ylim)
55 7cf747b0 Jim Regetz
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="multires spline")
56 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
57
58 7cf747b0 Jim Regetz
plot(lats300, rowMeans(as.matrix(a.bg), na.rm=TRUE), type="l",
59 4df82d84 Jim Regetz
    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 b02e39a0 Jim Regetz
# 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 4df82d84 Jim Regetz
}
77
78
par(mfrow=c(2,3), omi=c(1,1,1,1))
79
80 b02e39a0 Jim Regetz
ylim <- c(0, 100)
81 4df82d84 Jim Regetz
82
# ...with respect to ASTER
83 7cf747b0 Jim Regetz
plot(lats300, rmse(a.uncor, a.aster), type="l", xlab="Latitude",
84 4df82d84 Jim Regetz
    ylab="RMSE", ylim=ylim)
85 7cf747b0 Jim Regetz
lines(lats150, rmse(crop(a.uncor, extent(a.srtm)), a.srtm), col="blue")
86 4df82d84 Jim Regetz
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
87
    lty=c(1, 1), bty="n")
88 7cf747b0 Jim Regetz
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="simple fused")
89 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, rmse(a.enblend, a.aster), type="l", xlab="Latitude",
95 4df82d84 Jim Regetz
    ylab="RMSE", ylim=ylim)
96 7cf747b0 Jim Regetz
lines(lats150, rmse(crop(a.enblend, extent(a.srtm)), a.srtm), col="blue")
97 4df82d84 Jim Regetz
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
98
    lty=c(1, 1), bty="n")
99 7cf747b0 Jim Regetz
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="multires spline")
100 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
101
102 7cf747b0 Jim Regetz
plot(lats300, rmse(a.bg, a.aster), type="l", xlab="Latitude",
103 4df82d84 Jim Regetz
    ylab="RMSE", ylim=ylim)
104 7cf747b0 Jim Regetz
lines(lats150, rmse(crop(a.bg, extent(a.srtm)), a.srtm), col="blue")
105 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, rmse(a.uncor, a.can), type="l", xlab="Latitude",
112 4df82d84 Jim Regetz
    ylab="RMSE", ylim=ylim)
113 7cf747b0 Jim Regetz
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="simple fused")
114 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, rmse(a.enblend, a.can), type="l", xlab="Latitude",
120 4df82d84 Jim Regetz
    ylab="RMSE", ylim=ylim)
121 7cf747b0 Jim Regetz
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="multires spline")
122 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
123
124 7cf747b0 Jim Regetz
plot(lats300, rmse(a.bg, a.can), type="l", xlab="Latitude",
125 4df82d84 Jim Regetz
    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 b02e39a0 Jim Regetz
# close pdf device driver
130
dev.off()
131
132
stop("not doing correlations")
133 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.uncor, a.aster), type="l", xlab="Latitude",
155 4df82d84 Jim Regetz
    ylab="Correlation", ylim=ylim)
156 7cf747b0 Jim Regetz
lines(lats150, corByLat(crop(a.uncor, extent(a.srtm)), a.srtm), col="blue")
157 4df82d84 Jim Regetz
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
158
    lty=c(1, 1), bty="n")
159 7cf747b0 Jim Regetz
text(min(lats300), min(ylim), pos=4, font=3, labels="simple fused")
160 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.enblend, a.aster), type="l", xlab="Latitude",
166 4df82d84 Jim Regetz
    ylab="Correlation", ylim=ylim)
167 7cf747b0 Jim Regetz
lines(lats150, corByLat(crop(a.enblend, extent(a.srtm)), a.srtm), col="blue")
168 4df82d84 Jim Regetz
legend("bottomright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
169
    lty=c(1, 1), bty="n")
170 7cf747b0 Jim Regetz
text(min(lats300), min(ylim), pos=4, font=3, labels="multires spline")
171 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
172
173 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.bg, a.aster), type="l", xlab="Latitude",
174 4df82d84 Jim Regetz
    ylab="Correlation", ylim=ylim)
175 7cf747b0 Jim Regetz
lines(lats150, corByLat(crop(a.bg, extent(a.srtm)), a.srtm), col="blue")
176 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.uncor, a.can), type="l", xlab="Latitude",
183 4df82d84 Jim Regetz
    ylab="Correlation", ylim=ylim)
184 7cf747b0 Jim Regetz
text(min(lats300), min(ylim), pos=4, font=3, labels="simple fused")
185 4df82d84 Jim Regetz
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 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.enblend, a.can), type="l", xlab="Latitude",
191 4df82d84 Jim Regetz
    ylab="Correlation", ylim=ylim)
192 7cf747b0 Jim Regetz
text(min(lats300), min(ylim), pos=4, font=3, labels="multires spline")
193 4df82d84 Jim Regetz
abline(v=60, col="red", lty=2)
194
195 7cf747b0 Jim Regetz
plot(lats300, corByLat(a.bg, a.can), type="l", xlab="Latitude",
196 4df82d84 Jim Regetz
    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)