def main(iargs=None): ''' Create coregistered overlap secondarys. ''' inps = cmdLineParse(iargs) referenceSwathList = ut.getSwathList(inps.reference) secondarySwathList = ut.getSwathList(inps.secondary) swathList = list(sorted(set(referenceSwathList + secondarySwathList))) for swath in swathList: ####Load secondary metadata reference = ut.loadProduct( os.path.join(inps.reference, 'IW{0}.xml'.format(swath))) secondary = ut.loadProduct( os.path.join(inps.secondary, 'IW{0}.xml'.format(swath))) if inps.overlap: referenceTop = ut.loadProduct( os.path.join(inps.reference, inps.overlapDir, 'IW{0}_top.xml'.format(swath))) referenceBottom = ut.loadProduct( os.path.join(inps.reference, inps.overlapDir, 'IW{0}_bottom.xml'.format(swath))) dt = secondary.bursts[0].azimuthTimeInterval dr = secondary.bursts[0].rangePixelSize if os.path.exists(str(inps.misreg_az)): with open(inps.misreg_az, 'r') as f: misreg_az = float(f.readline()) else: misreg_az = 0.0 if os.path.exists(str(inps.misreg_rng)): with open(inps.misreg_rng, 'r') as f: misreg_rg = float(f.readline()) else: misreg_rg = 0.0 ###Output directory for coregistered SLCs if not inps.overlap: outdir = os.path.join(inps.coreg, 'IW{0}'.format(swath)) offdir = os.path.join(inps.coreg, 'IW{0}'.format(swath)) else: outdir = os.path.join(inps.coreg, inps.overlapDir, 'IW{0}'.format(swath)) offdir = os.path.join(inps.coreg, inps.overlapDir, 'IW{0}'.format(swath)) os.makedirs(outdir, exist_ok=True) ####Indices w.r.t reference burstoffset, minBurst, maxBurst = reference.getCommonBurstLimits( secondary) secondaryBurstStart = minBurst + burstoffset secondaryBurstEnd = maxBurst relShifts = ut.getRelativeShifts(reference, secondary, minBurst, maxBurst, secondaryBurstStart) print('Shifts: ', relShifts) if inps.overlap: maxBurst = maxBurst - 1 ###For overlaps ####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 = ut.coregSwathSLCProduct() topCoreg.configure() if inps.overlap: botCoreg = ut.coregSwathSLCProduct() botCoreg.configure() for ii in range(minBurst, maxBurst): jj = secondaryBurstStart + ii - minBurst if inps.overlap: botBurst = referenceBottom.bursts[ii] topBurst = referenceTop.bursts[ii] else: topBurst = reference.bursts[ii] secBurst = secondary.bursts[jj] #####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' .format(jj)) if inps.overlap: 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( secBurst, offset=-1.0 * offset) rdict['carrPoly'] = azCarrPoly rdict['doppPoly'] = dpoly outimg = resampSecondary(topBurst, secBurst, rdict, outname, (not inps.noflat)) copyBurst = copy.deepcopy(topBurst) ut.adjustValidSampleLine(copyBurst) copyBurst.image.filename = outimg.filename print('After: ', copyBurst.firstValidLine, copyBurst.numValidLines) topCoreg.bursts.append(copyBurst) ####################################################### secBurst = secondary.bursts[jj + 1] 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( secBurst, offset=-1.0 * offset) rdict['carrPoly'] = azCarrPoly rdict['doppPoly'] = dpoly outimg = resampSecondary(botBurst, secBurst, rdict, outname, (not inps.noflat)) copyBurst = copy.deepcopy(botBurst) ut.adjustValidSampleLine(copyBurst) copyBurst.image.filename = outimg.filename print('After: ', copyBurst.firstValidLine, copyBurst.numValidLines) botCoreg.bursts.append(copyBurst) ####################################################### else: outname = os.path.join(outdir, 'burst_%02d.slc' % (ii + 1)) ####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, f'range{suffix}_{ii+1:02d}.off'), 'azimuthOff': os.path.join(offdir, f'azimuth{suffix}_{ii+1:02d}.off') } ###For future - should account for azimuth and range misreg here .. ignoring for now. azCarrPoly, dpoly = secondary.estimateAzimuthCarrierPolynomials( secBurst, offset=-1.0 * offset) rdict['carrPoly'] = azCarrPoly rdict['doppPoly'] = dpoly outimg = resampSecondary(topBurst, secBurst, rdict, outname, (not inps.noflat)) minAz, maxAz, minRg, maxRg = ut.getValidLines( secBurst, rdict, outname, misreg_az=misreg_az - offset, misreg_rng=misreg_rg) copyBurst = copy.deepcopy(topBurst) ut.adjustValidSampleLine_V2(copyBurst, secBurst, minAz=minAz, maxAz=maxAz, minRng=minRg, maxRng=maxRg) copyBurst.image.filename = outimg.filename print('After: ', copyBurst.firstValidLine, copyBurst.numValidLines) topCoreg.bursts.append(copyBurst) ####################################################### topCoreg.numberOfBursts = len(topCoreg.bursts) topCoreg.source = ut.asBaseClass(secondary) if inps.overlap: botCoreg.numberOfBursts = len(botCoreg.bursts) topCoreg.reference = ut.asBaseClass(referenceTop) botCoreg.reference = ut.asBaseClass(referenceBottom) botCoreg.source = ut.asBaseClass(secondary) ut.saveProduct(topCoreg, outdir + '_top.xml') ut.saveProduct(botCoreg, outdir + '_bottom.xml') else: topCoreg.reference = reference ut.saveProduct(topCoreg, outdir + '.xml')
def main(iargs=None): ''' Create coregistered overlap secondarys. ''' inps = cmdLineParse(iargs) referenceSwathList = ut.getSwathList(inps.reference) secondarySwathList = ut.getSwathList(inps.secondary) swathList = list(sorted(set(referenceSwathList+secondarySwathList))) #if os.path.abspath(inps.reference) == os.path.abspath(inps.secondary): # print('secondary is the same as reference, only performing subband filtering') for swath in swathList: ####Load secondary metadata reference = ut.loadProduct( os.path.join(inps.reference , 'IW{0}.xml'.format(swath))) secondary = ut.loadProduct( os.path.join(inps.secondary , 'IW{0}.xml'.format(swath))) if os.path.exists(str(inps.misreg_az)): with open(inps.misreg_az, 'r') as f: misreg_az = float(f.readline()) else: misreg_az = 0.0 if os.path.exists(str(inps.misreg_rng)): with open(inps.misreg_rng, 'r') as f: misreg_rg = float(f.readline()) else: misreg_rg = 0.0 ###Output directory for coregistered SLCs outdir = os.path.join(inps.coreg,'IW{0}'.format(swath)) offdir = os.path.join(inps.coreg,'IW{0}'.format(swath)) os.makedirs(outdir, exist_ok=True) ####Indices w.r.t reference burstoffset, minBurst, maxBurst = reference.getCommonBurstLimits(secondary) secondaryBurstStart = minBurst + burstoffset secondaryBurstEnd = maxBurst relShifts = ut.getRelativeShifts(reference, secondary, minBurst, maxBurst, secondaryBurstStart) print('Shifts: ', 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.]]) #slvCoreg = createTOPSSwathSLCProduct() slvCoreg = ut.coregSwathSLCProduct() slvCoreg.configure() for ii in range(minBurst, maxBurst): outname = os.path.join(outdir, 'burst_%02d.slc'%(ii+1)) outnameLower = os.path.splitext(outname)[0]+'_lower.slc' outnameUpper = os.path.splitext(outname)[0]+'_upper.slc' if os.path.exists(outnameLower) and os.path.exists(outnameLower+'.vrt') and os.path.exists(outnameLower+'.xml') and \ os.path.exists(outnameUpper) and os.path.exists(outnameUpper+'.vrt') and os.path.exists(outnameUpper+'.xml'): print('burst %02d already processed, skip...'%(ii+1)) continue jj = secondaryBurstStart + ii - minBurst masBurst = reference.bursts[ii] slvBurst = secondary.bursts[jj] #####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'.format(jj)) ####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_%02d.off'%(ii+1)), 'azimuthOff': os.path.join(offdir, 'azimuth_%02d.off'%(ii+1))} ###For future - should account for azimuth and range misreg here .. ignoring for now. azCarrPoly, dpoly = secondary.estimateAzimuthCarrierPolynomials(slvBurst, offset = -1.0 * offset) rdict['carrPoly'] = azCarrPoly rdict['doppPoly'] = dpoly #subband filtering from Stack import ionParam from isceobj.Constants import SPEED_OF_LIGHT rangeSamplingRate = SPEED_OF_LIGHT / (2.0 * slvBurst.rangePixelSize) ionParamObj=ionParam() ionParamObj.configure() lower_tmpfile = os.path.splitext(slvBurst.image.filename)[0]+'_lower_tmp.slc' upper_tmpfile = os.path.splitext(slvBurst.image.filename)[0]+'_upper_tmp.slc' outputfile = [lower_tmpfile, upper_tmpfile] bw = [ionParamObj.rgBandwidthSub / rangeSamplingRate, ionParamObj.rgBandwidthSub / rangeSamplingRate] bc = [-ionParamObj.rgBandwidthForSplit / 3.0 / rangeSamplingRate, ionParamObj.rgBandwidthForSplit / 3.0 / rangeSamplingRate] rgRef = ionParamObj.rgRef subband(slvBurst, 2, outputfile, bw, bc, rgRef, True) #resampling slvBurst.radarWavelength = ionParamObj.radarWavelengthLower slvBurst.image.filename = lower_tmpfile outnameSubband = outnameLower outimg = resampSecondary(masBurst, slvBurst, rdict, outnameSubband, (not inps.noflat)) slvBurst.radarWavelength = ionParamObj.radarWavelengthUpper slvBurst.image.filename = upper_tmpfile outnameSubband = outnameUpper outimg = resampSecondary(masBurst, slvBurst, rdict, outnameSubband, (not inps.noflat)) #remove original subband images os.remove(lower_tmpfile) os.remove(lower_tmpfile+'.vrt') os.remove(lower_tmpfile+'.xml') os.remove(upper_tmpfile) os.remove(upper_tmpfile+'.vrt') os.remove(upper_tmpfile+'.xml')