Example #1
0
def create_demZero(dem, outdir):
    """ create DEM with zero elevation """

    demImage = isceobj.createDemImage()
    demImage.load(dem + '.xml')

    zerodem_name = outdir + '/demzero.wgs84'

    immap = IML.memmap(zerodem_name, mode='write', nchannels=demImage.bands,
                       nxx=demImage.coord1.coordSize, nyy=demImage.coord2.coordSize,
                       scheme=demImage.scheme, dataType=demImage.toNumpyDataType())

    IML.renderISCEXML(zerodem_name, demImage.bands, demImage.coord2.coordSize, demImage.coord1.coordSize,
                      demImage.toNumpyDataType(), demImage.scheme, bbox=demImage.getsnwe())

    demZero = isceobj.createDemImage()
    demZero.load(zerodem_name + '.xml')

    return demZero
Example #2
0
def main(iargs=None):
    
    inps = cmdLineParse(iargs)

    # see if the user compiled isce with GPU enabled
    run_GPU = False
    try:
        from zerodop.GPUtopozero.GPUtopozero import PyTopozero
        from zerodop.GPUgeo2rdr.GPUgeo2rdr import PyGeo2rdr
        run_GPU = True
    except:
        pass

    if inps.useGPU and not run_GPU:
        print("GPU mode requested but no GPU ISCE code found")

    # setting the respective version of geo2rdr for CPU and GPU
    if run_GPU and inps.useGPU:
        print('GPU mode')
        runTopo = runTopoGPU
    else:
        print('CPU mode')
        runTopo = runTopoCPU


    db = shelve.open(os.path.join(inps.master, 'data'))
    frame = db['frame']
    try:
        doppler = db['doppler']
    except:
        doppler = frame._dopplerVsPixel

    db.close()



    ####Setup dem
    demImage = isceobj.createDemImage()
    print('******', inps.dem)
    demImage.load(inps.dem + '.xml')
    demImage.setAccessMode('read')

    info = extractInfo(frame, inps)

    # define topo output names:
    info.latFilename = os.path.join(info.outdir, 'lat.rdr')
    info.lonFilename = os.path.join(info.outdir, 'lon.rdr')
    info.losFilename = os.path.join(info.outdir, 'los.rdr')
    info.heightFilename = os.path.join(info.outdir, 'hgt.rdr')
    info.incFilename = os.path.join(info.outdir, 'incLocal.rdr')
    info.maskFilename = os.path.join(info.outdir, 'shadowMask.rdr')


    runTopo(info,demImage,dop=doppler,nativedop=inps.nativedop, legendre=inps.legendre)
    runSimamp(os.path.dirname(info.heightFilename),os.path.basename(info.heightFilename))
Example #3
0
def run(tobeGeocoded,
        frame1,
        formSLC1,
        velocity,
        height,
        snwe,
        infos,
        catalog=None,
        sceneid='NO_ID'):
    logger.info("Geocoding Image: %s" % sceneid)

    stdWriter = create_writer("log",
                              "",
                              True,
                              filename=infos['ouputPath'] + ".geo.log")

    planet = frame1.getInstrument().getPlatform().getPlanet()
    referenceOrbit = formSLC1.getMocompPosition(posIndx)
    doppler = dopplerCentroid.getDopplerCoefficients(inHz=False)[0]
    #####Geocode one by one
    ge = Geocodable()
    for prod in tobeGeocoded:
        objGeo = stdproc.createGeocode(
            snwe=snwe,
            demCropFilename=infos['outputPath'] + '.' +
            infos['demCropFilename'],
            referenceOrbit=referenceOrbit,
            dopplerCentroidConstantTerm=doppler,
            bodyFixedVelocity=velocity,
            spacecraftHeight=height,
            numberRangeLooks=infos['numberRangeLooks'],
            numberAzimuthLooks=infos['numberAzimuthLooks'],
            isMocomp=infos['is_mocomp'])

        objGeo.stdWriter = stdWriter

        #create the instance of the image and return the method is supposed to use
        inImage, objGeo.method = ge.create(infos['outputPath'] + '.' + prod)
        if inImage:
            demImage = isceobj.createDemImage()
            IU.copyAttributes(infos['demImage'], demImage)
            objGeo(peg=infos['peg'],
                   frame=frame1,
                   planet=planet,
                   dem=demImage,
                   tobegeocoded=inImage,
                   geoPosting=None,
                   masterslc=formSLC1)

            if catalog is not None:
                isceobj.Catalog.recordInputsAndOutputs(
                    catalog, objGeo, "runGeocode.%s" % sceneid, logger,
                    "runGeocode.%s" % sceneid)

    stdWriter.finalize()
Example #4
0
def geocode(track, demFile, inputFile, bbox, numberRangeLooks, numberAzimuthLooks, interpMethod, topShift, leftShift, addMultilookOffset=True):
    import datetime
    from zerodop.geozero import createGeozero
    from isceobj.Planet.Planet import Planet

    pointingDirection = {'right': -1, 'left' :1}

    demImage = isceobj.createDemImage()
    demImage.load(demFile + '.xml')
    demImage.setAccessMode('read')

    inImage = isceobj.createImage()
    inImage.load(inputFile + '.xml')
    inImage.setAccessMode('read')

    planet = Planet(pname='Earth')

    topo = createGeozero()
    topo.configure()
    topo.slantRangePixelSpacing = numberRangeLooks * track.rangePixelSize
    topo.prf = 1.0 / (numberAzimuthLooks*track.azimuthLineInterval)
    topo.radarWavelength = track.radarWavelength
    topo.orbit = track.orbit
    topo.width = inImage.width
    topo.length = inImage.length
    topo.wireInputPort(name='dem', object=demImage)
    topo.wireInputPort(name='planet', object=planet)
    topo.wireInputPort(name='tobegeocoded', object=inImage)
    topo.numberRangeLooks = 1
    topo.numberAzimuthLooks = 1
    topo.lookSide = pointingDirection[track.pointingDirection]
    sensingStart = track.sensingStart + datetime.timedelta(seconds=topShift*track.azimuthLineInterval)
    rangeFirstSample = track.startingRange + leftShift * track.rangePixelSize
    if addMultilookOffset:
        sensingStart += datetime.timedelta(seconds=(numberAzimuthLooks-1.0)/2.0*track.azimuthLineInterval)
        rangeFirstSample += (numberRangeLooks-1.0)/2.0*track.rangePixelSize
    topo.setSensingStart(sensingStart)
    topo.rangeFirstSample = rangeFirstSample
    topo.method=interpMethod
    topo.demCropFilename = 'crop.dem'
    #looks like this does not work
    #topo.geoFilename = outputName
    topo.dopplerCentroidCoeffs = [0.]
    #snwe list <class 'list'>
    topo.snwe = bbox

    topo.geocode()

    print('South: ', topo.minimumGeoLatitude)
    print('North: ', topo.maximumGeoLatitude)
    print('West:  ', topo.minimumGeoLongitude)
    print('East:  ', topo.maximumGeoLongitude)
    
    return
Example #5
0
def geo2RdrCPU(secondaryTrack, numberRangeLooks, numberAzimuthLooks, latFile,
               lonFile, hgtFile, rangeOffsetFile, azimuthOffsetFile):
    import datetime
    from zerodop.geo2rdr import createGeo2rdr
    from isceobj.Planet.Planet import Planet

    pointingDirection = {'right': -1, 'left': 1}

    latImage = isceobj.createImage()
    latImage.load(latFile + '.xml')
    latImage.setAccessMode('read')

    lonImage = isceobj.createImage()
    lonImage.load(lonFile + '.xml')
    lonImage.setAccessMode('read')

    demImage = isceobj.createDemImage()
    demImage.load(hgtFile + '.xml')
    demImage.setAccessMode('read')

    planet = Planet(pname='Earth')

    topo = createGeo2rdr()
    topo.configure()
    #set parameters
    topo.slantRangePixelSpacing = numberRangeLooks * secondaryTrack.rangePixelSize
    topo.prf = 1.0 / (numberAzimuthLooks * secondaryTrack.azimuthLineInterval)
    topo.radarWavelength = secondaryTrack.radarWavelength
    topo.orbit = secondaryTrack.orbit
    topo.width = secondaryTrack.numberOfSamples
    topo.length = secondaryTrack.numberOfLines
    topo.demLength = demImage.length
    topo.demWidth = demImage.width
    topo.wireInputPort(name='planet', object=planet)
    topo.numberRangeLooks = 1  #
    topo.numberAzimuthLooks = 1  # must be set to be 1
    topo.lookSide = pointingDirection[secondaryTrack.pointingDirection]
    topo.setSensingStart(secondaryTrack.sensingStart + datetime.timedelta(
        seconds=(numberAzimuthLooks - 1.0) / 2.0 *
        secondaryTrack.azimuthLineInterval))
    topo.rangeFirstSample = secondaryTrack.startingRange + (
        numberRangeLooks - 1.0) / 2.0 * secondaryTrack.rangePixelSize
    topo.dopplerCentroidCoeffs = [0.]  # we are using zero doppler geometry
    #set files
    topo.latImage = latImage
    topo.lonImage = lonImage
    topo.demImage = demImage
    topo.rangeOffsetImageName = rangeOffsetFile
    topo.azimuthOffsetImageName = azimuthOffsetFile
    #run it
    topo.geo2rdr()

    return
Example #6
0
def dem2bbox(dem_file):
    """Grab bbox from DEM file in geo coordinates"""
    demImage = isceobj.createDemImage()
    demImage.load(dem_file + '.xml')
    demImage.setAccessMode('read')
    N = demImage.getFirstLatitude()
    W = demImage.getFirstLongitude()
    S = N + demImage.getDeltaLatitude() * demImage.getLength()
    E = W + demImage.getDeltaLongitude() * demImage.getWidth()
    bbox = [np.floor(S).astype(int), np.ceil(N).astype(int),
            np.floor(W).astype(int), np.ceil(E).astype(int)]
    return bbox
Example #7
0
    def verifyDEM(self):
        masterF = self._insar.masterFrame
        slaveF = self._insar.slaveFrame
        info = self.extractInfo(masterF, slaveF)
        #if an image has been specified, then no need to create one
        if not self.dem.filename:
            self.createDem(info)
        else:
            self.insar.demImage = self.dem

        #ensure that the dem vrt file exists by creating (or recreating) it
        self.insar.demImage.renderVRT()

        #at this point a dem image has been set into self.insar, whether it
        #was stitched together or read in input
        demImage =  self.insar.demImage
        #if the demImage is already in wgs84 (because was provided in input) then skip and proceed
        if demImage.reference.upper() != 'WGS84':
            wgs84demFilename = self.insar.demImage.filename+'.wgs84'
            wgs84demxmlFilename = wgs84demFilename+'.xml'
            #if the dem reference is EGM96 and the WGS84 corrected
            #dem files are not found, then create the WGS84 files
            #using the demStitcher's correct method
            if( demImage.reference.upper() == 'EGM96' and
                not (os.path.isfile(wgs84demFilename) and
                     os.path.isfile(wgs84demxmlFilename))
            ):
                self.insar.demImage = self.demStitcher.correct(demImage)
            #make sure to load the wgs84 if present
            elif(os.path.isfile(wgs84demFilename) and
                     os.path.isfile(wgs84demxmlFilename)):
                from isceobj import createDemImage
                self.insar.demImage  = createDemImage()
                self.insar.demImage.load(wgs84demxmlFilename)
                if(self.insar.demImage.reference.upper() != 'WGS84'):
                    print('The dem',wgs84demFilename,'is not wgs84')
                    raise Exception

        #ensure that the wgs84 dem vrt file exists
        self.insar.demImage.renderVRT()

        #get water mask
        self.runCreateWbdMask(info)


        return None
Example #8
0
def topoCPU(masterTrack, numberRangeLooks, numberAzimuthLooks, demFile,
            latFile, lonFile, hgtFile, losFile):
    import datetime
    import isceobj
    from zerodop.topozero import createTopozero
    from isceobj.Planet.Planet import Planet

    pointingDirection = {'right': -1, 'left': 1}

    demImage = isceobj.createDemImage()
    demImage.load(demFile + '.xml')
    demImage.setAccessMode('read')

    planet = Planet(pname='Earth')

    topo = createTopozero()
    topo.slantRangePixelSpacing = numberRangeLooks * masterTrack.rangePixelSize
    topo.prf = 1.0 / (numberAzimuthLooks * masterTrack.azimuthLineInterval)
    topo.radarWavelength = masterTrack.radarWavelength
    topo.orbit = masterTrack.orbit
    topo.width = masterTrack.numberOfSamples
    topo.length = masterTrack.numberOfLines
    topo.wireInputPort(name='dem', object=demImage)
    topo.wireInputPort(name='planet', object=planet)
    topo.numberRangeLooks = 1  #must be set as 1
    topo.numberAzimuthLooks = 1  #must be set as 1 Cunren
    topo.lookSide = pointingDirection[masterTrack.pointingDirection]
    topo.sensingStart = masterTrack.sensingStart + datetime.timedelta(
        seconds=(numberAzimuthLooks - 1.0) / 2.0 *
        masterTrack.azimuthLineInterval)
    topo.rangeFirstSample = masterTrack.startingRange + (
        numberRangeLooks - 1.0) / 2.0 * masterTrack.rangePixelSize
    topo.demInterpolationMethod = 'BIQUINTIC'

    topo.latFilename = latFile
    topo.lonFilename = lonFile
    topo.heightFilename = hgtFile
    topo.losFilename = losFile
    #topo.incFilename = incName
    #topo.maskFilename = mskName

    topo.topo()

    return list(topo.snwe)
Example #9
0
def main(iargs=None):

    inps = cmdLineParse(iargs)

    swathList = ut.getSwathList(inps.master)

    demImage = isceobj.createDemImage()
    demImage.load(inps.dem + '.xml')

    boxes = []
    inputs = []

    for swath in swathList:
        master = ut.loadProduct(
            os.path.join(inps.master, 'IW{0}.xml'.format(swath)))

        ###Check if geometry directory already exists.
        dirname = os.path.join(inps.geom_masterDir, 'IW{0}'.format(swath))

        if os.path.isdir(dirname):
            print('Geometry directory {0} already exists.'.format(dirname))
        else:
            os.makedirs(dirname)

        for ind in range(master.numberOfBursts):
            inputs.append((dirname, demImage, master, ind))

    pool = mp.Pool(mp.cpu_count())
    results = pool.map(call_topo, inputs)
    pool.close()

    for bbox in results:
        boxes.append(bbox)

    boxes = np.array(boxes)
    bbox = [
        np.min(boxes[:, 0]),
        np.max(boxes[:, 1]),
        np.min(boxes[:, 2]),
        np.max(boxes[:, 3])
    ]
    print('bbox : ', bbox)
Example #10
0
def runMultilook(in_dir, out_dir, alks, rlks):
    print(
        'generate multilooked geometry files with alks={} and rlks={}'.format(
            alks, rlks))
    from iscesys.Parsers.FileParserFactory import createFileParser
    FP = createFileParser('xml')

    if not os.path.isdir(out_dir):
        os.makedirs(out_dir)
        print('create directory: {}'.format(out_dir))

    for fbase in [
            'hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask', 'waterMask'
    ]:
        fname = '{}.rdr'.format(fbase)
        in_file = os.path.join(in_dir, fname)
        out_file = os.path.join(out_dir, fname)

        if os.path.isfile(in_file):
            xmlProp = FP.parse(in_file + '.xml')[0]
            if ('image_type' in xmlProp and xmlProp['image_type'] == 'dem'):
                inImage = isceobj.createDemImage()
            else:
                inImage = isceobj.createImage()

            inImage.load(in_file + '.xml')
            inImage.filename = in_file

            lkObj = Looks()
            lkObj.setDownLooks(alks)
            lkObj.setAcrossLooks(rlks)
            lkObj.setInputImage(inImage)
            lkObj.setOutputFilename(out_file)
            lkObj.looks()

            # copy the full resolution xml/vrt file from ./merged/geom_master to ./geom_master
            # to facilitate the number of looks extraction
            # the file path inside .xml file is not, but should, updated
            shutil.copy(in_file + '.xml', out_file + '.full.xml')
            shutil.copy(in_file + '.vrt', out_file + '.full.vrt')

    return out_dir
Example #11
0
def main(iargs=None):

    inps = cmdLineParse(iargs)

    swathList = ut.getSwathList(inps.reference)

    demImage = isceobj.createDemImage()
    demImage.load(inps.dem + '.xml')

    boxes = []
    inputs = []

    for swath in swathList:
        reference = ut.loadProduct(
            os.path.join(inps.reference, 'IW{0}.xml'.format(swath)))

        ###Check if geometry directory already exists.
        dirname = os.path.join(inps.geom_referenceDir, 'IW{0}'.format(swath))
        os.makedirs(dirname, exist_ok=True)

        for ind in range(reference.numberOfBursts):
            inputs.append((dirname, demImage, reference, ind))

    # parallel processing
    print('running in parallel with {} processes'.format(inps.numProcess))
    pool = mp.Pool(inps.numProcess)
    results = pool.map(call_topo, inputs)
    pool.close()

    for bbox in results:
        boxes.append(bbox)

    boxes = np.array(boxes)
    bbox = [
        np.min(boxes[:, 0]),
        np.max(boxes[:, 1]),
        np.min(boxes[:, 2]),
        np.max(boxes[:, 3])
    ]
    print('bbox : ', bbox)
Example #12
0
def copyGeoXML(xmlfile, outfile, shape):
    '''
    Copy Geocoding information from xmlfile to outfile.xml.
    '''
    import isceobj

    img = isceobj.createDemImage()
    img.load(xmlfile)

    img.setDataType('FLOAT')
    img.setBands(1)
    img.setAccessMode('READ')
    img.setInterleavedScheme('BIL')
    img.setFilename(outfile)

    ####Not needed if no cropping is performed.
    img.setWidth(shape[1])
    img.setLength(shape[0])

    img.addDescription('LOS velocity in mm/yr')
    img.renderHdr()

    return
Example #13
0
def createDem(self, info):
    #we get there only if a dem image was not specified as input
    import math
    from contrib.demUtils.DemStitcher import DemStitcher
    #import pdb
    #pdb.set_trace()
    bbox = info.bbox

    ####If the user has requested a bounding box
    if self.geocode_bbox:
        latMax = self.geocode_bbox[1]
        latMin = self.geocode_bbox[0]
        lonMin = self.geocode_bbox[3]
        lonMax = self.geocode_bbox[2]
    else:
        latMax = -1000
        latMin = 1000
        lonMax = -1000
        lonMin = 1000

    for bb in bbox:
        if bb[0] > latMax:
            latMax = bb[0]
        if bb[0] < latMin:
            latMin = bb[0]
        if bb[1] > lonMax:
            lonMax = bb[1]
        if bb[1] < lonMin:
            lonMin = bb[1]

    ####Extra padding around bbox
    #### To account for timing errors
    #### To account for shifts due to topography
    delta = 0.2

    latMin = math.floor(latMin - 0.2)
    latMax = math.ceil(latMax + 0.2)
    lonMin = math.floor(lonMin - 0.2)
    lonMax = math.ceil(lonMax + 0.2)
    demName = self.demStitcher.defaultName([latMin, latMax, lonMin, lonMax])
    demNameXml = demName + '.xml'
    self.demStitcher.setCreateXmlMetadata(True)
    self.demStitcher.setMetadataFilename(
        demNameXml)  #it adds the .xml automatically

    #if it's already there don't recreate it
    if not (os.path.exists(demNameXml) and os.path.exists(demName)):

        #check whether the user want to just use high res dems and filling the
        # gap or go to the lower res if it cannot complete the region
        # Better way would be to use the union of the dems and doing some
        # resampling
        if self.useHighResolutionDemOnly:
            #it will use the high res no matter how many are missing
            self.demStitcher.setFilling()
            #try first the best resolution
            source = 1
            stitchOk = self.demStitcher.stitchDems(
                [latMin, latMax], [lonMin, lonMax],
                source,
                demName,
                keep=False)  #remove zip files
        else:
            #try first the best resolution
            self.demStitcher.setNoFilling()
            source = 1
            stitchOk = self.demStitcher.stitchDems(
                [latMin, latMax], [lonMin, lonMax],
                source,
                demName,
                keep=False)  #remove zip files
            if not stitchOk:  #try lower resolution if there are no data
                self.demStitcher.setFilling()
                source = 3
                stitchOk = self.demStitcher.stitchDems([latMin, latMax],
                                                       [lonMin, lonMax],
                                                       source,
                                                       demName,
                                                       keep=False)

        if not stitchOk:
            logger.error(
                "Cannot form the DEM for the region of interest. If you have one, set the appropriate DEM component in the input file."
            )
            raise Exception

    #save the name just in case
    self.insar.demInitFile = demNameXml
    #if stitching is performed a DEM image instance is created (returns None otherwise). If not we have to create one
    demImage = self.demStitcher.getImage()
    if demImage is None:
        from iscesys.Parsers.FileParserFactory import createFileParser
        from isceobj import createDemImage
        parser = createFileParser('xml')
        #get the properties from the file init file
        prop, fac, misc = parser.parse(demNameXml)
        #this dictionary has an initial dummy key whose value is the dictionary with all the properties

        demImage = createDemImage()
        demImage.init(prop, fac, misc)
        demImage.metadatalocation = demNameXml

    self.insar.demImage = demImage
Example #14
0
def run(tobeGeocoded,
        frame1,
        formSLC1,
        velocity,
        height,
        snwe,
        infos,
        catalog=None,
        sceneid='NO_ID'):
    logger.info("Geocoding Image: %s" % sceneid)
    stdWriter = create_writer("log",
                              "",
                              True,
                              filename=infos['outputPath'] + ".geo.log")

    planet = frame1.getInstrument().getPlatform().getPlanet()
    doppler = infos['dopplerCentroid'].getDopplerCoefficients(inHz=False)[0]

    #####Geocode one by one
    for prod in tobeGeocoded:
        prodPath = infos['outputPath'] + '.' + prod
        if not os.path.isfile(prodPath):
            logger.info(
                "File not found. Skipping %s" %
                prodPath)  #KK some prods are only in refScene folder! (tbd)
            continue
        #else:
        objGeo = stdproc.createGeocode('insarapp_geocode_' +
                                       os.path.basename(prod).replace('.', ''))
        objGeo.configure()
        objGeo.referenceOrbit = formSLC1.getMocompPosition(posIndx)

        ####IF statements to check for user configuration
        if objGeo.minimumLatitude is None:
            objGeo.minimumLatitude = snwe[0]

        if objGeo.maximumLatitude is None:
            objGeo.maximumLatitude = snwe[1]

        if objGeo.minimumLongitude is None:
            objGeo.minimumLongitude = snwe[2]

        if objGeo.maximumLongitude is None:
            objGeo.maximumLongitude = snwe[3]

        if objGeo.demCropFilename is None:
            objGeo.demCropFilename = infos['outputPath'] + '.' + infos[
                'demCropFilename']

        if objGeo.dopplerCentroidConstantTerm is None:
            objGeo.dopplerCentroidConstantTerm = doppler

        if objGeo.bodyFixedVelocity is None:
            objGeo.bodyFixedVelocity = velocity

        if objGeo.spacecraftHeight is None:
            objGeo.spacecraftHeight = height

        if objGeo.numberRangeLooks is None:
            objGeo.numberRangeLooks = infos['numberRangeLooks']

        if objGeo.numberAzimuthLooks is None:
            objGeo.numberAzimuthLooks = infos['numberAzimuthLooks']

        if objGeo.isMocomp is None:
            objGeo.isMocomp = infos['is_mocomp']

        objGeo.stdWriter = stdWriter

        #create the instance of the image and return the method is supposed to use
        ge = Geocodable()
        inImage, objGeo.method = ge.create(prodPath)
        if objGeo.method is None:
            objGeo.method = method

        if inImage:
            demImage = isceobj.createDemImage()
            IU.copyAttributes(infos['demImage'], demImage)
            objGeo(peg=infos['peg'],
                   frame=frame1,
                   planet=planet,
                   dem=demImage,
                   tobegeocoded=inImage,
                   geoPosting=None,
                   referenceslc=formSLC1)

            if catalog is not None:
                isceobj.Catalog.recordInputsAndOutputs(
                    catalog, objGeo, "runGeocode.%s.%s" % (sceneid, prodPath),
                    logger, "runGeocode.%s.%s" % (sceneid, prodPath))

    stdWriter.finalize()
Example #15
0
    def runGeocodeCor(self):
        import stdproc

        logger.info("Geocoding Correlation")
        objFormSlc1 = self.insar.formSLC1
        # Initialize the Dem
        from isceobj import createDemImage, createIntImage, createImage
        demImage = createDemImage()
        IU.copyAttributes(self.insar.demImage, demImage)
        demImage.setAccessMode('read')
        demImage.createImage()

        topoflatIntFilename = self.insar.topophaseFlatFilename
        widthInt = self.insar.resampIntImage.width

        intImage = createIntImage()
        widthInt = self.insar.resampIntImage.width
        intImage.setFilename(corintFilename)
        intImage.setWidth(widthInt)
        intImage.setAccessMode('read')
        intImage.createImage()

        posIndx = 1
        mocompPosition1 = objFormSlc1.getMocompPosition()

        minLat, maxLat, minLon, maxLon = self.insar.topo.snwe

        planet = self.insar.masterFrame.instrument.getPlatform().getPlanet()

        objGeo = stdproc.createGeocode()
        objGeo.wireInputPort(name='peg', object=self.insar.peg)
        objGeo.wireInputPort(name='frame', object=self.insar.masterFrame)
        objGeo.wireInputPort(name='planet', object=planet)
        objGeo.wireInputPort(name='dem', object=demImage)
        objGeo.wireInputPort(name='interferogram', object=intImage)
        objGeo.snwe = minLat, maxLat, minLon, maxLon
        corGeocodeFilename = corintFilename + '.geo'
        demGeocodeFilename = corintFilename + '.demcrop'
        objGeo.setGeocodeFilename(corGeocodeFilename)
        objGeo.setDemCropFilename(demGeocodeFilename)
        #set the tag used in the outfile. each message is precided by this tag
        #is the writer is not of "file" type the call has no effect
        objGeo.stdWriter = self.stdWriter.set_file_tags(
            "geocode", "log", "err", "out")
        # see mocompbaseline
        objGeo.setReferenceOrbit(mocompPosition1[posIndx])
        prf1 = self.insar.masterFrame.instrument.getPulseRepetitionFrequency()
        dp = self.insar.dopplerCentroid.getDopplerCoefficients(inHz=False)[0]
        v = self.insar.procVelocity
        h = self.insar.averageHeight
        objGeo.setDopplerCentroidConstantTerm(dp)
        objGeo.setBodyFixedVelocity(v)
        objGeo.setSpacecraftHeight(h)
        objGeo.setNumberRangeLooks(self.insar.numberRangeLooks)
        objGeo.setNumberAzimuthLooks(self.insar.numberAzimuthLooks)
        # I have no idea what ismocomp means
        goodLines = self.insar.numberValidPulses
        patchSize = self.insar.patchSize
        # this variable was hardcoded in geocode.f90 and was equal to (8192 - 2048)/2
        is_mocomp = int((patchSize - goodLines) / 2)
        objGeo.setISMocomp(is_mocomp)

        objGeo.geocode()

        intImage.finalizeImage()
        demImage.finalizeImage()
        return objGeo
Example #16
0
    def runGeocode4rlks(self, inFilename, widthIn, geoFilename,
                        demcropFilename):
        import stdproc
        from isceobj import createIntImage, createImage

        print("runGeocode4rlks: inFilename, widthIn = ", inFilename, widthIn)
        print("runGeocode4rlks: geoFilename, demcropFilename = ", geoFilename,
              demcropFilename)
        pause(message="Paused in runGeocode4rlks")

        logger.info("Geocoding Image")

        # Initialize the Dem
        from isceobj import createDemImage
        demImage = createDemImage()
        IU.copyAttributes(self.insar.demImage, demImage)
        demImage.setAccessMode('read')
        demImage.createImage()
        print("demImage.firstLatitude = ", demImage.firstLatitude)
        print("demImage.firstLongitude = ", demImage.firstLongitude)
        print("demImage.deltaLatitude = ", demImage.deltaLatitude)
        print("demImage.deltaLongitude = ", demImage.deltaLongitude)
        print("demImage.width = ", demImage.width)
        print("demImage.length = ", demImage.length)
        demImage_lastLatitude = (
            demImage.firstLatitude +
            (demImage.length - 1) * demImage.deltaLatitude)
        demImage_lastLongitude = (
            demImage.firstLongitude +
            (demImage.width - 1) * demImage.deltaLongitude)

        print("demImage_lastLatitude = ", demImage_lastLatitude)
        print("demImage_lastLongitude = ", demImage_lastLongitude)

        # Initialize the input image
        intImage = createIntImage()
        intImage.setFilename(inFilename)
        intImage.setWidth(widthIn)
        intImage.setAccessMode('read')
        intImage.createImage()

        minLat, maxLat, minLon, maxLon = self.insar.topo.snwe
        print("objTopo.minLat = ", minLat)
        print("objTopo.minLon = ", minLon)
        print("objTopo.maxLat = ", maxLat)
        print("objTopo.maxLon = ", maxLon)
        pause(message="Paused in runGeocode4rlks")

        planet = self.insar.masterFrame.instrument.getPlatform().getPlanet()

        objGeo = stdproc.createGeocode()
        objGeo.listInputPorts()
        objGeo.wireInputPort(name='peg', object=self.insar.peg)
        #        objGeo.wireInputPort(name='frame',object=self.insar.masterFrame)
        objGeo.rangeFirstSample = self.insar.masterFrame.getStartingRange()
        objGeo.slantRangePixelSpacing = self.insar.masterFrame.instrument.getRangePixelSize(
        ) * 4
        objGeo.prf = self.insar.masterFrame.instrument.getPulseRepetitionFrequency(
        )
        objGeo.radarWavelength = self.insar.masterFrame.instrument.getRadarWavelength(
        )
        objGeo.wireInputPort(name='planet', object=planet)
        objGeo.wireInputPort(name='dem', object=demImage)
        objGeo.wireInputPort(name='interferogram', object=intImage)
        print("self.geoPosting = ", self.geoPosting)
        objGeo.wireInputPort(name='geoPosting', object=self.geoPosting)

        objGeo.snwe = minLat, maxLat, minLon, maxLon
        objGeo.setGeocodeFilename(geoFilename)
        objGeo.setDemCropFilename(demcropFilename)

        #set the tag used in the outfile. each message is precided by this tag
        #is the writer is not of "file" type the call has no effect
        objGeo.stdWriter = self.stdWriter.set_file_tags(
            "geocode", "log", "err", "out")

        # see mocompbaseline
        objFormSlc1 = self.insar.formSLC1
        mocompPosition1 = objFormSlc1.getMocompPosition()
        posIndx = 1
        objGeo.setReferenceOrbit(mocompPosition1[posIndx])
        prf1 = self.insar.masterFrame.instrument.getPulseRepetitionFrequency()
        dp = self.insar.dopplerCentroid.getDopplerCoefficients(inHz=False)[0]
        v = self.insar.procVelocity
        h = self.insar.averageHeight
        objGeo.setDopplerCentroidConstantTerm(dp)
        objGeo.setBodyFixedVelocity(v)
        objGeo.setSpacecraftHeight(h)
        objGeo.setNumberRangeLooks(1.0)  #self.insar.numberRangeLooks)
        objGeo.setNumberAzimuthLooks(1.0)  #self.insar.numberAzimuthLooks)
        # I have no idea what ismocomp means
        goodLines = self.insar.numberValidPulses
        patchSize = self.insar.patchSize
        # this variable was hardcoded in geocode.f90 and was equal to (8192 - 2048)/2
        is_mocomp = self.insar.is_mocomp
        #        is_mocomp = int((patchSize - goodLines)/2)
        objGeo.setISMocomp(is_mocomp)

        objGeo.geocode()

        print("Input state paraemters to gecode.f90:")
        print("Minimum Latitude = ", objGeo.minimumLatitude)
        print("Maximum Latitude = ", objGeo.maximumLatitude)
        print("Minimum Longitude = ", objGeo.minimumLongitude)
        print("Maximum Longitude = ", objGeo.maximumLongitude)
        print("Ellipsoid Major Semi Axis = ", objGeo.ellipsoidMajorSemiAxis)
        print("Ellipsoid Eccentricity Squared = ",
              objGeo.ellipsoidEccentricitySquared)
        print("Peg Latitude = ", objGeo.pegLatitude)
        print("Peg Longitude = ", objGeo.pegLongitude)
        print("Peg Heading = ", objGeo.pegHeading)
        print("Range Pixel Spacing = ", objGeo.slantRangePixelSpacing)
        print("Range First Sample  = ", objGeo.rangeFirstSample)
        print("Spacecraft Height  = ", objGeo.spacecraftHeight)
        print("Planet Local Radius  = ", objGeo.planetLocalRadius)
        print("Body Fixed Velocity  = ", objGeo.bodyFixedVelocity)
        print("Doppler Centroid Constant Term  = ",
              objGeo.dopplerCentroidConstantTerm)
        print("PRF  = ", objGeo.prf)
        print("Radar Wavelength  = ", objGeo.radarWavelength)
        print("S Coordinate First Line  = ", objGeo.sCoordinateFirstLine)
        print("Azimuth Spacing = ", objGeo.azimuthSpacing)
        print("First Latitude  = ", objGeo.firstLatitude)
        print("First Longitude  = ", objGeo.firstLongitude)
        print("Delta Latitude  = ", objGeo.deltaLatitude)
        print("Delta Longitude  = ", objGeo.deltaLongitude)
        print("Length  = ", objGeo.length)
        print("Width  = ", objGeo.width)
        print("Number Range Looks  = ", objGeo.numberRangeLooks)
        print("Number Azimuth Looks  = ", objGeo.numberAzimuthLooks)
        print("Number Points Per DEM Post  = ", objGeo.numberPointsPerDemPost)
        print("Is Mocomp  = ", objGeo.isMocomp)
        print("DEM Width  = ", objGeo.demWidth)
        print("DEM Length  = ", objGeo.demLength)
        #        print("Reference Orbit  = ", objGeo.referenceOrbit)
        print("Dim1 Reference Orbit  = ", objGeo.dim1_referenceOrbit)
        intImage.finalizeImage()
        demImage.finalizeImage()
        return objGeo
Example #17
0
    def runGeocode(self, inFilename, widthIn, geoFilename, demcropFilename):
        import stdproc
        from isceobj import createDemImage

        print("runGeocode: inFilename, widthIn = ", inFilename, widthIn)
        print("runGeocode: geoFilename, demcropFilename = ", geoFilename,
              demcropFilename)

        logger.info("Geocoding Image")

        # Initialize the Dem

        demImage = createDemImage()
        IU.copyAttributes(self.insar.demImage, demImage)
        demImage.setAccessMode('read')
        demImage.createImage()

        # Initialize the flattened interferogram
        from isceobj import createIntImage, createImage
        intImage = createIntImage()
        intImage.filename = inFilename
        intImage.width = widthIn
        intImage.setAccessMode('read')
        intImage.createImage()

        minLat, maxLat, minLon, maxLon = self.insar.topo.snwe

        planet = self.insar.referenceFrame.instrument.getPlatform().getPlanet()

        objGeo = stdproc.createGeocode()
        objGeo.listInputPorts()
        objGeo.wireInputPort(name='peg', object=self.insar.peg)
        objGeo.wireInputPort(name='frame', object=self.insar.referenceFrame)
        objGeo.wireInputPort(name='planet', object=planet)
        objGeo.wireInputPort(name='dem', object=demImage)
        objGeo.wireInputPort(name='interferogram', object=intImage)
        objGeo.wireInputPort(name='geoPosting', object=self.geoPosting)
        print("self.geoPosting = ", self.geoPosting)

        objGeo.snwe = minLat, maxLat, minLon, maxLon
        objGeo.geoFilename = geoFilename
        objGeo.demCropFilename = demcropFilename

        #set the tag used in the outfile. each message is precided by this tag
        #is the writer is not of "file" type the call has no effect
        objGeo.stdWriter = self.stdWriter.set_file_tags(
            "geocode", "log", "err", "out")

        # see mocompbaseline
        objFormSlc1 = self.insar.formSLC1
        mocompPosition1 = objFormSlc1.getMocompPosition()
        posIndx = 1
        objGeo.referenceOrbit = mocompPosition1[posIndx]
        prf1 = self.insar.referenceFrame.instrument.getPulseRepetitionFrequency(
        )
        dp = self.insar.dopplerCentroid.getDopplerCoefficients(inHz=False)[0]
        v = self.insar.procVelocity
        h = self.insar.averageHeight
        objGeo.setDopplerCentroidConstantTerm(dp)
        objGeo.setBodyFixedVelocity(v)
        objGeo.setSpacecraftHeight(h)
        objGeo.setNumberRangeLooks(self.insar.numberRangeLooks)
        objGeo.setNumberAzimuthLooks(self.insar.numberAzimuthLooks)
        # I have no idea what ismocomp means
        goodLines = self.insar.numberValidPulses
        patchSize = self.insar.patchSize
        # this variable was hardcoded in geocode.f90 and was equal to (8192 - 2048)/2
        is_mocomp = self.insar.is_mocomp
        #        is_mocomp = int((patchSize - goodLines)/2)
        objGeo.setISMocomp(is_mocomp)

        objGeo.geocode()

        intImage.finalizeImage()
        demImage.finalizeImage()
        return objGeo
Example #18
0
def runGeocode(self, prodlist, bbox, is_offset_mode=False):
    '''Generalized geocoding of all the files listed above.'''
    from isceobj.Catalog import recordInputsAndOutputs
    logger.info("Geocoding Image")
    insar = self._insar

    if (not self.doDenseOffsets) and (is_offset_mode):
        print('Skipping geocoding as Dense Offsets has not been requested ....')
        return


    if isinstance(prodlist,str):
        from isceobj.Util.StringUtils import StringUtils as SU
        tobeGeocoded = SU.listify(prodlist)
    else:
        tobeGeocoded = prodlist

    
    #remove files that have not been processed
    newlist=[]
    for toGeo in tobeGeocoded:
        if os.path.exists(toGeo):
            newlist.append(toGeo)

    
    tobeGeocoded = newlist
    print('Number of products to geocode: ', len(tobeGeocoded))

    if len(tobeGeocoded) == 0:
        print('No products found to geocode')
        return


    ###Read in the product
    burst = self._insar.loadProduct( self._insar.masterSlcCropProduct)

    ####Get required values from product
    t0 = burst.sensingStart
    prf = burst.PRF
    r0 = burst.startingRange
    dr = 0.5* SPEED_OF_LIGHT/ burst.rangeSamplingRate
    wvl = burst.radarWavelegth
    side= burst.getInstrument().getPlatform().pointingDirection
    orb = burst.orbit
    planet = Planet(pname='Earth')
     
    if (bbox is None):
        snwe = self._insar.estimatedBbox
    else:
        snwe = bbox
        if len(snwe) != 4 :
            raise Exception('Bbox must be 4 floats in SNWE order.')

    if is_offset_mode:  ### If using topsOffsetApp, image has been "pre-looked" by the
        numberRangeLooks = self.denseSkipWidth    ### skips in runDenseOffsets
        numberAzimuthLooks = self.denseSkipHeight
        rangeFirstSample = r0 + (self._insar.offset_left-1) * dr
        sensingStart =  t0 + datetime.timedelta(seconds=((self._insar.offset_top-1)/prf))
    else:
        ###Resolve number of looks
        azLooks, rgLooks = self.insar.numberOfLooks(burst, self.posting, self.numberAzimuthLooks, self.numberRangeLooks)

        numberRangeLooks = rgLooks
        numberAzimuthLooks = azLooks
        rangeFirstSample = r0 + ((numberRangeLooks-1)/2.0) * dr
        sensingStart = t0 + datetime.timedelta(seconds=(((numberAzimuthLooks-1)/2.0)/prf))


    ###Ughhh!! Doppler handling
    if self._insar.masterGeometrySystem.lower().startswith('native'):
        ###Need to fit polynomials
        ###Geozero fortran assumes that starting range for radar image and polynomial are same
        ###Also assumes that the polynomial spacing is original spacing at full looks
        ###This is not true for multilooked data. Need to fix this with good datastruct in ISCEv3
        ###Alternate method is to modify the mean and norm of a Poly1D structure such that the 
        ###translation is accounted for correctly.
        poly = burst._dopplerVsPixel

        if len(poly) != 1:
            slcPix = np.linspace(0., burst.numberOfSamples, len(poly)+1)
            dopplers = np.polyval(poly[::-1], slcPix)

            newPix = slcPix - (rangeFirstSample - r0)/dr
            nppoly = np.polyfit(newPix, dopplers, len(poly)-1)

            dopplercoeff = list(nppoly[::-1])
        else:
            dopplercoeff = poly

    else:
        dopplercoeff = [0.]

    ##Scale by PRF since the module needs it
    dopplercoeff = [x/prf for x in dopplercoeff]

    ###Setup DEM
    demfilename = self.verifyDEM()
    demImage = isceobj.createDemImage()
    demImage.load(demfilename + '.xml')

    ###Catalog for tracking
    catalog = isceobj.Catalog.createCatalog(insar.procDoc.name)
    catalog.addItem('Dem Used', demfilename, 'geocode')

    #####Geocode one by one
    first = False
    ge = Geocodable()
    for prod in tobeGeocoded:
        objGeo = createGeozero()
        objGeo.configure()

        ####IF statements to check for user configuration
        objGeo.snwe = snwe
        objGeo.demCropFilename = insar.demCropFilename


        objGeo.dopplerCentroidCoeffs = dopplercoeff
        objGeo.lookSide = side

        #create the instance of the input image and the appropriate
        #geocode method
        inImage,method = ge.create(prod)
        objGeo.method = method

        objGeo.slantRangePixelSpacing = dr
        objGeo.prf = prf
        objGeo.orbit = orb 
        objGeo.width = inImage.getWidth()
        objGeo.length = inImage.getLength()
        objGeo.dopplerCentroidCoeffs = dopplercoeff
        objGeo.radarWavelength = wvl
        objGeo.rangeFirstSample = rangeFirstSample
        objGeo.setSensingStart(sensingStart)
        objGeo.numberRangeLooks = numberRangeLooks
        objGeo.numberAzimuthLooks = numberAzimuthLooks


        objGeo.wireInputPort(name='dem', object=demImage)
        objGeo.wireInputPort(name='planet', object=planet)
        objGeo.wireInputPort(name='tobegeocoded', object=inImage)

        objGeo.geocode()
        # NEW COMMANDS added by YL --start
        catalog.addItem('Geocoding', inImage.filename, 'geocode')
        catalog.addItem('Output file', inImage.filename + '.geo', 'geocode')
        # NEW COMMANDS added by YL --end
        catalog.addItem('Width', inImage.width, 'geocode')
        catalog.addItem('Length', inImage.length, 'geocode')
        catalog.addItem('Range looks', objGeo.numberRangeLooks, 'geocode')
        catalog.addItem('Azimuth looks', objGeo.numberAzimuthLooks, 'geocode')
        catalog.addItem('South' , objGeo.minimumGeoLatitude, 'geocode')
        catalog.addItem('North', objGeo.maximumGeoLatitude, 'geocode')
        catalog.addItem('West', objGeo.minimumGeoLongitude, 'geocode')
        catalog.addItem('East', objGeo.maximumGeoLongitude, 'geocode')

    catalog.printToLog(logger, "runGeocode")
    self._insar.procDoc.addAllFromCatalog(catalog)
Example #19
0
def runVerifyDEM(self):
    '''
    Make sure that a DEM is available for processing the given data.
    '''

    self.demStitcher.noFilling = False

    ###If provided in the input XML file
    if self.demFilename not in ['', None]:
        demimg = isceobj.createDemImage()
        demimg.load(self.demFilename + '.xml')
        if not os.path.exists(self.demFilename + '.vrt'):
            demimg.renderVRT()

        if demimg.reference.upper() == 'EGM96':
            wgsdemname = self.demFilename + '.wgs84'

            if os.path.exists(wgsdemname) and os.path.exists(wgsdemname +
                                                             '.xml'):
                demimg = isceobj.createDemImage()
                demimg.load(wgsdemname + '.xml')

                if demimg.reference.upper() == 'EGM96':
                    raise Exception(
                        'WGS84 version of dem found by reference set to EGM96')

            else:
                demimg = self.demStitcher.correct(demimg)

        elif demimg.reference.upper() != 'WGS84':
            raise Exception('Unknown reference system for DEM: {0}'.format(
                demimg.reference))

    else:

        refPol = self._grd.polarizations[0]

        reference = self._grd.loadProduct(
            os.path.join(self._grd.outputFolder,
                         'beta_{0}.xml'.format(refPol)))
        bbox = reference.getBbox()

        ####Truncate to integers
        tbox = [
            np.floor(bbox[0]),
            np.ceil(bbox[1]),
            np.floor(bbox[2]),
            np.ceil(bbox[3])
        ]

        filename = self.demStitcher.defaultName(tbox)
        wgsfilename = filename + '.wgs84'

        ####Check if WGS84 file exists
        if os.path.exists(wgsfilename) and os.path.exists(wgsfilename +
                                                          '.xml'):
            demimg = isceobj.createDemImage()
            demimg.load(wgsfilename + '.xml')

            if not os.path.exists(wgsfilename + '.vrt'):
                demimg.renderVRT()

        ####Check if EGM96 file exists
        elif os.path.exists(filename) and os.path.exists(filename + '.xml'):
            inimg = isceobj.createDemImage()
            inimg.load(filename + '.xml')

            if not os.path.exists(filename + '.xml'):
                inimg.renderVRT()

            demimg = self.demStitcher.correct(inimg)

        else:
            stitchOk = self.demStitcher.stitch(tbox[0:2], tbox[2:4])

            if not stitchOk:
                logger.error(
                    "Cannot form the DEM for the region of interest. If you have one, set the appropriate DEM component in the input file."
                )
                raise Exception

            inimg = isceobj.createDemImage()
            inimg.load(filename + '.xml')
            if not os.path.exists(filename):
                inimg.renderVRT()

            demimg = self.demStitcher.correct(inimg)

        #get water mask
#        self.runCreateWbdMask(info)

    return demimg.filename
Example #20
0
def runTopo(self):
    from zerodop.topozero import createTopozero
    from isceobj.Planet.Planet import Planet

    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')

    boxes = []
    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')

            ###Check if geometry directory already exists.
            dirname = os.path.join(self._insar.geometryDirname,
                                   'IW{0}'.format(swath))

            if os.path.isdir(dirname):
                logger.info(
                    'Geometry directory {0} already exists.'.format(dirname))
            else:
                os.makedirs(dirname)

            ###For each burst
            for index in range(numCommon):
                ind = index + startIndex
                burst = master.bursts[ind]

                latname = os.path.join(dirname, 'lat_%02d.rdr' % (ind + 1))
                lonname = os.path.join(dirname, 'lon_%02d.rdr' % (ind + 1))
                hgtname = os.path.join(dirname, 'hgt_%02d.rdr' % (ind + 1))
                losname = os.path.join(dirname, 'los_%02d.rdr' % (ind + 1))
                incangname = os.path.join(dirname,
                                          'incang_%02d.rdr' % (ind + 1))

                demImage = isceobj.createDemImage()
                demImage.load(demfilename + '.xml')

                #####Run Topo
                planet = Planet(pname='Earth')
                topo = createTopozero()
                topo.slantRangePixelSpacing = burst.rangePixelSize
                topo.prf = 1.0 / burst.azimuthTimeInterval
                topo.radarWavelength = burst.radarWavelength
                topo.orbit = burst.orbit
                topo.width = burst.numberOfSamples
                topo.length = burst.numberOfLines
                topo.wireInputPort(name='dem', object=demImage)
                topo.wireInputPort(name='planet', object=planet)
                topo.numberRangeLooks = 1
                topo.numberAzimuthLooks = 1
                topo.lookSide = -1
                topo.sensingStart = burst.sensingStart
                topo.rangeFirstSample = burst.startingRange
                topo.demInterpolationMethod = 'BIQUINTIC'
                topo.latFilename = latname
                topo.lonFilename = lonname
                topo.heightFilename = hgtname
                topo.losFilename = losname
                topo.incFilename = incangname
                topo.topo()

                bbox = [
                    topo.minimumLatitude, topo.maximumLatitude,
                    topo.minimumLongitude, topo.maximumLongitude
                ]
                boxes.append(bbox)

                catalog.addItem(
                    'Number of lines for burst {0} - IW-{1}'.format(
                        index, swath), burst.numberOfLines, 'topo')
                catalog.addItem(
                    'Number of pixels for bursts {0} - IW-{1}'.format(
                        index, swath), burst.numberOfSamples, 'topo')
                catalog.addItem(
                    'Bounding box for burst {0} - IW-{1}'.format(index, swath),
                    bbox, 'topo')

        else:
            print('Skipping Processing for Swath {0}'.format(swath))

        topo = None

    boxes = np.array(boxes)
    bbox = [
        np.min(boxes[:, 0]),
        np.max(boxes[:, 1]),
        np.min(boxes[:, 2]),
        np.max(boxes[:, 3])
    ]
    catalog.addItem('Overall bounding box', bbox, 'topo')

    catalog.printToLog(logger, "runTopo")
    self._insar.procDoc.addAllFromCatalog(catalog)

    return
Example #21
0
if __name__ == '__main__':

    inps = cmdLineParse()

    with open(inps.sframe, 'rb') as fid:
        slaveFrame = pickle.load(fid)

    latImage = isceobj.createImage()
    latImage.load(inps.lat + '.xml')
    latImage.setAccessMode('read')

    lonImage = isceobj.createImage()
    lonImage.load(inps.lon + '.xml')
    lonImage.setAccessMode('read')

    demImage = isceobj.createDemImage()
    demImage.load(inps.hgt + '.xml')
    demImage.setAccessMode('read')

    #run it
    runGeo2rdr(slaveFrame,
               latImage,
               lonImage,
               demImage,
               rgoffName=inps.rgoff,
               azoffName=inps.azoff,
               rlks=inps.rlks,
               alks=inps.alks)

#./geo2rdr.py -s 20141211.slc.pck -a 20130927-20141211.lat -o 20130927-20141211.lon -z 20130927-20141211.hgt -r 20130927-20141211_rg.off -i 20130927-20141211_az.off
Example #22
0
def runRdrDemOffset(self):
    '''estimate between radar image and dem
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    referenceTrack = self._insar.loadTrack(reference=True)
    demFile = os.path.abspath(self._insar.dem)

    insarDir = 'insar'
    os.makedirs(insarDir, exist_ok=True)
    os.chdir(insarDir)

    rdrDemDir = 'rdr_dem_offset'
    os.makedirs(rdrDemDir, exist_ok=True)
    os.chdir(rdrDemDir)

    ##################################################################################################
    #compute dem pixel size
    demImage = isceobj.createDemImage()
    demImage.load(demFile + '.xml')
    #DEM pixel size in meters (appoximate value)
    demDeltaLon = abs(demImage.getDeltaLongitude()) / 0.0002777777777777778 * 30.0
    demDeltaLat = abs(demImage.getDeltaLatitude())  / 0.0002777777777777778 * 30.0

    #number of looks to take in range
    if self._insar.numberRangeLooksSim == None:
        if self._insar.numberRangeLooks1 * referenceTrack.rangePixelSize > demDeltaLon:
            self._insar.numberRangeLooksSim = 1
        else:
            self._insar.numberRangeLooksSim = int(demDeltaLon / (self._insar.numberRangeLooks1 * referenceTrack.rangePixelSize) + 0.5)
    #number of looks to take in azimuth
    if self._insar.numberAzimuthLooksSim == None:
        if self._insar.numberAzimuthLooks1 * referenceTrack.azimuthPixelSize > demDeltaLat:
            self._insar.numberAzimuthLooksSim = 1
        else:
            self._insar.numberAzimuthLooksSim = int(demDeltaLat / (self._insar.numberAzimuthLooks1 * referenceTrack.azimuthPixelSize) + 0.5)

    #simulate a radar image using dem
    simulateRadar(os.path.join('../', self._insar.height), self._insar.sim, scale=3.0, offset=100.0)
    sim = isceobj.createImage()
    sim.load(self._insar.sim+'.xml')

    #take looks
    if (self._insar.numberRangeLooksSim == 1) and (self._insar.numberAzimuthLooksSim == 1):
        simLookFile = self._insar.sim
        ampLookFile = 'amp_{}rlks_{}alks.float'.format(self._insar.numberRangeLooksSim*self._insar.numberRangeLooks1, 
                                                       self._insar.numberAzimuthLooksSim*self._insar.numberAzimuthLooks1)
        cmd = "imageMath.py -e='sqrt(a_0*a_0+a_1*a_1)' --a={} -o {} -t float".format(os.path.join('../', self._insar.amplitude), ampLookFile)
        runCmd(cmd)
    else:
        simLookFile = 'sim_{}rlks_{}alks.float'.format(self._insar.numberRangeLooksSim*self._insar.numberRangeLooks1, 
                                                       self._insar.numberAzimuthLooksSim*self._insar.numberAzimuthLooks1)
        ampLookFile = 'amp_{}rlks_{}alks.float'.format(self._insar.numberRangeLooksSim*self._insar.numberRangeLooks1, 
                                                       self._insar.numberAzimuthLooksSim*self._insar.numberAzimuthLooks1)
        ampTmpFile = 'amp_tmp.float'
        look(self._insar.sim, simLookFile, sim.width, self._insar.numberRangeLooksSim, self._insar.numberAzimuthLooksSim, 2, 0, 1)
        look(os.path.join('../', self._insar.amplitude), ampTmpFile, sim.width, self._insar.numberRangeLooksSim, self._insar.numberAzimuthLooksSim, 4, 1, 1)
 
        width = int(sim.width/self._insar.numberRangeLooksSim)
        length = int(sim.length/self._insar.numberAzimuthLooksSim)
        create_xml(simLookFile, width, length, 'float')
        create_xml(ampTmpFile, width, length, 'amp')

        cmd = "imageMath.py -e='sqrt(a_0*a_0+a_1*a_1)' --a={} -o {} -t float".format(ampTmpFile, ampLookFile)
        runCmd(cmd)
        os.remove(ampTmpFile)
        os.remove(ampTmpFile+'.vrt')
        os.remove(ampTmpFile+'.xml')

    #initial number of offsets to use
    numberOfOffsets = 800
    #compute land ratio to further determine the number of offsets to use
    wbd=np.memmap(os.path.join('../', self._insar.wbdOut), dtype='byte', mode='r', shape=(sim.length, sim.width))
    landRatio = np.sum(wbd[0:sim.length:10, 0:sim.width:10]!=-1) / int(sim.length/10) / int(sim.width/10)
    del wbd
    if (landRatio <= 0.00125):
        print('\n\nWARNING: land area too small for estimating offsets between radar and dem')
        print('do not estimate offsets between radar and dem\n\n')
        self._insar.radarDemAffineTransform = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
        catalog.addItem('warning message', 'land area too small for estimating offsets between radar and dem', 'runRdrDemOffset')

        os.chdir('../../')

        catalog.printToLog(logger, "runRdrDemOffset")
        self._insar.procDoc.addAllFromCatalog(catalog)

        return

    #total number of offsets to use
    numberOfOffsets /= landRatio
    #allocate number of offsets in range/azimuth according to image width/length
    width = int(sim.width/self._insar.numberRangeLooksSim)
    length = int(sim.length/self._insar.numberAzimuthLooksSim)
    #number of offsets to use in range/azimuth
    numberOfOffsetsRange = int(np.sqrt(numberOfOffsets * width / length))
    numberOfOffsetsAzimuth = int(length / width * np.sqrt(numberOfOffsets * width / length))

    #this should be better?
    numberOfOffsetsRange = int(np.sqrt(numberOfOffsets))
    numberOfOffsetsAzimuth = int(np.sqrt(numberOfOffsets))


    if numberOfOffsetsRange > int(width/2):
        numberOfOffsetsRange = int(width/2)
    if numberOfOffsetsAzimuth > int(length/2):
        numberOfOffsetsAzimuth = int(length/2)

    if numberOfOffsetsRange < 10:
        numberOfOffsetsRange = 10
    if numberOfOffsetsAzimuth < 10:
        numberOfOffsetsAzimuth = 10

    catalog.addItem('number of range offsets', '{}'.format(numberOfOffsetsRange), 'runRdrDemOffset')
    catalog.addItem('number of azimuth offsets', '{}'.format(numberOfOffsetsAzimuth), 'runRdrDemOffset')

    #matching
    ampcor = Ampcor(name='insarapp_slcs_ampcor')
    ampcor.configure()

    mMag = isceobj.createImage()
    mMag.load(ampLookFile+'.xml')
    mMag.setAccessMode('read')
    mMag.createImage()

    sMag = isceobj.createImage()
    sMag.load(simLookFile+'.xml')
    sMag.setAccessMode('read')
    sMag.createImage()

    ampcor.setImageDataType1('real')
    ampcor.setImageDataType2('real')

    ampcor.setReferenceSlcImage(mMag)
    ampcor.setSecondarySlcImage(sMag)

    #MATCH REGION
    rgoff = 0
    azoff = 0
    #it seems that we cannot use 0, haven't look into the problem
    if rgoff == 0:
        rgoff = 1
    if azoff == 0:
        azoff = 1
    firstSample = 1
    if rgoff < 0:
        firstSample = int(35 - rgoff)
    firstLine = 1
    if azoff < 0:
        firstLine = int(35 - azoff)
    ampcor.setAcrossGrossOffset(rgoff)
    ampcor.setDownGrossOffset(azoff)
    ampcor.setFirstSampleAcross(firstSample)
    ampcor.setLastSampleAcross(width)
    ampcor.setNumberLocationAcross(numberOfOffsetsRange)
    ampcor.setFirstSampleDown(firstLine)
    ampcor.setLastSampleDown(length)
    ampcor.setNumberLocationDown(numberOfOffsetsAzimuth)

    #MATCH PARAMETERS
    ampcor.setWindowSizeWidth(64)
    ampcor.setWindowSizeHeight(64)
    #note this is the half width/length of search area, so number of resulting correlation samples: 8*2+1
    ampcor.setSearchWindowSizeWidth(16)
    ampcor.setSearchWindowSizeHeight(16)

    #REST OF THE STUFF
    ampcor.setAcrossLooks(1)
    ampcor.setDownLooks(1)
    ampcor.setOversamplingFactor(64)
    ampcor.setZoomWindowSize(16)
    #1. The following not set
    #Matching Scale for Sample/Line Directions                       (-)    = 1. 1.
    #should add the following in Ampcor.py?
    #if not set, in this case, Ampcor.py'value is also 1. 1.
    #ampcor.setScaleFactorX(1.)
    #ampcor.setScaleFactorY(1.)

    #MATCH THRESHOLDS AND DEBUG DATA
    #2. The following not set
    #in roi_pac the value is set to 0 1
    #in isce the value is set to 0.001 1000.0
    #SNR and Covariance Thresholds                                   (-)    =  {s1} {s2}
    #should add the following in Ampcor?
    #THIS SHOULD BE THE ONLY THING THAT IS DIFFERENT FROM THAT OF ROI_PAC
    #ampcor.setThresholdSNR(0)
    #ampcor.setThresholdCov(1)
    ampcor.setDebugFlag(False)
    ampcor.setDisplayFlag(False)

    #in summary, only two things not set which are indicated by 'The following not set' above.

    #run ampcor
    ampcor.ampcor()
    offsets = ampcor.getOffsetField()
    ampcorOffsetFile = 'ampcor.off'
    cullOffsetFile = 'cull.off'
    affineTransformFile = 'affine_transform.txt'
    writeOffset(offsets, ampcorOffsetFile)

    #finalize image, and re-create it
    #otherwise the file pointer is still at the end of the image
    mMag.finalizeImage()
    sMag.finalizeImage()

    # #cull offsets
    # import io
    # from contextlib import redirect_stdout
    # f = io.StringIO()
    # with redirect_stdout(f):
    #     fitoff(ampcorOffsetFile, cullOffsetFile, 1.5, .5, 50)
    # s = f.getvalue()
    # #print(s)
    # with open(affineTransformFile, 'w') as f:
    #     f.write(s)

    #cull offsets
    import subprocess
    proc = subprocess.Popen(["python3", "-c", "import isce; from contrib.alos2proc_f.alos2proc_f import fitoff; fitoff('ampcor.off', 'cull.off', 1.5, .5, 50)"], stdout=subprocess.PIPE)
    out = proc.communicate()[0]
    with open(affineTransformFile, 'w') as f:
        f.write(out.decode('utf-8'))

    #check number of offsets left
    with open(cullOffsetFile, 'r') as f:
        numCullOffsets = sum(1 for linex in f)
    if numCullOffsets < 50:
        print('\n\nWARNING: too few points left after culling, {} left'.format(numCullOffsets))
        print('do not estimate offsets between radar and dem\n\n')
        self._insar.radarDemAffineTransform = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
        catalog.addItem('warning message', 'too few points left after culling, {} left'.format(numCullOffsets), 'runRdrDemOffset')

        os.chdir('../../')

        catalog.printToLog(logger, "runRdrDemOffset")
        self._insar.procDoc.addAllFromCatalog(catalog)

        return

    #read affine transform parameters
    with open(affineTransformFile) as f:
        lines = f.readlines()
    i = 0
    for linex in lines:
        if 'Affine Matrix ' in linex:
            m11 = float(lines[i + 2].split()[0])
            m12 = float(lines[i + 2].split()[1])
            m21 = float(lines[i + 3].split()[0])
            m22 = float(lines[i + 3].split()[1])
            t1  = float(lines[i + 7].split()[0])
            t2  = float(lines[i + 7].split()[1])
            break
        i += 1    

    self._insar.radarDemAffineTransform = [m11, m12, m21, m22, t1, t2]
    ##################################################################################################

    os.chdir('../../')


    catalog.printToLog(logger, "runRdrDemOffset")
    self._insar.procDoc.addAllFromCatalog(catalog)
Example #23
0
def runMultilook(in_dir,
                 out_dir,
                 alks,
                 rlks,
                 in_ext='.rdr',
                 out_ext='.rdr',
                 method='gdal',
                 fbase_list=[
                     'hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask',
                     'waterMask'
                 ]):
    """
    Multilook geometry files.
    """
    from iscesys.Parsers.FileParserFactory import createFileParser
    from mroipac.looks.Looks import Looks

    msg = 'generate multilooked geometry files with alks={} and rlks={}'.format(
        alks, rlks)
    if method == 'isce':
        msg += ' using mroipac.looks.Looks() ...'
    else:
        msg += ' using gdal.Translate() ...'
    print('-' * 50 + '\n' + msg)

    # create 'geom_reference' directory
    os.makedirs(out_dir, exist_ok=True)

    # multilook files one by one
    for fbase in fbase_list:
        in_file = os.path.join(in_dir, '{}{}'.format(fbase, in_ext))
        out_file = os.path.join(out_dir, '{}{}'.format(fbase, out_ext))

        if all(os.path.isfile(in_file + ext) for ext in ['', '.vrt', '.xml']):
            print('multilook {}'.format(in_file))

            # option 1 - Looks module (isce)
            if method == 'isce':
                xmlProp = createFileParser('xml').parse(in_file + '.xml')[0]
                if ('image_type' in xmlProp
                        and xmlProp['image_type'] == 'dem'):
                    inImage = isceobj.createDemImage()
                else:
                    inImage = isceobj.createImage()

                inImage.load(in_file + '.xml')
                inImage.filename = in_file

                lkObj = Looks()
                lkObj.setDownLooks(alks)
                lkObj.setAcrossLooks(rlks)
                lkObj.setInputImage(inImage)
                lkObj.setOutputFilename(out_file)
                lkObj.looks()

            # option 2 - gdal_translate (gdal)
            elif method == 'gdal':
                ds = gdal.Open(in_file, gdal.GA_ReadOnly)
                in_wid = ds.RasterXSize
                in_len = ds.RasterYSize

                out_wid = int(in_wid / rlks)
                out_len = int(in_len / alks)
                src_wid = out_wid * rlks
                src_len = out_len * alks

                options_str = '-of ENVI -a_nodata 0 -outsize {ox} {oy} -srcwin 0 0 {sx} {sy} '.format(
                    ox=out_wid, oy=out_len, sx=src_wid, sy=src_len)
                gdal.Translate(out_file, ds, options=options_str)

                # generate ISCE .xml file
                if not os.path.isfile(out_file + '.xml'):
                    cmd = 'gdal2isce_xml.py -i {}.vrt'.format(out_file)
                    print(cmd)
                    os.system(cmd)

            else:
                raise ValueError(
                    'un-supported multilook method: {}'.format(method))

            # copy the full resolution xml/vrt file from ./merged/geom_reference to ./geom_reference
            # to facilitate the number of looks extraction
            # the file path inside .xml file is not, but should, updated
            if in_file != out_file + '.full':
                shutil.copy(in_file + '.xml', out_file + '.full.xml')
                shutil.copy(in_file + '.vrt', out_file + '.full.vrt')

    return out_dir
Example #24
0
def geo2rdr(swath,
            track,
            latFile,
            lonFile,
            hgtFile,
            rangeOffsetFile,
            azimuthOffsetFile,
            numberRangeLooks=1,
            numberAzimuthLooks=1,
            multilookTimeOffset=True):
    import datetime
    import isceobj
    from zerodop.geo2rdr import createGeo2rdr
    from isceobj.Planet.Planet import Planet

    pointingDirection = {'right': -1, 'left': 1}

    latImage = isceobj.createImage()
    latImage.load(latFile + '.xml')
    latImage.setAccessMode('read')

    lonImage = isceobj.createImage()
    lonImage.load(lonFile + '.xml')
    lonImage.setAccessMode('read')

    hgtImage = isceobj.createDemImage()
    hgtImage.load(hgtFile + '.xml')
    hgtImage.setAccessMode('read')

    planet = Planet(pname='Earth')

    topo = createGeo2rdr()
    topo.configure()
    topo.slantRangePixelSpacing = numberRangeLooks * swath.rangePixelSize
    topo.prf = 1.0 / (numberAzimuthLooks * swath.azimuthLineInterval)
    topo.radarWavelength = track.radarWavelength
    topo.orbit = track.orbit
    topo.width = int(swath.numberOfSamples / numberRangeLooks)
    topo.length = int(swath.numberOfLines / numberAzimuthLooks)
    topo.demLength = hgtImage.length
    topo.demWidth = hgtImage.width
    topo.wireInputPort(name='planet', object=planet)
    topo.numberRangeLooks = 1
    topo.numberAzimuthLooks = 1  #must be set to be 1
    topo.lookSide = pointingDirection[track.pointingDirection]
    if multilookTimeOffset == True:
        topo.sensingStart = swath.sensingStart + datetime.timedelta(
            seconds=(numberAzimuthLooks - 1.0) / 2.0 *
            swath.azimuthLineInterval)
        topo.rangeFirstSample = swath.startingRange + (
            numberRangeLooks - 1.0) / 2.0 * swath.rangePixelSize
    else:
        topo.setSensingStart(swath.sensingStart)
        topo.rangeFirstSample = swath.startingRange
    topo.dopplerCentroidCoeffs = [0.]  #we are using zero doppler geometry
    topo.demImage = hgtImage
    topo.latImage = latImage
    topo.lonImage = lonImage
    topo.rangeOffsetImageName = rangeOffsetFile
    topo.azimuthOffsetImageName = azimuthOffsetFile
    topo.geo2rdr()

    return
Example #25
0
def waterBodyRadar(latFile, lonFile, wbdFile, wbdOutFile):
    '''
    create water boday in radar coordinates
    '''
    import numpy as np
    import isceobj
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    demImage = isceobj.createDemImage()
    demImage.load(wbdFile + '.xml')
    #demImage.setAccessMode('read')
    wbd = np.memmap(wbdFile,
                    dtype='byte',
                    mode='r',
                    shape=(demImage.length, demImage.width))

    image = isceobj.createImage()
    image.load(latFile + '.xml')
    width = image.width
    length = image.length

    latFp = open(latFile, 'rb')
    lonFp = open(lonFile, 'rb')
    wbdOutFp = open(wbdOutFile, 'wb')
    print("create water body in radar coordinates...")
    for i in range(length):
        if (((i + 1) % 200) == 0):
            print("processing line %6d of %6d" % (i + 1, length),
                  end='\r',
                  flush=True)
        wbdOut = np.zeros(width, dtype='byte') - 2
        lat = np.fromfile(latFp, dtype=np.float64, count=width)
        lon = np.fromfile(lonFp, dtype=np.float64, count=width)
        #indexes start with zero
        lineIndex = np.int32((lat - demImage.firstLatitude) /
                             demImage.deltaLatitude + 0.5)
        sampleIndex = np.int32((lon - demImage.firstLongitude) /
                               demImage.deltaLongitude + 0.5)
        inboundIndex = np.logical_and(
            np.logical_and(lineIndex >= 0, lineIndex <= demImage.length - 1),
            np.logical_and(sampleIndex >= 0,
                           sampleIndex <= demImage.width - 1))
        #keep SRTM convention. water body. (0) --- land; (-1) --- water; (-2 or other value) --- no data.
        wbdOut = wbd[(lineIndex[inboundIndex], sampleIndex[inboundIndex])]
        wbdOut.astype(np.int8).tofile(wbdOutFp)
    print("processing line %6d of %6d" % (length, length))
    #create_xml(wbdOutFile, width, length, 'byte')

    image = isceobj.createImage()
    image.setDataType('BYTE')
    image.addDescription(
        'water body. (0) --- land; (-1) --- water; (-2) --- no data.')
    image.setFilename(wbdOutFile)
    image.extraFilename = wbdOutFile + '.vrt'
    image.setWidth(width)
    image.setLength(length)
    image.renderHdr()

    del wbd, demImage, image
    latFp.close()
    lonFp.close()
    wbdOutFp.close()
Example #26
0
def runTopo(self):
    from zerodop.topozero import createTopozero
    from isceobj.Planet.Planet import Planet

    logger.info("Running topo")

    #IU.copyAttributes(demImage, objDem)
    geometryDir = self.insar.geometryDirname

    os.makedirs(geometryDir, exist_ok=True)

    demFilename = self.verifyDEM()
    objDem = isceobj.createDemImage()
    objDem.load(demFilename + '.xml')

    info = self._insar.loadProduct(self._insar.masterSlcCropProduct)
    intImage = info.getImage()

    planet = info.getInstrument().getPlatform().getPlanet()
    topo = createTopozero()

    topo.slantRangePixelSpacing = 0.5 * SPEED_OF_LIGHT / info.rangeSamplingRate
    topo.prf = info.PRF
    topo.radarWavelength = info.radarWavelegth
    topo.orbit = info.orbit
    topo.width = intImage.getWidth()
    topo.length = intImage.getLength()
    topo.wireInputPort(name='dem', object=objDem)
    topo.wireInputPort(name='planet', object=planet)
    topo.numberRangeLooks = 1
    topo.numberAzimuthLooks = 1
    topo.lookSide = info.getInstrument().getPlatform().pointingDirection
    topo.sensingStart = info.getSensingStart()
    topo.rangeFirstSample = info.startingRange

    topo.demInterpolationMethod = 'BIQUINTIC'
    topo.latFilename = os.path.join(geometryDir,
                                    self.insar.latFilename + '.full')
    topo.lonFilename = os.path.join(geometryDir,
                                    self.insar.lonFilename + '.full')
    topo.losFilename = os.path.join(geometryDir,
                                    self.insar.losFilename + '.full')
    topo.heightFilename = os.path.join(geometryDir,
                                       self.insar.heightFilename + '.full')
    #    topo.incFilename = os.path.join(info.outdir, 'inc.rdr')
    #    topo.maskFilename = os.path.join(info.outdir, 'mask.rdr')

    ####Doppler adjustment
    dop = [x / 1.0 for x in info._dopplerVsPixel]

    doppler = Poly2D()
    doppler.setWidth(topo.width // topo.numberRangeLooks)
    doppler.setLength(topo.length // topo.numberAzimuthLooks)

    if self._insar.masterGeometrySystem.lower().startswith('native'):
        doppler.initPoly(rangeOrder=len(dop) - 1, azimuthOrder=0, coeffs=[dop])
    else:
        doppler.initPoly(rangeOrder=0, azimuthOrder=0, coeffs=[[0.]])

    topo.polyDoppler = doppler

    topo.topo()

    # Record the inputs and outputs
    from isceobj.Catalog import recordInputsAndOutputs
    recordInputsAndOutputs(self._insar.procDoc, topo, "runTopo", logger,
                           "runTopo")

    self._insar.estimatedBbox = [
        topo.minimumLatitude, topo.maximumLatitude, topo.minimumLongitude,
        topo.maximumLongitude
    ]
    return topo
Example #27
0
def main(iargs=None):

    inps = cmdLineParse(iargs)

    swathList = ut.getSwathList(inps.master)

    demImage = isceobj.createDemImage()
    demImage.load(inps.dem + '.xml')

    boxes = []
    for swath in swathList:
        master =  ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
    
        ###Check if geometry directory already exists.
        dirname = os.path.join(inps.geom_masterDir, 'IW{0}'.format(swath))

        if os.path.isdir(dirname):
            print('Geometry directory {0} already exists.'.format(dirname))
        else:
            os.makedirs(dirname)


    ###For each burst
        for ind in range(master.numberOfBursts):
            burst = master.bursts[ind]

            latname = os.path.join(dirname, 'lat_%02d.rdr'%(ind+1))
            lonname = os.path.join(dirname, 'lon_%02d.rdr'%(ind+1))
            hgtname = os.path.join(dirname, 'hgt_%02d.rdr'%(ind+1))
            losname = os.path.join(dirname, 'los_%02d.rdr'%(ind+1))
            maskname = os.path.join(dirname, 'shadowMask_%02d.rdr'%(ind+1))
            incname = os.path.join(dirname, 'incLocal_%02d.rdr'%(ind+1))
        #####Run Topo
            planet = Planet(pname='Earth')
            topo = createTopozero()
            topo.slantRangePixelSpacing = burst.rangePixelSize
            topo.prf = 1.0/burst.azimuthTimeInterval
            topo.radarWavelength = burst.radarWavelength
            topo.orbit = burst.orbit
            topo.width = burst.numberOfSamples
            topo.length = burst.numberOfLines
            topo.wireInputPort(name='dem', object=demImage)
            topo.wireInputPort(name='planet', object=planet)
            topo.numberRangeLooks = 1
            topo.numberAzimuthLooks = 1
            topo.lookSide = -1
            topo.sensingStart = burst.sensingStart
            topo.rangeFirstSample = burst.startingRange
            topo.demInterpolationMethod='BIQUINTIC'
            topo.latFilename = latname
            topo.lonFilename = lonname
            topo.heightFilename = hgtname
            topo.losFilename = losname
            topo.maskFilename = maskname
            topo.incFilename = incname
            topo.topo()

            bbox = [topo.minimumLatitude, topo.maximumLatitude, topo.minimumLongitude, topo.maximumLongitude]
            boxes.append(bbox)

            topo = None

    boxes = np.array(boxes)
    bbox = [np.min(boxes[:,0]), np.max(boxes[:,1]), np.min(boxes[:,2]), np.max(boxes[:,3])]
    print ('bbox : ',bbox)
Example #28
0
def main():
    #if not argument provided force the --help flag
    if (len(sys.argv) == 1):
        sys.argv.append('-h')

    # Use the epilog to add usege eamples
    epilog = 'Usage examples:\n\n'
    epilog += 'mask.py -a stitch -i dem.xml -r -n your_username -w your_password  -u https://aria-dav.jpl.nasa.gov/repository/products \n\n'
    epilog += 'mask.py -a download -i dem.xml \n\n'
    epilog += 'mask.py -a stitch -i dem.xml -k  -r -l\n'
    #set the formatter_class=argparse.RawDescriptionHelpFormatter othewise it splits the epilog lines with its own default format
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog)

    parser.add_argument(
        '-a',
        '--action',
        type=str,
        default='stitch',
        dest='action',
        help='Possible actions: stitch or download (default: %(default)s). ')
    parser.add_argument(
        '-m',
        '--meta',
        type=str,
        default='xml',
        dest='meta',
        help='What type of metadata file is created. Possible values: \
                        xml or rsc (default: %(default)s)')
    parser.add_argument(
        '-i',
        '--input',
        type=str,
        required=True,
        dest='indem',
        help='Input DEM for which the land water mask is desired.')
    parser.add_argument(
        '-k',
        '--keep',
        action='store_true',
        dest='keep',
        help=
        'If the option is present then the single files used for stitching are kept. If -l or --local is specified than the flag is automatically set (default: %(default)s)'
    )
    parser.add_argument(
        '-r',
        '--report',
        action='store_true',
        dest='report',
        help=
        'If the option is present then failed and succeeded downloads are printed (default: %(default)s)'
    )
    parser.add_argument(
        '-l',
        '--local',
        action='store_true',
        dest='local',
        help=
        'If the option is present then use the files that are in the location \
                        specified by --dir. If not present --dir indicates the directory where the files are downloaded (default: %(default)s)'
    )
    parser.add_argument(
        '-d',
        '--dir',
        type=str,
        dest='dir',
        default='./',
        help=
        'If used in conjunction with --local it specifies the location where the DEMs are located \
                        otherwise it specifies the directory where the DEMs are downloaded and the stitched DEM is generated (default: %(default)s)'
    )

    parser.add_argument(
        '-o',
        '--output',
        type=str,
        dest='output',
        default=None,
        help=
        'Name of the output file to be created in --dir. If not provided the system generates one based on the bbox extremes'
    )
    parser.add_argument(
        '-n',
        '--uname',
        type=str,
        dest='uname',
        default=None,
        help='User name if using a server that requires authentication')
    parser.add_argument(
        '-w',
        '--password',
        type=str,
        dest='password',
        default=None,
        help='Password if using a server that requires authentication')
    parser.add_argument(
        '-u',
        '--url',
        type=str,
        dest='url',
        default=None,
        help=
        'Part of the url where the DEM files are located. The actual location must be \
                        the one specified by --url plus /srtm/version2_1/SRTM(1,3)'
    )

    args = parser.parse_args()
    #first get the url,uname and password since are needed in the constructor

    ds = MaskStitcher()
    ds.configure()
    if (args.url):
        ds.setUrl(args.url)
    ds.setUsername(args.uname)
    ds.setPassword(args.password)
    ds._keepAfterFailed = True
    #avoid to accidentally remove local file if -k is forgotten
    #if one wants can remove them manually
    if (args.local):
        args.keep = True
    if (args.meta == 'xml'):
        ds.setCreateXmlMetadata(True)
    elif (args.meta == 'rsc'):
        ds.setCreateRscMetadata(True)

    ds.setUseLocalDirectory(args.local)

    ####Parse input DEM xml to get bbox
    inimg = isceobj.createDemImage()
    inimg.load(args.indem + '.xml')

    north = inimg.coord2.coordStart
    south = north + inimg.coord2.coordDelta * (inimg.length - 1)

    west = inimg.coord1.coordStart
    east = west + inimg.coord1.coordDelta * (inimg.width - 1)

    bbox = [south, north, west, east]

    ds.setWidth(inimg.width)
    ds.setLength(inimg.length)
    ds.setFirstLatitude(north)
    ds.setFirstLongitude(west)
    ds.setLastLatitude(south)
    ds.setLastLongitude(east)

    if (args.action == 'stitch'):
        lat = bbox[0:2]
        lon = bbox[2:4]
        if (args.output is None):
            args.output = ds.defaultName(bbox)

        if not (ds.stitchMasks(lat, lon, args.output, args.dir,
                               keep=args.keep)):
            print('Some tiles are missing. Maybe ok')

    elif (args.action == 'download'):
        lat = bbox[0:2]
        lon = bbox[2:4]
        ds.getMasksInBox(lat, lon, args.dir)

    else:
        print('Unrecognized action -a or --action', args.action)
        return

    if (args.report):
        for k, v in ds._downloadReport.items():
            print(k, '=', v)
Example #29
0
def main(iargs=None): 
    inps = cmdLineParse(iargs)
    #print(inps.slave)

    # see if the user compiled isce with GPU enabled
    run_GPU = False
    try:
        from zerodop.GPUtopozero.GPUtopozero import PyTopozero
        from zerodop.GPUgeo2rdr.GPUgeo2rdr import PyGeo2rdr
        run_GPU = True
    except:
        pass

    if inps.useGPU and not run_GPU:
        print("GPU mode requested but no GPU ISCE code found")


    # setting the respective version of geo2rdr for CPU and GPU
    if run_GPU and inps.useGPU:
        print('GPU mode')
        runGeo2rdr = runGeo2rdrGPU
    else:
        print('CPU mode')
        runGeo2rdr = runGeo2rdrCPU

    db = shelve.open( os.path.join(inps.slave, 'data'), flag='r')
    #print( os.path.join(inps.slave, 'data'))
    frame = db['frame']
    try:
        dop = db['doppler']
    except:
        dop = frame._dopplerVsPixel

    db.close()

    ####Setup dem
    demImage = isceobj.createDemImage()
    demImage.load(os.path.join(inps.geom, 'hgt.rdr.xml'))
    demImage.setAccessMode('read')

    latImage = isceobj.createImage()
    latImage.load(os.path.join(inps.geom, 'lat.rdr.xml'))
    latImage.setAccessMode('read')

    lonImage = isceobj.createImage()
    lonImage.load(os.path.join(inps.geom, 'lon.rdr.xml'))
    lonImage.setAccessMode('read')

    if not os.path.isdir(inps.outdir):
        os.makedirs(inps.outdir)

    
    azoff = 0.0
    rgoff = 0.0
    if inps.poly is not None:
        db1 = shelve.open(inps.poly, flag='r')
        azpoly = db1['azpoly']
        rgpoly = db1['rgpoly']
        db1.close()

        azoff = azpoly._coeffs[0][0]
        rgoff = rgpoly._coeffs[0][0]
        print('Azimuth line shift: ', azoff)
        print('Range pixel shift: ', rgoff)


    ####Setup input file
    runGeo2rdr(frame,latImage,lonImage,demImage, inps.outdir, 
            dop=dop, nativedop = inps.native, legendre=inps.legendre,
            azoff=azoff,rgoff=rgoff,
            alks=inps.alks, rlks=inps.rlks)
Example #30
0
def download_wbd(s, n, w, e):
    '''
    download water body
    water body. (0) --- land; (-1) --- water; (-2) --- no data.

    set no-value pixel inside of latitude [-56, 60] to -1
    set no-value pixel outside of latitidue [-56, 60] to -2

    look at this figure for SRTM coverage:
    https://www2.jpl.nasa.gov/srtm/images/SRTM_2-24-2016.gif
    '''
    import os
    import numpy as np
    import isceobj
    from iscesys.DataManager import createManager

    latMin = np.floor(s)
    latMax = np.ceil(n)
    lonMin = np.floor(w)
    lonMax = np.ceil(e)

    ############################################################
    #1. download and stitch wbd
    ############################################################
    sw = createManager('wbd')
    sw.configure()

    outputFile = sw.defaultName([latMin, latMax, lonMin, lonMax])
    if os.path.exists(outputFile) and os.path.exists(outputFile + '.xml'):
        print('water body file: {}'.format(outputFile))
        print('exists, do not download and correct')
        return outputFile

    #download and stitch the SWBD tiles
    sw.noFilling = False
    sw._fillingValue = -1
    sw.stitch([latMin, latMax], [lonMin, lonMax])

    ############################################################
    #2. replace 'areas with SRTM but no SWBD' with zeros (land)
    ############################################################
    print('post-process water body file')

    print('get SRTM tiles')
    srtmListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'srtm_tiles.txt')
    with open(srtmListFile) as f:
        srtmList = f.readlines()
    srtmList = [x[0:7] for x in srtmList]

    #get tiles that have SRTM DEM, but no SWBD, these are mostly tiles that do not have water body
    print('get tiles with SRTM and without SWBD')
    noSwbdListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  'srtm_no_swbd_tiles.txt')
    with open(noSwbdListFile) as f:
        noSwbdList = f.readlines()
    noSwbdList = [x[0:7] for x in noSwbdList]

    print('get SWBD tiles')
    swbdListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'swbd_tiles.txt')
    with open(swbdListFile) as f:
        swbdList = f.readlines()
    swbdList = [x[0:7] for x in swbdList]

    #read resulting mosaicked water body
    wbdImage = isceobj.createDemImage()
    wbdImage.load(outputFile + '.xml')
    #using memmap instead, which should be faster, since we only have a few pixels to change
    wbd = np.memmap(outputFile,
                    dtype=np.int8,
                    mode='r+',
                    shape=(wbdImage.length, wbdImage.width))

    #replace 'areas with SRTM but no SWBD' with zeros (land)
    names, nlats, nlons = sw.createNameListFromBounds([latMin, latMax],
                                                      [lonMin, lonMax])
    sign = {'S': -1, 'N': 1, 'W': -1, 'E': 1}
    for tile in names:
        print('checking tile: {}'.format(tile))
        firstLatitude = sign[tile[0].upper()] * int(tile[1:3]) + 1
        firstLongitude = sign[tile[3].upper()] * int(tile[4:7])
        lineOffset = np.int32((firstLatitude - wbdImage.firstLatitude) /
                              wbdImage.deltaLatitude + 0.5)
        sampleOffset = np.int32((firstLongitude - wbdImage.firstLongitude) /
                                wbdImage.deltaLongitude + 0.5)

        #first line/sample of mosaicked SWBD is integer lat/lon, but it does not include last integer lat/lon line/sample
        #so here the size is 3600*3600 instead of 3601*3601

        #assuming areas without swbd are water
        if tile[0:7] not in swbdList:
            wbd[0 + lineOffset:3600 + lineOffset,
                0 + sampleOffset:3600 + sampleOffset] = -1
        #assuming areas with srtm and without swbd are land
        if tile[0:7] in noSwbdList:
            wbd[0 + lineOffset:3600 + lineOffset,
                0 + sampleOffset:3600 + sampleOffset] = 0

    ############################################################
    #3. set values outside of lat[-56, 60] to -2 (no data)
    ############################################################
    print('check water body file')
    print('set areas outside of lat[-56, 60] to -2 (no data)')
    for i in range(wbdImage.length):
        lat = wbdImage.firstLatitude + wbdImage.deltaLatitude * i
        if lat > 60.0 or lat < -56.0:
            wbd[i, :] = -2
    del wbd, wbdImage

    return outputFile