Project

General

Profile

Download (7.54 KB) Statistics
| Branch: | Revision:
1
##################################################################################### 
2
# FixDEMMosaicBlendedRowsExpDecay: Implements Image Blending treatment to DEM Mosaic image
3
# Collection of functions that attempt to mitigate ASTER / SRTM the boundary edge artifact
4
# This version implements the Exponential Decay function to control image blending rate.
5
# Inputs: 
6
#   1) DEM Mosaic image containing 'boundary edge' artifact 
7
#   2) 'Aster-Only' subimage corresponding to the NORTHernmost part (adjacent to the 60 degree Lat 
8
#     boundary) of the SRTM component of the mosaic.
9
#   3) 'Aster-Only' subimage corresponding to the SOUTHernmost part (adjacent to the 60 degree Lat 
10
#     boundary) of the ASTER component of the mosaic.
11
#   4) numBlendRows: number of number of rows south of 60 degrees N Lat to modify ('blend') by
12
#      generating (lnear) combination of SRTM and ASTER image pixels. 
13
#   5) dKc: Empirically-determined exponential decay constant. Rule of thumb: set this such 
14
#      that exponential decay function approaches zero as row 'numBlendRows' is treated.
15
#
16
# Output: 
17
#   1) A copy of the input DEM mosaic, with 'blended image' boundary edge treatment.
18
#
19
# To run:
20
#
21
#  1) start R
22
#  2) > source("FixDEMMosaicBlendedRows.r")
23
#  3) > FixDEMMosaicBlendedRows()
24
#
25
# Note: the input images used by this program located on /jupiter: /data/project/organisms/rcr/BoundaryFixTest
26
#    
27
# Author: Rick Reeves, NCEAS
28
# June 13, 2011
29
#
30
##############################################################################################
31
FixDEMMosaicBlendedRowsExpDecay <- function()
32
{
33
  require(raster)
34
  require(rgdal)
35
 
36
# Key parameter: number of rows south of 60 degrees N Lat to modify ('blend') by
37
# generating (lnear) combination of SRTM and ASTER image pixels. 
38

    
39
  numBlendRows <- 100
40
  dKc <- .045 # empirically determined exponential decay constant. Select it 
41
#              so that exp - 1 decay goes to zero in the desired number of image rows.
42
  
43
# Read the mosaic image and component sub-images:
44
#   - Aster/SRTM mosaic image (with boundary edge to be fixed)
45
#   - 'Aster-only' subimage adjacent to Southern edge of 60 degree bounls -s dary,
46
#     sampled into a grid with same extent and spatial resolution as image mosaic
47
#   - 'Aster-only' subimage adjacent to Northern edge of 60 degree boundary,
48
#     sampled into a grid with same extent and spatial resolution as image mosaic
49
#     inAsterNorthFG  <- raster("./aster60DegNorthRasterFG.tif")     
50
#     inAsterSouthFG  <- raster("./aster59DegSouthRasterFG.tif")
51
#     inBdyAsterBoth  <- raster("./boundaryTestCase115DegBothAsterMos.img")      
52

    
53
     inMosaic      <- raster("./AsterSrtm3ArcSecTestImg.tif") 
54
     inAsterSouth  <- raster("./boundaryTest115_59DegreeAster.img")   
55
     inAsterSouthFG  <- raster("./aster59DegSouthRasterFG.tif")     
56
     inAsterNorth  <- raster("./boundaryTest115_60DegreeAster.img")     
57

    
58
# First, get the extent of the 'below 60' sub image in the (big) input raster
59

    
60
     northAsterEx <- extent(inAsterNorth)
61
     southAsterEx <- extent(inAsterSouth)
62

    
63
# Create copy of input raster that we will use to create a 'fixed boundary' iamge
64
# Even SIMPLER, according to raster documentation
65

    
66
     outMosaic <- raster(inMosaic) 
67
     
68
     sFixedRasterFile <- "TestOutRasterBdyBlendFixExpDec.tif"
69
     
70
#  create the output raster by copying the input file with 'block copy'
71
     
72
message("Copy input mosaic: Create output (repaired) mosaic")
73
#browser()
74
     bs <- blockSize(inMosaic) 
75
     outMosaic <- writeStart(outMosaic,sFixedRasterFile, datatype="INT2S", format="GTiff",overwrite=TRUE)
76
     for (iCtr in 1 : bs$n)
77
     {
78
        mosaicVals <- getValues(inMosaic,row=bs$row[iCtr],nrows=bs$nrows[iCtr])
79
        writeValues(outMosaic,mosaicVals,bs$row[iCtr])
80
        message(sprintf(".....processed input mosaic block %d",iCtr))        
81
     }
82
     outMosaic <- writeStop(outMosaic)
83
# 
84
message("Input mosaic copied to output raster: now, process boundary")
85
browser()
86

    
87
# now, we SHOULD be able to modify outMosaic with new 'column values'.
88

    
89
     southCellsOfInterest <- cellsFromExtent(outMosaic,southAsterEx)
90
     northCellsOfInterest <- cellsFromExtent(outMosaic,northAsterEx)
91
     
92
     firstNorthSubImgRow <- rowFromCell(outMosaic,northCellsOfInterest[1])   
93
     lastNorthSubImgRow <- rowFromCell(outMosaic,northCellsOfInterest[(length(northCellsOfInterest) - 1)])
94
 
95
     firstSouthSubImgRow <- rowFromCell(outMosaic,southCellsOfInterest[1])   
96
     lastSouthSubImgRow <- rowFromCell(outMosaic,southCellsOfInterest[(length(southCellsOfInterest) - 1)])
97

    
98
# note: last north image and first south image rows are the same.
99

    
100
     northBorderEdgeRow <- lastNorthSubImgRow
101
     southBorderEdgeRow <- firstSouthSubImgRow + 1
102

    
103
# The border 'edge' row is one row below the top of the south sub image
104

    
105
     northBrdrRowVals <- getValues(outMosaic,northBorderEdgeRow,1)
106
     southBrdrRowVals <- getValues(outMosaic,southBorderEdgeRow,1)     
107
 
108
# Process the mosaic rows:
109
# Blend the first 'numBlendRows' south of the 60 degree line (first rows of the SRTM component)
110
# with the underlying and corresponding ASTER image rows.
111
# For first attempt, use a linear combination; for second attempt, use an exponential function.
112
#
113
     nImageCols <- ncol(inMosaic)     
114
     sRows <- seq(southBorderEdgeRow,((southBorderEdgeRow + numBlendRows) - 1))
115
# 
116
     rowVecValuesBlend <- vector(mode="integer",length=nImageCols)
117
     iRowCtr <- 1
118
     for (curMosRow in sRows)     
119
     {
120
         message(sprintf("transforming cur row: %d",curMosRow))
121
# exponential decay diminishes impact of the 'delta' edge adjustment with distance from the boundary.
122

    
123
         dK <- exp(-(iRowCtr * dKc))
124
#         colToMod <- (numBlendRows-iCtr)+1
125
#               qq <- as.integer(round(colVecValues[colToMod] + (curRowBoundaryOffset * dK)))
126
#               colVecValues[colToMod] <- as.integer(round(colVecValues[colToMod] + (curRowBoundaryOffset * dK)))
127
#               colVecValues[iCtr] <- as.integer(round(colVecValues[iCtr] + (curRowBoundaryOffset * dK)))
128

    
129
# even simpler here, as we implement a 'linecar distance decay' function,
130
# and let each new column be a linear combination of the Aster and CGIAR image layers
131

    
132
#       deltaInc <- iRowCtr / as.numeric(numBlendRows)
133
        colVecCells <- cellFromRowCol(outMosaic,curMosRow,1:nImageCols) 
134

    
135
# get the current row from the mosaic(SRTM data) and raster images
136
# We get the ASTER values from a version of the 'South' ASTER image 
137
# with same extent as the image mosaic.
138

    
139
        rowVecValuesMosaic <- getValuesBlock(outMosaic,row=curMosRow,nrows=1,col=1,ncols=nImageCols) 
140
        rowVecValuesAster <- getValuesBlock(inAsterSouthFG,row=curMosRow,nrows=1,col=1,ncols=nImageCols)        
141
#        rowVecValuesBlend <- (rowVecValuesMosaic * deltaInc) + (rowVecValuesAster * (1.0 - deltaInc))
142
        rowVecValuesBlend <- (rowVecValuesAster * dK) + (rowVecValuesMosaic * (1.0 - dK))
143

    
144
# create the merged row as a linear combination of the Mosaic and Aster image. 
145
# note: we need to deal with NA values, which are possible. 
146

    
147
# write the blended row to the output mosaic
148

    
149
        outMosaic[colVecCells] <- as.integer(round(rowVecValuesBlend))
150
        iRowCtr <- iRowCtr + 1
151
     }
152
#message(sprintf("cur col: %d about to write to outMosaic",curMosCol))
153
#browser()
154

    
155
# write the modified outMosaic values to the image file that we created.
156

    
157
message("Done with raster blending : hit key to update raster disk file..")
158
browser()
159
     writeRaster(outMosaic, sFixedRasterFile, datatype="INT2S", format="GTiff",overwrite=TRUE)
160
#browser()
161
}
(7-7/19)