Project

General

Profile

Download (7.69 KB) Statistics
| Branch: | Revision:
1 230f2a79 Rick Reeves
##############################################################################################
2
#
3
# SampleDemDiffCols
4
# 
5
# R script generates (or reads in) the CDEM / ASTER-SRTM mosaic 'difference image',
6
# and for randomly-selected one column / two row subimages, computes and saves tbe 
7
# difference between the pixels in the pair to create a distribution of differences.
8
# Three distributions created: North (ASTER CDEM), South i(SRTM/CGIAR), and 
9
# Border (boundary between ASTER and SRTM), Summary statistics are created for each 
10
# distribution.
11
#
12
# Author: Rick Reeves, NCEAS
13
# April 29, 2011
14
##############################################################################################
15
#
16
#ExtractDemRasterSubimages <- function(sFirstImageName,sSecondImageName)
17
SampleDemDiffCols <- function()
18
{
19
require(raster)
20
require(rgdal)
21
22
#inputFirstRaster <- raster(sFirstImageName)
23
#inputSecondRaster <- raster(sSecondImageName)
24
25
inputFirstRaster <- raster("mergeCgiarAsterBdyTuesdayClip.tif")
26
inputSecondRaster <- raster("CDemMosTuesdayClipMergeSpace.tif")
27
#
28
# Difference image for entire merged image takes a while to create, 
29
# so we created it once, now read it back in.
30
#
31
rDeltaWhole <- raster("DeltaEntireImage.tif")
32
rDeltaWhole@data@values <-getValues(rDeltaWhole)
33
34
# Create extent objects used to extract raster subimges. 
35
# The object will be centered along the 60 degree North latitude line,
36
# and have varying depths (number of rows).
37
# The Western Canada study area runs from -135 (west) to -100 (west) longitude,
38
# and 55.0 to 64.00 degrees (north) latitude. 
39
# the ASTER and SRTM/CGIAR image components are merged at the 60 Deg N Latitude line.
40
41
#eTestAreaExtent <- extent(-135.2,-100.2, 59.997,60.001) # Creates a 5 row subimage
42
eTestAreaExtent <- extent(-135.2,-100.2, 59.995,60.005) # Creates a 12 row subimage
43
44
# Extract a sub image corresponding to the selected extent.
45
# Two different alternatives:
46
# The extract() function returns a vector of cell values, 
47
# the crop() function returns a complete raster* object.
48
49
vEdgeRegionFirst <- extract(inputFirstRaster,eTestAreaExtent)
50
rEdgeRegionFirst <- crop(inputFirstRaster,eTestAreaExtent)
51
vEdgeRegionSecond <- extract(inputSecondRaster,eTestAreaExtent)
52
rEdgeRegionSecond <- crop(inputSecondRaster,eTestAreaExtent)
53
54
# Important: In order for the image subtraction to work, the extents
55
#            of the two images must be IDENTICAL. I used ArcMap GIS Raster Crop By Mask
56
#            to create subimages with identical extents. 
57
58
# Compute the difference image  for the entire study area, and for the region along
59
# the boundary (narrow, maybe 10 pixels either side)
60
61
rDeltaEdge <- rEdgeRegionFirst - rEdgeRegionSecond
62
63
# Create this image one time, read it in thereafter.
64
#rDeltaWhole <- inputFirstRaster - inputSecondRaster
65
#writeRaster(rDeltaWhole,filename="DeltaEntireImage.tif",format="GTiff",datatype="INT2S",overwrite=TRUE)
66
67
# Using the large difference image, compute subimagee statistics for areas
68
# North (ASTER) and South (SRTM) of the boundary. These give us an idea 
69
# re: differences between ASTER and CDEM and CGIAR/SRT and CDEM
70
# what is raster package way of using subscripts to extract? 
71
72
# Now, the interesting part: using the boundary difference image, randomly select 
73
# one-degree N-S strips throughout the image, and compare adjacent pixel pairs 
74
# above and below the boundary with pixel pairs straddling the boundary. Subtract  
75
# the pairs, save the collection of (absolute value) of the differences in a vector,
76
# so that we have a population of differences above, below, and straddling the boundary 
77
# line. Compare the populations.
78
79
# get a vector of random column index numbers, constrained by column dimension of image
80
# Loop three times, sampling pixel pairs from above, below, across the border
81
82
nColsToGet <- 1000
83
iDiffVecNorth <- vector(mode="integer",length=nColsToGet)
84
iDiffVecBorder <- vector(mode="integer",length=nColsToGet)
85
iDiffVecSouth <- vector(mode="integer",length=nColsToGet)
86
87
#colsToGet <-sample(1:50,nColsToGet)
88
89
# Note: initially, sample the same columns in all regions to get a profile.
90
#       other 'sample()' calls can be commented out to sample differenct
91
#       coluns in each 'region'.
92
93
# iDiffVecxxxx is a population of differences between adjacent cell pairs. 
94
# Compute iDiffVecNorth/Border/South on either side of border, and across it. 
95
# note that North and South samples taken from larger difference image for 
96
# entire mosaic (sub) image; iDiffBorder taken from the edge region extracted
97
# from the center of the lerger image.
98
99
# Remember, we are sampling a PAIR of pixels (same column from two adjacent rows)
100
101
colsToGet <-sample(1:inputFirstRaster@ncols,nColsToGet)
102
message("North Sample")
103
#browser()
104
# debug
105
#nColsToGet <- 2
106
#colsToGet <- c(20,100)
107
iFirstRow <- 300
108
iCtr = 1
109
for (iNextCol in colsToGet)
110
{
111
  rColVec <- cellFromRowCol(rDeltaWhole,iFirstRow:(iFirstRow+1),iNextCol:iNextCol)
112
  neighborCells <- rDeltaWhole@data@values[rColVec]
113
  iDiffVecNorth[iCtr] <- neighborCells[1] - neighborCells[2]
114
  iCtr = iCtr + 1
115
}
116
#
117
message("Border Sample")
118
#browser()
119
#colsToGet <-sample(1:inputFirstRaster@ncols,nColsToGet)
120
iFirstRow <- 6 # straddle the border of 12 row center section 
121
iCtr = 1
122
for (iNextCol in colsToGet)
123
{
124
  rColVec <- cellFromRowCol(rDeltaEdge,iFirstRow:(iFirstRow+1),iNextCol:iNextCol)
125
  neighborCells <- rDeltaEdge@data@values[rColVec]
126
  iDiffVecBorder[iCtr] <- neighborCells[1] - neighborCells[2]
127
  iCtr = iCtr + 1 
128
}
129
#
130
message("South Sample")
131
#browser()
132
#colsToGet <-sample(1:inputFirstRaster@ncols,nColsToGet)
133
iFirstRow <- 3600
134
iCtr = 1
135
for (iNextCol in colsToGet)
136
{
137
  rColVec <- cellFromRowCol(rDeltaWhole,iFirstRow:(iFirstRow+1),iNextCol:iNextCol)
138
  neighborCells <- rDeltaWhole@data@values[rColVec]
139
  iDiffVecSouth[iCtr] <- neighborCells[1] - neighborCells[2]
140
  iCtr = iCtr + 1 
141
}
142
# Compute iDiffVecs on either side of border, and across it. 
143
message("Check the cell difference vectors...")
144
#browser()
145
146
# summary stats for each population
147
148
sNorthSum <- sprintf("ASTER sample summary: Min: %f / Median: %d / Mean: %f / Max: %f / Variance: %f sDev: %f",
149
                     min(iDiffVecNorth,na.rm=TRUE),median(iDiffVecNorth,na.rm=TRUE),mean(iDiffVecNorth,na.rm=TRUE),
150
                     max(iDiffVecNorth,na.rm=TRUE),var(iDiffVecNorth,na.rm=TRUE),sd(iDiffVecNorth,na.rm=TRUE))
151
152
sBorderSum <- sprintf("Border sample summary: Min: %f / Median: %d / Mean: %f / Max: %f / Variance: %f sDev: %f",
153
                     min(iDiffVecBorder,na.rm=TRUE),median(iDiffVecBorder,na.rm=TRUE),mean(iDiffVecBorder,na.rm=TRUE),
154
                     max(iDiffVecBorder,na.rm=TRUE),var(iDiffVecBorder,na.rm=TRUE),sd(iDiffVecBorder,na.rm=TRUE))
155
156
sSouthSum <- sprintf("STRM sample summary: Min: %f / Median: %d / Mean: %f / Max: %f / Variance: %f sDev: %f",
157
                     min(iDiffVecSouth,na.rm=TRUE),median(iDiffVecSouth,na.rm=TRUE),mean(iDiffVecSouth,na.rm=TRUE),
158
                     max(iDiffVecSouth,na.rm=TRUE),var(iDiffVecSouth,na.rm=TRUE),sd(iDiffVecSouth,na.rm=TRUE))
159
#
160
message(sprintf("statistics for %d N/S adjacent pixel pairs from three mosaic image regions:",nColsToGet))
161
message(sNorthSum)
162
message(sBorderSum)
163
message(sSouthSum)
164
165
message("hit key to write output images...")
166
browser()
167
# Write the extracted subimage and its difference image to disk
168
# For now, use 'gdalinfo' to check image statistics
169
170
writeRaster(rEdgeRegionFirst,filename="EdgeRegionFirst.tif",format="GTiff",datatype="INT2S",overwrite=TRUE)
171
            
172
writeRaster(rEdgeRegionAsterCgiar,filename="EdgeRegionSecond.tif",format="GTiff",datatype="INT2S",overwrite=TRUE)
173
174
writeRaster(rDelta,filename="EdgeRegionDelta.tif",format="GTiff",datatype="INT2S",overwrite=TRUE)
175
}