Ejemplo n.º 1
0
def unwrap_error_correction_bridging(ifgram_file, mask_file, y_list, x_list, ramp_type='plane',\
                                     ifgram_cor_file=None, save_cor_deramp_file=False):
    '''Unwrapping error correction with bridging.
    Inputs:
        ifgram_file : string, name/path of interferogram(s) to be corrected
        mask_file   : string, name/path of mask file to mark different patches 
        y/x_list    : list of int, bonding points in y/x 
        ifgram_cor_file : string, optional, output file name
        save_cor_deramp_file : bool, optional
    Output:
        ifgram_cor_file
    Example:
        y_list = [235, 270, 350, 390]
        x_list = [880, 890, 1200, 1270]
        unwrap_error_correction_bridging('unwrapIfgram.h5', 'mask_all.h5', y_list, x_list, 'quadratic')
    '''
    ##### Mask and Ramp
    mask = readfile.read(mask_file)[0]
    ramp_mask = mask == 1
    print 'estimate phase ramp during the correction'
    print 'ramp type: ' + ramp_type

    ##### Bridge Info
    # Check
    for i in range(len(x_list)):
        if mask[y_list[i], x_list[i]] == 0:
            print '\nERROR: Connecting point (%d,%d) is out of masked area! Select them again!\n' % (
                y_list[i], x_list[i])
            sys.exit(1)
    print 'Number of bridges: ' + str(len(x_list) / 2)
    print 'Bonding points coordinates:\nx: ' + str(x_list) + '\ny: ' + str(
        y_list)

    # Plot Connecting Pair of Points
    plot_bonding_points = False
    if plot_bonding_points:
        point_yx = ''
        line_yx = ''
        n_bridge = len(x) / 2
        for i in range(n_bridge):
            pair_yx = str(y[2 * i]) + ',' + str(x[2 * i]) + ',' + str(
                y[2 * i + 1]) + ',' + str(x[2 * i + 1])
            if not i == n_bridge - 1:
                point_yx += pair_yx + ','
                line_yx += pair_yx + ';'
            else:
                point_yx += pair_yx
                line_yx += pair_yx

        try:
            plot_cmd = 'view.py --point-yx="'+point_yx+'" --line-yx="'+line_yx+\
                       '" --nodisplay -o bonding_points.png -f '+maskFile
            print plot_cmd
            os.system(plot_cmd)
        except:
            pass

    # Basic info
    ext = os.path.splitext(ifgram_file)[1]
    atr = readfile.read_attribute(ifgram_file)
    k = atr['FILE_TYPE']

    try:
        ref_y = int(atr['ref_y'])
        ref_x = int(atr['ref_x'])
        print 'reference pixel in y/x: %d/%d' % (ref_y, ref_x)
    except:
        sys.exit(
            'ERROR: Can not find ref_y/x value, input file is not referenced in space!'
        )

    # output file name
    if not ifgram_cor_file:
        ifgram_cor_file = os.path.splitext(ifgram_file)[0] + '_unwCor' + ext
    ifgram_cor_deramp_file = os.path.splitext(
        ifgram_cor_file)[0] + '_' + ramp_type + ext

    ##### HDF5 file
    if ext == '.h5':
        ##### Read
        h5 = h5py.File(ifgram_file, 'r')
        ifgram_list = sorted(h5[k].keys())
        ifgram_num = len(ifgram_list)

        h5out = h5py.File(ifgram_cor_file, 'w')
        group = h5out.create_group(k)
        print 'writing >>> ' + ifgram_cor_file

        if save_cor_deramp_file:
            h5out_deramp = h5py.File(ifgram_cor_deramp_file, 'w')
            group_deramp = h5out_deramp.create_group(k)
            print 'writing >>> ' + ifgram_cor_deramp_file

        ##### Loop
        print 'Number of interferograms: ' + str(ifgram_num)
        prog_bar = ptime.progress_bar(maxValue=ifgram_num)
        date12_list = ptime.list_ifgram2date12(ifgram_list)
        for i in range(ifgram_num):
            ifgram = ifgram_list[i]
            data = h5[k][ifgram].get(ifgram)[:]
            data -= data[ref_y, ref_x]

            data_deramp, ramp = rm.remove_data_surface(data, ramp_mask,
                                                       ramp_type)
            data_derampCor = bridging_data(data_deramp, mask, x_list, y_list)

            ramp[data == 0.] = 0.
            gg = group.create_group(ifgram)
            dset = gg.create_dataset(ifgram,
                                     data=data_derampCor + ramp,
                                     compression='gzip')
            for key, value in h5[k][ifgram].attrs.iteritems():
                gg.attrs[key] = value

            if save_cor_deramp_file:
                gg_deramp = group_deramp.create_group(ifgram)
                dset = gg_deramp.create_dataset(ifgram,
                                                data=data_derampCor,
                                                compression='gzip')
                for key, value in h5[k][ifgram].attrs.iteritems():
                    gg_deramp.attrs[key] = value
            prog_bar.update(i + 1, suffix=date12_list[i])

        prog_bar.close()
        h5.close()
        h5out.close()
        try:
            h5out_deramp.close()
        except:
            pass

    #### .unw file
    elif ext == '.unw':
        print 'read ' + ifgram_file
        data = readfile.read(ifgram_file)[0]
        data -= data[ref_y, ref_x]

        data_deramp, ramp = rm.remove_data_surface(data, ramp_mask, ramp_type)
        data_derampCor = bridging_data(data_deramp, mask, x_list, y_list)

        print 'writing >>> ' + ifgram_cor_file
        ramp[data == 0.] = 0.
        ifgram_cor_file = writefile.write(data_derampCor + ramp, atr,
                                          ifgram_cor_file)
        if save_cor_deramp_file:
            print 'writing >>> ' + ifgram_cor_deramp_file
            ifgram_cor_deramp_file = writefile.write(data_derampCor, atr,
                                                     ifgram_cor_deramp_file)

    else:
        sys.exit('Un-supported file type: ' + ext)

    return ifgram_cor_file, ifgram_cor_deramp_file
Ejemplo n.º 2
0
def main(argv):

    method = 'triangular_consistency'  ## or 'bonding_point'
    ramp_type = 'plane'
    save_rampCor = 'yes'
    plot_bonding_points = 'yes'

    ##### Check Inputs
    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, 'h:f:m:x:y:o:t:',
                                       ['ramp=', 'no-ramp-save'])
        except getopt.GetoptError:
            print 'Error while getting args'
            usage()
            sys.exit(1)

        for opt, arg in opts:
            if opt in ['-h', '--help']:
                usage()
                sys.exit()
            elif opt in '-f':
                File = arg
            elif opt in '-m':
                maskFile = arg
            elif opt in '-o':
                outName = arg
            elif opt in '-x':
                x = [int(i) for i in arg.split(',')]
                method = 'bonding_point'
            elif opt in '-y':
                y = [int(i) for i in arg.split(',')]
                method = 'bonding_point'
            elif opt in '-t':
                templateFile = arg
            elif opt in '--ramp':
                ramp_type = arg.lower()
            elif opt in '--no-ramp-save':
                save_rampCor = 'no'

    elif len(sys.argv) == 2:
        if argv[0] in ['-h', '--help']:
            usage()
            sys.exit()
        elif os.path.isfile(argv[0]):
            File = argv[0]
            maskFile = argv[1]
        else:
            print 'Input file does not existed: ' + argv[0]
            sys.exit(1)

    else:
        usage()
        sys.exit(1)

    ##### Check template file
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except:
        pass

    try:
        yx = [
            int(i) for i in templateContents['pysar.unwrapError.yx'].split(',')
        ]
        x = yx[1::2]
        y = yx[0::2]
        method = 'bonding_point'
    except:
        pass

    ##### Read Mask File
    ## Priority:
    ## Input mask file > pysar.mask.file > existed Modified_Mask.h5 > existed Mask.h5
    try:
        maskFile
    except:
        try:
            maskFile = templateContents['pysar.mask.file']
        except:
            if os.path.isfile('Modified_Mask.h5'):
                maskFile = 'Modified_Mask.h5'
            elif os.path.isfile('Mask.h5'):
                maskFile = 'Mask.h5'
            else:
                print 'No mask found!'
                sys.exit(1)
    try:
        Mask, Matr = readfile.read(maskFile)
        print 'mask: ' + maskFile
    except:
        print 'Can not open mask file: ' + maskFile
        sys.exit(1)

    ##### Output file name
    ext = os.path.splitext(File)[1]
    try:
        outName
    except:
        outName = File.split('.')[0] + '_unwCor' + ext

    print '\n**************** Unwrapping Error Correction ******************'

    ####################  Triangular Consistency (Phase Closure)  ####################
    if method == 'triangular_consistency':
        print 'Phase unwrapping error correction using Triangular Consistency / Phase Closure'

        h5file = h5py.File(File)
        ifgramList = h5file['interferograms'].keys()
        sx = int(h5file['interferograms'][ifgramList[0]].attrs['WIDTH'])
        sy = int(h5file['interferograms'][ifgramList[0]].attrs['FILE_LENGTH'])
        curls, Triangles, C = ut.get_triangles(h5file)
        A, B = ut.design_matrix(h5file)
        ligram, lv = np.shape(B)
        lcurls = np.shape(curls)[0]
        print 'Number of all triangles: ' + str(lcurls)
        print 'Number of interferograms: ' + str(ligram)
        #print curls

        curlfile = 'curls.h5'
        if not os.path.isfile(curlfile):
            ut.generate_curls(curlfile, h5file, Triangles, curls)

        thr = 0.50
        curls = np.array(curls)
        n1 = curls[:, 0]
        n2 = curls[:, 1]
        n3 = curls[:, 2]

        numPixels = sy * sx
        print 'reading interferograms...'
        data = np.zeros((ligram, numPixels), np.float32)
        for ni in range(ligram):
            dset = h5file['interferograms'][ifgramList[ni]].get(ifgramList[ni])
            d = dset[0:dset.shape[0], 0:dset.shape[1]]
            data[ni] = d.flatten(1)

        print np.shape(data)
        print 'reading curls ...'
        h5curl = h5py.File(curlfile)
        curlList = h5curl['interferograms'].keys()
        curlData = np.zeros((lcurls, numPixels), np.float32)
        for ni in range(lcurls):
            dset = h5curl['interferograms'][curlList[ni]].get(curlList[ni])
            d = dset[0:dset.shape[0], 0:dset.shape[1]]
            curlData[ni] = d.flatten(1)
        pi = np.pi
        EstUnwrap = np.zeros((ligram, numPixels), np.float32)

        #try:
        #    maskFile=argv[1]
        #    h5Mask=h5py.File(maskFile)
        #    dset = h5Mask['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        #except:
        #    dset = h5file['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]

        Mask = Mask.flatten(1)

        for ni in range(numPixels):
            #dU = np.zeros([ligram,1])
            #print np.shape(dU)
            #print np.shape(data[:,ni])

            if Mask[ni] == 1:
                dU = data[:, ni]
                #nan_ndx = dataPoint == 0.
                unwCurl = np.array(curlData[:, ni])
                #print unwCurl

                ind = np.abs(unwCurl) >= thr
                N1 = n1[ind]
                N2 = n2[ind]
                N3 = n3[ind]
                indC = np.abs(unwCurl) < thr
                Nc1 = n1[indC]
                Nc2 = n2[indC]
                Nc3 = n3[indC]

                N = np.hstack([N1, N2, N3])
                UniN = np.unique(N)
                Nc = np.hstack([Nc1, Nc2, Nc3])
                UniNc = np.unique(Nc)

                inter = list(set(UniNc) & set(UniN))  # intersetion
                UniNc = list(UniNc)
                for x in inter:
                    UniNc.remove(x)

                D = np.zeros([len(UniNc), ligram])
                for i in range(len(UniNc)):
                    D[i, UniNc[i]] = 1

                AAA = np.vstack([-2 * pi * C, D])
                #AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])])
                #AAA2=np.hstack([-2*pi*np.eye(ligram),B])
                #AAAA=np.vstack([AAA1,AAA2])
                AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)])

                #print '************************'
                #print np.linalg.matrix_rank(C)
                #print np.linalg.matrix_rank(AAA)
                #print np.linalg.matrix_rank(AAAA)
                #print '************************'

                #LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU)
                #ind=np.isnan(AAA)
                #M1=pinv(AAA)
                #M=np.dot(M1,LLL)
                #EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi

                ##########
                # with Tikhonov regularization:
                AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)])
                LLL = list(np.dot(C, dU)) + list(np.zeros(
                    np.shape(UniNc)[0])) + list(np.zeros(ligram))
                ind = np.isnan(AAAA)
                M1 = pinv(AAAA)
                M = np.dot(M1, LLL)
                EstUnwrap[:, ni] = np.round(M[0:ligram]) * 2.0 * np.pi
                #print M[0:ligram]
                #print np.round(M[0:ligram])

            else:
                EstUnwrap[:, ni] = np.zeros([ligram])
                if not np.remainder(ni, 10000):
                    print 'Processing point: %7d of %7d ' % (ni, numPixels)

        ##### Output
        dataCor = data + EstUnwrap
        unwCorFile = File.replace('.h5', '') + '_unwCor.h5'
        print 'writing >>> ' + unwCorFile
        h5unwCor = h5py.File(unwCorFile, 'w')
        gg = h5unwCor.create_group('interferograms')
        for i in range(ligram):
            group = gg.create_group(ifgramList[i])
            dset = group.create_dataset(ifgramList[i],
                                        data=np.reshape(
                                            dataCor[i, :], [sx, sy]).T,
                                        compression='gzip')
            for key, value in h5file['interferograms'][
                    ifgramList[i]].attrs.iteritems():
                group.attrs[key] = value

        try:
            MASK = h5file['mask'].get('mask')
            gm = h5unwCor.create_group('mask')
            dset = gm.create_dataset('mask', data=MASK, compression='gzip')
        except:
            pass

        h5unwCor.close()
        h5file.close()
        h5curl.close()

    ####################  Bonding Points (Spatial Continuity)  ####################
    elif method == 'bonding_point':
        print 'Phase unwrapping error correction using Bonding Points / Spatial Continuity'

        ##### Read Bridge Points Info
        try:
            x
            y
            if len(x) != len(y) or np.mod(len(x), 2) != 0:
                print 'Wrong number of bridge points input: ' + str(
                    len(x)) + ' for x, ' + str(len(y)) + ' for y'
                usage()
                sys.exit(1)
        except:
            print 'Error in reading bridge points info!'
            usage()
            sys.exit(1)
        for i in range(0, len(x)):
            if Mask[y[i], x[i]] == 0:
                print '\nERROR: Connecting point (' + str(y[i]) + ',' + str(
                    x[i]) + ') is out of masked area! Select them again!\n'
                sys.exit(1)

        print 'Number of bonding point pairs: ' + str(len(x) / 2)
        print 'Bonding points coordinates:\nx: ' + str(x) + '\ny: ' + str(y)

        ## Plot Connecting Pair of Points
        if plot_bonding_points == 'yes':
            point_yx = ''
            line_yx = ''
            n_bridge = len(x) / 2
            for i in range(n_bridge):
                pair_yx = str(y[2 * i]) + ',' + str(x[2 * i]) + ',' + str(
                    y[2 * i + 1]) + ',' + str(x[2 * i + 1])
                if not i == n_bridge - 1:
                    point_yx += pair_yx + ','
                    line_yx += pair_yx + ';'
                else:
                    point_yx += pair_yx
                    line_yx += pair_yx

            try:
                plot_cmd = 'view.py --point-yx="'+point_yx+'" --line-yx="'+line_yx+\
                           '" --nodisplay -o bonding_points.png -f '+maskFile
                print plot_cmd
                os.system(plot_cmd)
            except:
                pass

        ##### Ramp Info
        ramp_mask = Mask == 1
        print 'estimate phase ramp during the correction'
        print 'ramp type: ' + ramp_type
        if save_rampCor == 'yes':
            outName_ramp = os.path.basename(outName).split(
                ext)[0] + '_' + ramp_type + ext

        ########## PySAR ##########
        if ext == '.h5':
            ##### Read
            try:
                h5file = h5py.File(File, 'r')
            except:
                print 'ERROR: Cannot open input file: ' + File
                sys.exit(1)
            k = h5file.keys()
            if 'interferograms' in k:
                k[0] = 'interferograms'
                print 'Input file is ' + k[0]
            else:
                print 'Input file - ' + File + ' - is not interferograms.'
                usage()
                sys.exit(1)
            igramList = sorted(h5file[k[0]].keys())

            #### Write
            h5out = h5py.File(outName, 'w')
            gg = h5out.create_group(k[0])
            print 'writing >>> ' + outName

            if save_rampCor == 'yes':
                h5out_ramp = h5py.File(outName_ramp, 'w')
                gg_ramp = h5out_ramp.create_group(k[0])
                print 'writing >>> ' + outName_ramp

            ##### Loop
            print 'Number of interferograms: ' + str(len(igramList))
            for igram in igramList:
                print igram
                data = h5file[k[0]][igram].get(igram)[:]

                data_ramp, ramp = rm.remove_data_surface(
                    data, ramp_mask, ramp_type)
                #ramp = data_ramp - data
                data_rampCor = phase_bonding(data_ramp, Mask, x, y)
                dataCor = data_rampCor - ramp

                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=dataCor,
                                            compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key] = value

                if save_rampCor == 'yes':
                    group_ramp = gg_ramp.create_group(igram)
                    dset = group_ramp.create_dataset(igram,
                                                     data=data_rampCor,
                                                     compression='gzip')
                    for key, value in h5file[k[0]][igram].attrs.iteritems():
                        group_ramp.attrs[key] = value

            try:
                mask = h5file['mask'].get('mask')
                gm = h5out.create_group('mask')
                dset = gm.create_dataset('mask',
                                         data=mask[0:mask.shape[0],
                                                   0:mask.shape[1]],
                                         compression='gzip')
            except:
                print 'no mask group found.'

            h5file.close()
            h5out.close()
            if save_rampCor == 'yes':
                h5out_ramp.close()

        ########## ROI_PAC ##########
        elif ext == '.unw':
            print 'Input file is ' + ext
            a, data, atr = readfile.read_float32(File)

            data_ramp, ramp = rm.remove_data_surface(data, ramp_mask,
                                                     ramp_type)
            #ramp = data_ramp - data
            data_rampCor = phase_bonding(data_ramp, Mask, x, y)
            dataCor = data_rampCor - ramp

            writefile.write(dataCor, atr, outName)
            if save_rampCor == 'yes':
                writefile.write(data_rampCor, atr, outName_ramp)

        else:
            print 'Un-supported file type: ' + ext
            usage()
            sys.exit(1)
Ejemplo n.º 3
0
def main(argv):

    method    = 'triangular_consistency'    ## or 'bonding_point'
    ramp_type = 'plane'
    save_rampCor = 'yes'
    plot_bonding_points = 'yes'
  
    ##### Check Inputs
    if len(sys.argv)>2:
        try: opts, args = getopt.getopt(argv,'h:f:m:x:y:o:t:',['ramp=','no-ramp-save'])
        except getopt.GetoptError:  print 'Error while getting args';  Usage(); sys.exit(1)
  
        for opt,arg in opts:
            if   opt in ['-h','--help']:    Usage(); sys.exit()
            elif opt in '-f':    File     = arg
            elif opt in '-m':    maskFile = arg
            elif opt in '-o':    outName  = arg
            elif opt in '-x':    x = [int(i) for i in arg.split(',')];    method = 'bonding_point'
            elif opt in '-y':    y = [int(i) for i in arg.split(',')];    method = 'bonding_point'
            elif opt in '-t':    templateFile = arg
            elif opt in '--ramp'         :  ramp_type    = arg.lower()
            elif opt in '--no-ramp-save' :  save_rampCor = 'no'
  
    elif len(sys.argv)==2:
        if argv[0] in ['-h','--help']:    Usage();  sys.exit()
        elif os.path.isfile(argv[0]):     File = argv[0];  maskFile = argv[1]
        else:    print 'Input file does not existed: '+argv[0];  sys.exit(1)
  
    else:  Usage(); sys.exit(1)
  
    ##### Check template file
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except: pass
  
    try:
        yx = [int(i) for i in templateContents['pysar.unwrapError.yx'].split(',')]
        x = yx[1::2]
        y = yx[0::2]
        method = 'bonding_point'
    except: pass

    ##### Read Mask File 
    ## Priority:
    ## Input mask file > pysar.mask.file > existed Modified_Mask.h5 > existed Mask.h5
    try:       maskFile
    except:
        try:    maskFile = templateContents['pysar.mask.file']
        except:
            if   os.path.isfile('Modified_Mask.h5'):  maskFile = 'Modified_Mask.h5'
            elif os.path.isfile('Mask.h5'):           maskFile = 'Mask.h5'
            else: print 'No mask found!'; sys.exit(1)
    try:    Mask,Matr = readfile.read(maskFile);   print 'mask: '+maskFile
    except: print 'Can not open mask file: '+maskFile; sys.exit(1)
  
    ##### Output file name
    ext = os.path.splitext(File)[1]
    try:    outName
    except: outName = File.split('.')[0]+'_unwCor'+ext
  
    print '\n**************** Unwrapping Error Correction ******************'

    ####################  Triangular Consistency (Phase Closure)  ####################
    if method == 'triangular_consistency':
        print 'Phase unwrapping error correction using Triangular Consistency / Phase Closure'
  
        h5file=h5py.File(File)
        ifgramList = h5file['interferograms'].keys()
        sx = int(h5file['interferograms'][ifgramList[0]].attrs['WIDTH'])
        sy = int(h5file['interferograms'][ifgramList[0]].attrs['FILE_LENGTH'])
        curls,Triangles,C=ut.get_triangles(h5file)
        A,B = ut.design_matrix(h5file)   
        ligram,lv=np.shape(B)
        lcurls=np.shape(curls)[0]
        print 'Number of all triangles: '+  str(lcurls)
        print 'Number of interferograms: '+ str(ligram)
        #print curls
  
        curlfile='curls.h5'
        if not os.path.isfile(curlfile):
            ut.generate_curls(curlfile,h5file,Triangles,curls)
         
        thr=0.50
        curls=np.array(curls);   n1=curls[:,0];   n2=curls[:,1];   n3=curls[:,2]
  
        numPixels=sy*sx
        print 'reading interferograms...'   
        data = np.zeros((ligram,numPixels),np.float32)
        for ni in range(ligram):
            dset=h5file['interferograms'][ifgramList[ni]].get(ifgramList[ni])
            d = dset[0:dset.shape[0],0:dset.shape[1]]
            data[ni] = d.flatten(1)   
  
        print np.shape(data)
        print 'reading curls ...' 
        h5curl=h5py.File(curlfile)
        curlList=h5curl['interferograms'].keys()
        curlData = np.zeros((lcurls,numPixels),np.float32)
        for ni in range(lcurls):
            dset=h5curl['interferograms'][curlList[ni]].get(curlList[ni])
            d = dset[0:dset.shape[0],0:dset.shape[1]]
            curlData[ni] = d.flatten(1)
        pi=np.pi
        EstUnwrap=np.zeros((ligram,numPixels),np.float32)
  
        #try:
        #    maskFile=argv[1]
        #    h5Mask=h5py.File(maskFile)
        #    dset = h5Mask['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        #except:
        #    dset = h5file['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        
        Mask=Mask.flatten(1)

        from scipy.linalg import pinv as pinv
        for ni in range(numPixels):
            #dU = np.zeros([ligram,1])
            #print np.shape(dU)
            #print np.shape(data[:,ni])
  
            if Mask[ni]==1:
                dU = data[:,ni]
                #nan_ndx = dataPoint == 0.
                unwCurl = np.array(curlData[:,ni])
                #print unwCurl
  
                ind  = np.abs(unwCurl)>=thr;      N1 =n1[ind];      N2 =n2[ind];      N3 =n3[ind]
                indC = np.abs(unwCurl)< thr;      Nc1=n1[indC];     Nc2=n2[indC];     Nc3=n3[indC]
  
                N =np.hstack([N1, N2, N3]);       UniN =np.unique(N)
                Nc=np.hstack([Nc1,Nc2,Nc3]);      UniNc=np.unique(Nc)
  
                inter=list(set(UniNc) & set(UniN)) # intersetion
                UniNc= list(UniNc)
                for x in inter:
                    UniNc.remove(x)
  
                D=np.zeros([len(UniNc),ligram])
                for i in range(len(UniNc)):
                    D[i,UniNc[i]]=1
  
                AAA=np.vstack([-2*pi*C,D])
                #AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])])
                #AAA2=np.hstack([-2*pi*np.eye(ligram),B]) 
                #AAAA=np.vstack([AAA1,AAA2])
                AAAA=np.vstack([AAA,0.25*np.eye(ligram)])
  
                #print '************************'
                #print np.linalg.matrix_rank(C)
                #print np.linalg.matrix_rank(AAA) 
                #print np.linalg.matrix_rank(AAAA)
                #print '************************'
  
                #LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU)
                #ind=np.isnan(AAA)
                #M1=pinv(AAA)      
                #M=np.dot(M1,LLL)
                #EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi
  
                ##########
                # with Tikhonov regularization:
                AAAA=np.vstack([AAA,0.25*np.eye(ligram)])
                LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0])) + list(np.zeros(ligram))
                ind=np.isnan(AAAA)
                M1=pinv(AAAA)
                M=np.dot(M1,LLL)
                EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi
                #print M[0:ligram]
                #print np.round(M[0:ligram])
  
            else:
                EstUnwrap[:,ni]=np.zeros([ligram])
                if not np.remainder(ni,10000): print 'Processing point: %7d of %7d ' % (ni,numPixels)

        ##### Output
        dataCor = data+EstUnwrap
        unwCorFile=File.replace('.h5','')+'_unwCor.h5';  print 'writing >>> '+unwCorFile
        h5unwCor=h5py.File(unwCorFile,'w') 
        gg = h5unwCor.create_group('interferograms') 
        for i in range(ligram):
            group = gg.create_group(ifgramList[i])
            dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i,:],[sx,sy]).T, compression='gzip')
            for key, value in h5file['interferograms'][ifgramList[i]].attrs.iteritems():
                group.attrs[key] = value
  
        try:
            MASK=h5file['mask'].get('mask')
            gm = h5unwCor.create_group('mask')
            dset = gm.create_dataset('mask', data=MASK, compression='gzip')
        except: pass
  
        h5unwCor.close()
        h5file.close()
        h5curl.close() 


    ####################  Bonding Points (Spatial Continuity)  ####################
    elif method == 'bonding_point':
        print 'Phase unwrapping error correction using Bonding Points / Spatial Continuity'
  
        ##### Read Bridge Points Info
        try:
            x
            y
            if len(x) != len(y) or np.mod(len(x),2) != 0:
                print 'Wrong number of bridge points input: '+str(len(x))+' for x, '+str(len(y))+' for y'
                Usage();  sys.exit(1)
        except: print 'Error in reading bridge points info!';  Usage();  sys.exit(1)
        for i in range(0,len(x)):
            if Mask[y[i],x[i]] == 0:
                print '\nERROR: Connecting point ('+str(y[i])+','+str(x[i])+') is out of masked area! Select them again!\n'
                sys.exit(1)
  
        print 'Number of bonding point pairs: '+str(len(x)/2)
        print 'Bonding points coordinates:\nx: '+str(x)+'\ny: '+str(y)
  
        ## Plot Connecting Pair of Points
        if plot_bonding_points == 'yes':
            point_yx = ''
            line_yx  = ''
            n_bridge = len(x)/2
            for i in range(n_bridge):
                pair_yx = str(y[2*i])+','+str(x[2*i])+','+str(y[2*i+1])+','+str(x[2*i+1])
                if not i == n_bridge-1:
                    point_yx += pair_yx+','
                    line_yx  += pair_yx+';'
                else:
                    point_yx += pair_yx
                    line_yx  += pair_yx

            try:
                plot_cmd = 'view.py --point-yx="'+point_yx+'" --line-yx="'+line_yx+\
                           '" --nodisplay -o bonding_points.png -f '+maskFile
                print plot_cmd
                os.system(plot_cmd)
            except: pass


        ##### Ramp Info
        ramp_mask = Mask==1
        print 'estimate phase ramp during the correction'
        print 'ramp type: '+ramp_type
        if save_rampCor == 'yes':
            outName_ramp = os.path.basename(outName).split(ext)[0]+'_'+ramp_type+ext
  
        ########## PySAR ##########
        if ext == '.h5':
            ##### Read
            try:     h5file=h5py.File(File,'r')
            except:  print 'ERROR: Cannot open input file: '+File; sys.exit(1)
            k=h5file.keys()
            if 'interferograms' in k: k[0] = 'interferograms';  print 'Input file is '+k[0]
            else: print 'Input file - '+File+' - is not interferograms.';  Usage();  sys.exit(1)
            igramList = h5file[k[0]].keys()
            igramList = sorted(igramList)
  
            #### Write
            h5out = h5py.File(outName,'w')
            gg = h5out.create_group(k[0])
            print 'writing >>> '+outName
  
            if save_rampCor == 'yes':
                h5out_ramp = h5py.File(outName_ramp,'w')
                gg_ramp = h5out_ramp.create_group(k[0])
                print 'writing >>> '+outName_ramp
  
            ##### Loop
            print 'Number of interferograms: '+str(len(igramList))
            for igram in igramList:
                print igram
                data = h5file[k[0]][igram].get(igram)[:]
  
                data_ramp,ramp = rm.remove_data_surface(data,ramp_mask,ramp_type)
                #ramp = data_ramp - data
                data_rampCor = phase_bonding(data_ramp,Mask,x,y)
                dataCor = data_rampCor - ramp
  
                group = gg.create_group(igram)
                dset = group.create_dataset(igram, data=dataCor, compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key]=value
  
                if save_rampCor == 'yes':
                    group_ramp = gg_ramp.create_group(igram)
                    dset = group_ramp.create_dataset(igram, data=data_rampCor, compression='gzip')
                    for key, value in h5file[k[0]][igram].attrs.iteritems():
                        group_ramp.attrs[key]=value
  
            try:
                mask = h5file['mask'].get('mask');
                gm = h5out.create_group('mask')
                dset = gm.create_dataset('mask', data=mask[0:mask.shape[0],0:mask.shape[1]], compression='gzip')
            except: print 'no mask group found.'
  
            h5file.close()
            h5out.close()
            if save_rampCor == 'yes':
                h5out_ramp.close()

        ########## ROI_PAC ##########
        elif ext == '.unw':
            print 'Input file is '+ext
            a,data,atr = readfile.read_float32(File);
  
            data_ramp,ramp = rm.remove_data_surface(data,ramp_mask,ramp_type)
            #ramp = data_ramp - data
            data_rampCor = phase_bonding(data_ramp,Mask,x,y)
            dataCor = data_rampCor - ramp
  
            writefile.write(dataCor, atr, outName)
            if save_rampCor == 'yes':
                writefile.write(data_rampCor,atr,outName_ramp)
  
        else: print 'Un-supported file type: '+ext;  Usage();  sys.exit(1)