ISCE_INSAR/components/isceobj/TopsProc/runCoarseResamp.py

211 lines
7.3 KiB
Python
Raw Normal View History

2019-01-16 19:40:08 +00:00
#
# Author: Piyush Agram
# Copyright 2016
#
import isce
import isceobj
import stdproc
from stdproc.stdproc import crossmul
import numpy as np
from isceobj.Util.Poly2D import Poly2D
import os
import copy
from isceobj.Sensor.TOPS import createTOPSSwathSLCProduct
from .runFineResamp import getRelativeShifts, adjustValidSampleLine
def resampSecondary(mas, slv, rdict, outname ):
2019-01-16 19:40:08 +00:00
'''
Resample burst by burst.
'''
azpoly = rdict['azpoly']
rgpoly = rdict['rgpoly']
azcarrpoly = rdict['carrPoly']
dpoly = rdict['doppPoly']
rngImg = isceobj.createImage()
rngImg.load(rdict['rangeOff'] + '.xml')
rngImg.setAccessMode('READ')
aziImg = isceobj.createImage()
aziImg.load(rdict['azimuthOff'] + '.xml')
aziImg.setAccessMode('READ')
inimg = isceobj.createSlcImage()
inimg.load(slv.image.filename + '.xml')
inimg.setAccessMode('READ')
rObj = stdproc.createResamp_slc()
rObj.slantRangePixelSpacing = slv.rangePixelSize
rObj.radarWavelength = slv.radarWavelength
rObj.azimuthCarrierPoly = azcarrpoly
rObj.dopplerPoly = dpoly
rObj.azimuthOffsetsPoly = azpoly
rObj.rangeOffsetsPoly = rgpoly
rObj.imageIn = inimg
####Setting reference values
rObj.startingRange = slv.startingRange
rObj.referenceSlantRangePixelSpacing = mas.rangePixelSize
rObj.referenceStartingRange = mas.startingRange
rObj.referenceWavelength = mas.radarWavelength
width = mas.numberOfSamples
length = mas.numberOfLines
imgOut = isceobj.createSlcImage()
imgOut.setWidth(width)
imgOut.filename = outname
imgOut.setAccessMode('write')
rObj.outputWidth = width
rObj.outputLines = length
rObj.residualRangeImage = rngImg
rObj.residualAzimuthImage = aziImg
rObj.resamp_slc(imageOut=imgOut)
imgOut.renderHdr()
return imgOut
def runCoarseResamp(self):
'''
Create coregistered overlap secondarys.
2019-01-16 19:40:08 +00:00
'''
if not self.doESD:
return
swathList = self._insar.getValidSwathList(self.swaths)
for swath in swathList:
if self._insar.numberOfCommonBursts[swath-1] < 2:
print('Skipping coarse resamp for swath IW{0}'.format(swath))
continue
####Load secondary metadata
reference = self._insar.loadProduct( os.path.join(self._insar.referenceSlcProduct, 'IW{0}.xml'.format(swath)))
secondary = self._insar.loadProduct( os.path.join(self._insar.secondarySlcProduct, 'IW{0}.xml'.format(swath)))
referenceTop = self._insar.loadProduct( os.path.join(self._insar.referenceSlcOverlapProduct, 'top_IW{0}.xml'.format(swath)))
referenceBottom = self._insar.loadProduct( os.path.join(self._insar.referenceSlcOverlapProduct, 'bottom_IW{0}.xml'.format(swath)))
2019-01-16 19:40:08 +00:00
dt = secondary.bursts[0].azimuthTimeInterval
dr = secondary.bursts[0].rangePixelSize
2019-01-16 19:40:08 +00:00
###Output directory for coregistered SLCs
outdir = os.path.join(self._insar.coarseCoregDirname, self._insar.overlapsSubDirname, 'IW{0}'.format(swath))
os.makedirs(outdir, exist_ok=True)
2019-01-16 19:40:08 +00:00
###Directory with offsets
offdir = os.path.join(self._insar.coarseOffsetsDirname, self._insar.overlapsSubDirname, 'IW{0}'.format(swath))
####Indices w.r.t reference
minBurst, maxBurst = self._insar.commonReferenceBurstLimits(swath-1)
secondaryBurstStart, secondaryBurstEnd = self._insar.commonSecondaryBurstLimits(swath-1)
2019-01-16 19:40:08 +00:00
relShifts = getRelativeShifts(reference, secondary, minBurst, maxBurst, secondaryBurstStart)
2019-01-16 19:40:08 +00:00
maxBurst = maxBurst - 1 ###For overlaps
print('Shifts for swath IW-{0}: {1}'.format(swath,relShifts))
####Can corporate known misregistration here
apoly = Poly2D()
apoly.initPoly(rangeOrder=0,azimuthOrder=0,coeffs=[[0.]])
rpoly = Poly2D()
rpoly.initPoly(rangeOrder=0,azimuthOrder=0,coeffs=[[0.]])
topCoreg = createTOPSSwathSLCProduct()
topCoreg.configure()
botCoreg = createTOPSSwathSLCProduct()
botCoreg.configure()
for ii in range(minBurst, maxBurst):
jj = secondaryBurstStart + ii - minBurst
2019-01-16 19:40:08 +00:00
topBurst = referenceTop.bursts[ii-minBurst]
botBurst = referenceBottom.bursts[ii-minBurst]
slvBurst = secondary.bursts[jj]
2019-01-16 19:40:08 +00:00
#####Top burst processing
try:
offset = relShifts[jj]
except:
raise Exception('Trying to access shift for secondary burst index {0}, which may not overlap with reference - IW-{1}'.format(jj, swath))
2019-01-16 19:40:08 +00:00
outname = os.path.join(outdir, 'burst_top_%02d_%02d.slc'%(ii+1,ii+2))
####Setup initial polynomials
### If no misregs are given, these are zero
### If provided, can be used for resampling without running to geo2rdr again for fast results
rdict = {'azpoly' : apoly,
'rgpoly' : rpoly,
'rangeOff' : os.path.join(offdir, 'range_top_%02d_%02d.off'%(ii+1,ii+2)),
'azimuthOff': os.path.join(offdir, 'azimuth_top_%02d_%02d.off'%(ii+1,ii+2))}
###For future - should account for azimuth and range misreg here .. ignoring for now.
azCarrPoly, dpoly = secondary.estimateAzimuthCarrierPolynomials(slvBurst, offset = -1.0 * offset)
2019-01-16 19:40:08 +00:00
rdict['carrPoly'] = azCarrPoly
rdict['doppPoly'] = dpoly
outimg = resampSecondary(topBurst, slvBurst, rdict, outname)
2019-01-16 19:40:08 +00:00
copyBurst = topBurst.clone()
adjustValidSampleLine(copyBurst, slvBurst)
copyBurst.image.filename = outimg.filename
print('After: ', copyBurst.firstValidLine, copyBurst.numValidLines)
topCoreg.bursts.append(copyBurst)
#######################################################
slvBurst = secondary.bursts[jj+1]
2019-01-16 19:40:08 +00:00
outname = os.path.join(outdir, 'burst_bot_%02d_%02d.slc'%(ii+1,ii+2))
####Setup initial polynomials
### If no misregs are given, these are zero
### If provided, can be used for resampling without running to geo2rdr again for fast results
rdict = {'azpoly' : apoly,
'rgpoly' : rpoly,
'rangeOff' : os.path.join(offdir, 'range_bot_%02d_%02d.off'%(ii+1,ii+2)),
'azimuthOff': os.path.join(offdir, 'azimuth_bot_%02d_%02d.off'%(ii+1,ii+2))}
azCarrPoly, dpoly = secondary.estimateAzimuthCarrierPolynomials(slvBurst, offset = -1.0 * offset)
2019-01-16 19:40:08 +00:00
rdict['carrPoly'] = azCarrPoly
rdict['doppPoly'] = dpoly
outimg = resampSecondary(botBurst, slvBurst, rdict, outname)
2019-01-16 19:40:08 +00:00
copyBurst = botBurst.clone()
adjustValidSampleLine(copyBurst, slvBurst)
copyBurst.image.filename = outimg.filename
print('After: ', copyBurst.firstValidLine, copyBurst.numValidLines)
botCoreg.bursts.append(copyBurst)
#######################################################
topCoreg.numberOfBursts = len(topCoreg.bursts)
botCoreg.numberOfBursts = len(botCoreg.bursts)
self._insar.saveProduct(topCoreg, os.path.join(self._insar.coregOverlapProduct, 'top_IW{0}.xml'.format(swath)))
self._insar.saveProduct(botCoreg, os.path.join(self._insar.coregOverlapProduct, 'bottom_IW{0}.xml'.format(swath)))