Beispiel #1
0
def runCoregGeom(self):
    '''compute geometric offset
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    masterTrack = self._insar.loadTrack(master=True)
    slaveTrack = self._insar.loadTrack(master=False)

    demFile = os.path.abspath(self._insar.dem)
    wbdFile = os.path.abspath(self._insar.wbd)
    ###############################################################################

    for i, frameNumber in enumerate(self._insar.masterFrames):
        frameDir = 'f{}_{}'.format(i + 1, frameNumber)
        os.chdir(frameDir)
        for j, swathNumber in enumerate(
                range(self._insar.startingSwath, self._insar.endingSwath + 1)):
            swathDir = 's{}'.format(swathNumber)
            os.chdir(swathDir)

            print('processing frame {}, swath {}'.format(
                frameNumber, swathNumber))

            masterSwath = masterTrack.frames[i].swaths[j]
            slaveSwath = slaveTrack.frames[i].swaths[j]

            ##################################################
            # compute geometric offsets
            ##################################################
            #set up track parameters just for computing offsets
            #ALL track parameters are listed here
            #master
            #masterTrack.passDirection =
            #masterTrack.pointingDirection =
            #masterTrack.operationMode =
            #masterTrack.radarWavelength =
            masterTrack.numberOfSamples = masterSwath.numberOfSamples
            masterTrack.numberOfLines = masterSwath.numberOfLines
            masterTrack.startingRange = masterSwath.startingRange
            #masterTrack.rangeSamplingRate =
            masterTrack.rangePixelSize = masterSwath.rangePixelSize
            masterTrack.sensingStart = masterSwath.sensingStart
            #masterTrack.prf =
            #masterTrack.azimuthPixelSize =
            masterTrack.azimuthLineInterval = masterSwath.azimuthLineInterval
            #masterTrack.dopplerVsPixel =
            #masterTrack.frames =
            #masterTrack.orbit =

            #slave
            slaveTrack.numberOfSamples = slaveSwath.numberOfSamples
            slaveTrack.numberOfLines = slaveSwath.numberOfLines
            slaveTrack.startingRange = slaveSwath.startingRange
            slaveTrack.rangePixelSize = slaveSwath.rangePixelSize
            slaveTrack.sensingStart = slaveSwath.sensingStart
            slaveTrack.azimuthLineInterval = slaveSwath.azimuthLineInterval

            if self.useGPU and self._insar.hasGPU():
                topoGPU(masterTrack, 1, 1, demFile, self._insar.latitude,
                        self._insar.longitude, self._insar.height,
                        self._insar.los)
                geo2RdrGPU(slaveTrack, 1, 1, self._insar.latitude,
                           self._insar.longitude, self._insar.height,
                           self._insar.rangeOffset, self._insar.azimuthOffset)
            else:
                topoCPU(masterTrack, 1, 1, demFile, self._insar.latitude,
                        self._insar.longitude, self._insar.height,
                        self._insar.los)
                geo2RdrCPU(slaveTrack, 1, 1, self._insar.latitude,
                           self._insar.longitude, self._insar.height,
                           self._insar.rangeOffset, self._insar.azimuthOffset)

            waterBodyRadar(self._insar.latitude, self._insar.longitude,
                           wbdFile, self._insar.wbdOut)

            #clear up, leaving only range/azimuth offsets
            os.remove(self._insar.latitude)
            os.remove(self._insar.latitude + '.vrt')
            os.remove(self._insar.latitude + '.xml')
            os.remove(self._insar.longitude)
            os.remove(self._insar.longitude + '.vrt')
            os.remove(self._insar.longitude + '.xml')
            os.remove(self._insar.height)
            os.remove(self._insar.height + '.vrt')
            os.remove(self._insar.height + '.xml')
            os.remove(self._insar.los)
            os.remove(self._insar.los + '.vrt')
            os.remove(self._insar.los + '.xml')

            ##################################################
            # resample bursts
            ##################################################
            slaveBurstResampledDir = self._insar.slaveBurstPrefix + '_1_coreg_geom'
            #interferogramDir = self._insar.masterBurstPrefix + '-' + self._insar.slaveBurstPrefix + '_coreg_geom'
            interferogramDir = 'burst_interf_1_coreg_geom'
            interferogramPrefix = self._insar.masterBurstPrefix + '-' + self._insar.slaveBurstPrefix
            resampleBursts(masterSwath,
                           slaveSwath,
                           self._insar.masterBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           slaveBurstResampledDir,
                           interferogramDir,
                           self._insar.masterBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           interferogramPrefix,
                           self._insar.rangeOffset,
                           self._insar.azimuthOffset,
                           rangeOffsetResidual=0,
                           azimuthOffsetResidual=0)

            ##################################################
            # mosaic burst amplitudes and interferograms
            ##################################################
            os.chdir(slaveBurstResampledDir)
            mosaicBurstAmplitude(masterSwath,
                                 self._insar.slaveBurstPrefix,
                                 self._insar.slaveMagnitude,
                                 numberOfLooksThreshold=4)
            os.chdir('../')

            #the interferogram is not good enough, do not mosaic
            mosaic = False
            if mosaic:
                os.chdir(interferogramDir)
                mosaicBurstInterferogram(masterSwath,
                                         interferogramPrefix,
                                         self._insar.interferogram,
                                         numberOfLooksThreshold=4)
                os.chdir('../')

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

###############################################################################
    catalog.printToLog(logger, "runCoregGeom")
    self._insar.procDoc.addAllFromCatalog(catalog)
Beispiel #2
0
def runCoregCc(self):
    '''coregister bursts by cross correlation
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    masterTrack = self._insar.loadTrack(master=True)
    slaveTrack = self._insar.loadTrack(master=False)

    #demFile = os.path.abspath(self._insar.dem)
    #wbdFile = os.path.abspath(self._insar.wbd)
    ###############################################################################
    self._insar.rangeResidualOffsetCc = [
        [] for i in range(len(masterTrack.frames))
    ]
    self._insar.azimuthResidualOffsetCc = [
        [] for i in range(len(masterTrack.frames))
    ]
    for i, frameNumber in enumerate(self._insar.masterFrames):
        frameDir = 'f{}_{}'.format(i + 1, frameNumber)
        os.chdir(frameDir)
        for j, swathNumber in enumerate(
                range(self._insar.startingSwath, self._insar.endingSwath + 1)):
            swathDir = 's{}'.format(swathNumber)
            os.chdir(swathDir)

            print('processing frame {}, swath {}'.format(
                frameNumber, swathNumber))

            masterSwath = masterTrack.frames[i].swaths[j]
            slaveSwath = slaveTrack.frames[i].swaths[j]

            ##################################################
            # estimate cross-correlation offsets
            ##################################################
            #compute number of offsets to use
            wbdImg = isceobj.createImage()
            wbdImg.load(self._insar.wbdOut + '.xml')
            width = wbdImg.width
            length = wbdImg.length

            #initial number of offsets to use
            numberOfOffsets = 800

            #compute land ratio to further determine the number of offsets to use
            if self.useWbdForNumberOffsets:
                wbd = np.memmap(self._insar.wbdOut,
                                dtype='byte',
                                mode='r',
                                shape=(length, width))
                landRatio = np.sum(wbd == 0) / length / width
                del wbd
                if (landRatio <= 0.00125):
                    print(
                        '\n\nWARNING: land area too small for estimating offsets between master and slave magnitudes at frame {}, swath {}'
                        .format(frameNumber, swathNumber))
                    print('set offsets to zero\n\n')
                    self._insar.rangeResidualOffsetCc[i].append(0.0)
                    self._insar.azimuthResidualOffsetCc[i].append(0.0)
                    catalog.addItem(
                        'warning message',
                        'land area too small for estimating offsets between master and slave magnitudes at frame {}, swath {}'
                        .format(frameNumber, swathNumber), 'runCoregCc')
                    continue
                #total number of offsets to use
                numberOfOffsets /= landRatio

            #allocate number of offsets in range/azimuth according to image width/length
            #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

            #user's settings
            if self.numberRangeOffsets != None:
                numberOfOffsetsRange = self.numberRangeOffsets[i][j]
            if self.numberAzimuthOffsets != None:
                numberOfOffsetsAzimuth = self.numberAzimuthOffsets[i][j]

            catalog.addItem(
                'number of range offsets at frame {}, swath {}'.format(
                    frameNumber, swathNumber),
                '{}'.format(numberOfOffsetsRange), 'runCoregCc')
            catalog.addItem(
                'number of azimuth offsets at frame {}, swath {}'.format(
                    frameNumber, swathNumber),
                '{}'.format(numberOfOffsetsAzimuth), 'runCoregCc')

            #need to cp to current directory to make it (gdal) work
            if not os.path.isfile(self._insar.masterMagnitude):
                os.symlink(
                    os.path.join(self._insar.masterBurstPrefix,
                                 self._insar.masterMagnitude),
                    self._insar.masterMagnitude)
            #shutil.copy2() can overwrite
            shutil.copy2(
                os.path.join(self._insar.masterBurstPrefix,
                             self._insar.masterMagnitude + '.vrt'),
                self._insar.masterMagnitude + '.vrt')
            shutil.copy2(
                os.path.join(self._insar.masterBurstPrefix,
                             self._insar.masterMagnitude + '.xml'),
                self._insar.masterMagnitude + '.xml')

            if not os.path.isfile(self._insar.slaveMagnitude):
                os.symlink(
                    os.path.join(
                        self._insar.slaveBurstPrefix + '_1_coreg_geom',
                        self._insar.slaveMagnitude),
                    self._insar.slaveMagnitude)
            #shutil.copy2() can overwrite
            shutil.copy2(
                os.path.join(self._insar.slaveBurstPrefix + '_1_coreg_geom',
                             self._insar.slaveMagnitude + '.vrt'),
                self._insar.slaveMagnitude + '.vrt')
            shutil.copy2(
                os.path.join(self._insar.slaveBurstPrefix + '_1_coreg_geom',
                             self._insar.slaveMagnitude + '.xml'),
                self._insar.slaveMagnitude + '.xml')

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

            mMag = isceobj.createImage()
            mMag.load(self._insar.masterMagnitude + '.xml')
            mMag.setAccessMode('read')
            mMag.createImage()

            sMag = isceobj.createImage()
            sMag.load(self._insar.slaveMagnitude + '.xml')
            sMag.setAccessMode('read')
            sMag.createImage()

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

            ampcor.setMasterSlcImage(mMag)
            ampcor.setSlaveSlcImage(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(mMag.width)
            ampcor.setNumberLocationAcross(numberOfOffsetsRange)
            ampcor.setFirstSampleDown(firstLine)
            ampcor.setLastSampleDown(mMag.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(8)
            ampcor.setSearchWindowSizeHeight(8)

            #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()
            refinedOffsets = cullOffsetsRoipac(offsets, numThreshold=50)

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

            #clear up
            os.remove(self._insar.masterMagnitude)
            os.remove(self._insar.masterMagnitude + '.vrt')
            os.remove(self._insar.masterMagnitude + '.xml')
            os.remove(self._insar.slaveMagnitude)
            os.remove(self._insar.slaveMagnitude + '.vrt')
            os.remove(self._insar.slaveMagnitude + '.xml')

            #compute average offsets to use in resampling
            if refinedOffsets == None:
                rangeOffset = 0
                azimuthOffset = 0
                self._insar.rangeResidualOffsetCc[i].append(rangeOffset)
                self._insar.azimuthResidualOffsetCc[i].append(azimuthOffset)
                print(
                    '\n\nWARNING: too few offsets left in matching master and slave magnitudes at frame {}, swath {}'
                    .format(frameNumber, swathNumber))
                print('set offsets to zero\n\n')
                catalog.addItem(
                    'warning message',
                    'too few offsets left in matching master and slave magnitudes at frame {}, swath {}'
                    .format(frameNumber, swathNumber), 'runCoregCc')
            else:
                rangeOffset, azimuthOffset = meanOffset(refinedOffsets)
                #for range offset, need to compute from a polynomial
                #see components/isceobj/Location/Offset.py and components/isceobj/Util/Library/python/Poly2D.py for definations
                (azimuthPoly,
                 rangePoly) = refinedOffsets.getFitPolynomials(rangeOrder=2,
                                                               azimuthOrder=2)
                #make a deep copy, otherwise it also changes original coefficient list of rangePoly, which affects following rangePoly(*, *) computation
                polyCoeff = copy.deepcopy(rangePoly.getCoeffs())
                rgIndex = (np.arange(width) - rangePoly.getMeanRange()
                           ) / rangePoly.getNormRange()
                azIndex = (np.arange(length) - rangePoly.getMeanAzimuth()
                           ) / rangePoly.getNormAzimuth()
                rangeOffset =  polyCoeff[0][0] + polyCoeff[0][1]*rgIndex[None,:] + polyCoeff[0][2]*rgIndex[None,:]**2 + \
                              (polyCoeff[1][0] + polyCoeff[1][1]*rgIndex[None,:]) * azIndex[:, None] + \
                               polyCoeff[2][0] * azIndex[:, None]**2
                polyCoeff.append([
                    rangePoly.getMeanRange(),
                    rangePoly.getNormRange(),
                    rangePoly.getMeanAzimuth(),
                    rangePoly.getNormAzimuth()
                ])
                self._insar.rangeResidualOffsetCc[i].append(polyCoeff)
                self._insar.azimuthResidualOffsetCc[i].append(azimuthOffset)

                catalog.addItem(
                    'range residual offset at {} {} at frame {}, swath {}'.
                    format(0, 0, frameNumber, swathNumber),
                    '{}'.format(rangePoly(0, 0)), 'runCoregCc')
                catalog.addItem(
                    'range residual offset at {} {} at frame {}, swath {}'.
                    format(0, width - 1, frameNumber, swathNumber),
                    '{}'.format(rangePoly(0, width - 1)), 'runCoregCc')
                catalog.addItem(
                    'range residual offset at {} {} at frame {}, swath {}'.
                    format(length - 1, 0, frameNumber, swathNumber),
                    '{}'.format(rangePoly(length - 1, 0)), 'runCoregCc')
                catalog.addItem(
                    'range residual offset at {} {} at frame {}, swath {}'.
                    format(length - 1, width - 1, frameNumber, swathNumber),
                    '{}'.format(rangePoly(length - 1,
                                          width - 1)), 'runCoregCc')
                catalog.addItem(
                    'azimuth residual offset at frame {}, swath {}'.format(
                        frameNumber, swathNumber), '{}'.format(azimuthOffset),
                    'runCoregCc')

                DEBUG = False
                if DEBUG:
                    print('+++++++++++++++++++++++++++++')
                    print(rangeOffset[0, 0], rangePoly(0, 0))
                    print(rangeOffset[0, width - 1], rangePoly(0, width - 1))
                    print(rangeOffset[length - 1, 0], rangePoly(length - 1, 0))
                    print(rangeOffset[length - 1, width - 1],
                          rangePoly(length - 1, width - 1))
                    print(
                        rangeOffset[int((length - 1) / 2),
                                    int((width - 1) / 2)],
                        rangePoly(int((length - 1) / 2), int((width - 1) / 2)))
                    print('+++++++++++++++++++++++++++++')

            ##################################################
            # resample bursts
            ##################################################
            slaveBurstResampledDir = self._insar.slaveBurstPrefix + '_2_coreg_cc'
            #interferogramDir = self._insar.masterBurstPrefix + '-' + self._insar.slaveBurstPrefix + '_coreg_geom'
            interferogramDir = 'burst_interf_2_coreg_cc'
            interferogramPrefix = self._insar.masterBurstPrefix + '-' + self._insar.slaveBurstPrefix
            resampleBursts(masterSwath,
                           slaveSwath,
                           self._insar.masterBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           slaveBurstResampledDir,
                           interferogramDir,
                           self._insar.masterBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           self._insar.slaveBurstPrefix,
                           interferogramPrefix,
                           self._insar.rangeOffset,
                           self._insar.azimuthOffset,
                           rangeOffsetResidual=rangeOffset,
                           azimuthOffsetResidual=azimuthOffset)

            ##################################################
            # mosaic burst amplitudes and interferograms
            ##################################################
            os.chdir(slaveBurstResampledDir)
            mosaicBurstAmplitude(masterSwath,
                                 self._insar.slaveBurstPrefix,
                                 self._insar.slaveMagnitude,
                                 numberOfLooksThreshold=4)
            os.chdir('../')

            os.chdir(interferogramDir)
            mosaicBurstInterferogram(masterSwath,
                                     interferogramPrefix,
                                     self._insar.interferogram,
                                     numberOfLooksThreshold=4)
            os.chdir('../')

            ##################################################
            # final amplitude and interferogram
            ##################################################
            amp = np.zeros(
                (masterSwath.numberOfLines, 2 * masterSwath.numberOfSamples),
                dtype=np.float32)
            amp[0:, 1:masterSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(slaveBurstResampledDir, self._insar.slaveMagnitude), \
                dtype=np.float32).reshape(masterSwath.numberOfLines, masterSwath.numberOfSamples)
            amp[0:, 0:masterSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(self._insar.masterBurstPrefix, self._insar.masterMagnitude), \
                dtype=np.float32).reshape(masterSwath.numberOfLines, masterSwath.numberOfSamples)
            amp.astype(np.float32).tofile(self._insar.amplitude)
            create_xml(self._insar.amplitude, masterSwath.numberOfSamples,
                       masterSwath.numberOfLines, 'amp')

            os.rename(
                os.path.join(interferogramDir, self._insar.interferogram),
                self._insar.interferogram)
            os.rename(
                os.path.join(interferogramDir,
                             self._insar.interferogram + '.vrt'),
                self._insar.interferogram + '.vrt')
            os.rename(
                os.path.join(interferogramDir,
                             self._insar.interferogram + '.xml'),
                self._insar.interferogram + '.xml')

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

###############################################################################
    catalog.printToLog(logger, "runCoregCc")
    self._insar.procDoc.addAllFromCatalog(catalog)
Beispiel #3
0
def runCoregSd(self):
    '''coregister bursts by spectral diversity
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    referenceTrack = self._insar.loadTrack(reference=True)
    secondaryTrack = self._insar.loadTrack(reference=False)

    #demFile = os.path.abspath(self._insar.dem)
    #wbdFile = os.path.abspath(self._insar.wbd)
    ###############################################################################
    #self._insar.rangeResidualOffsetSd = [[] for i in range(len(referenceTrack.frames))]
    self._insar.azimuthResidualOffsetSd = [
        [] for i in range(len(referenceTrack.frames))
    ]
    for i, frameNumber in enumerate(self._insar.referenceFrames):
        frameDir = 'f{}_{}'.format(i + 1, frameNumber)
        os.chdir(frameDir)
        for j, swathNumber in enumerate(
                range(self._insar.startingSwath, self._insar.endingSwath + 1)):
            swathDir = 's{}'.format(swathNumber)
            os.chdir(swathDir)

            print('processing frame {}, swath {}'.format(
                frameNumber, swathNumber))

            referenceSwath = referenceTrack.frames[i].swaths[j]
            secondarySwath = secondaryTrack.frames[i].swaths[j]

            ##################################################
            # spectral diversity or mai
            ##################################################
            sdDir = 'spectral_diversity'
            os.makedirs(sdDir, exist_ok=True)
            os.chdir(sdDir)

            interferogramDir = 'burst_interf_2_coreg_cc'
            interferogramPrefix = self._insar.referenceBurstPrefix + '-' + self._insar.secondaryBurstPrefix
            offsetSd = spectralDiversity(referenceSwath,
                                         os.path.join('../', interferogramDir),
                                         interferogramPrefix,
                                         self._insar.interferogramSd,
                                         numberLooksScanSAR=4,
                                         numberRangeLooks=28,
                                         numberAzimuthLooks=8,
                                         coherenceThreshold=0.85,
                                         keep=True,
                                         filt=True,
                                         filtWinSizeRange=5,
                                         filtWinSizeAzimuth=5)
            #here use the number of looks for sd as filtWinSizeRange and filtWinSizeAzimuth to get the best filtering result?

            os.chdir('../')

            self._insar.azimuthResidualOffsetSd[i].append(offsetSd)
            catalog.addItem(
                'azimuth residual offset at frame {}, swath {}'.format(
                    frameNumber, swathNumber), '{}'.format(offsetSd),
                'runCoregSd')

            #this small residual azimuth offset has small impact, it's not worth the time to resample secondary bursts again.
            formInterferogram = False
            if formInterferogram:
                ##################################################
                # resample bursts
                ##################################################
                secondaryBurstResampledDir = self._insar.secondaryBurstPrefix + '_3_coreg_sd'
                #interferogramDir = self._insar.referenceBurstPrefix + '-' + self._insar.secondaryBurstPrefix + '_coreg_geom'
                interferogramDir = 'burst_interf_3_coreg_sd'
                interferogramPrefix = self._insar.referenceBurstPrefix + '-' + self._insar.secondaryBurstPrefix
                resampleBursts(
                    referenceSwath,
                    secondarySwath,
                    self._insar.referenceBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    secondaryBurstResampledDir,
                    interferogramDir,
                    self._insar.referenceBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    interferogramPrefix,
                    self._insar.rangeOffset,
                    self._insar.azimuthOffset,
                    rangeOffsetResidual=self._insar.rangeResidualOffsetCc[i]
                    [j],
                    azimuthOffsetResidual=self._insar.
                    azimuthResidualOffsetCc[i][j] + offsetSd)

                ##################################################
                # mosaic burst amplitudes and interferograms
                ##################################################
                os.chdir(secondaryBurstResampledDir)
                mosaicBurstAmplitude(referenceSwath,
                                     self._insar.secondaryBurstPrefix,
                                     self._insar.secondaryMagnitude,
                                     numberOfLooksThreshold=4)
                os.chdir('../')

                os.chdir(interferogramDir)
                mosaicBurstInterferogram(referenceSwath,
                                         interferogramPrefix,
                                         self._insar.interferogram,
                                         numberOfLooksThreshold=4)
                os.chdir('../')

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


###############################################################################
    catalog.printToLog(logger, "runCoregSd")
    self._insar.procDoc.addAllFromCatalog(catalog)
Beispiel #4
0
def runIonSubband(self):
    '''create subband interferograms
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    if not self.doIon:
        catalog.printToLog(logger, "runIonSubband")
        self._insar.procDoc.addAllFromCatalog(catalog)
        return

    referenceTrack = self._insar.loadTrack(reference=True)
    secondaryTrack = self._insar.loadTrack(reference=False)

    #using 1/3, 1/3, 1/3 band split
    radarWavelength = referenceTrack.radarWavelength
    rangeBandwidth = referenceTrack.frames[0].swaths[0].rangeBandwidth
    rangeSamplingRate = referenceTrack.frames[0].swaths[0].rangeSamplingRate
    radarWavelengthLower = SPEED_OF_LIGHT / (SPEED_OF_LIGHT / radarWavelength -
                                             rangeBandwidth / 3.0)
    radarWavelengthUpper = SPEED_OF_LIGHT / (SPEED_OF_LIGHT / radarWavelength +
                                             rangeBandwidth / 3.0)
    subbandRadarWavelength = [radarWavelengthLower, radarWavelengthUpper]
    subbandBandWidth = [
        rangeBandwidth / 3.0 / rangeSamplingRate,
        rangeBandwidth / 3.0 / rangeSamplingRate
    ]
    subbandFrequencyCenter = [
        -rangeBandwidth / 3.0 / rangeSamplingRate,
        rangeBandwidth / 3.0 / rangeSamplingRate
    ]

    subbandPrefix = ['lower', 'upper']
    '''
    ionDir = {
        ionDir['swathMosaic'] : 'mosaic',
        ionDir['insar'] : 'insar',
        ionDir['ion'] : 'ion',
        ionDir['subband'] : ['lower', 'upper'],
        ionDir['ionCal'] : 'ion_cal'
        }
    '''
    #define upper level directory names
    ionDir = defineIonDir()

    self._insar.subbandRadarWavelength = subbandRadarWavelength

    ############################################################
    # STEP 1. create directories
    ############################################################
    #create and enter 'ion' directory
    #after finishing each step, we are in this directory
    os.makedirs(ionDir['ion'], exist_ok=True)
    os.chdir(ionDir['ion'])

    #create insar processing directories
    for k in range(2):
        subbandDir = ionDir['subband'][k]
        for i, frameNumber in enumerate(self._insar.referenceFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            for j, swathNumber in enumerate(
                    range(self._insar.startingSwath,
                          self._insar.endingSwath + 1)):
                swathDir = 's{}'.format(swathNumber)
                fullDir = os.path.join(subbandDir, frameDir, swathDir)
                os.makedirs(fullDir, exist_ok=True)

    #create ionospheric phase directory
    os.makedirs(ionDir['ionCal'])

    ############################################################
    # STEP 2. create subband interferograms
    ############################################################
    import shutil
    import numpy as np
    from contrib.alos2proc.alos2proc import rg_filter
    from isceobj.Alos2Proc.Alos2ProcPublic import resampleBursts
    from isceobj.Alos2Proc.Alos2ProcPublic import mosaicBurstAmplitude
    from isceobj.Alos2Proc.Alos2ProcPublic import mosaicBurstInterferogram
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for i, frameNumber in enumerate(self._insar.referenceFrames):
        frameDir = 'f{}_{}'.format(i + 1, frameNumber)
        for j, swathNumber in enumerate(
                range(self._insar.startingSwath, self._insar.endingSwath + 1)):
            swathDir = 's{}'.format(swathNumber)
            #filter reference and secondary images
            for burstPrefix, swath in zip([
                    self._insar.referenceBurstPrefix,
                    self._insar.secondaryBurstPrefix
            ], [
                    referenceTrack.frames[i].swaths[j],
                    secondaryTrack.frames[i].swaths[j]
            ]):
                slcDir = os.path.join('../', frameDir, swathDir, burstPrefix)
                slcLowerDir = os.path.join(ionDir['subband'][0], frameDir,
                                           swathDir, burstPrefix)
                slcUpperDir = os.path.join(ionDir['subband'][1], frameDir,
                                           swathDir, burstPrefix)
                os.makedirs(slcLowerDir, exist_ok=True)
                os.makedirs(slcUpperDir, exist_ok=True)
                for k in range(swath.numberOfBursts):
                    print('processing burst: %02d' % (k + 1))
                    slc = os.path.join(slcDir,
                                       burstPrefix + '_%02d.slc' % (k + 1))
                    slcLower = os.path.join(
                        slcLowerDir, burstPrefix + '_%02d.slc' % (k + 1))
                    slcUpper = os.path.join(
                        slcUpperDir, burstPrefix + '_%02d.slc' % (k + 1))
                    rg_filter(slc, 2, [slcLower, slcUpper], subbandBandWidth,
                              subbandFrequencyCenter, 257, 2048, 0.1, 0, 0.0)
            #resample
            for l in range(2):
                os.chdir(os.path.join(ionDir['subband'][l], frameDir,
                                      swathDir))
                #recreate xml file to remove the file path
                #can also use fixImageXml.py?
                for burstPrefix, swath in zip([
                        self._insar.referenceBurstPrefix,
                        self._insar.secondaryBurstPrefix
                ], [
                        referenceTrack.frames[i].swaths[j],
                        secondaryTrack.frames[i].swaths[j]
                ]):
                    os.chdir(burstPrefix)
                    for k in range(swath.numberOfBursts):
                        slc = burstPrefix + '_%02d.slc' % (k + 1)
                        img = isceobj.createSlcImage()
                        img.load(slc + '.xml')
                        img.setFilename(slc)
                        img.extraFilename = slc + '.vrt'
                        img.setAccessMode('READ')
                        img.renderHdr()
                    os.chdir('../')

                #############################################
                #1. form interferogram
                #############################################
                referenceSwath = referenceTrack.frames[i].swaths[j]
                secondarySwath = secondaryTrack.frames[i].swaths[j]

                #set up resampling parameters
                width = referenceSwath.numberOfSamples
                length = referenceSwath.numberOfLines
                polyCoeff = self._insar.rangeResidualOffsetCc[i][j]
                rgIndex = (np.arange(width) -
                           polyCoeff[-1][0]) / polyCoeff[-1][1]
                azIndex = (np.arange(length) -
                           polyCoeff[-1][2]) / polyCoeff[-1][3]
                rangeOffset =  polyCoeff[0][0] + polyCoeff[0][1]*rgIndex[None,:] + polyCoeff[0][2]*rgIndex[None,:]**2 + \
                              (polyCoeff[1][0] + polyCoeff[1][1]*rgIndex[None,:]) * azIndex[:, None] + \
                               polyCoeff[2][0] * azIndex[:, None]**2
                azimuthOffset = self._insar.azimuthResidualOffsetCc[i][j]

                secondaryBurstResampledDir = self._insar.secondaryBurstPrefix + '_2_coreg_cc'
                interferogramDir = 'burst_interf_2_coreg_cc'
                interferogramPrefix = self._insar.referenceBurstPrefix + '-' + self._insar.secondaryBurstPrefix
                resampleBursts(
                    referenceSwath,
                    secondarySwath,
                    self._insar.referenceBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    secondaryBurstResampledDir,
                    interferogramDir,
                    self._insar.referenceBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    self._insar.secondaryBurstPrefix,
                    interferogramPrefix,
                    os.path.join(
                        '../../../../{}/{}'.format(frameDir, swathDir),
                        self._insar.rangeOffset),
                    os.path.join(
                        '../../../../{}/{}'.format(frameDir, swathDir),
                        self._insar.azimuthOffset),
                    rangeOffsetResidual=rangeOffset,
                    azimuthOffsetResidual=azimuthOffset)

                os.chdir(self._insar.referenceBurstPrefix)
                mosaicBurstAmplitude(referenceSwath,
                                     self._insar.referenceBurstPrefix,
                                     self._insar.referenceMagnitude,
                                     numberOfLooksThreshold=4)
                os.chdir('../')

                os.chdir(secondaryBurstResampledDir)
                mosaicBurstAmplitude(referenceSwath,
                                     self._insar.secondaryBurstPrefix,
                                     self._insar.secondaryMagnitude,
                                     numberOfLooksThreshold=4)
                os.chdir('../')

                os.chdir(interferogramDir)
                mosaicBurstInterferogram(referenceSwath,
                                         interferogramPrefix,
                                         self._insar.interferogram,
                                         numberOfLooksThreshold=4)
                os.chdir('../')

                amp = np.zeros((referenceSwath.numberOfLines,
                                2 * referenceSwath.numberOfSamples),
                               dtype=np.float32)
                amp[0:, 1:referenceSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(secondaryBurstResampledDir, self._insar.secondaryMagnitude), \
                    dtype=np.float32).reshape(referenceSwath.numberOfLines, referenceSwath.numberOfSamples)
                amp[0:, 0:referenceSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(self._insar.referenceBurstPrefix, self._insar.referenceMagnitude), \
                    dtype=np.float32).reshape(referenceSwath.numberOfLines, referenceSwath.numberOfSamples)
                amp.astype(np.float32).tofile(self._insar.amplitude)
                create_xml(self._insar.amplitude,
                           referenceSwath.numberOfSamples,
                           referenceSwath.numberOfLines, 'amp')

                os.rename(
                    os.path.join(interferogramDir, self._insar.interferogram),
                    self._insar.interferogram)
                os.rename(
                    os.path.join(interferogramDir,
                                 self._insar.interferogram + '.vrt'),
                    self._insar.interferogram + '.vrt')
                os.rename(
                    os.path.join(interferogramDir,
                                 self._insar.interferogram + '.xml'),
                    self._insar.interferogram + '.xml')

                #############################################
                #2. delete subband slcs
                #############################################
                shutil.rmtree(self._insar.referenceBurstPrefix)
                shutil.rmtree(self._insar.secondaryBurstPrefix)
                shutil.rmtree(secondaryBurstResampledDir)
                shutil.rmtree(interferogramDir)

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

    ############################################################
    # STEP 3. mosaic swaths
    ############################################################
    from isceobj.Alos2Proc.runSwathMosaic import swathMosaic
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for k in range(2):
        os.chdir(ionDir['subband'][k])
        for i, frameNumber in enumerate(self._insar.referenceFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            os.chdir(frameDir)

            mosaicDir = 'mosaic'
            os.makedirs(mosaicDir, exist_ok=True)
            os.chdir(mosaicDir)

            if self._insar.endingSwath - self._insar.startingSwath + 1 == 1:
                import shutil
                swathDir = 's{}'.format(
                    referenceTrack.frames[i].swaths[0].swathNumber)

                # if not os.path.isfile(self._insar.interferogram):
                #     os.symlink(os.path.join('../', swathDir, self._insar.interferogram), self._insar.interferogram)
                # shutil.copy2(os.path.join('../', swathDir, self._insar.interferogram+'.vrt'), self._insar.interferogram+'.vrt')
                # shutil.copy2(os.path.join('../', swathDir, self._insar.interferogram+'.xml'), self._insar.interferogram+'.xml')
                # if not os.path.isfile(self._insar.amplitude):
                #     os.symlink(os.path.join('../', swathDir, self._insar.amplitude), self._insar.amplitude)
                # shutil.copy2(os.path.join('../', swathDir, self._insar.amplitude+'.vrt'), self._insar.amplitude+'.vrt')
                # shutil.copy2(os.path.join('../', swathDir, self._insar.amplitude+'.xml'), self._insar.amplitude+'.xml')

                os.rename(
                    os.path.join('../', swathDir, self._insar.interferogram),
                    self._insar.interferogram)
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.interferogram + '.vrt'),
                    self._insar.interferogram + '.vrt')
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.interferogram + '.xml'),
                    self._insar.interferogram + '.xml')
                os.rename(os.path.join('../', swathDir, self._insar.amplitude),
                          self._insar.amplitude)
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.amplitude + '.vrt'),
                    self._insar.amplitude + '.vrt')
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.amplitude + '.xml'),
                    self._insar.amplitude + '.xml')

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

            #choose offsets
            numberOfFrames = len(referenceTrack.frames)
            numberOfSwaths = len(referenceTrack.frames[i].swaths)
            if self.swathOffsetMatching:
                #no need to do this as the API support 2-d list
                #rangeOffsets = (np.array(self._insar.swathRangeOffsetMatchingReference)).reshape(numberOfFrames, numberOfSwaths)
                #azimuthOffsets = (np.array(self._insar.swathAzimuthOffsetMatchingReference)).reshape(numberOfFrames, numberOfSwaths)
                rangeOffsets = self._insar.swathRangeOffsetMatchingReference
                azimuthOffsets = self._insar.swathAzimuthOffsetMatchingReference

            else:
                #rangeOffsets = (np.array(self._insar.swathRangeOffsetGeometricalReference)).reshape(numberOfFrames, numberOfSwaths)
                #azimuthOffsets = (np.array(self._insar.swathAzimuthOffsetGeometricalReference)).reshape(numberOfFrames, numberOfSwaths)
                rangeOffsets = self._insar.swathRangeOffsetGeometricalReference
                azimuthOffsets = self._insar.swathAzimuthOffsetGeometricalReference

            rangeOffsets = rangeOffsets[i]
            azimuthOffsets = azimuthOffsets[i]

            #list of input files
            inputInterferograms = []
            inputAmplitudes = []
            phaseDiff = [None]
            for j, swathNumber in enumerate(
                    range(self._insar.startingSwath,
                          self._insar.endingSwath + 1)):
                swathDir = 's{}'.format(swathNumber)
                inputInterferograms.append(
                    os.path.join('../', swathDir, self._insar.interferogram))
                inputAmplitudes.append(
                    os.path.join('../', swathDir, self._insar.amplitude))

                #compute phase needed to be compensated using startingRange
                if j >= 1:
                    #phaseDiffSwath1 = -4.0 * np.pi * (referenceTrack.frames[i].swaths[j-1].startingRange - secondaryTrack.frames[i].swaths[j-1].startingRange)/subbandRadarWavelength[k]
                    #phaseDiffSwath2 = -4.0 * np.pi * (referenceTrack.frames[i].swaths[j].startingRange - secondaryTrack.frames[i].swaths[j].startingRange)/subbandRadarWavelength[k]
                    phaseDiffSwath1 = +4.0 * np.pi * referenceTrack.frames[i].swaths[j-1].startingRange * (1.0/radarWavelength - 1.0/subbandRadarWavelength[k]) \
                                      -4.0 * np.pi * secondaryTrack.frames[i].swaths[j-1].startingRange * (1.0/radarWavelength - 1.0/subbandRadarWavelength[k])
                    phaseDiffSwath2 = +4.0 * np.pi * referenceTrack.frames[i].swaths[j].startingRange * (1.0/radarWavelength - 1.0/subbandRadarWavelength[k]) \
                                      -4.0 * np.pi * secondaryTrack.frames[i].swaths[j].startingRange * (1.0/radarWavelength - 1.0/subbandRadarWavelength[k])
                    if referenceTrack.frames[i].swaths[j-1].startingRange - secondaryTrack.frames[i].swaths[j-1].startingRange == \
                       referenceTrack.frames[i].swaths[j].startingRange - secondaryTrack.frames[i].swaths[j].startingRange:
                        #phaseDiff.append(phaseDiffSwath2 - phaseDiffSwath1)
                        #if reference and secondary versions are all before or after version 2.025 (starting range error < 0.5 m),
                        #it should be OK to do the above.
                        #see results in neom where it meets the above requirement, but there is still phase diff
                        #to be less risky, we do not input values here
                        phaseDiff.append(None)
                    else:
                        phaseDiff.append(None)

            #note that frame parameters are updated after mosaicking
            #mosaic amplitudes
            swathMosaic(referenceTrack.frames[i],
                        inputAmplitudes,
                        self._insar.amplitude,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        resamplingMethod=0)
            #mosaic interferograms
            #These are for ALOS-2, may need to change for ALOS-4!
            phaseDiffFixed = [
                0.0, 0.4754024578084084, 0.9509913179406437,
                1.4261648478671614, 2.179664007520499, 2.6766909968024932,
                3.130810857
            ]

            snapThreshold = 0.2

            #the above preparetions only applies to 'self._insar.modeCombination == 21'
            #looks like it also works for 31 (scansarNominalModes-stripmapModes)
            if self._insar.modeCombination != 21:
                phaseDiff = None
                phaseDiffFixed = None
                snapThreshold = None

            (phaseDiffEst, phaseDiffUsed,
             phaseDiffSource) = swathMosaic(referenceTrack.frames[i],
                                            inputInterferograms,
                                            self._insar.interferogram,
                                            rangeOffsets,
                                            azimuthOffsets,
                                            self._insar.numberRangeLooks1,
                                            self._insar.numberAzimuthLooks1,
                                            updateFrame=False,
                                            phaseCompensation=True,
                                            phaseDiff=phaseDiff,
                                            phaseDiffFixed=phaseDiffFixed,
                                            snapThreshold=snapThreshold,
                                            pcRangeLooks=1,
                                            pcAzimuthLooks=3,
                                            filt=False,
                                            resamplingMethod=1)

            #the first item is meaningless for all the following list, so only record the following items
            if phaseDiff == None:
                phaseDiff = [
                    None for iii in range(self._insar.startingSwath,
                                          self._insar.endingSwath + 1)
                ]
            catalog.addItem(
                '{} subswath phase difference input'.format(
                    ionDir['subband'][k]), phaseDiff[1:], 'runIonSubband')
            catalog.addItem(
                '{} subswath phase difference estimated'.format(
                    ionDir['subband'][k]), phaseDiffEst[1:], 'runIonSubband')
            catalog.addItem(
                '{} subswath phase difference used'.format(
                    ionDir['subband'][k]), phaseDiffUsed[1:], 'runIonSubband')
            catalog.addItem(
                '{} subswath phase difference used source'.format(
                    ionDir['subband'][k]), phaseDiffSource[1:],
                'runIonSubband')
            #check if there is value around 3.130810857, which may not be stable
            phaseDiffUnstableExist = False
            for xxx in phaseDiffUsed:
                if abs(abs(xxx) - 3.130810857) < 0.2:
                    phaseDiffUnstableExist = True
            catalog.addItem(
                '{} subswath phase difference unstable exists'.format(
                    ionDir['subband'][k]), phaseDiffUnstableExist,
                'runIonSubband')

            create_xml(self._insar.amplitude,
                       referenceTrack.frames[i].numberOfSamples,
                       referenceTrack.frames[i].numberOfLines, 'amp')
            create_xml(self._insar.interferogram,
                       referenceTrack.frames[i].numberOfSamples,
                       referenceTrack.frames[i].numberOfLines, 'int')

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

    ############################################################
    # STEP 4. mosaic frames
    ############################################################
    from isceobj.Alos2Proc.runFrameMosaic import frameMosaic
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for k in range(2):
        os.chdir(ionDir['subband'][k])

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

        numberOfFrames = len(referenceTrack.frames)
        if numberOfFrames == 1:
            import shutil
            frameDir = os.path.join('f1_{}/mosaic'.format(
                self._insar.referenceFrames[0]))
            # if not os.path.isfile(self._insar.interferogram):
            #     os.symlink(os.path.join('../', frameDir, self._insar.interferogram), self._insar.interferogram)
            # #shutil.copy2() can overwrite
            # shutil.copy2(os.path.join('../', frameDir, self._insar.interferogram+'.vrt'), self._insar.interferogram+'.vrt')
            # shutil.copy2(os.path.join('../', frameDir, self._insar.interferogram+'.xml'), self._insar.interferogram+'.xml')
            # if not os.path.isfile(self._insar.amplitude):
            #     os.symlink(os.path.join('../', frameDir, self._insar.amplitude), self._insar.amplitude)
            # shutil.copy2(os.path.join('../', frameDir, self._insar.amplitude+'.vrt'), self._insar.amplitude+'.vrt')
            # shutil.copy2(os.path.join('../', frameDir, self._insar.amplitude+'.xml'), self._insar.amplitude+'.xml')

            os.rename(os.path.join('../', frameDir, self._insar.interferogram),
                      self._insar.interferogram)
            os.rename(
                os.path.join('../', frameDir,
                             self._insar.interferogram + '.vrt'),
                self._insar.interferogram + '.vrt')
            os.rename(
                os.path.join('../', frameDir,
                             self._insar.interferogram + '.xml'),
                self._insar.interferogram + '.xml')
            os.rename(os.path.join('../', frameDir, self._insar.amplitude),
                      self._insar.amplitude)
            os.rename(
                os.path.join('../', frameDir, self._insar.amplitude + '.vrt'),
                self._insar.amplitude + '.vrt')
            os.rename(
                os.path.join('../', frameDir, self._insar.amplitude + '.xml'),
                self._insar.amplitude + '.xml')

        else:
            #choose offsets
            if self.frameOffsetMatching:
                rangeOffsets = self._insar.frameRangeOffsetMatchingReference
                azimuthOffsets = self._insar.frameAzimuthOffsetMatchingReference
            else:
                rangeOffsets = self._insar.frameRangeOffsetGeometricalReference
                azimuthOffsets = self._insar.frameAzimuthOffsetGeometricalReference

            #list of input files
            inputInterferograms = []
            inputAmplitudes = []
            for i, frameNumber in enumerate(self._insar.referenceFrames):
                frameDir = 'f{}_{}'.format(i + 1, frameNumber)
                inputInterferograms.append(
                    os.path.join('../', frameDir, 'mosaic',
                                 self._insar.interferogram))
                inputAmplitudes.append(
                    os.path.join('../', frameDir, 'mosaic',
                                 self._insar.amplitude))

            #note that track parameters are updated after mosaicking
            #mosaic amplitudes
            frameMosaic(referenceTrack,
                        inputAmplitudes,
                        self._insar.amplitude,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        updateTrack=False,
                        phaseCompensation=False,
                        resamplingMethod=0)
            #mosaic interferograms
            frameMosaic(referenceTrack,
                        inputInterferograms,
                        self._insar.interferogram,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        updateTrack=False,
                        phaseCompensation=True,
                        resamplingMethod=1)

            create_xml(self._insar.amplitude, referenceTrack.numberOfSamples,
                       referenceTrack.numberOfLines, 'amp')
            create_xml(self._insar.interferogram,
                       referenceTrack.numberOfSamples,
                       referenceTrack.numberOfLines, 'int')

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

    ############################################################
    # STEP 5. clear frame processing files
    ############################################################
    import shutil
    from isceobj.Alos2Proc.Alos2ProcPublic import runCmd

    for k in range(2):
        os.chdir(ionDir['subband'][k])
        for i, frameNumber in enumerate(self._insar.referenceFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            shutil.rmtree(frameDir)
            #cmd = 'rm -rf {}'.format(frameDir)
            #runCmd(cmd)
        os.chdir('../')

    ############################################################
    # STEP 6. create differential interferograms
    ############################################################
    import numpy as np
    from isceobj.Alos2Proc.Alos2ProcPublic import runCmd

    for k in range(2):
        os.chdir(ionDir['subband'][k])

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

        rangePixelSize = self._insar.numberRangeLooks1 * referenceTrack.rangePixelSize
        radarWavelength = subbandRadarWavelength[k]
        rectRangeOffset = os.path.join('../../../', insarDir,
                                       self._insar.rectRangeOffset)

        cmd = "imageMath.py -e='a*exp(-1.0*J*b*4.0*{}*{}/{}) * (b!=0)' --a={} --b={} -o {} -t cfloat".format(
            np.pi, rangePixelSize, radarWavelength, self._insar.interferogram,
            rectRangeOffset, self._insar.differentialInterferogram)
        runCmd(cmd)

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

    os.chdir('../')
    catalog.printToLog(logger, "runIonSubband")
    self._insar.procDoc.addAllFromCatalog(catalog)
Beispiel #5
0
def runIonSubband(self):
    '''create subband interferograms
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    if not self.doIon:
        catalog.printToLog(logger, "runIonSubband")
        self._insar.procDoc.addAllFromCatalog(catalog)
        return

    masterTrack = self._insar.loadTrack(master=True)
    slaveTrack = self._insar.loadTrack(master=False)

    #using 1/3, 1/3, 1/3 band split
    radarWavelength = masterTrack.radarWavelength
    rangeBandwidth = masterTrack.frames[0].swaths[0].rangeBandwidth
    rangeSamplingRate = masterTrack.frames[0].swaths[0].rangeSamplingRate
    radarWavelengthLower = SPEED_OF_LIGHT / (SPEED_OF_LIGHT / radarWavelength -
                                             rangeBandwidth / 3.0)
    radarWavelengthUpper = SPEED_OF_LIGHT / (SPEED_OF_LIGHT / radarWavelength +
                                             rangeBandwidth / 3.0)
    subbandRadarWavelength = [radarWavelengthLower, radarWavelengthUpper]
    subbandBandWidth = [
        rangeBandwidth / 3.0 / rangeSamplingRate,
        rangeBandwidth / 3.0 / rangeSamplingRate
    ]
    subbandFrequencyCenter = [
        -rangeBandwidth / 3.0 / rangeSamplingRate,
        rangeBandwidth / 3.0 / rangeSamplingRate
    ]

    subbandPrefix = ['lower', 'upper']
    '''
    ionDir = {
        ionDir['swathMosaic'] : 'mosaic',
        ionDir['insar'] : 'insar',
        ionDir['ion'] : 'ion',
        ionDir['subband'] : ['lower', 'upper'],
        ionDir['ionCal'] : 'ion_cal'
        }
    '''
    #define upper level directory names
    ionDir = defineIonDir()

    self._insar.subbandRadarWavelength = subbandRadarWavelength

    ############################################################
    # STEP 1. create directories
    ############################################################
    #create and enter 'ion' directory
    #after finishing each step, we are in this directory
    if not os.path.exists(ionDir['ion']):
        os.makedirs(ionDir['ion'])
    os.chdir(ionDir['ion'])

    #create insar processing directories
    for k in range(2):
        subbandDir = ionDir['subband'][k]
        for i, frameNumber in enumerate(self._insar.masterFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            for j, swathNumber in enumerate(
                    range(self._insar.startingSwath,
                          self._insar.endingSwath + 1)):
                swathDir = 's{}'.format(swathNumber)
                fullDir = os.path.join(subbandDir, frameDir, swathDir)
                if not os.path.exists(fullDir):
                    os.makedirs(fullDir)

    #create ionospheric phase directory
    if not os.path.exists(ionDir['ionCal']):
        os.makedirs(ionDir['ionCal'])

    ############################################################
    # STEP 2. create subband interferograms
    ############################################################
    import shutil
    import numpy as np
    from contrib.alos2proc.alos2proc import rg_filter
    from isceobj.Alos2Proc.Alos2ProcPublic import resampleBursts
    from isceobj.Alos2Proc.Alos2ProcPublic import mosaicBurstAmplitude
    from isceobj.Alos2Proc.Alos2ProcPublic import mosaicBurstInterferogram
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for i, frameNumber in enumerate(self._insar.masterFrames):
        frameDir = 'f{}_{}'.format(i + 1, frameNumber)
        for j, swathNumber in enumerate(
                range(self._insar.startingSwath, self._insar.endingSwath + 1)):
            swathDir = 's{}'.format(swathNumber)
            #filter master and slave images
            for burstPrefix, swath in zip(
                [self._insar.masterBurstPrefix, self._insar.slaveBurstPrefix],
                [
                    masterTrack.frames[i].swaths[j],
                    slaveTrack.frames[i].swaths[j]
                ]):
                slcDir = os.path.join('../', frameDir, swathDir, burstPrefix)
                slcLowerDir = os.path.join(ionDir['subband'][0], frameDir,
                                           swathDir, burstPrefix)
                slcUpperDir = os.path.join(ionDir['subband'][1], frameDir,
                                           swathDir, burstPrefix)
                if not os.path.exists(slcLowerDir):
                    os.makedirs(slcLowerDir)
                if not os.path.exists(slcUpperDir):
                    os.makedirs(slcUpperDir)
                for k in range(swath.numberOfBursts):
                    print('processing burst: %02d' % (k + 1))
                    slc = os.path.join(slcDir,
                                       burstPrefix + '_%02d.slc' % (k + 1))
                    slcLower = os.path.join(
                        slcLowerDir, burstPrefix + '_%02d.slc' % (k + 1))
                    slcUpper = os.path.join(
                        slcUpperDir, burstPrefix + '_%02d.slc' % (k + 1))
                    rg_filter(slc, 2, [slcLower, slcUpper], subbandBandWidth,
                              subbandFrequencyCenter, 257, 2048, 0.1, 0, 0.0)
            #resample
            for l in range(2):
                os.chdir(os.path.join(ionDir['subband'][l], frameDir,
                                      swathDir))
                #recreate xml file to remove the file path
                #can also use fixImageXml.py?
                for burstPrefix, swath in zip([
                        self._insar.masterBurstPrefix,
                        self._insar.slaveBurstPrefix
                ], [
                        masterTrack.frames[i].swaths[j],
                        slaveTrack.frames[i].swaths[j]
                ]):
                    os.chdir(burstPrefix)
                    for k in range(swath.numberOfBursts):
                        slc = burstPrefix + '_%02d.slc' % (k + 1)
                        img = isceobj.createSlcImage()
                        img.load(slc + '.xml')
                        img.setFilename(slc)
                        img.extraFilename = slc + '.vrt'
                        img.setAccessMode('READ')
                        img.renderHdr()
                    os.chdir('../')

                #############################################
                #1. form interferogram
                #############################################
                masterSwath = masterTrack.frames[i].swaths[j]
                slaveSwath = slaveTrack.frames[i].swaths[j]

                #set up resampling parameters
                width = masterSwath.numberOfSamples
                length = masterSwath.numberOfLines
                polyCoeff = self._insar.rangeResidualOffsetCc[i][j]
                rgIndex = (np.arange(width) -
                           polyCoeff[-1][0]) / polyCoeff[-1][1]
                azIndex = (np.arange(length) -
                           polyCoeff[-1][2]) / polyCoeff[-1][3]
                rangeOffset =  polyCoeff[0][0] + polyCoeff[0][1]*rgIndex[None,:] + polyCoeff[0][2]*rgIndex[None,:]**2 + \
                              (polyCoeff[1][0] + polyCoeff[1][1]*rgIndex[None,:]) * azIndex[:, None] + \
                               polyCoeff[2][0] * azIndex[:, None]**2
                azimuthOffset = self._insar.azimuthResidualOffsetCc[i][j]

                slaveBurstResampledDir = self._insar.slaveBurstPrefix + '_2_coreg_cc'
                interferogramDir = 'burst_interf_2_coreg_cc'
                interferogramPrefix = self._insar.masterBurstPrefix + '-' + self._insar.slaveBurstPrefix
                resampleBursts(
                    masterSwath,
                    slaveSwath,
                    self._insar.masterBurstPrefix,
                    self._insar.slaveBurstPrefix,
                    slaveBurstResampledDir,
                    interferogramDir,
                    self._insar.masterBurstPrefix,
                    self._insar.slaveBurstPrefix,
                    self._insar.slaveBurstPrefix,
                    interferogramPrefix,
                    os.path.join(
                        '../../../../{}/{}'.format(frameDir, swathDir),
                        self._insar.rangeOffset),
                    os.path.join(
                        '../../../../{}/{}'.format(frameDir, swathDir),
                        self._insar.azimuthOffset),
                    rangeOffsetResidual=rangeOffset,
                    azimuthOffsetResidual=azimuthOffset)

                os.chdir(self._insar.masterBurstPrefix)
                mosaicBurstAmplitude(masterSwath,
                                     self._insar.masterBurstPrefix,
                                     self._insar.masterMagnitude,
                                     numberOfLooksThreshold=4)
                os.chdir('../')

                os.chdir(slaveBurstResampledDir)
                mosaicBurstAmplitude(masterSwath,
                                     self._insar.slaveBurstPrefix,
                                     self._insar.slaveMagnitude,
                                     numberOfLooksThreshold=4)
                os.chdir('../')

                os.chdir(interferogramDir)
                mosaicBurstInterferogram(masterSwath,
                                         interferogramPrefix,
                                         self._insar.interferogram,
                                         numberOfLooksThreshold=4)
                os.chdir('../')

                amp = np.zeros((masterSwath.numberOfLines,
                                2 * masterSwath.numberOfSamples),
                               dtype=np.float32)
                amp[0:, 1:masterSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(slaveBurstResampledDir, self._insar.slaveMagnitude), \
                    dtype=np.float32).reshape(masterSwath.numberOfLines, masterSwath.numberOfSamples)
                amp[0:, 0:masterSwath.numberOfSamples*2:2] = np.fromfile(os.path.join(self._insar.masterBurstPrefix, self._insar.masterMagnitude), \
                    dtype=np.float32).reshape(masterSwath.numberOfLines, masterSwath.numberOfSamples)
                amp.astype(np.float32).tofile(self._insar.amplitude)
                create_xml(self._insar.amplitude, masterSwath.numberOfSamples,
                           masterSwath.numberOfLines, 'amp')

                os.rename(
                    os.path.join(interferogramDir, self._insar.interferogram),
                    self._insar.interferogram)
                os.rename(
                    os.path.join(interferogramDir,
                                 self._insar.interferogram + '.vrt'),
                    self._insar.interferogram + '.vrt')
                os.rename(
                    os.path.join(interferogramDir,
                                 self._insar.interferogram + '.xml'),
                    self._insar.interferogram + '.xml')

                #############################################
                #2. delete subband slcs
                #############################################
                shutil.rmtree(self._insar.masterBurstPrefix)
                shutil.rmtree(self._insar.slaveBurstPrefix)
                shutil.rmtree(slaveBurstResampledDir)
                shutil.rmtree(interferogramDir)

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

    ############################################################
    # STEP 3. mosaic swaths
    ############################################################
    from isceobj.Alos2Proc.runSwathMosaic import swathMosaic
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for k in range(2):
        os.chdir(ionDir['subband'][k])
        for i, frameNumber in enumerate(self._insar.masterFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            os.chdir(frameDir)

            mosaicDir = 'mosaic'
            if not os.path.exists(mosaicDir):
                os.makedirs(mosaicDir)
            os.chdir(mosaicDir)

            if self._insar.endingSwath - self._insar.startingSwath + 1 == 1:
                import shutil
                swathDir = 's{}'.format(
                    masterTrack.frames[i].swaths[0].swathNumber)

                # if not os.path.isfile(self._insar.interferogram):
                #     os.symlink(os.path.join('../', swathDir, self._insar.interferogram), self._insar.interferogram)
                # shutil.copy2(os.path.join('../', swathDir, self._insar.interferogram+'.vrt'), self._insar.interferogram+'.vrt')
                # shutil.copy2(os.path.join('../', swathDir, self._insar.interferogram+'.xml'), self._insar.interferogram+'.xml')
                # if not os.path.isfile(self._insar.amplitude):
                #     os.symlink(os.path.join('../', swathDir, self._insar.amplitude), self._insar.amplitude)
                # shutil.copy2(os.path.join('../', swathDir, self._insar.amplitude+'.vrt'), self._insar.amplitude+'.vrt')
                # shutil.copy2(os.path.join('../', swathDir, self._insar.amplitude+'.xml'), self._insar.amplitude+'.xml')

                os.rename(
                    os.path.join('../', swathDir, self._insar.interferogram),
                    self._insar.interferogram)
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.interferogram + '.vrt'),
                    self._insar.interferogram + '.vrt')
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.interferogram + '.xml'),
                    self._insar.interferogram + '.xml')
                os.rename(os.path.join('../', swathDir, self._insar.amplitude),
                          self._insar.amplitude)
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.amplitude + '.vrt'),
                    self._insar.amplitude + '.vrt')
                os.rename(
                    os.path.join('../', swathDir,
                                 self._insar.amplitude + '.xml'),
                    self._insar.amplitude + '.xml')

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

            #choose offsets
            numberOfFrames = len(masterTrack.frames)
            numberOfSwaths = len(masterTrack.frames[i].swaths)
            if self.swathOffsetMatching:
                #no need to do this as the API support 2-d list
                #rangeOffsets = (np.array(self._insar.swathRangeOffsetMatchingMaster)).reshape(numberOfFrames, numberOfSwaths)
                #azimuthOffsets = (np.array(self._insar.swathAzimuthOffsetMatchingMaster)).reshape(numberOfFrames, numberOfSwaths)
                rangeOffsets = self._insar.swathRangeOffsetMatchingMaster
                azimuthOffsets = self._insar.swathAzimuthOffsetMatchingMaster

            else:
                #rangeOffsets = (np.array(self._insar.swathRangeOffsetGeometricalMaster)).reshape(numberOfFrames, numberOfSwaths)
                #azimuthOffsets = (np.array(self._insar.swathAzimuthOffsetGeometricalMaster)).reshape(numberOfFrames, numberOfSwaths)
                rangeOffsets = self._insar.swathRangeOffsetGeometricalMaster
                azimuthOffsets = self._insar.swathAzimuthOffsetGeometricalMaster

            rangeOffsets = rangeOffsets[i]
            azimuthOffsets = azimuthOffsets[i]

            #list of input files
            inputInterferograms = []
            inputAmplitudes = []
            for j, swathNumber in enumerate(
                    range(self._insar.startingSwath,
                          self._insar.endingSwath + 1)):
                swathDir = 's{}'.format(swathNumber)
                inputInterferograms.append(
                    os.path.join('../', swathDir, self._insar.interferogram))
                inputAmplitudes.append(
                    os.path.join('../', swathDir, self._insar.amplitude))

            #note that frame parameters are updated after mosaicking
            #mosaic amplitudes
            swathMosaic(masterTrack.frames[i],
                        inputAmplitudes,
                        self._insar.amplitude,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        resamplingMethod=0)
            #mosaic interferograms
            swathMosaic(masterTrack.frames[i],
                        inputInterferograms,
                        self._insar.interferogram,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        updateFrame=False,
                        phaseCompensation=True,
                        resamplingMethod=1)

            create_xml(self._insar.amplitude,
                       masterTrack.frames[i].numberOfSamples,
                       masterTrack.frames[i].numberOfLines, 'amp')
            create_xml(self._insar.interferogram,
                       masterTrack.frames[i].numberOfSamples,
                       masterTrack.frames[i].numberOfLines, 'int')

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

    ############################################################
    # STEP 4. mosaic frames
    ############################################################
    from isceobj.Alos2Proc.runFrameMosaic import frameMosaic
    from isceobj.Alos2Proc.Alos2ProcPublic import create_xml

    for k in range(2):
        os.chdir(ionDir['subband'][k])

        mosaicDir = 'insar'
        if not os.path.exists(mosaicDir):
            os.makedirs(mosaicDir)
        os.chdir(mosaicDir)

        numberOfFrames = len(masterTrack.frames)
        if numberOfFrames == 1:
            import shutil
            frameDir = os.path.join('f1_{}/mosaic'.format(
                self._insar.masterFrames[0]))
            # if not os.path.isfile(self._insar.interferogram):
            #     os.symlink(os.path.join('../', frameDir, self._insar.interferogram), self._insar.interferogram)
            # #shutil.copy2() can overwrite
            # shutil.copy2(os.path.join('../', frameDir, self._insar.interferogram+'.vrt'), self._insar.interferogram+'.vrt')
            # shutil.copy2(os.path.join('../', frameDir, self._insar.interferogram+'.xml'), self._insar.interferogram+'.xml')
            # if not os.path.isfile(self._insar.amplitude):
            #     os.symlink(os.path.join('../', frameDir, self._insar.amplitude), self._insar.amplitude)
            # shutil.copy2(os.path.join('../', frameDir, self._insar.amplitude+'.vrt'), self._insar.amplitude+'.vrt')
            # shutil.copy2(os.path.join('../', frameDir, self._insar.amplitude+'.xml'), self._insar.amplitude+'.xml')

            os.rename(os.path.join('../', frameDir, self._insar.interferogram),
                      self._insar.interferogram)
            os.rename(
                os.path.join('../', frameDir,
                             self._insar.interferogram + '.vrt'),
                self._insar.interferogram + '.vrt')
            os.rename(
                os.path.join('../', frameDir,
                             self._insar.interferogram + '.xml'),
                self._insar.interferogram + '.xml')
            os.rename(os.path.join('../', frameDir, self._insar.amplitude),
                      self._insar.amplitude)
            os.rename(
                os.path.join('../', frameDir, self._insar.amplitude + '.vrt'),
                self._insar.amplitude + '.vrt')
            os.rename(
                os.path.join('../', frameDir, self._insar.amplitude + '.xml'),
                self._insar.amplitude + '.xml')

        else:
            #choose offsets
            if self.frameOffsetMatching:
                rangeOffsets = self._insar.frameRangeOffsetMatchingMaster
                azimuthOffsets = self._insar.frameAzimuthOffsetMatchingMaster
            else:
                rangeOffsets = self._insar.frameRangeOffsetGeometricalMaster
                azimuthOffsets = self._insar.frameAzimuthOffsetGeometricalMaster

            #list of input files
            inputInterferograms = []
            inputAmplitudes = []
            for i, frameNumber in enumerate(self._insar.masterFrames):
                frameDir = 'f{}_{}'.format(i + 1, frameNumber)
                inputInterferograms.append(
                    os.path.join('../', frameDir, 'mosaic',
                                 self._insar.interferogram))
                inputAmplitudes.append(
                    os.path.join('../', frameDir, 'mosaic',
                                 self._insar.amplitude))

            #note that track parameters are updated after mosaicking
            #mosaic amplitudes
            frameMosaic(masterTrack,
                        inputAmplitudes,
                        self._insar.amplitude,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        updateTrack=False,
                        phaseCompensation=False,
                        resamplingMethod=0)
            #mosaic interferograms
            frameMosaic(masterTrack,
                        inputInterferograms,
                        self._insar.interferogram,
                        rangeOffsets,
                        azimuthOffsets,
                        self._insar.numberRangeLooks1,
                        self._insar.numberAzimuthLooks1,
                        updateTrack=False,
                        phaseCompensation=True,
                        resamplingMethod=1)

            create_xml(self._insar.amplitude, masterTrack.numberOfSamples,
                       masterTrack.numberOfLines, 'amp')
            create_xml(self._insar.interferogram, masterTrack.numberOfSamples,
                       masterTrack.numberOfLines, 'int')

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

    ############################################################
    # STEP 5. clear frame processing files
    ############################################################
    import shutil
    from isceobj.Alos2Proc.Alos2ProcPublic import runCmd

    for k in range(2):
        os.chdir(ionDir['subband'][k])
        for i, frameNumber in enumerate(self._insar.masterFrames):
            frameDir = 'f{}_{}'.format(i + 1, frameNumber)
            shutil.rmtree(frameDir)
            #cmd = 'rm -rf {}'.format(frameDir)
            #runCmd(cmd)
        os.chdir('../')

    ############################################################
    # STEP 6. create differential interferograms
    ############################################################
    import numpy as np
    from isceobj.Alos2Proc.Alos2ProcPublic import runCmd

    for k in range(2):
        os.chdir(ionDir['subband'][k])

        insarDir = ionDir['insar']
        if not os.path.exists(insarDir):
            os.makedirs(insarDir)
        os.chdir(insarDir)

        rangePixelSize = self._insar.numberRangeLooks1 * masterTrack.rangePixelSize
        radarWavelength = subbandRadarWavelength[k]
        rectRangeOffset = os.path.join('../../../', insarDir,
                                       self._insar.rectRangeOffset)

        cmd = "imageMath.py -e='a*exp(-1.0*J*b*4.0*{}*{}/{}) * (b!=0)' --a={} --b={} -o {} -t cfloat".format(
            np.pi, rangePixelSize, radarWavelength, self._insar.interferogram,
            rectRangeOffset, self._insar.differentialInterferogram)
        runCmd(cmd)

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

    os.chdir('../')
    catalog.printToLog(logger, "runIonSubband")
    self._insar.procDoc.addAllFromCatalog(catalog)