Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Revision:
1
# R script for calculating row-wise RMSEs and row-wise correlation coefficients.
2
#
3
# The results (plots) are saved at 
4
#   "/data/project/organisms/DEM?Yuni/documents/rmse_cor"
5
#
6
#
7
# Original author: Jim Regetz 
8
# [13-JUL-2011]
9
#	Edits by Yuni to apply ASTER GDEM2, instead of GDEM1, as well as auto-applying 
10
#	for each nine region.  [9-Dec-2011] 
11
# 
12

    
13

    
14

    
15
# simple helper function to calculate row-wise RMSEs
16
rmse <- function(r1, r2, na.rm=TRUE, use) {
17
    diffs <- abs(as.matrix(r1) - as.matrix(r2))
18
    if (!missing(use)) diffs[!use] <- NA
19
    sqrt(rowMeans(diffs^2, na.rm=na.rm))
20
}
21

    
22

    
23
# simple helper function to calculate row-wise correlation coefficients
24
corByLat <- function(r1, r2, rows) {
25
    if (missing(rows)) {
26
        rows <- 1:nrow(r1)
27
    }
28
    m1 <- as.matrix(r1)
29
    m2 <- as.matrix(r2)
30
    sapply(rows, function(row) cor(m1[row,], m2[row,],
31
    use="pairwise.complete.obs"))
32
}
33

    
34

    
35

    
36
library(raster)
37
aster.dir      <- "/data/project/organisms/DEM/Yuni/Data/aster2"
38
srtm.dir       <- "/data/project/organisms/DEM/Yuni/Data/srtm"
39
aster.straddle <- list.files(aster.dir, pattern="^aster2.*_straddle_s.tif")
40
srtm.below     <- list.files(srtm.dir, pattern="^srtm_.*_below_s.tif") 
41
fused.straddle <- list.files(aster.dir, pattern="^fused.*_straddle_s.tif")
42
fused.bgau     <- list.files(aster.dir, pattern="^fused.*_blendgau_s.tif")
43

    
44

    
45

    
46
rmse.corByLat <- function(d.aster2, d.srtm, d.uncor2, d.bg2, file.name,  aster.dir, srtm.dir){
47

    
48
    # extract raster latitudes for later
49
    lats2400 <- yFromRow(d.aster2, 1:nrow(d.aster2))
50
    lats1200 <- yFromRow(d.srtm, 1:nrow(d.srtm))
51

    
52
    # initialize output pdf device driver
53
    name <- paste("rmse.cor_", file.name, ".pdf", sep="") 
54
    pdf(file.path("/data/project/organisms/DEM/Yuni/documents/rmse_cor", file=name), height=8, width=11.5)
55

    
56
    par(mfrow=c(2,2), omi=c(1,1,1,1))
57
    ylim <- c(0,30)
58
 
59
    # plot rmse
60
    par(mfrow=c(2,3), omi=c(1,1,1,1))
61
    ylim <- ylim
62

    
63
    # with uncorrected fusion 
64
    plot(lats2400, rmse(d.uncor2, d.aster2), type="l", xlab="Latitude", ylab="RMSE", ylim=ylim)
65
    lines(lats1200, rmse(crop(d.uncor2, extent(d.srtm)), d.srtm), col="blue")
66
    legend("topright", legend=c("ASTER2", "SRTM"), col=c("black", "blue"), lty=c(1, 1), bty="n")
67
    text(min(lats2400), max(ylim)-1, pos=4, font=3, labels="simple fuse")
68
    abline(v=60, col="red", lty=2)
69
    mtext(expression(paste("Elevation discrepancies with respect to separate ASTER2/SRTM components", file.name)), adj=0, line=2, font=2)
70

    
71
    # with gaussian fusion 
72
    plot(lats2400, rmse(d.bg2, d.aster2), type="l", xlab="Latitude", ylab="RMSE", ylim=ylim)
73
    lines(lats1200, rmse(crop(d.bg2, extent(d.srtm)), d.srtm), col="blue")
74
    legend("topright", legend=c("ASTER2", "SRTM"), col=c("black", "blue"), lty=c(1, 1),
75
        bty="n")
76
    text(min(lats2400), max(ylim)-1, pos=4, font=3, labels="gaussian blend")
77
    abline(v=60, col="red", lty=2)
78

    
79

    
80
    # plot latitudinal profiles of correlation coefficients
81
    par(mfrow=c(2,3), omi=c(1,1,1,1))
82
    ylim <- c(0.99, 1) 
83

    
84
    plot(lats2400, corByLat(d.uncor2, d.aster2), type="l", xlab="Latitude",
85
    ylab="Correlation", ylim=ylim)
86
    lines(lats1200, corByLat(crop(d.uncor2, extent(d.srtm)), d.srtm), col="blue")
87
    legend("bottomright", legend=c("ASTER2", "SRTM"), col=c("black", "blue"),
88
    lty=c(1, 1), bty="n")
89
    text(min(lats2400), min(ylim), pos=4, font=3, labels="simple fuse")
90
    abline(v=60, col="red", lty=2)
91
    mtext(expression(paste("Elevation correlations with respect to separate ASTER2/SRTM components", file.name)), adj=0, line=2, font=2)
92

    
93

    
94
    plot(lats2400, corByLat(d.bg2, d.aster2), type="l", xlab="Latitude", 
95
        ylab="Correlation", ylim=ylim)
96
    lines(lats1200, corByLat(crop(d.bg2, extent(d.srtm)), d.srtm), col="blue")
97
    legend("bottomright", legend=c("ASTER2", "SRTM"), col=c("black", "blue"),
98
        lty=c(1, 1), bty="n")
99
    text(min(lats2400), min(ylim), pos=4, font=3, labels="gaussian blend")
100
    abline(v=60, col="red", lty=2)
101

    
102
   dev.off () 
103
}
104

    
105

    
106

    
107

    
108

    
109
num.regions <- 9
110
plot.rmse.corByLat <-lapply(1:num.regions, function(region, aster.dir, srtm.dir){
111
    d.aster <- raster(file.path(aster.dir, aster.straddle[region])) 
112
    d.srtm <- raster(file.path(srtm.dir, srtm.below[region]))  
113
    d.uncor <- raster(file.path(aster.dir, fused.straddle[region]))
114
    d.bg <- raster(file.path(aster.dir, fused.bgau[region])) 
115
    file.name <- substr(srtm.below[region], 6, 13)
116

    
117
   rmse.corByLat(d.aster, d.srtm, d.uncor, d.bg, file.name, aster.dir, srtm.dir)
118
}, aster.dir=aster.dir, srtm.dir=srtm.dir)
119

    
(21-21/23)