Project

General

Profile

Download (5.94 KB) Statistics
| Branch: | Revision:
1
##############################################################################################
2
#
3
# makeImagePairTable.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
makeImagePairTable <- function()
14
{
15
require(raster)
16
require(rgdal)
17

    
18
inputCgiarRaster  <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdySRTM_BLEven.tif")
19
inputAsterRaster  <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyASTER_BLEven.tif")
20
inputMosaicRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyFinalBLEven.tif")
21
inputCDEMRaster   <- raster("/data/project/organisms/rcr/ValidateBoundary/CDemMosSundayClipMergeSpaceEven.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
#eTestAreaExtent <- extent(-135.0,-105.0, 59.990,60.010) # Creates a 24 row subimage
39

    
40
# Extract a sub image corresponding to the selected extent.
41
# Two different alternatives:
42
# The extract() function returns a vector of cell values, 
43
# the crop() function returns a complete raster* object.
44

    
45
rEdgeRegionAster <- crop(inputAsterRaster,eTestAreaExtent)
46
rEdgeRegionCgiar <- crop(inputCgiarRaster,eTestAreaExtent)
47
rEdgeRegionMosaic <- crop(inputMosaicRaster,eTestAreaExtent)
48
rEdgeRegionCDEM <- crop(inputCDEMRaster,eTestAreaExtent)
49

    
50
# Important: In order for the image subtraction to work, the extents
51
#            of the two images must be IDENTICAL. I used ArcMap GIS Raster Crop By Mask
52
#            to create subimages with identical extents. 
53

    
54
# Compute the difference image  for the entire study area, and for the region along
55
# the boundary (narrow, maybe 10 pixels either side)
56
#extent(rEdgeRegionMosaic) = extent(rEdgeRegionCDEM) # raster package author suggests this to resolve slight extent differences
57
rEdgeRegionDelta <- rEdgeRegionMosaic - rEdgeRegionCDEM  
58

    
59
# get a vector of random column index numbers, constrained by column dimension of image
60
# Loop three times, sampling pixel pairs from above, below, across the border
61

    
62
nColsToGet <- 2000
63
iDiffVecNorth <- vector(mode="integer",length=nColsToGet)
64

    
65
iDiffVecBorder <- vector(mode="integer",length=nColsToGet)
66
iDiffVecSouth <- vector(mode="integer",length=nColsToGet)
67

    
68
# Here is the output matrix, (nColSamples * 3) rows, four columns 
69
colsToGet <-sample(1:inputMosaicRaster@ncols,nColsToGet)
70

    
71
mOutTable <- matrix(nrow=(nColsToGet * 3), ncol=5)
72
colnames(mOutTable) <- c("ColumnID","elevNorth","elevSouth","cdemNorth","cdemSouth")
73
# in this difference image, the border edge occurs at column 6
74

    
75
# NOTE Wed eve: A better start and end for the border: firstRow=5, lastRow = 8....
76
iFirstRow <- 5 #4 # two rows before the border           
77
iLastRow  <- 8 #7 # two rows after the border
78
iRowCtr = 1 # points to latest output table being written
79
message(sprintf("starting col sample loop"))
80
for (iNextCol in colsToGet)
81
{
82
#message(sprintf("in col sample loop getting col sample %d - hit key..",iNextCol))
83
#browser()
84

    
85
   rColVecMosaic <- cellFromRowCol(rEdgeRegionMosaic,iFirstRow:(iLastRow),iNextCol:iNextCol)
86
   rColVecCDEM   <- cellFromRowCol(rEdgeRegionCDEM,iFirstRow:(iLastRow),iNextCol:iNextCol)  
87
  
88
# Split the column vector into the pairs that Mark requested 
89
# For each column sampled, the output table has three rows:
90
#
91
# Row 1: from (input) Aster layer:  Two pixels above border
92
# Row 2: from (input) Mosaic layer: Two pixels straddling border rDeltaWhole@data@values[rColVec]
93
# Row 3: from (input) Cgiar layer:  Two pixels below border
94
#
95
# each with four columns, arranged into two column pairs:
96
#
97
#  North Pixel and South Pixel elevation, North Pixel and South Pixel CDEM (baseline)
98
# 
99
   mOutTable[iRowCtr,1]   <- iNextCol           # The (radomly sampled) col ID (surrogate for longitude) 
100
   mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[1:2]]  # First column pair from extracted vector: 
101
   mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[1:2]]   # entirely top (ASTER part of mosaic) image 
102
   iRowCtr = iRowCtr + 1
103
#  
104
   mOutTable[iRowCtr,1]   <- iNextCol           # The (radomly sampled) col ID (surrogate for longitude)
105
   mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[2:3]] # Second column pair: 
106
   mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[2:3]]   # straddles border region 
107
   iRowCtr = iRowCtr + 1  
108
#   
109
   mOutTable[iRowCtr,1]   <- iNextCol           # The (radomly sampled) col ID (surrogate for longitude)
110
   mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[3:4]]  # third column pair:
111
   mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[3:4]]   # entirely bottom (SRTM part of mosaic) image 
112
   iRowCtr = iRowCtr + 1  
113
}
114

    
115
# write the table out as CSV file
116

    
117
message("end of loop - hit key to write output table..")
118
browser()
119
write.csv(mOutTable,file="/data/project/organisms/rcr/tableForRick2000_5_8Even.csv",row.names=FALSE)
120
#
121
}
(5-5/10)