예제 #1
0
def main(argv):

    try:  file=argv[0]
    except:  Usage();sys.exit(1)
 
    h5file=h5py.File(file)
    curls,Triangles,C=ut.get_triangles(h5file)
 
    try:     curlfile=argv[1]
    except:  curlfile='curls.h5'
 
    if not os.path.isfile(curlfile):
        ut.generate_curls(curlfile,h5file,Triangles,curls)
    else:
        print curlfile + " already exists!"
예제 #2
0
def main(argv):

    try:
        file = argv[0]
    except:
        usage()
        sys.exit(1)

    h5file = h5py.File(file)
    curls, Triangles, C = ut.get_triangles(h5file)

    try:
        curlfile = argv[1]
    except:
        curlfile = 'curls.h5'

    if not os.path.isfile(curlfile):
        ut.generate_curls(curlfile, h5file, Triangles, curls)
    else:
        print curlfile + " already exists!"
예제 #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)

        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)
예제 #4
0
def unwrap_error_correction_phase_closure(ifgram_file,
                                          mask_file,
                                          ifgram_cor_file=None):
    '''Correct unwrapping errors in network of interferograms using phase closure.
    Inputs:
        ifgram_file     - string, name/path of interferograms file
        mask_file       - string, name/path of mask file to mask the pixels to be corrected
        ifgram_cor_file - string, optional, name/path of corrected interferograms file
    Output:
        ifgram_cor_file
    Example:
        'unwrapIfgram_unwCor.h5' = unwrap_error_correction_phase_closure('Seeded_unwrapIfgram.h5','mask.h5')
    '''
    print 'read mask from file: ' + mask_file
    mask = readfile.read(mask_file)[0].flatten(1)

    atr = readfile.read_attribute(ifgram_file)
    length = int(atr['FILE_LENGTH'])
    width = int(atr['WIDTH'])
    k = atr['FILE_TYPE']
    pixel_num = length * width

    # Check reference pixel
    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!'
        )

    h5 = h5py.File(ifgram_file, 'r')
    ifgram_list = sorted(h5[k].keys())
    ifgram_num = len(ifgram_list)

    ##### Prepare curls
    curls, Triangles, C = ut.get_triangles(h5)
    curl_num = np.shape(curls)[0]
    print 'Number of      triangles: ' + str(curl_num)

    curl_file = 'curls.h5'
    if not os.path.isfile(curl_file):
        print 'writing >>> ' + curl_file
        ut.generate_curls(curl_file, h5, Triangles, curls)

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

    print 'reading interferograms...'
    print 'Number of interferograms: ' + str(ifgram_num)
    data = np.zeros((ifgram_num, pixel_num), np.float32)
    prog_bar = ptime.progress_bar(maxValue=ifgram_num)
    for ni in range(ifgram_num):
        ifgram = ifgram_list[ni]
        d = h5[k][ifgram].get(ifgram)[:].flatten(1)
        data[ni, :] = d
        prog_bar.update(ni + 1)
    prog_bar.close()

    print 'reading curls ...'
    print 'number of culrs: ' + str(curl_num)
    h5curl = h5py.File(curl_file, 'r')
    curl_list = sorted(h5curl[k].keys())
    curl_data = np.zeros((curl_num, pixel_num), np.float32)
    prog_bar = ptime.progress_bar(maxValue=curl_num)
    for ni in range(curl_num):
        d = h5curl[k][curl_list[ni]].get(curl_list[ni])[:].flatten(1)
        curl_data[ni, :] = d.flatten(1)
        prog_bar.update(ni + 1)
    prog_bar.close()
    h5curl.close()

    print 'estimating unwrapping error pixel by pixel ...'
    EstUnwrap = np.zeros((ifgram_num, pixel_num), np.float32)
    prog_bar = ptime.progress_bar(maxValue=pixel_num)
    for ni in range(pixel_num):
        if mask[ni] == 1:
            dU = data[:, ni]
            unwCurl = np.array(curl_data[:, ni])

            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), ifgram_num])
            for i in range(len(UniNc)):
                D[i, UniNc[i]] = 1

            AAA = np.vstack([-2 * np.pi * C, D])
            AAAA = np.vstack([AAA, 0.25 * np.eye(ifgram_num)])

            ##########
            # with Tikhonov regularization:
            LLL = list(np.dot(C, dU)) + list(np.zeros(
                np.shape(UniNc)[0])) + list(np.zeros(ifgram_num))
            ind = np.isnan(AAAA)
            M1 = pinv(AAAA)
            M = np.dot(M1, LLL)
            EstUnwrap[:, ni] = np.round(M[0:ifgram_num]) * 2.0 * np.pi
        prog_bar.update(ni + 1, suffix='%s/%d' % (ni, pixel_num))
    prog_bar.close()

    dataCor = data + EstUnwrap

    ##### Output
    if not ifgram_cor_file:
        ifgram_cor_file = os.path.splitext(ifgram_file)[0] + '_unwCor.h5'
    print 'writing >>> ' + ifgram_cor_file
    h5unwCor = h5py.File(ifgram_cor_file, 'w')
    gg = h5unwCor.create_group(k)

    prog_bar = ptime.progress_bar(maxValue=ifgram_num)
    for i in range(ifgram_num):
        ifgram = ifgram_list[i]
        group = gg.create_group(ifgram)
        dset = group.create_dataset(ifgram,
                                    data=np.reshape(dataCor[i, :],
                                                    [width, length]).T,
                                    compression='gzip')
        for key, value in h5[k][ifgram].attrs.iteritems():
            group.attrs[key] = value
        prog_bar.update(i + 1)
    prog_bar.close()
    h5unwCor.close()
    h5.close()
    return ifgram_cor_file
예제 #5
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)