1
|
##############################################################################################
|
2
|
#
|
3
|
# CreateImageDiffPlots
|
4
|
#
|
5
|
# R script generates collection of nine plots that display the distributions of populations of
|
6
|
# three elevation pixel pairs in proximity to the border between ASTER and STRM data in mosaic
|
7
|
# images# under development for the Environment and Organisms project.
|
8
|
#
|
9
|
#
|
10
|
# Inputs:
|
11
|
# 1) Comma Separated Value (CSV) table containing randomly-sampled elevation value pairs,
|
12
|
# extracted from ASTER/CGIAR mosaic and CDEM images, created by the R script: makeImagePairTable.r
|
13
|
# Note: At present, be sure that this file has been sorted by column 1 (ColumnID) in Excel
|
14
|
# before using in this program.
|
15
|
#
|
16
|
# 2) sampling factor: integer (range=1 to number of recors in input table. Determines
|
17
|
# the size of randomly-selected sampe of total table record 'triplets' (north, border,
|
18
|
# and south) rows of each column sample) to be displayed in the plots:
|
19
|
# sampling factor = 1 : plot every table record
|
20
|
# 10 : plot a random sample containing 1/10th of records
|
21
|
# 100 : plot random sample containing 1/100th of records.
|
22
|
#
|
23
|
# To run:
|
24
|
#
|
25
|
# 1) place this file and the input file "tableForMark4000_5_8_SortColID.csv" in folder
|
26
|
# 2) start R
|
27
|
# 3) > source("CreateImageDiffPlotsOverlay.r")
|
28
|
# 4) > CreateImageDiffPlots(sampling factor)
|
29
|
# < press <cr> key to view plots sequentially
|
30
|
#
|
31
|
# TODO: add symbol legend to each plot.
|
32
|
# Author: Rick Reeves, NCEAS
|
33
|
# May 14, 2011
|
34
|
# May 17, 2011: This version generates 'delta scatterplots' specified by Mark and Jim.
|
35
|
##############################################################################################
|
36
|
CreateImageDiffPlots <- function(plotSampFact = 1)
|
37
|
{
|
38
|
# Check plotSampleFact range
|
39
|
|
40
|
if (plotSampFact < 1)
|
41
|
plotSampFact = 1
|
42
|
|
43
|
# if (plotSampFact > )
|
44
|
# plotSampFact = 100
|
45
|
|
46
|
# Read input table that was sorted OFFLINE in Excel on the first column (ColumnID)
|
47
|
|
48
|
pointTable <-read.csv("pixelPairs36000_5_8EvenSortCol1.csv")
|
49
|
# Table created by randomly sampling from two superimposed
|
50
|
# image:
|
51
|
# 1) DOM mosaic image comprised of ASTER and SRTM components.
|
52
|
# 2) 'Baseline' Canadian DEM (CDEM) image.
|
53
|
#
|
54
|
# The input table contains three rows for each randomly-selected
|
55
|
# pixel pair from both images. Each row contains two pixel pairs,
|
56
|
# the first pair drawn from the image mosaic, the second pair
|
57
|
# drawn from the CDEM image:
|
58
|
# First pair: North pixel, South pixel (ASTER/SRTM mosaic)
|
59
|
# Second pair: North pixel, South pixel (CDEM)
|
60
|
#
|
61
|
# The first row of each 'triplet' contains pixel pairs North of border,
|
62
|
# The second row contains pixel pairs spanning border,
|
63
|
# The third row contains pixel pairs South of border,
|
64
|
#
|
65
|
# This script generates a series of plots that display
|
66
|
# differences between:
|
67
|
# 1) The mosaic and CDEM images
|
68
|
# 2) Image pixels on and away from the ASTER / SRTM boundary.
|
69
|
#
|
70
|
northRowIndexes = seq(from=1, to=(nrow(pointTable) - 3),by=3)
|
71
|
borderRowIndexes = seq(from=2, to=(nrow(pointTable) - 1),by=3)
|
72
|
southRowIndexes = seq(from=3, to=(nrow(pointTable)),by=3)
|
73
|
#
|
74
|
# calculate and append the difference between elevations
|
75
|
# and CDEM
|
76
|
# these lines create the inputs for differnce image plots for each of three
|
77
|
# pixel pair subsets: North of border (All Aster), border (combo Aster/Srtm),
|
78
|
# South of border (all Srtm)
|
79
|
#
|
80
|
# First, add the 'difference columns' to the entire table
|
81
|
#
|
82
|
pointTable <-cbind(pointTable,(pointTable$elevNorth - pointTable$elevSouth))
|
83
|
pointTable <-cbind(pointTable,(pointTable$cdemNorth - pointTable$cdemSouth))
|
84
|
pointTable <-cbind(pointTable,(pointTable$elevNorth - pointTable$cdemNorth))
|
85
|
pointTable <-cbind(pointTable,(pointTable$elevSouth - pointTable$cdemSouth))
|
86
|
#
|
87
|
colnames(pointTable)[6] <- "diffMosaicNorthSouth"
|
88
|
colnames(pointTable)[7] <- "diffCDEMNorthSouth"
|
89
|
colnames(pointTable)[8] <- "diffNorthMosaicCDEM"
|
90
|
colnames(pointTable)[9] <- "diffSouthMosaicCDEM"
|
91
|
|
92
|
# add a placeholder for the 'boundary' value, across each table
|
93
|
|
94
|
# pointTable <-cbind(pointTable,(-1))
|
95
|
|
96
|
# colnames(pointTable)[10] <- "deltaAcrossBorder"
|
97
|
|
98
|
# Difference between Mosaic (ASTER or CGIAR or border)
|
99
|
# and CDEM elevation as pertentage of the mosaic elevation
|
100
|
|
101
|
pointTable <-cbind(pointTable,(pointTable$diffNorthMosaicCDEM/pointTable$elevNorth * 100))
|
102
|
pointTable <-cbind(pointTable,(pointTable$diffSouthMosaicCDEM/pointTable$elevSouth * 100))
|
103
|
|
104
|
colnames(pointTable)[10] <- "magDiffMosaicCDEMNorthPct"
|
105
|
colnames(pointTable)[11] <- "magDiffMosaicCDEMSouthPct"
|
106
|
|
107
|
# For the plots, subdivide the table into three segments:
|
108
|
# rows north of border
|
109
|
# rows crossing border
|
110
|
# rows south of border
|
111
|
|
112
|
northRowTblAll = pointTable[northRowIndexes,]
|
113
|
borderRowTblAll = pointTable[borderRowIndexes,]
|
114
|
southRowTblAll = pointTable[southRowIndexes,]
|
115
|
|
116
|
subset <- 1:nrow(northRowTblAll)
|
117
|
|
118
|
randSub <- sample(subset,as.integer(length(subset) / plotSampFact))
|
119
|
|
120
|
northRowTbl <- northRowTblAll[randSub,]
|
121
|
borderRowTbl <- borderRowTblAll[randSub,]
|
122
|
southRowTbl <- southRowTblAll[randSub,]
|
123
|
|
124
|
message("hit key to create each plot...")
|
125
|
browser()
|
126
|
|
127
|
# Three plotting characters used
|
128
|
|
129
|
plotCh1 <- 17 # 'north' (aster) points
|
130
|
plotCh2 <- 18 # 'border' (aster+srtm) points
|
131
|
plotCh3 <- 20 # 'south' (srtm) points
|
132
|
|
133
|
# NEW: Three plots: The difference between Mosaic and CDEM for pixels along
|
134
|
# each of three border edges: North (ASTER), Border, South (SRTM)
|
135
|
# for now, north, border, south have separate plots
|
136
|
|
137
|
# We need to tailor the plot 'triplet' X and Y axes to the dynamic ranges of all three data sets.
|
138
|
|
139
|
|
140
|
|
141
|
# Create values for the three 'delta across pixel boundaries' plots:
|
142
|
# 1) Y-Axis: columnIDs
|
143
|
# 2) Y-Axis: CDEM elevations
|
144
|
# 3) Y-Axis:
|
145
|
|
146
|
deltaBoundaryASTER <- northRowTbl$diffNorthMosaicCDEM - northRowTbl$diffSouthMosaicCDEM
|
147
|
deltaBoundaryBorder <- borderRowTbl$diffNorthMosaicCDEM - borderRowTbl$diffSouthMosaicCDEM
|
148
|
deltaBoundarySRTM <- southRowTbl$diffNorthMosaicCDEM - southRowTbl$diffSouthMosaicCDEM
|
149
|
|
150
|
normDeltaBoundaryASTER <- (deltaBoundaryASTER / northRowTbl$cdemNorth * 100)
|
151
|
normDeltaBoundaryBorder <- (deltaBoundaryBorder / borderRowTbl$cdemNorth * 100)
|
152
|
normDeltaBoundarySRTM <- (deltaBoundarySRTM / southRowTbl$cdemNorth * 100)
|
153
|
|
154
|
par(mfrow=c(1,3)) # Create a three column multi-plot
|
155
|
|
156
|
commonXAxis <- c(0,max(pointTable$ColumnID))
|
157
|
commonYAxis <- range(c(deltaBoundarySRTM,deltaBoundaryBorder,deltaBoundaryASTER),na.rm=TRUE)
|
158
|
|
159
|
xAxisLbl <- sprintf("M/SD: ASTER: %.2f / %.2f",mean(deltaBoundaryASTER,na.rm=TRUE),sd(deltaBoundaryASTER,na.rm=TRUE))
|
160
|
plot(northRowTbl$ColumnID,deltaBoundaryASTER,xlim=commonXAxis, ylim=commonYAxis,main="Row Boundary Delta: ASTER",xlab=xAxisLbl,col="red",pch=plotCh1)
|
161
|
|
162
|
xAxisLbl <- sprintf("M/SD: BORDER: %.2f / %.2f",mean(deltaBoundaryBorder,na.rm=TRUE),sd(deltaBoundaryBorder,na.rm=TRUE))
|
163
|
plot(northRowTbl$ColumnID,deltaBoundaryBorder,xlim=commonXAxis, ylim=commonYAxis,main="Row Boundary Delta: Border",sub="E-W Col",xlab=xAxisLbl,col="darkgreen",pch=plotCh2)
|
164
|
|
165
|
xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(deltaBoundarySRTM,na.rm=TRUE),sd(deltaBoundarySRTM,na.rm=TRUE))
|
166
|
plot(northRowTbl$ColumnID,deltaBoundarySRTM,main="Row Boundary Delta: SRTM",xlim=commonXAxis, ylim=commonYAxis,xlab=xAxisLbl,col="blue",pch=plotCh3)
|
167
|
|
168
|
message("ColumnID-X-Axis is done")
|
169
|
browser()
|
170
|
#dev.new()
|
171
|
|
172
|
par(mfrow=c(1,3)) # Create a three column multi-plot pointTable$diffNorthMosaicCDEM/pointTable$elevNorth * 100))
|
173
|
|
174
|
commonXAxis <- c(0,max(pointTable$cdemNorth))
|
175
|
commonYAxis <- range(c(deltaBoundarySRTM,deltaBoundaryBorder,deltaBoundaryASTER),na.rm=TRUE)
|
176
|
|
177
|
xAxisLbl <- sprintf("M/SD: ASTER: %.2f / %.2f",mean(deltaBoundaryASTER,na.rm=TRUE),sd(deltaBoundaryASTER,na.rm=TRUE))
|
178
|
plot(northRowTbl$cdemNorth,deltaBoundaryASTER,xlim=commonXAxis, ylim=commonYAxis,main="Boundary Delta (CDEM Elev): ASTER",xlab=xAxisLbl,col="red",pch=plotCh1)
|
179
|
|
180
|
xAxisLbl <- sprintf("M/SD: BORDER: %.2f / %.2f",mean(deltaBoundaryBorder,na.rm=TRUE),sd(deltaBoundaryBorder,na.rm=TRUE))
|
181
|
plot(northRowTbl$cdemNorth,deltaBoundaryBorder,xlim=commonXAxis, ylim=commonYAxis,main="Boundary Delta (CDEM Elev): Border",sub="vs Elev",xlab=xAxisLbl,col="darkgreen",pch=plotCh2)
|
182
|
|
183
|
xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(deltaBoundarySRTM,na.rm=TRUE),sd(deltaBoundarySRTM,na.rm=TRUE))
|
184
|
plot(northRowTbl$cdemNorth,deltaBoundarySRTM,main="Boundary Delta (CDEM Elev): SRTM",xlim=commonXAxis, ylim=commonYAxis,xlab=xAxisLbl,col="blue",pch=plotCh3)
|
185
|
message("ColumnID-CDEM Elevation done")
|
186
|
browser()
|
187
|
commonXAxis <- c(0,max(pointTable$cdemNorth))
|
188
|
commonYAxis <- range(c(normDeltaBoundarySRTM,normDeltaBoundaryBorder,normDeltaBoundaryASTER),na.rm=TRUE)
|
189
|
|
190
|
xAxisLbl <- sprintf("M/SD: ASTER: %.2f / %.2f",mean(normDeltaBoundaryASTER,na.rm=TRUE),sd(normDeltaBoundaryASTER,na.rm=TRUE))
|
191
|
plot(northRowTbl$cdemNorth,normDeltaBoundaryASTER,xlim=commonXAxis, ylim=commonYAxis,main="Norm Bdry Delta (CDEM Elev): ASTER",xlab=xAxisLbl,col="red",pch=plotCh1)
|
192
|
|
193
|
xAxisLbl <- sprintf("M/SD: BORDER: %.2f / %.2f",mean(normDeltaBoundaryBorder,na.rm=TRUE),sd(normDeltaBoundaryBorder,na.rm=TRUE))
|
194
|
plot(northRowTbl$cdemNorth,normDeltaBoundaryBorder,xlim=commonXAxis, ylim=commonYAxis,main="Norm Bdry Delta (CDEM Elev): Border",sub="vs Elev",xlab=xAxisLbl,col="darkgreen",pch=plotCh2)
|
195
|
|
196
|
xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(normDeltaBoundarySRTM,na.rm=TRUE),sd(normDeltaBoundarySRTM,na.rm=TRUE))
|
197
|
plot(northRowTbl$cdemNorth,normDeltaBoundarySRTM,main="Norm Bdry Delta (CDEM Elev): SRTM",xlim=commonXAxis, ylim=commonYAxis,xlab=xAxisLbl,col="blue",pch=plotCh3)
|
198
|
message("NORMALIZED ColumnID-CDEM Elevation done")
|
199
|
browser()
|
200
|
commonXAxis <- c(0,max(pointTable$ColumnID))
|
201
|
commonYAxis <- range(c(normDeltaBoundarySRTM,normDeltaBoundaryBorder,normDeltaBoundaryASTER),na.rm=TRUE)
|
202
|
|
203
|
xAxisLbl <- sprintf("M/SD: ASTER: %.2f / %.2f",mean(normDeltaBoundaryASTER,na.rm=TRUE),sd(normDeltaBoundaryASTER,na.rm=TRUE))
|
204
|
plot(northRowTbl$ColumnID,normDeltaBoundaryASTER,xlim=commonXAxis, ylim=commonYAxis,main="Norm Bdry Delta: ASTER",xlab=xAxisLbl,col="red",pch=plotCh1)
|
205
|
|
206
|
xAxisLbl <- sprintf("M/SD: BORDER: %.2f / %.2f",mean(normDeltaBoundaryBorder,na.rm=TRUE),sd(normDeltaBoundaryBorder,na.rm=TRUE))
|
207
|
plot(northRowTbl$ColumnID,normDeltaBoundaryBorder,xlim=commonXAxis, ylim=commonYAxis,main="Norm Bdry Delta: Border",sub="E-W Col",xlab=xAxisLbl,col="darkgreen",pch=plotCh2)
|
208
|
|
209
|
xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(normDeltaBoundarySRTM,na.rm=TRUE),sd(normDeltaBoundarySRTM,na.rm=TRUE))
|
210
|
plot(northRowTbl$ColumnID,normDeltaBoundarySRTM,main="Norm Bdry Delta: SRTM",xlim=commonXAxis, ylim=commonYAxis,xlab=xAxisLbl,col="blue",pch=plotCh3)
|
211
|
|
212
|
message("NORMALIZED ColumnID-X-Axis is done...")
|
213
|
message("...All plots created - hit key to delete them...")
|
214
|
browser()
|
215
|
graphics.off()
|
216
|
}
|