Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in DenseAmpcor.py #217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 63 additions & 25 deletions components/mroipac/ampcor/DenseAmpcor.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,42 +345,67 @@ def denseampcor(self,slcImage1 = None,slcImage2 = None):
yMargin = 2*self.searchWindowSizeHeight + self.windowSizeHeight

#####Set image limits for search
offAc = max(self.margin,-coarseAcross)+xMargin
if offAc % self.skipSampleAcross != 0:
leftlim = offAc
offAc = self.skipSampleAcross*(1 + int(offAc/self.skipSampleAcross)) - self.pixLocOffAc
while offAc < leftlim:
offAc += self.skipSampleAcross
# limit_setting_option = 0 is the default and conventional logic of setting limits
# limit_setting_option = 1 is a simpler logic of setting limits
# which is used in the driver (cuDenseOffsets.py) of GPU ampcor (PyCuAmpcor) (Add by Minyan Zhong)

limit_setting_option = 0

if limit_setting_option == 0:
offAc = max(self.margin,-coarseAcross)+xMargin
if offAc % self.skipSampleAcross != 0:
leftlim = offAc
offAc = self.skipSampleAcross*(1 + int(offAc/self.skipSampleAcross)) - self.pixLocOffAc
while offAc < leftlim:
offAc += self.skipSampleAcross

offDn = max(self.margin,-coarseDown)+yMargin
if offDn % self.skipSampleDown != 0:
toplim = offDn
offDn = self.skipSampleDown*(1 + int(offDn/self.skipSampleDown)) - self.pixLocOffDn
while offDn < toplim:
offDn += self.skipSampleDown

offAcmax = int(coarseAcross + ((self.rangeSpacing1/self.rangeSpacing2)-1)*self.lineLength1)
lastAc = int(min(self.lineLength1, self.lineLength2-offAcmax) - xMargin -1 - self.margin)

offDnmax = int(coarseDown + ((self.prf2/self.prf1)-1)*self.fileLength1)
lastDn = int(min(self.fileLength1, self.fileLength2-offDnmax) - yMargin -1 - self.margin)

offDn = max(self.margin,-coarseDown)+yMargin
if offDn % self.skipSampleDown != 0:
toplim = offDn
offDn = self.skipSampleDown*(1 + int(offDn/self.skipSampleDown)) - self.pixLocOffDn
while offDn < toplim:
offDn += self.skipSampleDown

self.gridLocAcross = range(offAc + self.pixLocOffAc, lastAc - self.pixLocOffAc, self.skipSampleAcross)
self.gridLocDown = range(offDn + self.pixLocOffDn, lastDn - self.pixLocOffDn, self.skipSampleDown)

offAcmax = int(coarseAcross + ((self.rangeSpacing1/self.rangeSpacing2)-1)*self.lineLength1)
lastAc = int(min(self.lineLength1, self.lineLength2-offAcmax) - xMargin -1 - self.margin)
startAc, endAc = offAc, self.gridLocAcross[-1] - self.pixLocOffAc
self.numLocationAcross = int((endAc-startAc)/self.skipSampleAcross + 1)
self.numLocationDown = len(self.gridLocDown)

offDnmax = int(coarseDown + ((self.prf2/self.prf1)-1)*self.fileLength1)
lastDn = int(min(self.fileLength1, self.fileLength2-offDnmax) - yMargin -1 - self.margin)
self.offsetCols, self.offsetLines = self.numLocationAcross, self.numLocationDown

elif limit_setting_option == 1:
width = self.lineLength1
length = self.fileLength1

self.gridLocAcross = range(offAc + self.pixLocOffAc, lastAc - self.pixLocOffAc, self.skipSampleAcross)
self.gridLocDown = range(offDn + self.pixLocOffDn, lastDn - self.pixLocOffDn, self.skipSampleDown)
# determine the max number of windows
self.numLocationAcross = (width - 2*self.margin - xMargin)//self.skipSampleAcross
self.numLocationDown = (length - 2*self.margin - yMargin)//self.skipSampleDown

startAc, endAc = offAc, self.gridLocAcross[-1] - self.pixLocOffAc
self.numLocationAcross = int((endAc-startAc)/self.skipSampleAcross + 1)
self.numLocationDown = len(self.gridLocDown)
# deterimine the location of windows
self.gridLocAcross = self.margin + self.skipSampleAcross * np.arange(self.numLocationAcross) + self.pixLocOffAc
self.gridLocDown = self.margin + self.skipSampleDown * np.arange(self.numLocationDown) + self.pixLocOffDn

self.offsetCols, self.offsetLines = self.numLocationAcross, self.numLocationDown
startAc = self.gridLocAcross[0] - self.pixLocOffAc
endAc = self.gridLocAcross[-1] - self.pixLocOffAc

self.offsetCols, self.offsetLines = self.numLocationAcross, self.numLocationDown
else:
raise Exception("limit_setting_option must be 0 or 1")

print('Pixels: ', self.lineLength1, self.lineLength2)
print('Lines: ', self.fileLength1, self.fileLength2)
print('Wins : ', self.windowSizeWidth, self.windowSizeHeight)
print('Srch: ', self.searchWindowSizeWidth, self.searchWindowSizeHeight)


#####Create shared memory objects
numlen = self.numLocationAcross * self.numLocationDown
self.locationDown = np.frombuffer(mp.Array('i', numlen).get_obj(), dtype='i')
Expand Down Expand Up @@ -415,14 +440,20 @@ def denseampcor(self,slcImage1 = None,slcImage2 = None):
iend = istart + proc_num_grid

# Compute corresponding global line/down indices
proc_loc_down = self.gridLocDown[istart:iend]
startDown, endDown = proc_loc_down[0], proc_loc_down[-1]
proc_loc_down = np.asarray(self.gridLocDown[istart:iend])

# Get start and end down
startDown, endDown = proc_loc_down[0] - self.pixLocOffDn, proc_loc_down[-1] - self.pixLocOffDn

numDown = int((endDown - startDown)//self.skipSampleDown + 1)

# Get flattened grid indices
firstind = flat_indices[istart:iend,:].ravel()[0]
lastind = flat_indices[istart:iend,:].ravel()[-1]

print('startAc', 'endAc', 'startDown', 'endDown')
print(startAc, endAc, startDown, endDown)

print(ofmt % (thrd, firstind, lastind, startAc, endAc, startDown, endDown))

# Launch job
Expand Down Expand Up @@ -462,13 +493,15 @@ def _run_ampcor(self, firstAc, lastAc, firstDn, lastDn,
objAmpcor.setImageDataType1(self.imageDataType1)
objAmpcor.setImageDataType2(self.imageDataType2)

# Setting the limits
objAmpcor.setFirstSampleAcross(firstAc)
objAmpcor.setLastSampleAcross(lastAc)
objAmpcor.setNumberLocationAcross(numAc)

objAmpcor.setFirstSampleDown(firstDn)
objAmpcor.setLastSampleDown(lastDn)
objAmpcor.setNumberLocationDown(numDn)
##

objAmpcor.setAcrossGrossOffset(self.acrossGrossOffset)
objAmpcor.setDownGrossOffset(self.downGrossOffset)
Expand Down Expand Up @@ -644,9 +677,14 @@ def checkWindows(self):
if self._stdWriter is None:
self._stdWriter = create_writer("log", "", True, filename="denseampcor.log")

# start from the limit of secondary window
self.pixLocOffAc = self.windowSizeWidth//2 + self.searchWindowSizeWidth - 1
self.pixLocOffDn = self.windowSizeHeight//2 + self.searchWindowSizeHeight - 1

# start from the limit of reference window
self.centerOffsetWidth = self.windowSizeWidth//2 - 1
self.centerOffsetHgt = self.windowSizeHeight//2 - 1

def setImageDataType1(self, var):
self.imageDataType1 = str(var)
return
Expand Down