Project

General

Profile

Download (16.1 KB) Statistics
| Branch: | Revision:
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
##############################################################################################
35
CreateImageDiffPlots <- function(plotSampFact = 100)
36
{
37
# Check plotSampleFact range  
38

    
39
   if (plotSampFact < 1)
40
      plotSampFact = 1
41
      
42
   if (plotSampFact > 100)
43
      plotSampFact = 100
44
 
45
# Read input table that was sorted OFFLINE in Excel on the first column (ColumnID)
46

    
47
   pointTable <-read.csv("tableForMark4000_5_8_SortColID.csv")
48

    
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
# Difference between Mosaic (ASTER or CGIAR or border) 
93
# and CDEM elevation as pertentage of the mosaic elevation
94

    
95
   pointTable <-cbind(pointTable,(pointTable$diffNorthMosaicCDEM/pointTable$elevNorth * 100)) 
96
   pointTable <-cbind(pointTable,(pointTable$diffSouthMosaicCDEM/pointTable$elevSouth * 100)) 
97
   
98
   colnames(pointTable)[10] <- "magDiffMosaicCDEMNorthPct"
99
   colnames(pointTable)[11] <- "magDiffMosaicCDEMSouthPct"   
100

    
101
# For the plots, subdivide the table into three segments: 
102
#     rows north of border
103
#     rows crossing border
104
#     rows south of border
105

    
106
   northRowTblAll = pointTable[northRowIndexes,]
107
   borderRowTblAll = pointTable[borderRowIndexes,]
108
   southRowTblAll = pointTable[southRowIndexes,]
109

    
110
   subset <- 1:nrow(northRowTblAll)
111
   
112
   randSub <- sample(subset,as.integer(length(subset) / plotSampFact)) 
113
   
114
   northRowTbl <- northRowTblAll[randSub,]
115
   borderRowTbl <- borderRowTblAll[randSub,]
116
   southRowTbl <- southRowTblAll[randSub,]   
117
   
118
message("hit key to create each plot...")
119
browser()
120

    
121
# Three plotting characters used
122

    
123
   plotCh1 <- 17 # 'north' (Aster) points
124
   plotCh2 <- 18 # 'border' (Aster+srtm) points 
125
   plotCh3 <- 20 # 'south' (srtm) points
126
   
127
# First plot pair: North/South Mosaic Image pixel elevation for North/South subsets 'distance East from Western edge'.
128

    
129
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$elevNorth,na.rm=TRUE),sd(northRowTbl$elevNorth,na.rm=TRUE),
130
                                                                            mean(borderRowTbl$elevNorth,na.rm=TRUE),sd(borderRowTbl$elevNorth,na.rm=TRUE),
131
                                                                            mean(southRowTbl$elevNorth,na.rm=TRUE),sd(southRowTbl$elevNorth,na.rm=TRUE))
132
   plot(northRowTbl$ColumnID,northRowTbl$elevNorth,main="North Mosaic Pixel Elev: ASTER (red) Border (green) SRTM (blue)",xlab=xAxisLbl,col="red",pch=plotCh1)
133
   points(borderRowTbl$ColumnID,borderRowTbl$elevNorth,col="darkgreen",pch=plotCh2)
134
   points(southRowTbl$ColumnID,southRowTbl$elevNorth,col="blue",pch=plotCh3)
135
browser()
136
dev.new()
137
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$elevSouth,na.rm=TRUE),sd(northRowTbl$elevSouth,na.rm=TRUE),
138
                                                                            mean(borderRowTbl$elevSouth,na.rm=TRUE),sd(borderRowTbl$elevSouth,na.rm=TRUE),
139
                                                                            mean(southRowTbl$elevSouth,na.rm=TRUE),sd(southRowTbl$elevSouth,na.rm=TRUE))
140
   plot(northRowTbl$ColumnID,northRowTbl$elevSouth,main="South Mosaic Pixel Elev: ASTER (red) Border (green) SRTM (blue)",xlab=xAxisLbl,col="red",pch=plotCh1)
141
   points(borderRowTbl$ColumnID,borderRowTbl$elevSouth,col="darkgreen",pch=plotCh2)
142
   points(southRowTbl$ColumnID,southRowTbl$elevSouth,col="blue",pch=plotCh3)
143
browser()
144
dev.new()
145
# Second plot pair: North/South CDEM pixel elevation for North/South subsets 'distance East from Western edge'.
146

    
147
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$cdemNorth,na.rm=TRUE),sd(northRowTbl$cdemNorth,na.rm=TRUE),
148
                                                                            mean(borderRowTbl$cdemNorth,na.rm=TRUE),sd(borderRowTbl$cdemNorth,na.rm=TRUE),
149
                                                                            mean(southRowTbl$cdemNorth,na.rm=TRUE),sd(southRowTbl$cdemNorth,na.rm=TRUE))
150
   plot(northRowTbl$ColumnID,northRowTbl$cdemNorth,main="North CDEM Pixel Elev ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
151
   points(borderRowTbl$ColumnID,borderRowTbl$cdemNorth,col="darkgreen",pch=plotCh2)
152
   points(southRowTbl$ColumnID,southRowTbl$cdemNorth,col="blue",pch=plotCh3)
153
browser()   
154
dev.new()
155
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$cdemSouth,na.rm=TRUE),sd(northRowTbl$cdemSouth,na.rm=TRUE),
156
                                                                            mean(borderRowTbl$cdemSouth,na.rm=TRUE),sd(borderRowTbl$cdemSouth,na.rm=TRUE),
157
                                                                            mean(southRowTbl$cdemSouth,na.rm=TRUE),sd(southRowTbl$cdemSouth,na.rm=TRUE))   
158
   plot(northRowTbl$ColumnID,northRowTbl$cdemSouth,main="South CDEM Pixel Elev ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
159
   points(borderRowTbl$ColumnID,borderRowTbl$cdemSouth,col="darkgreen",pch=plotCh2)
160
   points(southRowTbl$ColumnID,southRowTbl$cdemSouth,col="blue",pch=plotCh3)
161
browser()
162
dev.new()
163
# Third plot pair: difference between North and South pixels in pair.
164

    
165
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(northRowTbl$diffMosaicNorthSouth,na.rm=TRUE),
166
                                                                            mean(borderRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(borderRowTbl$diffMosaicNorthSouth,na.rm=TRUE),
167
                                                                            mean(southRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(southRowTbl$diffMosaicNorthSouth,na.rm=TRUE))
168
   plot(northRowTbl$ColumnID,northRowTbl$diffMosaicNorthSouth,main="Mosaic N/S Pixel Diff: ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
169
   points(borderRowTbl$ColumnID,borderRowTbl$diffMosaicNorthSouth,col="darkgreen",pch=plotCh2)
170
   points(southRowTbl$ColumnID,southRowTbl$diffMosaicNorthSouth,col="blue",pch=plotCh3)   
171
browser()  
172
dev.new()
173
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(northRowTbl$diffCDEMNorthSouth,na.rm=TRUE),
174
                                                                            mean(borderRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(borderRowTbl$diffCDEMNorthSouth,na.rm=TRUE),
175
                                                                            mean(southRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(southRowTbl$diffCDEMNorthSouth,na.rm=TRUE))
176
   plot(northRowTbl$ColumnID,northRowTbl$diffCDEMNorthSouth,main="CDEM N/S Pixel Diff: ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
177
   points(borderRowTbl$ColumnID,borderRowTbl$diffCDEMNorthSouth,col="darkgreen",pch=plotCh2)
178
   points(southRowTbl$ColumnID,southRowTbl$diffCDEMNorthSouth,col="blue",pch=plotCh3)
179
browser()
180
dev.new()
181

    
182
# Fourth plot pair: the Mosaic/CDEM difference for North and South pixels
183

    
184
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffNorthMosaicCDEM,na.rm=TRUE),sd(northRowTbl$diffNorthMosaicCDEM,na.rm=TRUE),
185
                                                                            mean(borderRowTbl$diffNorthMosaicCDEM,na.rm=TRUE),sd(borderRowTbl$diffNorthMosaicCDEM,na.rm=TRUE),
186
                                                                            mean(southRowTbl$diffNorthMosaicCDEM,na.rm=TRUE),sd(southRowTbl$diffNorthMosaicCDEM,na.rm=TRUE))
187
   plot(northRowTbl$ColumnID,northRowTbl$diffMosaicNorthSouth,main="Mosaic / CDEM Pixel Diff (North): ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
188
   points(borderRowTbl$ColumnID,borderRowTbl$diffMosaicNorthSouth,col="darkgreen",pch=plotCh2)
189
   points(southRowTbl$ColumnID,southRowTbl$diffMosaicNorthSouth,col="blue",pch=plotCh3)   
190
browser()
191
dev.new()
192
   xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffSouthMosaicCDEM,na.rm=TRUE),sd(northRowTbl$diffSouthMosaicCDEM,na.rm=TRUE),
193
                                                                            mean(borderRowTbl$diffSouthMosaicCDEM,na.rm=TRUE),sd(borderRowTbl$diffSouthMosaicCDEM,na.rm=TRUE),
194
                                                                            mean(southRowTbl$diffSouthMosaicCDEM,na.rm=TRUE),sd(southRowTbl$diffSouthMosaicCDEM,na.rm=TRUE))
195
   plot(northRowTbl$ColumnID,northRowTbl$diffCDEMNorthSouth,main="Mosaic / CDEM Pixel Diff (South): ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
196
   points(borderRowTbl$ColumnID,borderRowTbl$diffCDEMNorthSouth,col="darkgreen",pch=plotCh2)
197
   points(southRowTbl$ColumnID,southRowTbl$diffCDEMNorthSouth,col="blue",pch=plotCh3)
198
browser()
199
dev.new()
200
# Fifth plot pair: the Mosaic/CDEM difference divided by the (ASTER or SRTM) elevation
201

    
202
  xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE),sd(northRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE),
203
                                                                            mean(borderRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE),sd(borderRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE),
204
                                                                            mean(southRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE),sd(southRowTbl$magDiffMosaicCDEMNorthPct,na.rm=TRUE))  
205
  plot(northRowTbl$ColumnID,northRowTbl$magDiffMosaicCDEMNorthPct,main="Pixel Elev Diff as % of Mosaic Elev (North) - ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
206
  points(borderRowTbl$ColumnID,borderRowTbl$magDiffMosaicCDEMNorthPct,col="darkgreen",pch=plotCh2)  
207
  points(southRowTbl$ColumnID,southRowTbl$magDiffMosaicCDEMNorthPct,col="blue",pch=plotCh3)  
208
browser()
209
dev.new()
210
  xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE),sd(northRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE),
211
                                                                            mean(borderRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE),sd(borderRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE),
212
                                                                             mean(southRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE),sd(southRowTbl$magDiffMosaicCDEMSouthPct,na.rm=TRUE))
213
  plot(northRowTbl$ColumnID,northRowTbl$magDiffMosaicCDEMSouthPct,main="Pixel Elev Diff as % of Mosaic Elev (South) - ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
214
  points(borderRowTbl$ColumnID,borderRowTbl$magDiffMosaicCDEMSouthPct,col="darkgreen",pch=plotCh2)  
215
  points(southRowTbl$ColumnID,southRowTbl$magDiffMosaicCDEMSouthPct,col="blue",pch=plotCh3)  
216
browser()
217
dev.new()
218
# Final plot pair: Mosaic vs CDEM difference vs elevation (north or south)
219

    
220
xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(northRowTbl$diffMosaicNorthSouth,na.rm=TRUE),
221
                                                                          mean(borderRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(borderRowTbl$diffMosaicNorthSouth,na.rm=TRUE),
222
                                                                          mean(southRowTbl$diffMosaicNorthSouth,na.rm=TRUE),sd(southRowTbl$diffMosaicNorthSouth,na.rm=TRUE))
223
plot(northRowTbl$elevNorth,northRowTbl$diffMosaicNorthSouth,main="Mosaic Elev N/S Diff ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
224
points(borderRowTbl$elevNorth,borderRowTbl$diffMosaicNorthSouth,col="darkgreen",pch=plotCh2)
225
points(southRowTbl$elevNorth,southRowTbl$diffMosaicNorthSouth,col="blue",pch=plotCh3)
226
browser()
227
dev.new()
228
  xAxisLbl <- sprintf("M/SD: AST: %.2f / %.2f BRD: %.2f / %.2f STM: %.2f / %.2f",mean(northRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(northRowTbl$diffCDEMNorthSouth,na.rm=TRUE),
229
                                                                            mean(borderRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(borderRowTbl$diffCDEMNorthSouth,na.rm=TRUE),
230
                                                                            mean(southRowTbl$diffCDEMNorthSouth,na.rm=TRUE),sd(southRowTbl$diffCDEMNorthSouth,na.rm=TRUE))
231
  plot(northRowTbl$elevNorth,northRowTbl$diffCDEMNorthSouth,main="CDEM Elev N/S Diff ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
232
  points(borderRowTbl$elevNorth,borderRowTbl$diffCDEMNorthSouth,col="darkgreen",pch=plotCh2)
233
  points(southRowTbl$elevNorth,southRowTbl$diffCDEMNorthSouth,col="blue",pch=plotCh3)
234
message("All plots created - hit key to delete them...")
235
browser()
236
graphics.off()
237
} 
(1-1/19)