Project

General

Profile

Download (6.16 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
# May 17: This version includes raster() extents and input
12
#         image names used to create the (normalized)boundary  
13
#         area Difference Image scatterplots used in the most
14
#         recent 'boundary analysis'
15
##############################################################################################
16
#
17
makeImagePairTable <- function()
18
{
19
require(raster)
20
require(rgdal)
21

    
22
inputCgiarRaster  <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdySRTM_BLEven.tif")
23
inputAsterRaster  <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyASTER_BLEven.tif")
24
inputMosaicRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyFinalBLEven.tif")
25
inputCDEMRaster   <- raster("/data/project/organisms/rcr/ValidateBoundary/CDemMosSundayClipMergeSpaceEven.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.0,-105.0, 59.995,60.005) # Creates a 12 row, 36000 col 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
rEdgeRegionAster <- crop(inputAsterRaster,eTestAreaExtent)
49
rEdgeRegionCgiar <- crop(inputCgiarRaster,eTestAreaExtent)
50
rEdgeRegionMosaic <- crop(inputMosaicRaster,eTestAreaExtent)
51
rEdgeRegionCDEM <- crop(inputCDEMRaster,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
#extent(rEdgeRegionMosaic) = extent(rEdgeRegionCDEM) # raster package author suggests this to resolve slight extent differences
60
rEdgeRegionDelta <- rEdgeRegionMosaic - rEdgeRegionCDEM  
61

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

    
65
nColsToGet <- 36000 # Latest input images on even boundary have 36000 columns
66

    
67
iDiffVecNorth <- vector(mode="integer",length=nColsToGet)
68

    
69
iDiffVecBorder <- vector(mode="integer",length=nColsToGet)
70
iDiffVecSouth <- vector(mode="integer",length=nColsToGet)
71

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

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

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

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

    
119
# write the table out as CSV file
120

    
121
message("end of loop - hit key to write output table..")
122
browser()
123
write.csv(mOutTable,file="/data/project/organisms/rcr/ValidateBoundary/pixelPairs36000_5_8Even.csv",row.names=FALSE)
124
#
125
}
(10-10/17)