Project

General

Profile

« Previous | Next » 

Revision 8fd072d3

Added by Rick Reeves over 13 years ago

  • ID 8fd072d35fb82274c10b0668214d583beb5caf9f

The first version of 'CreateImageDiffPlotsOverlay.r' that produces the 'three-panel' plot containing the boundary differences for ASTER / Border / SRTM.

View differences:

terrain/rscripts/CreateImageDiffPlotsOverlayNewPlots.R
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
CreateImageDiffPlotsNew <- 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
# 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
   par(mfrow=c(1,3)) # Create a three column multi-plot
140

  
141
# Create values for the 'delta across pixel boundaries' plot
142

  
143
   deltaBoundaryASTER <- northRowTbl$diffNorthMosaicCDEM - northRowTbl$diffSouthMosaicCDEM
144
   deltaBoundaryBorder <- borderRowTbl$diffNorthMosaicCDEM - borderRowTbl$diffSouthMosaicCDEM  
145
   deltaBoundarySRTM <- southRowTbl$diffNorthMosaicCDEM - southRowTbl$diffSouthMosaicCDEM   
146
   commonXAxis <- c(0,max(pointTable$ColumnID))   
147
   commonYAxis <- range(c(deltaBoundarySRTM,deltaBoundaryBorder,deltaBoundaryASTER),na.rm=TRUE)   
148
   
149
   xAxisLbl <- sprintf("M/SD: ASTER: %.2f / %.2f",mean(deltaBoundaryASTER,na.rm=TRUE),sd(deltaBoundaryASTER,na.rm=TRUE))
150
   plot(northRowTbl$ColumnID,deltaBoundaryASTER,xlim=commonXAxis, ylim=commonYAxis,main="Difference along Row Boundary: ASTER",xlab=xAxisLbl,col="red",pch=plotCh1)
151

  
152
   xAxisLbl <- sprintf("M/SD: BORDER: %.2f / %.2f",mean(deltaBoundaryBorder,na.rm=TRUE),sd(deltaBoundaryBorder,na.rm=TRUE))
153
   plot(northRowTbl$ColumnID,deltaBoundaryBorder,xlim=commonXAxis, ylim=commonYAxis,main="Difference along Row Boundary: Border",xlab=xAxisLbl,col="darkgreen",pch=plotCh2)
154

  
155
   xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(deltaBoundarySRTM,na.rm=TRUE),sd(deltaBoundarySRTM,na.rm=TRUE))
156
   plot(northRowTbl$ColumnID,deltaBoundarySRTM,main="Difference along Row Boundary: SRTM",xlim=commonXAxis, ylim=commonYAxis,xlab=xAxisLbl,col="blue",pch=plotCh3)
157
message("Three-Plot done")
158
browser()
159
dev.new()
160
   deltaBoundary <- southRowTbl$diffNorthMosaicCDEM - southRowTbl$diffSouthMosaicCDEM
161
   xAxisLbl <- sprintf("M/SD: SRTM: %.2f / %.2f",mean(deltaBoundary,na.rm=TRUE),sd(deltaBoundary,na.rm=TRUE))
162
   plot(northRowTbl$ColumnID,deltaBoundary,main="Difference along Row Boundary: Border",xlab=xAxisLbl,col="red",pch=plotCh1)
163
browser()
164
   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),
165
                                                                            mean(borderRowTbl$elevSouth,na.rm=TRUE),sd(borderRowTbl$elevSouth,na.rm=TRUE),
166
                                                                            mean(southRowTbl$elevSouth,na.rm=TRUE),sd(southRowTbl$elevSouth,na.rm=TRUE))
167
   plot(northRowTbl$ColumnID,northRowTbl$elevSouth,main="South Mosaic Pixel Elev: ASTER (red) Border (green) SRTM (blue)",xlab=xAxisLbl,col="red",pch=plotCh1)
168
   points(borderRowTbl$ColumnID,borderRowTbl$elevSouth,col="darkgreen",pch=plotCh2)
169
   points(southRowTbl$ColumnID,southRowTbl$elevSouth,col="blue",pch=plotCh3)
170
browser()
171
dev.new()
172
# Second plot pair: North/South CDEM pixel elevation for North/South subsets 'distance East from Western edge'.
173

  
174
   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),
175
                                                                            mean(borderRowTbl$cdemNorth,na.rm=TRUE),sd(borderRowTbl$cdemNorth,na.rm=TRUE),
176
                                                                            mean(southRowTbl$cdemNorth,na.rm=TRUE),sd(southRowTbl$cdemNorth,na.rm=TRUE))
177
   plot(northRowTbl$ColumnID,northRowTbl$cdemNorth,main="North CDEM Pixel Elev ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
178
   points(borderRowTbl$ColumnID,borderRowTbl$cdemNorth,col="darkgreen",pch=plotCh2)
179
   points(southRowTbl$ColumnID,southRowTbl$cdemNorth,col="blue",pch=plotCh3)
180
browser()   
181
dev.new()
182
   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),
183
                                                                            mean(borderRowTbl$cdemSouth,na.rm=TRUE),sd(borderRowTbl$cdemSouth,na.rm=TRUE),
184
                                                                            mean(southRowTbl$cdemSouth,na.rm=TRUE),sd(southRowTbl$cdemSouth,na.rm=TRUE))   
185
   plot(northRowTbl$ColumnID,northRowTbl$cdemSouth,main="South CDEM Pixel Elev ASTER (r) Border (g) SRTM (b)",xlab=xAxisLbl,col="red",pch=plotCh1)
186
   points(borderRowTbl$ColumnID,borderRowTbl$cdemSouth,col="darkgreen",pch=plotCh2)
187
   points(southRowTbl$ColumnID,southRowTbl$cdemSouth,col="blue",pch=plotCh3)
188
browser()
189
dev.new()
190

  
191
message("All plots created - hit key to delete them...")
192
browser()
193
graphics.off()
194
} 

Also available in: Unified diff