1 |
b67964e8
|
Rick Reeves
|
##############################################################################################
|
2 |
|
|
#
|
3 |
|
|
# makeMarkTable.r
|
4 |
|
|
#
|
5 |
|
|
# R script generates table of adjacent ASTER / SRTM / Mosaic Border / CDEM pixel values
|
6 |
|
|
# to be used to generate plots of 'delta elevation' vs 'pixel pair proximity' and 'elevation'
|
7 |
|
|
# suggested by Mark Schildhauer on May 3.
|
8 |
|
|
#
|
9 |
|
|
# Author: Rick Reeves, NCEAS
|
10 |
|
|
# May 4, 2011
|
11 |
|
|
##############################################################################################
|
12 |
|
|
#
|
13 |
|
|
makeMarkTable <- function()
|
14 |
|
|
{
|
15 |
|
|
require(raster)
|
16 |
|
|
require(rgdal)
|
17 |
|
|
|
18 |
|
|
inputCgiarRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdySRTM_BL.tif")
|
19 |
|
|
inputAsterRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyASTER_BL.tif")
|
20 |
|
|
inputMosaicRaster <- raster("/data/project/organisms/rcr/ValidateBoundary/mergeCgiarAsterBdyTuesdayClip.tif")
|
21 |
|
|
inputCDEMRaster <- raster("/data/project/organisms/rcr/ValidateBoundary/CDemMosTuesdayClipMergeSpace.tif")
|
22 |
|
|
#
|
23 |
|
|
# Difference image for entire merged image takes a while to create,
|
24 |
|
|
# so we created it once, now read it back in.
|
25 |
|
|
#
|
26 |
|
|
rDeltaWhole <- raster("/data/project/organisms/rcr/ValidateBoundary/DeltaEntireImage.tif")
|
27 |
|
|
rDeltaWhole@data@values <-getValues(rDeltaWhole)
|
28 |
|
|
|
29 |
|
|
# Create extent objects used to extract raster subimges.
|
30 |
|
|
# The object will be centered along the 60 degree North latitude line,
|
31 |
|
|
# and have varying depths (number of rows).
|
32 |
|
|
# The Western Canada study area runs from -135 (west) to -100 (west) longitude,
|
33 |
|
|
# and 55.0 to 64.00 degrees (north) latitude.
|
34 |
|
|
# the ASTER and SRTM/CGIAR image components are merged at the 60 Deg N Latitude line.
|
35 |
|
|
|
36 |
|
|
#eTestAreaExtent <- extent(-135.2,-100.2, 59.997,60.001) # Creates a 5 row subimage
|
37 |
|
|
eTestAreaExtent <- extent(-135.2,-100.2, 59.995,60.005) # Creates a 12 row subimage
|
38 |
|
|
|
39 |
|
|
# Extract a sub image corresponding to the selected extent.
|
40 |
|
|
# Two different alternatives:
|
41 |
|
|
# The extract() function returns a vector of cell values,
|
42 |
|
|
# the crop() function returns a complete raster* object.
|
43 |
|
|
|
44 |
|
|
rEdgeRegionAster <- crop(inputAsterRaster,eTestAreaExtent)
|
45 |
|
|
rEdgeRegionCgiar <- crop(inputCgiarRaster,eTestAreaExtent)
|
46 |
|
|
rEdgeRegionMosaic <- crop(inputMosaicRaster,eTestAreaExtent)
|
47 |
|
|
rEdgeRegionCDEM <- crop(inputCDEMRaster,eTestAreaExtent)
|
48 |
|
|
|
49 |
|
|
# Important: In order for the image subtraction to work, the extents
|
50 |
|
|
# of the two images must be IDENTICAL. I used ArcMap GIS Raster Crop By Mask
|
51 |
|
|
# to create subimages with identical extents.
|
52 |
|
|
|
53 |
|
|
# Compute the difference image for the entire study area, and for the region along
|
54 |
|
|
# the boundary (narrow, maybe 10 pixels either side)
|
55 |
|
|
|
56 |
|
|
rEdgeRegionDelta <- rEdgeRegionMosaic - rEdgeRegionCDEM # not used in this version (yet)
|
57 |
|
|
|
58 |
|
|
# get a vector of random column index numbers, constrained by column dimension of image
|
59 |
|
|
# Loop three times, sampling pixel pairs from above, below, across the border
|
60 |
|
|
|
61 |
ad35c236
|
Rick Reeves
|
nColsToGet <- 4000
|
62 |
b67964e8
|
Rick Reeves
|
iDiffVecNorth <- vector(mode="integer",length=nColsToGet)
|
63 |
|
|
iDiffVecBorder <- vector(mode="integer",length=nColsToGet)
|
64 |
|
|
iDiffVecSouth <- vector(mode="integer",length=nColsToGet)
|
65 |
|
|
|
66 |
|
|
# Here is the output matrix, (nColSamples * 3) rows, four columns
|
67 |
|
|
colsToGet <-sample(1:inputMosaicRaster@ncols,nColsToGet)
|
68 |
|
|
|
69 |
|
|
mOutTable <- matrix(nrow=(nColsToGet * 3), ncol=5)
|
70 |
|
|
colnames(mOutTable) <- c("ColumnID","elevNorth","elevSouth","cdemNorth","cdemSouth")
|
71 |
|
|
# in this difference image, the border edge occurs at column 6
|
72 |
ad35c236
|
Rick Reeves
|
|
73 |
|
|
# NOTE Wed eve: A better start and end for the border: firstRow=5, lastRow = 8....
|
74 |
|
|
iFirstRow <- 5 #4 # two rows before the border
|
75 |
|
|
iLastRow <- 8 #7 # two rows after the border
|
76 |
b67964e8
|
Rick Reeves
|
iRowCtr = 1 # points to latest output table being written
|
77 |
ad35c236
|
Rick Reeves
|
message(sprintf("starting col sample loop"))
|
78 |
b67964e8
|
Rick Reeves
|
for (iNextCol in colsToGet)
|
79 |
|
|
{
|
80 |
ad35c236
|
Rick Reeves
|
#message(sprintf("in col sample loop getting col sample %d - hit key..",iNextCol))
|
81 |
|
|
#browser()
|
82 |
|
|
# slight misalignement in the Aster and Cgiar edge regions, getting NAs wheen extracting values.
|
83 |
|
|
# For now, get values on both sides of border from the mosaic
|
84 |
|
|
# rColVecAster <- cellFromRowCol(rEdgeRegionAster,iFirstRow:(iLastRow),iNextCol:iNextCol) # figure out the actual row numbers for mosaic components LATER
|
85 |
|
|
# rColVecCgiar <- cellFromRowCol(rEdgeRegionCgiar,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
86 |
|
|
# rColVecAster <- cellFromRowCol(rEdgeRegionMosaic,iFirstRow:(iLastRow),iNextCol:iNextCol) # approximation is the Cgiar/Aster area in mosaic.
|
87 |
|
|
# rColVecCgiar <- cellFromRowCol(rEdgeRegionMosaic,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
88 |
|
|
rColVecMosaic <- cellFromRowCol(rEdgeRegionMosaic,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
89 |
|
|
rColVecCDEM <- cellFromRowCol(rEdgeRegionCDEM,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
90 |
b67964e8
|
Rick Reeves
|
|
91 |
|
|
# Split the column vector into the pairs that Mark requested
|
92 |
|
|
# For each column sampled, the output table has three rows:
|
93 |
|
|
#
|
94 |
|
|
# Row 1: from (input) Aster layer: Two pixels above border
|
95 |
ad35c236
|
Rick Reeves
|
# Row 2: from (input) Mosaic layer: Two pixels straddling border rDeltaWhole@data@values[rColVec]
|
96 |
b67964e8
|
Rick Reeves
|
# Row 3: from (input) Cgiar layer: Two pixels below border
|
97 |
|
|
#
|
98 |
|
|
# each with four columns, arranged into two column pairs:
|
99 |
|
|
#
|
100 |
|
|
# North Pixel and South Pixel elevation, North Pixel and South Pixel CDEM (baseline)
|
101 |
|
|
#
|
102 |
ad35c236
|
Rick Reeves
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
103 |
|
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[1:2]] # First column pair from extracted vector:
|
104 |
|
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[1:2]] # entirely top (ASTER part of mosaic) image
|
105 |
|
|
iRowCtr = iRowCtr + 1
|
106 |
b67964e8
|
Rick Reeves
|
#
|
107 |
ad35c236
|
Rick Reeves
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
108 |
|
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[2:3]] # Second column pair:
|
109 |
|
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[2:3]] # straddles border region
|
110 |
|
|
iRowCtr = iRowCtr + 1
|
111 |
|
|
#
|
112 |
|
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
113 |
|
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[3:4]] # third column pair:
|
114 |
|
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[3:4]] # entirely bottom (SRTM part of mosaic) image
|
115 |
|
|
iRowCtr = iRowCtr + 1
|
116 |
b67964e8
|
Rick Reeves
|
}
|
117 |
|
|
|
118 |
ad35c236
|
Rick Reeves
|
# write the table out as CSV file
|
119 |
b67964e8
|
Rick Reeves
|
|
120 |
ad35c236
|
Rick Reeves
|
message("end of loop - hit key to write output table..")
|
121 |
|
|
browser()
|
122 |
|
|
write.csv(mOutTable,file="tableForMark4000_5_8.csv",row.names=FALSE)
|
123 |
b67964e8
|
Rick Reeves
|
#
|
124 |
|
|
}
|