def topoGPU(masterTrack, numberRangeLooks, numberAzimuthLooks, demFile, latFile, lonFile, hgtFile, losFile): ''' Try with GPU module. ''' import datetime import numpy as np from isceobj.Planet.Planet import Planet from zerodop.GPUtopozero.GPUtopozero import PyTopozero from isceobj.Util.Poly2D import Poly2D from iscesys import DateTimeUtil as DTU pointingDirection = {'right': -1, 'left': 1} #creat poynomials polyDoppler = Poly2D(name='topsApp_dopplerPoly') polyDoppler.setWidth(masterTrack.numberOfSamples) polyDoppler.setLength(masterTrack.numberOfLines) polyDoppler.setNormRange(1.0) polyDoppler.setNormAzimuth(1.0) polyDoppler.setMeanRange(0.0) polyDoppler.setMeanAzimuth(0.0) polyDoppler.initPoly(rangeOrder=0, azimuthOrder=0, coeffs=[[0.]]) polyDoppler.createPoly2D() slantRangeImage = Poly2D() slantRangeImage.setWidth(masterTrack.numberOfSamples) slantRangeImage.setLength(masterTrack.numberOfLines) slantRangeImage.setNormRange(1.0) slantRangeImage.setNormAzimuth(1.0) slantRangeImage.setMeanRange(0.) slantRangeImage.setMeanAzimuth(0.) slantRangeImage.initPoly( rangeOrder=1, azimuthOrder=0, coeffs=[[ masterTrack.startingRange + (numberRangeLooks - 1.0) / 2.0 * masterTrack.rangePixelSize, numberRangeLooks * masterTrack.rangePixelSize ]]) slantRangeImage.createPoly2D() #creat images latImage = isceobj.createImage() latImage.initImage(latFile, 'write', masterTrack.numberOfSamples, 'DOUBLE') latImage.createImage() lonImage = isceobj.createImage() lonImage.initImage(lonFile, 'write', masterTrack.numberOfSamples, 'DOUBLE') lonImage.createImage() losImage = isceobj.createImage() losImage.initImage(losFile, 'write', masterTrack.numberOfSamples, 'FLOAT', bands=2, scheme='BIL') losImage.setCaster('write', 'DOUBLE') losImage.createImage() heightImage = isceobj.createImage() heightImage.initImage(hgtFile, 'write', masterTrack.numberOfSamples, 'DOUBLE') heightImage.createImage() demImage = isceobj.createDemImage() demImage.load(demFile + '.xml') demImage.setCaster('read', 'FLOAT') demImage.createImage() #compute a few things t0 = masterTrack.sensingStart + datetime.timedelta( seconds=(numberAzimuthLooks - 1.0) / 2.0 * masterTrack.azimuthLineInterval) orb = masterTrack.orbit pegHdg = np.radians(orb.getENUHeading(t0)) elp = Planet(pname='Earth').ellipsoid #call gpu topo topo = PyTopozero() topo.set_firstlat(demImage.getFirstLatitude()) topo.set_firstlon(demImage.getFirstLongitude()) topo.set_deltalat(demImage.getDeltaLatitude()) topo.set_deltalon(demImage.getDeltaLongitude()) topo.set_major(elp.a) topo.set_eccentricitySquared(elp.e2) topo.set_rSpace(numberRangeLooks * masterTrack.rangePixelSize) topo.set_r0(masterTrack.startingRange + (numberRangeLooks - 1.0) / 2.0 * masterTrack.rangePixelSize) topo.set_pegHdg(pegHdg) topo.set_prf(1.0 / (numberAzimuthLooks * masterTrack.azimuthLineInterval)) topo.set_t0(DTU.seconds_since_midnight(t0)) topo.set_wvl(masterTrack.radarWavelength) topo.set_thresh(.05) topo.set_demAccessor(demImage.getImagePointer()) topo.set_dopAccessor(polyDoppler.getPointer()) topo.set_slrngAccessor(slantRangeImage.getPointer()) topo.set_latAccessor(latImage.getImagePointer()) topo.set_lonAccessor(lonImage.getImagePointer()) topo.set_losAccessor(losImage.getImagePointer()) topo.set_heightAccessor(heightImage.getImagePointer()) topo.set_incAccessor(0) topo.set_maskAccessor(0) topo.set_numIter(25) topo.set_idemWidth(demImage.getWidth()) topo.set_idemLength(demImage.getLength()) topo.set_ilrl(pointingDirection[masterTrack.pointingDirection]) topo.set_extraIter(10) topo.set_length(masterTrack.numberOfLines) topo.set_width(masterTrack.numberOfSamples) topo.set_nRngLooks(1) topo.set_nAzLooks(1) topo.set_demMethod(5) # BIQUINTIC METHOD topo.set_orbitMethod(0) # HERMITE # Need to simplify orbit stuff later nvecs = len(orb._stateVectors) topo.set_orbitNvecs(nvecs) topo.set_orbitBasis(1) # Is this ever different? topo.createOrbit( ) # Initializes the empty orbit to the right allocated size count = 0 for sv in orb._stateVectors: td = DTU.seconds_since_midnight(sv.getTime()) pos = sv.getPosition() vel = sv.getVelocity() topo.set_orbitVector(count, td, pos[0], pos[1], pos[2], vel[0], vel[1], vel[2]) count += 1 topo.runTopo() #tidy up latImage.addDescription('Pixel-by-pixel latitude in degrees.') latImage.finalizeImage() latImage.renderHdr() lonImage.addDescription('Pixel-by-pixel longitude in degrees.') lonImage.finalizeImage() lonImage.renderHdr() heightImage.addDescription('Pixel-by-pixel height in meters.') heightImage.finalizeImage() heightImage.renderHdr() descr = '''Two channel Line-Of-Sight geometry image (all angles in degrees). Represents vector drawn from target to platform. Channel 1: Incidence angle measured from vertical at target (always +ve). Channel 2: Azimuth angle measured from North in Anti-clockwise direction.''' losImage.setImageType('bil') losImage.addDescription(descr) losImage.finalizeImage() losImage.renderHdr() demImage.finalizeImage() if slantRangeImage: try: slantRangeImage.finalizeImage() except: pass
def runTopoGPU(info, demImage, dop=None, nativedop=False, legendre=False): from isceobj import Constants as CN from isceobj.Planet.Planet import Planet from isceobj.Util.Poly2D import Poly2D from iscesys import DateTimeUtil as DTU from zerodop.GPUtopozero.GPUtopozero import PyTopozero ## TODO GPU does not support shadow and layover and local inc file generation full = False os.makedirs(info.outdir, exist_ok=True) # define variables to be used later on r0 = info.rangeFirstSample + ( (info.numberRangeLooks - 1) / 2) * info.slantRangePixelSpacing tbef = info.sensingStart + datetime.timedelta(seconds=( (info.numberAzimuthLooks - 1) / 2) / info.prf) pegHdg = np.radians(info.orbit.getENUHeading(tbef)) width = info.width // info.numberRangeLooks length = info.length // info.numberAzimuthLooks dr = info.slantRangePixelSpacing * info.numberRangeLooks # output file names latFilename = info.latFilename lonFilename = info.lonFilename losFilename = info.losFilename heightFilename = info.heightFilename incFilename = info.incFilename maskFilename = info.maskFilename # orbit interpolator if legendre: omethod = 2 # LEGENDRE INTERPOLATION else: omethod = 0 # HERMITE INTERPOLATION # tracking doppler specifications if nativedop and (dop is not None): try: coeffs = dop._coeffs except: coeffs = dop polyDoppler = Poly2D() polyDoppler.setWidth(width) polyDoppler.setLength(length) polyDoppler.initPoly(rangeOrder=len(coeffs) - 1, azimuthOrder=0, coeffs=[coeffs]) else: print('Zero doppler') polyDoppler = Poly2D(name='stripmapStack_dopplerPoly') polyDoppler.setWidth(width) polyDoppler.setLength(length) polyDoppler.setNormRange(1.0) polyDoppler.setNormAzimuth(1.0) polyDoppler.setMeanRange(0.0) polyDoppler.setMeanAzimuth(0.0) polyDoppler.initPoly(rangeOrder=0, azimuthOrder=0, coeffs=[[0.0]]) polyDoppler.createPoly2D() # dem demImage.setCaster('read', 'FLOAT') demImage.createImage() # slant range file slantRangeImage = Poly2D() slantRangeImage.setWidth(width) slantRangeImage.setLength(length) slantRangeImage.setNormRange(1.0) slantRangeImage.setNormAzimuth(1.0) slantRangeImage.setMeanRange(0.0) slantRangeImage.setMeanAzimuth(0.0) slantRangeImage.initPoly(rangeOrder=1, azimuthOrder=0, coeffs=[[r0, dr]]) slantRangeImage.createPoly2D() # lat file latImage = isceobj.createImage() accessMode = 'write' dataType = 'DOUBLE' latImage.initImage(latFilename, accessMode, width, dataType) latImage.createImage() # lon file lonImage = isceobj.createImage() lonImage.initImage(lonFilename, accessMode, width, dataType) lonImage.createImage() # LOS file losImage = isceobj.createImage() dataType = 'FLOAT' bands = 2 scheme = 'BIL' losImage.initImage(losFilename, accessMode, width, dataType, bands=bands, scheme=scheme) losImage.setCaster('write', 'DOUBLE') losImage.createImage() # height file heightImage = isceobj.createImage() dataType = 'DOUBLE' heightImage.initImage(heightFilename, accessMode, width, dataType) heightImage.createImage() # add inc and mask file if requested if full: incImage = isceobj.createImage() dataType = 'FLOAT' incImage.initImage(incFilename, accessMode, width, dataType, bands=bands, scheme=scheme) incImage.createImage() incImagePtr = incImage.getImagePointer() maskImage = isceobj.createImage() dataType = 'BYTE' bands = 1 maskImage.initImage(maskFilename, accessMode, width, dataType, bands=bands, scheme=scheme) maskImage.createImage() maskImagePtr = maskImage.getImagePointer() else: incImagePtr = 0 maskImagePtr = 0 # initalize planet elp = Planet(pname='Earth').ellipsoid # initialize topo object and fill with parameters topo = PyTopozero() topo.set_firstlat(demImage.getFirstLatitude()) topo.set_firstlon(demImage.getFirstLongitude()) topo.set_deltalat(demImage.getDeltaLatitude()) topo.set_deltalon(demImage.getDeltaLongitude()) topo.set_major(elp.a) topo.set_eccentricitySquared(elp.e2) topo.set_rSpace(info.slantRangePixelSpacing) topo.set_r0(r0) topo.set_pegHdg(pegHdg) topo.set_prf(info.prf) topo.set_t0(DTU.seconds_since_midnight(tbef)) topo.set_wvl(info.radarWavelength) topo.set_thresh(.05) topo.set_demAccessor(demImage.getImagePointer()) topo.set_dopAccessor(polyDoppler.getPointer()) topo.set_slrngAccessor(slantRangeImage.getPointer()) topo.set_latAccessor(latImage.getImagePointer()) topo.set_lonAccessor(lonImage.getImagePointer()) topo.set_losAccessor(losImage.getImagePointer()) topo.set_heightAccessor(heightImage.getImagePointer()) topo.set_incAccessor(incImagePtr) topo.set_maskAccessor(maskImagePtr) topo.set_numIter(25) topo.set_idemWidth(demImage.getWidth()) topo.set_idemLength(demImage.getLength()) topo.set_ilrl(info.lookSide) topo.set_extraIter(10) topo.set_length(length) topo.set_width(width) topo.set_nRngLooks(info.numberRangeLooks) topo.set_nAzLooks(info.numberAzimuthLooks) topo.set_demMethod(5) # BIQUINTIC METHOD topo.set_orbitMethod(omethod) # Need to simplify orbit stuff later nvecs = len(info.orbit.stateVectors.list) topo.set_orbitNvecs(nvecs) topo.set_orbitBasis(1) # Is this ever different? topo.createOrbit( ) # Initializes the empty orbit to the right allocated size count = 0 for sv in info.orbit.stateVectors.list: td = DTU.seconds_since_midnight(sv.getTime()) pos = sv.getPosition() vel = sv.getVelocity() topo.set_orbitVector(count, td, pos[0], pos[1], pos[2], vel[0], vel[1], vel[2]) count += 1 # run topo topo.runTopo() # close the written files and add description etc # lat file latImage.addDescription('Pixel-by-pixel latitude in degrees.') latImage.finalizeImage() latImage.renderHdr() # lon file lonImage.addDescription('Pixel-by-pixel longitude in degrees.') lonImage.finalizeImage() lonImage.renderHdr() # height file heightImage.addDescription('Pixel-by-pixel height in meters.') heightImage.finalizeImage() heightImage.renderHdr() # los file descr = '''Two channel Line-Of-Sight geometry image (all angles in degrees). Represents vector drawn from target to platform. Channel 1: Incidence angle measured from vertical at target (always +ve). Channel 2: Azimuth angle measured from North in Anti-clockwise direction.''' losImage.setImageType('bil') losImage.addDescription(descr) losImage.finalizeImage() losImage.renderHdr() # dem/ height file demImage.finalizeImage() # adding in additional files if requested if full: descr = '''Two channel angle file. Channel 1: Angle between ray to target and the vertical at the sensor Channel 2: Local incidence angle accounting for DEM slope at target''' incImage.addDescription(descr) incImage.finalizeImage() incImage.renderHdr() descr = 'Radar shadow-layover mask. 1 - Radar Shadow. 2 - Radar Layover. 3 - Both.' maskImage.addDescription(descr) maskImage.finalizeImage() maskImage.renderHdr() if slantRangeImage: try: slantRangeImage.finalizeImage() except: pass
def runTopoGPU(self): ''' Try with GPU module. ''' from isceobj.Planet.Planet import Planet from zerodop.GPUtopozero.GPUtopozero import PyTopozero from isceobj import Constants as CN from isceobj.Util.Poly2D import Poly2D from iscesys import DateTimeUtil as DTU swathList = self._insar.getValidSwathList(self.swaths) ####Catalog for logging catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name) ####Load in DEM demfilename = self.verifyDEM() catalog.addItem('Dem Used', demfilename, 'topo') frames = [] swaths = [] swathStarts = [] for swath in swathList: #####Load the master product master = self._insar.loadProduct( os.path.join(self._insar.masterSlcProduct, 'IW{0}.xml'.format(swath))) numCommon = self._insar.numberOfCommonBursts[swath - 1] startIndex = self._insar.commonBurstStartMasterIndex[swath - 1] if numCommon > 0: catalog.addItem('Number of common bursts IW-{0}'.format(swath), self._insar.numberOfCommonBursts[swath - 1], 'topo') master.bursts = master.bursts[startIndex:startIndex + numCommon] master.numberOfBursts = numCommon frames.append(master) swaths.append(swath) swathStarts.append(startIndex) if len(frames) == 0: raise Exception( 'There is no common region between the two dates to process') topSwath = min(frames, key=lambda x: x.sensingStart) leftSwath = min(frames, key=lambda x: x.startingRange) bottomSwath = max(frames, key=lambda x: x.sensingStop) rightSwath = max(frames, key=lambda x: x.farRange) r0 = leftSwath.startingRange rmax = rightSwath.farRange dr = frames[0].bursts[0].rangePixelSize t0 = topSwath.sensingStart tmax = bottomSwath.sensingStop dt = frames[0].bursts[0].azimuthTimeInterval wvl = frames[0].bursts[0].radarWavelength width = int(np.round((rmax - r0) / dr) + 1) lgth = int(np.round((tmax - t0).total_seconds() / dt) + 1) polyDoppler = Poly2D(name='topsApp_dopplerPoly') polyDoppler.setWidth(width) polyDoppler.setLength(lgth) polyDoppler.setNormRange(1.0) polyDoppler.setNormAzimuth(1.0) polyDoppler.setMeanRange(0.0) polyDoppler.setMeanAzimuth(0.0) polyDoppler.initPoly(rangeOrder=0, azimuthOrder=0, coeffs=[[0.]]) polyDoppler.createPoly2D() slantRangeImage = Poly2D() slantRangeImage.setWidth(width) slantRangeImage.setLength(lgth) slantRangeImage.setNormRange(1.0) slantRangeImage.setNormAzimuth(1.0) slantRangeImage.setMeanRange(0.) slantRangeImage.setMeanAzimuth(0.) slantRangeImage.initPoly(rangeOrder=1, azimuthOrder=0, coeffs=[[r0, dr]]) slantRangeImage.createPoly2D() dirname = self._insar.geometryDirname os.makedirs(dirname, exist_ok=True) latImage = isceobj.createImage() latImage.initImage(os.path.join(dirname, 'lat.rdr'), 'write', width, 'DOUBLE') latImage.createImage() lonImage = isceobj.createImage() lonImage.initImage(os.path.join(dirname, 'lon.rdr'), 'write', width, 'DOUBLE') lonImage.createImage() losImage = isceobj.createImage() losImage.initImage(os.path.join(dirname, 'los.rdr'), 'write', width, 'FLOAT', bands=2, scheme='BIL') losImage.setCaster('write', 'DOUBLE') losImage.createImage() heightImage = isceobj.createImage() heightImage.initImage(os.path.join(dirname, 'hgt.rdr'), 'write', width, 'DOUBLE') heightImage.createImage() demImage = isceobj.createDemImage() demImage.load(demfilename + '.xml') demImage.setCaster('read', 'FLOAT') demImage.createImage() orb = self._insar.getMergedOrbit(frames) pegHdg = np.radians(orb.getENUHeading(t0)) elp = Planet(pname='Earth').ellipsoid topo = PyTopozero() topo.set_firstlat(demImage.getFirstLatitude()) topo.set_firstlon(demImage.getFirstLongitude()) topo.set_deltalat(demImage.getDeltaLatitude()) topo.set_deltalon(demImage.getDeltaLongitude()) topo.set_major(elp.a) topo.set_eccentricitySquared(elp.e2) topo.set_rSpace(dr) topo.set_r0(r0) topo.set_pegHdg(pegHdg) topo.set_prf(1.0 / dt) topo.set_t0(DTU.seconds_since_midnight(t0)) topo.set_wvl(wvl) topo.set_thresh(.05) topo.set_demAccessor(demImage.getImagePointer()) topo.set_dopAccessor(polyDoppler.getPointer()) topo.set_slrngAccessor(slantRangeImage.getPointer()) topo.set_latAccessor(latImage.getImagePointer()) topo.set_lonAccessor(lonImage.getImagePointer()) topo.set_losAccessor(losImage.getImagePointer()) topo.set_heightAccessor(heightImage.getImagePointer()) topo.set_incAccessor(0) topo.set_maskAccessor(0) topo.set_numIter(25) topo.set_idemWidth(demImage.getWidth()) topo.set_idemLength(demImage.getLength()) topo.set_ilrl(-1) topo.set_extraIter(10) topo.set_length(lgth) topo.set_width(width) topo.set_nRngLooks(1) topo.set_nAzLooks(1) topo.set_demMethod(5) # BIQUINTIC METHOD topo.set_orbitMethod(0) # HERMITE # Need to simplify orbit stuff later nvecs = len(orb._stateVectors) topo.set_orbitNvecs(nvecs) topo.set_orbitBasis(1) # Is this ever different? topo.createOrbit( ) # Initializes the empty orbit to the right allocated size count = 0 for sv in orb._stateVectors: td = DTU.seconds_since_midnight(sv.getTime()) pos = sv.getPosition() vel = sv.getVelocity() topo.set_orbitVector(count, td, pos[0], pos[1], pos[2], vel[0], vel[1], vel[2]) count += 1 topo.runTopo() latImage.addDescription('Pixel-by-pixel latitude in degrees.') latImage.finalizeImage() latImage.renderHdr() lonImage.addDescription('Pixel-by-pixel longitude in degrees.') lonImage.finalizeImage() lonImage.renderHdr() heightImage.addDescription('Pixel-by-pixel height in meters.') heightImage.finalizeImage() heightImage.renderHdr() descr = '''Two channel Line-Of-Sight geometry image (all angles in degrees). Represents vector drawn from target to platform. Channel 1: Incidence angle measured from vertical at target (always +ve). Channel 2: Azimuth angle measured from North in Anti-clockwise direction.''' losImage.setImageType('bil') losImage.addDescription(descr) losImage.finalizeImage() losImage.renderHdr() demImage.finalizeImage() if slantRangeImage: try: slantRangeImage.finalizeImage() except: pass ####Start creating VRTs to point to global topo output for swath, frame, istart in zip(swaths, frames, swathStarts): outname = os.path.join(dirname, 'IW{0}'.format(swath)) os.makedirs(outname, exist_ok=True) for ind, burst in enumerate(frame.bursts): top = int(np.rint((burst.sensingStart - t0).total_seconds() / dt)) bottom = top + burst.numberOfLines left = int(np.rint((burst.startingRange - r0) / dr)) right = left + burst.numberOfSamples buildVRT(os.path.join(dirname, 'lat.rdr'), os.path.join(outname, 'lat_%02d.rdr' % (ind + istart + 1)), [width, lgth], [top, bottom, left, right], bands=1, dtype='DOUBLE') buildVRT(os.path.join(dirname, 'lon.rdr'), os.path.join(outname, 'lon_%02d.rdr' % (ind + istart + 1)), [width, lgth], [top, bottom, left, right], bands=1, dtype='DOUBLE') buildVRT(os.path.join(dirname, 'hgt.rdr'), os.path.join(outname, 'hgt_%02d.rdr' % (ind + istart + 1)), [width, lgth], [top, bottom, left, right], bands=1, dtype='DOUBLE') buildVRT(os.path.join(dirname, 'los.rdr'), os.path.join(outname, 'los_%02d.rdr' % (ind + istart + 1)), [width, lgth], [top, bottom, left, right], bands=2, dtype='FLOAT') catalog.addItem( 'Subset for IW{0}-B{1}'.format(swath, ind + 1 + istart), 'Lines: {0}-{1} out of {2}, Pixels: {3}-{4} out of {5}'.format( top, bottom, lgth, left, right, width), 'topo') # print('IW{0}-B{1}: {2} - {3}/ {4}, {5} - {6} /{7}'.format(swath, ind+1+istart, top, bottom, lgth, left, right, width)) catalog.printToLog(logger, "runTopo") self._insar.procDoc.addAllFromCatalog(catalog) return