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