def updateValidRegion(topReference, secondaryPath, swath):

    #secondarySwathList = ut.getSwathList(secondary)
    #swathList = list(sorted(set(referenceSwathList+secondarySwathList)))

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

    print(secondaryPath)
    topCoreg = ut.loadProduct(os.path.join(secondaryPath , 'IW{0}.xml'.format(swath)))

    topIfg = ut.coregSwathSLCProduct()
    topIfg.configure()


    minReference = topReference.bursts[0].burstNumber
    maxReference = topReference.bursts[-1].burstNumber

    minSecondary = topCoreg.bursts[0].burstNumber
    maxSecondary = topCoreg.bursts[-1].burstNumber

    minBurst = max(minSecondary, minReference)
    maxBurst = min(maxSecondary, maxReference)
    print ('minSecondary,maxSecondary',minSecondary, maxSecondary)
    print ('minReference,maxReference',minReference, maxReference)
    print ('minBurst, maxBurst: ', minBurst, maxBurst)

    for ii in range(minBurst, maxBurst + 1):

            ####Process the top bursts
        reference = topReference.bursts[ii-minReference]
        secondary  = topCoreg.bursts[ii-minSecondary]
        ut.adjustCommonValidRegion(reference,secondary)
        #topReference.bursts[ii-minReference].firstValidLine = reference.firstValidLine

    return topReference
def updateValidRegion(topMaster, slavePath, swath):

    #slaveSwathList = ut.getSwathList(slave)
    #swathList = list(sorted(set(masterSwathList+slaveSwathList)))

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

    topCoreg = ut.loadProduct(os.path.join(slavePath , 'IW{0}.xml'.format(swath)))

    topIfg = ut.coregSwathSLCProduct()
    topIfg.configure()


    minMaster = topMaster.bursts[0].burstNumber
    maxMaster = topMaster.bursts[-1].burstNumber

    minSlave = topCoreg.bursts[0].burstNumber
    maxSlave = topCoreg.bursts[-1].burstNumber

    minBurst = max(minSlave, minMaster)
    maxBurst = min(maxSlave, maxMaster)
    print ('minSlave,maxSlave',minSlave, maxSlave)
    print ('minMaster,maxMaster',minMaster, maxMaster)
    print ('minBurst, maxBurst: ', minBurst, maxBurst)

    for ii in range(minBurst, maxBurst + 1):

            ####Process the top bursts
        master = topMaster.bursts[ii-minMaster]
        slave  = topCoreg.bursts[ii-minSlave]
        ut.adjustCommonValidRegion(master,slave)
        #topMaster.bursts[ii-minMaster].firstValidLine = master.firstValidLine

    return topMaster
def main(iargs=None):
    '''Create overlap interferograms.
    '''
    inps=cmdLineParse(iargs)

    if inps.overlap:
        masterSwathList = ut.getSwathList(os.path.join(inps.master, 'overlap'))
        slaveSwathList = ut.getSwathList(os.path.join(inps.slave, 'overlap'))
    else:
        masterSwathList = ut.getSwathList(inps.master)
        slaveSwathList = ut.getSwathList(inps.slave)
    swathList = list(sorted(set(masterSwathList+slaveSwathList)))

    for swath in swathList:
        IWstr = 'IW{0}'.format(swath)
        if inps.overlap:
            ifgdir = os.path.join(inps.interferogram, 'overlap', 'IW{0}'.format(swath))
        else:
            ifgdir = os.path.join(inps.interferogram, 'IW{0}'.format(swath))
            
        if not os.path.exists(ifgdir):
                os.makedirs(ifgdir)

    ####Load relevant products
        if inps.overlap:
            topMaster = ut.loadProduct(os.path.join(inps.master , 'overlap','IW{0}_top.xml'.format(swath)))
            botMaster = ut.loadProduct(os.path.join(inps.master ,'overlap', 'IW{0}_bottom.xml'.format(swath)))
            topCoreg = ut.loadProduct(os.path.join(inps.slave, 'overlap', 'IW{0}_top.xml'.format(swath)))
            botCoreg = ut.loadProduct(os.path.join(inps.slave, 'overlap', 'IW{0}_bottom.xml'.format(swath)))

        else:
            topMaster = ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
            topCoreg = ut.loadProduct(os.path.join(inps.slave , 'IW{0}.xml'.format(swath)))

        if inps.overlap:
            coregdir = os.path.join(inps.slave, 'overlap', 'IW{0}'.format(swath))
        else:
            coregdir = os.path.join(inps.slave,'IW{0}'.format(swath))
    
        topIfg = ut.coregSwathSLCProduct()
        topIfg.configure()

        if inps.overlap:
            botIfg = ut.coregSwathSLCProduct()
            botIfg.configure()

        minMaster = topMaster.bursts[0].burstNumber
        maxMaster = topMaster.bursts[-1].burstNumber

        minSlave = topCoreg.bursts[0].burstNumber
        maxSlave = topCoreg.bursts[-1].burstNumber

        minBurst = max(minSlave, minMaster)
        maxBurst = min(maxSlave, maxMaster)
        print ('minSlave,maxSlave',minSlave, maxSlave)
        print ('minMaster,maxMaster',minMaster, maxMaster)
        print ('minBurst, maxBurst: ', minBurst, maxBurst)

        for ii in range(minBurst, maxBurst + 1):

            ####Process the top bursts
            master = topMaster.bursts[ii-minMaster]
            slave  = topCoreg.bursts[ii-minSlave]

            print('matching burst numbers: ',master.burstNumber, slave.burstNumber)

            mastername = master.image.filename
            slavename = slave.image.filename

            if inps.overlap:
                rdict = { 'rangeOff1' : os.path.join(inps.master, 'overlap', IWstr, 'range_top_%02d_%02d.off'%(ii,ii+1)),
                     'rangeOff2' : os.path.join(inps.slave, 'overlap', IWstr, 'range_top_%02d_%02d.off'%(ii,ii+1)),
                     'azimuthOff': os.path.join(inps.slave, 'overlap', IWstr, 'azimuth_top_%02d_%02d.off'%(ii,ii+1))}

                intname = os.path.join(ifgdir, '%s_top_%02d_%02d.int'%(inps.intprefix,ii,ii+1))
        
            else:

                rdict = {'rangeOff1' : os.path.join(inps.master, IWstr, 'range_%02d.off'%(ii)),
                     'rangeOff2' : os.path.join(inps.slave, IWstr, 'range_%02d.off'%(ii)),
                     'azimuthOff1': os.path.join(inps.slave, IWstr, 'azimuth_%02d.off'%(ii))}
            
                intname = os.path.join(ifgdir, '%s_%02d.int'%(inps.intprefix,ii))


            ut.adjustCommonValidRegion(master,slave)
            fact = 4 * np.pi * slave.rangePixelSize / slave.radarWavelength
            intimage = multiply(mastername, slavename, intname,
                        rdict['rangeOff1'], rdict['rangeOff2'], fact, master, flatten=inps.flatten)

            burst = copy.deepcopy(master)
            burst.image = intimage
            burst.burstNumber = ii
            topIfg.bursts.append(burst)


            if inps.overlap:
                ####Process the bottom bursts
                master = botMaster.bursts[ii-minMaster]
                slave = botCoreg.bursts[ii-minSlave]


                mastername =  master.image.filename
                slavename = slave.image.filename
#            rdict = {'rangeOff' : os.path.join(coregdir, 'range_bot_%02d_%02d.off'%(ii,ii+1)),
#                   'azimuthOff': os.path.join(coregdir, 'azimuth_bot_%02d_%02d.off'%(ii,ii+1))}

                rdict = { 'rangeOff1' : os.path.join(inps.master, 'overlap', IWstr, 'range_bot_%02d_%02d.off'%(ii,ii+1)),
                     'rangeOff2' : os.path.join(inps.slave, 'overlap', IWstr, 'range_bot_%02d_%02d.off'%(ii,ii+1)),
                    'azimuthOff': os.path.join(inps.slave, 'overlap', IWstr, 'azimuth_bot_%02d_%02d.off'%(ii,ii+1))}


                print ('rdict: ', rdict)

                ut.adjustCommonValidRegion(master,slave)
                intname = os.path.join(ifgdir, '%s_bot_%02d_%02d.int'%(inps.intprefix,ii,ii+1))
                fact = 4 * np.pi * slave.rangePixelSize / slave.radarWavelength

            #intimage = multiply(mastername, slavename, intname,
            #        rdict['rangeOff'], fact, master, flatten=True)

                intimage = multiply(mastername, slavename, intname,
                        rdict['rangeOff1'], rdict['rangeOff2'], fact, master, flatten=inps.flatten)

                burst = copy.deepcopy(master)
                burst.burstNumber = ii
                burst.image = intimage
                botIfg.bursts.append(burst)


        topIfg.numberOfBursts = len(topIfg.bursts)
        if hasattr(topCoreg, 'reference'):
            topIfg.reference = topCoreg.reference
        else:
            topIfg.reference = topMaster.reference

        print('Type: ',type(topIfg.reference))

        if inps.overlap:
            ut.saveProduct(topIfg, ifgdir +  '_top.xml')
            botIfg.numberOfBursts = len(botIfg.bursts)
            botIfg.reference = botCoreg.reference
            print(botIfg.reference)
            ut.saveProduct(botIfg, ifgdir + '_bottom.xml')
        else:
            ut.saveProduct(topIfg, ifgdir + '.xml')    
def main(iargs=None):
    '''Create overlap interferograms.
    '''
    inps = cmdLineParse(iargs)

    if inps.overlap:
        referenceSwathList = ut.getSwathList(
            os.path.join(inps.reference, 'overlap'))
        secondarySwathList = ut.getSwathList(
            os.path.join(inps.secondary, 'overlap'))
    else:
        referenceSwathList = ut.getSwathList(inps.reference)
        secondarySwathList = ut.getSwathList(inps.secondary)
    swathList = list(sorted(set(referenceSwathList + secondarySwathList)))

    for swath in swathList:
        IWstr = 'IW{0}'.format(swath)
        if inps.overlap:
            ifgdir = os.path.join(inps.interferogram, 'overlap', IWstr)
        else:
            ifgdir = os.path.join(inps.interferogram, IWstr)

        os.makedirs(ifgdir, exist_ok=True)

        ####Load relevant products
        if inps.overlap:
            topReference = ut.loadProduct(
                os.path.join(inps.reference, 'overlap',
                             'IW{0}_top.xml'.format(swath)))
            botReference = ut.loadProduct(
                os.path.join(inps.reference, 'overlap',
                             'IW{0}_bottom.xml'.format(swath)))
            topCoreg = ut.loadProduct(
                os.path.join(inps.secondary, 'overlap',
                             'IW{0}_top.xml'.format(swath)))
            botCoreg = ut.loadProduct(
                os.path.join(inps.secondary, 'overlap',
                             'IW{0}_bottom.xml'.format(swath)))

        else:
            topReference = ut.loadProduct(
                os.path.join(inps.reference, 'IW{0}.xml'.format(swath)))
            topCoreg = ut.loadProduct(
                os.path.join(inps.secondary, 'IW{0}.xml'.format(swath)))

        if inps.overlap:
            coregdir = os.path.join(inps.secondary, 'overlap',
                                    'IW{0}'.format(swath))
        else:
            coregdir = os.path.join(inps.secondary, 'IW{0}'.format(swath))

        topIfg = ut.coregSwathSLCProduct()
        topIfg.configure()

        if inps.overlap:
            botIfg = ut.coregSwathSLCProduct()
            botIfg.configure()

        minReference = topReference.bursts[0].burstNumber
        maxReference = topReference.bursts[-1].burstNumber

        minSecondary = topCoreg.bursts[0].burstNumber
        maxSecondary = topCoreg.bursts[-1].burstNumber

        minBurst = max(minSecondary, minReference)
        maxBurst = min(maxSecondary, maxReference)
        print('minSecondary,maxSecondary', minSecondary, maxSecondary)
        print('minReference,maxReference', minReference, maxReference)
        print('minBurst, maxBurst: ', minBurst, maxBurst)

        for ii in range(minBurst, maxBurst + 1):

            ####Process the top bursts
            reference = topReference.bursts[ii - minReference]
            secondary = topCoreg.bursts[ii - minSecondary]

            print('matching burst numbers: ', reference.burstNumber,
                  secondary.burstNumber)

            referencename = reference.image.filename
            secondaryname = secondary.image.filename

            if inps.reference_suffix is not None:
                referencename = os.path.splitext(referencename)[
                    0] + inps.reference_suffix + os.path.splitext(
                        referencename)[1]
            if inps.secondary_suffix is not None:
                secondaryname = os.path.splitext(secondaryname)[
                    0] + inps.secondary_suffix + os.path.splitext(
                        secondaryname)[1]

            if inps.overlap:
                rdict = {
                    'rangeOff1':
                    os.path.join(inps.reference, 'overlap', IWstr,
                                 'range_top_%02d_%02d.off' % (ii, ii + 1)),
                    'rangeOff2':
                    os.path.join(inps.secondary, 'overlap', IWstr,
                                 'range_top_%02d_%02d.off' % (ii, ii + 1)),
                    'azimuthOff':
                    os.path.join(inps.secondary, 'overlap', IWstr,
                                 'azimuth_top_%02d_%02d.off' % (ii, ii + 1))
                }

                intname = os.path.join(
                    ifgdir,
                    '%s_top_%02d_%02d.int' % (inps.intprefix, ii, ii + 1))

            else:

                rdict = {
                    'rangeOff1':
                    os.path.join(inps.reference, IWstr,
                                 'range_%02d.off' % (ii)),
                    'rangeOff2':
                    os.path.join(inps.secondary, IWstr,
                                 'range_%02d.off' % (ii)),
                    'azimuthOff1':
                    os.path.join(inps.secondary, IWstr,
                                 'azimuth_%02d.off' % (ii))
                }

                intname = os.path.join(ifgdir,
                                       '%s_%02d.int' % (inps.intprefix, ii))

            ut.adjustCommonValidRegion(reference, secondary)
            fact = 4 * np.pi * secondary.rangePixelSize / secondary.radarWavelength
            intimage = multiply(referencename,
                                secondaryname,
                                intname,
                                rdict['rangeOff1'],
                                rdict['rangeOff2'],
                                fact,
                                reference,
                                flatten=inps.flatten)

            burst = copy.deepcopy(reference)
            burst.image = intimage
            burst.burstNumber = ii
            topIfg.bursts.append(burst)

            if inps.overlap:
                ####Process the bottom bursts
                reference = botReference.bursts[ii - minReference]
                secondary = botCoreg.bursts[ii - minSecondary]

                referencename = reference.image.filename
                secondaryname = secondary.image.filename
                #            rdict = {'rangeOff' : os.path.join(coregdir, 'range_bot_%02d_%02d.off'%(ii,ii+1)),
                #                   'azimuthOff': os.path.join(coregdir, 'azimuth_bot_%02d_%02d.off'%(ii,ii+1))}

                rdict = {
                    'rangeOff1':
                    os.path.join(inps.reference, 'overlap', IWstr,
                                 'range_bot_%02d_%02d.off' % (ii, ii + 1)),
                    'rangeOff2':
                    os.path.join(inps.secondary, 'overlap', IWstr,
                                 'range_bot_%02d_%02d.off' % (ii, ii + 1)),
                    'azimuthOff':
                    os.path.join(inps.secondary, 'overlap', IWstr,
                                 'azimuth_bot_%02d_%02d.off' % (ii, ii + 1))
                }

                print('rdict: ', rdict)

                ut.adjustCommonValidRegion(reference, secondary)
                intname = os.path.join(
                    ifgdir,
                    '%s_bot_%02d_%02d.int' % (inps.intprefix, ii, ii + 1))
                fact = 4 * np.pi * secondary.rangePixelSize / secondary.radarWavelength

                #intimage = multiply(referencename, secondaryname, intname,
                #        rdict['rangeOff'], fact, reference, flatten=True)

                intimage = multiply(referencename,
                                    secondaryname,
                                    intname,
                                    rdict['rangeOff1'],
                                    rdict['rangeOff2'],
                                    fact,
                                    reference,
                                    flatten=inps.flatten)

                burst = copy.deepcopy(reference)
                burst.burstNumber = ii
                burst.image = intimage
                botIfg.bursts.append(burst)

        topIfg.numberOfBursts = len(topIfg.bursts)
        if hasattr(topCoreg, 'reference'):
            topIfg.reference = topCoreg.reference
        else:
            topIfg.reference = topReference.reference

        print('Type: ', type(topIfg.reference))

        if inps.overlap:
            ut.saveProduct(topIfg, ifgdir + '_top.xml')
            botIfg.numberOfBursts = len(botIfg.bursts)
            botIfg.reference = botCoreg.reference
            print(botIfg.reference)
            ut.saveProduct(botIfg, ifgdir + '_bottom.xml')
        else:
            ut.saveProduct(topIfg, ifgdir + '.xml')