Project

General

Profile

Download (4.14 KB) Statistics
| Branch: | Revision:
1
# R code to plot latitudinal profiles of (circular) mean aspect, along
2
# with both RMSE and (circular) correlation coefficients comparing fused
3
# layers with both the raw ASTER and with the Canada DEM
4
#
5
# Aspect layers were generated from the respect DEMs in this way:
6
#   $ gdaldem aspect -s 111120 <layer>.tif <layer>_a.tif
7
# ...where the default azimuthal behavior produces output values ranging
8
# from 0-360 where 0 is north, and proceeding clockwise
9
#
10
# For exploratory plotting, note the following (uses 'circular'
11
# package):
12
#  > cx <- circular(as.matrix(a.bg)[151,], units="degrees",
13
#      rotation="clock", zero=pi/2)
14
#  > rose.diag(cx, bins=8)
15
#  > points(mean.circular(cx, na.rm=TRUE), col="red")
16
#
17
#
18
# The results (plots) are saved at 
19
#   "/data/project/organisms/DEM?Yuni/documents/aspect"
20
#
21
# Original author: Jim Regetz
22
# [13-JUL-2011]
23
#   Edits by Yuina to auto-apply for nine regions.
24
#   
25
#   9-Dec-2011
26
#   Yuina Nunokawa
27

    
28

    
29

    
30

    
31

    
32
library(raster)
33
library(circular)
34

    
35

    
36
# load aspecct rasters
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_a.tif")
40
srtm.below     <- list.files(srtm.dir, pattern="^srtm_.*_below_a.tif") 
41
fused.straddle <- list.files(aster.dir, pattern="^fused.*_straddle_a.tif")
42
fused.bgau     <- list.files(aster.dir, pattern="^fused.*_blendgau_a.tif")
43

    
44

    
45

    
46
# simple helper function to calculate row-wise means using circular
47
# mean, patterned after circ.mean in the CircStats package
48
rowMeansC <- function(r1, na.rm=TRUE) {
49
    m1 <- as.matrix(r1)
50
    m1[] <- (m1 * pi)/180
51
    sinr <- rowSums(sin(m1), na.rm=na.rm)
52
    cosr <- rowSums(cos(m1), na.rm=na.rm)
53
    cmeans <- atan2(sinr, cosr)
54
    (cmeans * 180)/pi
55
}
56

    
57

    
58

    
59
aspect <- function(a.aster2, a.srtm, a.uncor, a.bg, file.name, aster.dir, srtm.dir){
60

    
61
        # extract raster latitudes for later
62
        lats2400 <- yFromRow(a.aster2, 1:nrow(a.aster2))
63
        lats1200 <- yFromRow(a.srtm, 1:nrow(a.srtm))
64

    
65
        # initialize output pdf device driver
66
        name <- paste("aspect_", file.name, ".pdf", sep="") 
67
        pdf(file.path("/data/project/organisms/DEM/Yuni/documents/aspect", file=name), height=8, width=11.5)
68

    
69
        #
70
        # plot latitudinal profiles of mean aspect
71
        #
72
       
73
        par(mfrow=c(2,2), omi=c(1,1,1,1))
74
        ylim <- c(-180, 180)
75

    
76

    
77
        plot(lats2400, rowMeansC(a.aster2), type="l", xlab="Latitude", ylab="Mean aspect", ylim=ylim)
78
        axis(2, at=c(-180, -90, 0, 90, 180), labels=c("S", "W", "N", "E", "S"))
79
        lines(lats1200, rowMeans(as.matrix(a.srtm), na.rm=TRUE), col="red")
80
        text(min(lats2400), max(ylim)+0.5, pos=4, font=3, labels="Original")
81
        legend("topright", legend=c("ASTER2", "SRTM"), col=c("blue", "red"), lty=c(1, 1), bty="n")
82
        abline(v=60, col="red", lty=2)
83
        mtext(expression(paste("Latitudinal profiles of mean aspect", file.name)), adj=0, line=2, font=2)
84

    
85
        plot(lats2400, rowMeansC(a.uncor), type="l", xlab="Latitude", ylab="Mean aspect", ylim=ylim)
86
        axis(2, at=c(-180, -90, 0, 90, 180), labels=c("S", "W", "N", "E", "S"))
87
        legend("topright", legend=c("ASTER2"), col=c("black"), lty=c(1, 1), bty="n")
88
        text(min(lats2400), max(ylim)+0.5, pos=4, font=3, labels="simple fused")
89
        abline(v=60, col="red", lty=2)
90

    
91
        plot(lats2400, rowMeansC(a.bg), type="l", xlab="Latitude", ylab="Mean aspect", ylim=ylim)
92
        legend("topright", legend=c("ASTER2"), col=c("black"), lty=c(1, 1), bty="n")
93
        text(min(lats2400), max(ylim)+0.5, pos=4, font=3, labels="gaussian blend")
94
        abline(v=60, col="red", lty=2)
95

    
96
        dev.off () 
97

    
98
}
99

    
100

    
101

    
102

    
103
num.regions <- 9
104
plotAspect <-lapply(1:num.regions, function(region, aster.dir, srtm.dir){
105
    a.aster2 <- raster(file.path(aster.dir, aster.straddle[region])) 
106
    a.srtm <- raster(file.path(srtm.dir, srtm.below[region]))  
107
    a.uncor <- raster(file.path(aster.dir, fused.straddle[region]))
108
    a.bg <- raster(file.path(aster.dir, fused.bgau[region])) 
109
    file.name <- substr(srtm.below[region], 6, 13)
110
    aspect(a.aster2, a.srtm, a.uncor, a.bg, file.name, aster.dir, srtm.dir)
111
}, aster.dir=aster.dir, srtm.dir=srtm.dir)
112

    
113

    
(2-2/22)