1
|
# R code to plot latitudinal profiles of mean flow direction, along with
|
2
|
# both RMSE and correlation coefficients comparing fused layers with
|
3
|
# both the 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/flow"
|
12
|
|
13
|
# create function to recode values into degrees
|
14
|
recode <- function(r) {
|
15
|
v <- values(r)
|
16
|
v[v==0] <- NA
|
17
|
v[v==1] <- 0
|
18
|
v[v==2] <- 45
|
19
|
v[v==3] <- 90
|
20
|
v[v==4] <- 90
|
21
|
v[v==8] <- 135
|
22
|
v[v==16] <- 180
|
23
|
v[v==32] <- 225
|
24
|
v[v==64] <- 270
|
25
|
v[v==128] <- 315
|
26
|
r[] <- v
|
27
|
return(r)
|
28
|
}
|
29
|
|
30
|
# load flow direction rasters, recoding on the fly
|
31
|
sfd.aster <- recode(raster(file.path(datadir, "aster_300straddle_sfd.tif")))
|
32
|
sfd.srtm <- recode(raster(file.path(datadir, "srtm_150below_sfd.tif")))
|
33
|
sfd.uncor <- recode(raster(file.path(datadir, "fused_300straddle_sfd.tif")))
|
34
|
#sfd.eramp <- recode(raster(file.path(datadir, "fused_300straddle_rampexp_sfd.tif")))
|
35
|
sfd.bg <- recode(raster(file.path(datadir, "fused_300straddle_blendgau_sfd.tif")))
|
36
|
sfd.can <- recode(raster(file.path(datadir, "cdem_300straddle_sfd.tif")))
|
37
|
|
38
|
# extract raster latitudes for later
|
39
|
lats300 <- yFromRow(sfd.aster, 1:nrow(sfd.aster))
|
40
|
lats150 <- yFromRow(sfd.srtm, 1:nrow(sfd.srtm))
|
41
|
|
42
|
# initialize output pdf device driver
|
43
|
pdf("flowdir-assessment.pdf", height=8, width=11.5)
|
44
|
|
45
|
#
|
46
|
# plot latitudinal profiles of mean flow direction
|
47
|
#
|
48
|
|
49
|
par(mfrow=c(2,2), omi=c(1,1,1,1))
|
50
|
|
51
|
ylim <- c(80, 280)
|
52
|
|
53
|
plot(lats300, rowMeans(as.matrix(sfd.uncor), na.rm=TRUE), type="l",
|
54
|
xlab="Latitude", ylab="Mean flow direction", ylim=ylim)
|
55
|
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="uncorrected")
|
56
|
abline(v=60, col="red", lty=2)
|
57
|
mtext(expression(paste("Latitudinal profiles of mean flow direction (",
|
58
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
59
|
|
60
|
plot(lats300, rowMeans(as.matrix(sfd.can), na.rm=TRUE), type="l",
|
61
|
xlab="Latitude", ylab="Mean flow direction", ylim=ylim)
|
62
|
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="Canada DEM")
|
63
|
abline(v=60, col="red", lty=2)
|
64
|
|
65
|
#plot(lats300, rowMeans(as.matrix(sfd.eramp), na.rm=TRUE), type="l",
|
66
|
plot(lats300, rowMeans(as.matrix(sfd.can), na.rm=TRUE), type="n",
|
67
|
xlab="Latitude", ylab="Mean flow direction", ylim=ylim)
|
68
|
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="exponential ramp")
|
69
|
text(mean(lats300), mean(ylim), pos=1, font=3, labels="(skipped)")
|
70
|
#abline(v=60, col="red", lty=2)
|
71
|
|
72
|
plot(lats300, rowMeans(as.matrix(sfd.bg), na.rm=TRUE), type="l",
|
73
|
xlab="Latitude", ylab="Mean flow direction", ylim=ylim)
|
74
|
text(min(lats300), min(ylim)+0.5, pos=4, font=3, labels="gaussian blend")
|
75
|
abline(v=60, col="red", lty=2)
|
76
|
|
77
|
|
78
|
#
|
79
|
# plot latitudinal profiles of RMSE
|
80
|
#
|
81
|
|
82
|
# simple helper function to calculate row-wise RMSEs, accounting for the
|
83
|
# fact that flow dir values are circular (0-360), so the difference
|
84
|
# between e.g. 5 and 355 should only be 10
|
85
|
rmse <- function(r1, r2, na.rm=TRUE, use) {
|
86
|
diffs <- abs(as.matrix(r1) - as.matrix(r2))
|
87
|
if (!missing(use)) diffs[!use] <- NA
|
88
|
diffs[] <- ifelse(diffs>180, 360-diffs, diffs)
|
89
|
sqrt(rowMeans(diffs^2, na.rm=na.rm))
|
90
|
}
|
91
|
|
92
|
par(mfrow=c(2,3), omi=c(1,1,1,1))
|
93
|
|
94
|
ylim <- c(0, 100)
|
95
|
|
96
|
# ...with respect to ASTER
|
97
|
plot(lats300, rmse(sfd.uncor, sfd.aster), type="l", xlab="Latitude",
|
98
|
ylab="RMSE", ylim=ylim)
|
99
|
lines(lats150, rmse(crop(sfd.uncor, extent(sfd.srtm)), sfd.srtm), col="blue")
|
100
|
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
101
|
lty=c(1, 1), bty="n")
|
102
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="uncorrected")
|
103
|
abline(v=60, col="red", lty=2)
|
104
|
mtext(expression(paste(
|
105
|
"Flowdir discrepancies with respect to separate ASTER/SRTM components (",
|
106
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
107
|
|
108
|
#plot(lats300, rmse(sfd.eramp, sfd.aster), type="l", xlab="Latitude",
|
109
|
plot(lats300, rep(0, 300), type="n", xlab="Latitude",
|
110
|
ylab="RMSE", ylim=ylim)
|
111
|
#lines(lats150, rmse(crop(sfd.eramp, extent(sfd.srtm)), sfd.srtm), col="blue")
|
112
|
#legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
113
|
# lty=c(1, 1), bty="n")
|
114
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="exponential ramp")
|
115
|
text(mean(lats300), mean(ylim), font=3, labels="(skipped)")
|
116
|
#abline(v=60, col="red", lty=2)
|
117
|
|
118
|
plot(lats300, rmse(sfd.bg, sfd.aster), type="l", xlab="Latitude",
|
119
|
ylab="RMSE", ylim=ylim)
|
120
|
lines(lats150, rmse(crop(sfd.bg, extent(sfd.srtm)), sfd.srtm), col="blue")
|
121
|
legend("topright", legend=c("ASTER", "SRTM"), col=c("black", "blue"),
|
122
|
lty=c(1, 1), bty="n")
|
123
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="gaussian blend")
|
124
|
abline(v=60, col="red", lty=2)
|
125
|
|
126
|
# ...with respect to CDEM
|
127
|
plot(lats300, rmse(sfd.uncor, sfd.can), type="l", xlab="Latitude",
|
128
|
ylab="RMSE", ylim=ylim)
|
129
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="uncorrected")
|
130
|
abline(v=60, col="red", lty=2)
|
131
|
mtext(expression(paste(
|
132
|
"Flowdir discrepancies with respect to Canada DEM (",
|
133
|
136*degree, "W to ", 96*degree, "W)")), adj=0, line=2, font=2)
|
134
|
|
135
|
#plot(lats300, rmse(sfd.eramp, sfd.can), type="l", xlab="Latitude",
|
136
|
plot(lats300, rep(0, 300), type="n", xlab="Latitude",
|
137
|
ylab="RMSE", ylim=ylim)
|
138
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="exponential ramp")
|
139
|
text(mean(lats300), mean(ylim), font=3, labels="(skipped)")
|
140
|
#abline(v=60, col="red", lty=2)
|
141
|
|
142
|
plot(lats300, rmse(sfd.bg, sfd.can), type="l", xlab="Latitude",
|
143
|
ylab="RMSE", ylim=ylim)
|
144
|
text(min(lats300), max(ylim)-5, pos=4, font=3, labels="gaussian blend")
|
145
|
abline(v=60, col="red", lty=2)
|
146
|
|
147
|
# close pdf device driver
|
148
|
dev.off()
|