Exemplo n.º 1
0
def main(argv):
    try:
        file = argv[0]
    except:
        usage()
        sys.exit(1)

    outfile = os.path.splitext(file)[0] + '_wrap' + os.path.splitext(file)[1]
    one_cycle = 2 * np.pi
    one_cycle = 0.05

    atr = readfile.read_attribute(file)
    k = atr['FILE_TYPE']

    if k in ['interferograms', 'coherence', 'wrapped', 'timeseries']:
        h5 = h5py.File(file, 'r')
        epochList = sorted(h5[k].keys())
        epoch_num = len(epochList)
        prog_bar = ptime.progressBar(maxValue=epoch_num)

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

        if k in ['interferograms', 'coherence', 'wrapped']:
            date12_list = ptime.list_ifgram2date12(epochList)
            print('number of interferograms: ' + str(len(epochList)))
            for i in range(epoch_num):
                epoch = epochList[i]
                data = h5[k][epoch].get(epoch)[:]

                data_wrap = rewrap(data)

                gg = group.create_group(epoch)
                dset = gg.create_dataset(epoch, data=data_wrap)
                for key, value in h5[k][epoch].attrs.items():
                    gg.attrs[key] = value
                prog_bar.update(i + 1, suffix=date12_list[i])

        elif k == 'timeseries':
            print('number of acquisitions: ' + str(len(epochList)))
            for i in range(epoch_num):
                epoch = epochList[i]
                data = h5[k].get(epoch)[:]

                data_wrap = rewrap(data, one_cycle)

                dset = group.create_dataset(epoch, data=data_wrap)
                prog_bar.update(i + 1, suffix=epoch)
            for key, value in h5[k].attrs.items():
                group.attrs[key] = value

        h5.close()
        h5out.close()
        prog_bar.close()

    print('Done.')
    return outfile
Exemplo n.º 2
0
def main(argv):

    # Inputs
    try:
        ifgram_file = argv[0]
        timeseries_file = argv[1]
    except:
        usage()
        sys.exit(1)

    try:
        outfile = argv[2]
    except:
        outfile = 'recon_'+ifgram_file

    atr = readfile.read_attribute(timeseries_file)
    length = int(atr['LENGTH'])
    width = int(atr['WIDTH'])

    # Read time-series file
    print('loading timeseries ...')
    h5ts = h5py.File(timeseries_file, 'r')
    date_list = sorted(h5ts['timeseries'].keys())
    date_num = len(date_list)
    timeseries = np.zeros((date_num, length*width))

    print('number of acquisitions: '+str(date_num))
    prog_bar = ptime.progressBar(maxValue=date_num)
    for i in range(date_num):
        date = date_list[i]
        d = h5ts['timeseries'].get(date)[:]
        timeseries[i, :] = d.flatten(0)
        prog_bar.update(i+1, suffix=date)
    prog_bar.close()
    h5ts.close()
    del d

    range2phase = -4*np.pi/float(atr['WAVELENGTH'])
    timeseries = range2phase*timeseries

    # Estimate interferograms from timeseries
    print('estimating interferograms from timeseries using design matrix from input interferograms')
    A, B = ut.design_matrix(ifgram_file)
    p = -1*np.ones([A.shape[0], 1])
    Ap = np.hstack((p, A))
    estData = np.dot(Ap, timeseries)
    del timeseries

    # Write interferograms file
    print('writing >>> '+outfile)
    h5 = h5py.File(ifgram_file, 'r')
    ifgram_list = sorted(h5['interferograms'].keys())
    ifgram_num = len(ifgram_list)
    date12_list = ptime.list_ifgram2date12(ifgram_list)

    h5out = h5py.File(outfile, 'w')
    group = h5out.create_group('interferograms')

    print('number of interferograms: '+str(ifgram_num))
    prog_bar = ptime.progressBar(maxValue=ifgram_num)
    for i in range(ifgram_num):
        ifgram = ifgram_list[i]
        data = np.reshape(estData[i, :], (length, width))

        gg = group.create_group(ifgram)
        dset = gg.create_dataset(ifgram, data=data)
        for key, value in h5['interferograms'][ifgram].attrs.items():
            gg.attrs[key] = value
        prog_bar.update(i+1, suffix=date12_list[i])
    prog_bar.close()
    h5.close()
    h5out.close()
    print('Done.')
    return outfile
Exemplo n.º 3
0
def main(iargs=None):
    inps = cmd_line_parse(iargs)

    atr = readfile.read_attribute(inps.velocity_file)
    length = int(atr['LENGTH'])
    width = int(atr['WIDTH'])

    # Check subset input
    if inps.subset_y:
        inps.subset_y = sorted(inps.subset_y)
        print('subset in y/azimuth direction: ' + str(inps.subset_y))
    else:
        inps.subset_y = [0, length]

    if inps.subset_x:
        inps.subset_x = sorted(inps.subset_x)
        print('subset in x/range direction: ' + str(inps.subset_x))
    else:
        inps.subset_x = [0, width]
    y0, y1 = inps.subset_y
    x0, x1 = inps.subset_x

    # Read velocity/rate
    velocity = readfile.read(inps.velocity_file)[0]
    print('read velocity file: ' + inps.velocity_file)

    k = 'interferograms'
    h5 = h5py.File(inps.ifgram_file, 'r')
    ifgram_list = sorted(h5[k].keys())
    ifgram_num = len(ifgram_list)
    date12_list = ptime.list_ifgram2date12(ifgram_list)
    print('number of interferograms: ' + str(ifgram_num))

    # Select interferograms with unwrapping error
    if inps.percentage > 0.0:
        mask = readfile.read(inps.mask_file, datasetName='mask')[0]
        print('read mask for pixels with unwrapping error from file: ' +
              inps.mask_file)

        unw_err_ifgram_num = int(np.rint(inps.percentage * ifgram_num))
        unw_err_ifgram_idx = random.sample(list(range(ifgram_num)),
                                           unw_err_ifgram_num)
        unw_err_ifgram_list = [ifgram_list[i] for i in unw_err_ifgram_idx]
        unw_err_date12_list = [date12_list[i] for i in unw_err_ifgram_idx]
        print(
            'randomly choose the following %d interferograms with unwrapping error'
            % unw_err_ifgram_num)
        print(unw_err_date12_list)

        unit_unw_err = 2.0 * np.pi * mask
    else:
        unw_err_ifgram_list = []

    # Generate simulated interferograms
    m_dates = ptime.yyyymmdd([i.split('_')[0] for i in date12_list])
    s_dates = ptime.yyyymmdd([i.split('_')[1] for i in date12_list])
    range2phase = -4.0 * np.pi / float(atr['WAVELENGTH'])

    print('writing simulated interferograms file: ' + inps.outfile)
    h5out = h5py.File(inps.outfile, 'w')
    group = h5out.create_group('interferograms')
    for i in range(ifgram_num):
        ifgram = ifgram_list[i]
        # Get temporal baseline in years
        t1 = datetime.datetime(*time.strptime(m_dates[i], "%Y%m%d")[0:5])
        t2 = datetime.datetime(*time.strptime(s_dates[i], "%Y%m%d")[0:5])
        dt = (t2 - t1)
        dt = float(dt.days) / 365.25

        # Simuated interferograms with unwrap error
        unw = velocity * dt * range2phase
        if ifgram in unw_err_ifgram_list:
            rand_int = random.sample(list(range(1, 10)), 1)[0]
            unw += rand_int * unit_unw_err
            print(ifgram + '  - add unwrapping error of %d*2*pi' % rand_int)
        else:
            print(ifgram)

        gg = group.create_group(ifgram)
        dset = gg.create_dataset(ifgram, data=unw[y0:y1, x0:x1])

        for key, value in h5[k][ifgram].attrs.items():
            gg.attrs[key] = value
        if ifgram in unw_err_ifgram_list:
            gg.attrs['unwrap_error'] = 'yes'
        else:
            gg.attrs['unwrap_error'] = 'no'
        gg.attrs['LENGTH'] = y1 - y0
        gg.attrs['WIDTH'] = x1 - x0
    h5.close()
    h5out.close()
    print('Done.')
    return inps.outfile
Exemplo n.º 4
0
def main(argv):
    inps = cmdLineParse()

    if inps.timeseries_file:
        inps.timeseries_file = ut.get_file_list([inps.timeseries_file])[0]
        atr = readfile.read_attribute(inps.timeseries_file)
        k = atr['FILE_TYPE']
        if 'ref_y' not in list(atr.keys()) and inps.ref_yx:
            print('No reference info found in input file, use input ref_yx: ' +
                  str(inps.ref_yx))
            atr['ref_y'] = inps.ref_yx[0]
            atr['ref_x'] = inps.ref_yx[1]

    #****reading incidence angle file***/
    if os.path.isfile(inps.inc_angle):
        inps.inc_angle = readfile.read(inps.inc_angle,
                                       epoch='incidenceAngle')[0]
        inps.inc_angle = np.nan_to_num(inps.inc_angle)
    else:
        inps.inps.inc_angle = float(inps.inc_angle)
        print('incidence angle: ' + str(inps.inc_angle))
    cinc = np.cos(inps.inc_angle * np.pi / 180.0)

    #****look up file****/
    if inps.lookup_file:
        inps.lookup_file = ut.get_file_list(
            [inps.lookup_file])[0]  #'geomap_32rlks_tight.trans'

    #****GACOS****/
    delay_source = 'GACOS'
    # Get weather directory
    if not inps.GACOS_dir:
        if inps.timeseries_file:
            inps.GACOS_dir = os.path.dirname(
                os.path.abspath(inps.timeseries_file)) + '/../WEATHER/GACOS'
        elif inps.lookup_file:
            inps.GACOS_dir = os.path.dirname(os.path.abspath(
                inps.lookup_file)) + '/../WEATHER/GACOS'
        else:
            inps.GACOS_dir = os.path.abspath(os.getcwd())

    print('Store weather data into directory: ' + inps.GACOS_dir)

    #source_dir=os.path.dirname(os.path.abspath('timeseries_file'))+'/Agung/GACOS/data';print source_dir
    #os.makedirs(GACOS_dir)  -----------------------------------------------add part to copy/download weather data------#
    #----get date list-----#
    if not inps.date_list_file:
        print('read date list info from: ' + inps.timeseries_file)
        h5 = h5py.File(inps.timeseries_file, 'r')
        if 'timeseries' in list(h5.keys()):
            date_list = sorted(h5[k].keys())
        elif k in ['interferograms', 'coherence', 'wrapped']:
            ifgram_list = sorted(h5[k].keys())
            date12_list = ptime.list_ifgram2date12(ifgram_list)
            m_dates = [i.split('-')[0] for i in date12_list]
            s_dates = [i.split('-')[1] for i in date12_list]
            date_list = ptime.yyyymmdd(sorted(list(set(m_dates + s_dates))))
        else:
            raise ValueError('Un-support input file type:' + k)
        h5.close()
    else:
        date_list = ptime.yyyymmdd(
            np.loadtxt(inps.date_list_file, dtype=str, usecols=(0, )).tolist())
        print('read date list info from: ' + inps.date_list_file)

    #****cheacking availability of delays****/
    print('checking availability of delays')
    delay_file_list = []
    for d in date_list:
        if delay_source == 'GACOS':
            delay_file = inps.GACOS_dir + '/' + d + '.ztd'
        delay_file_list.append(delay_file)
    delay_file_existed = ut.get_file_list(delay_file_list)

    if len(delay_file_existed) == len(date_list):
        print('no missing files')
    else:
        print('no. of date files found:', len(delay_file_existed))
        print('no. of dates:', len(date_list))

    #*****Calculating delays***/
    print('calculating delays')

    length = int(atr['FILE_LENGTH'])
    width = int(atr['WIDTH'])
    #initialise delay files
    date_num = len(date_list)
    trop_ts = np.zeros((date_num, length, width), np.float32)

    #reading wrf files for each epoch and getting delay
    for i in range(date_num):
        delay_file = delay_file_existed[i]
        date = date_list[i]
        print('calculating delay for date', date)
        trop_ts[i] = get_delay(delay_file, atr, inps.lookup_file, cinc)

    print('Delays Calculated')
    # Convert relative phase delay on reference date
    try:
        ref_date = atr['ref_date']
    except:
        ref_date = date_list[0]
    print('convert to relative phase delay with reference date: ' + ref_date)
    ref_idx = date_list.index(ref_date)
    trop_ts -= np.tile(trop_ts[ref_idx, :, :], (date_num, 1, 1))

    ## Write tropospheric delay to HDF5
    tropFile = 'GACOSdelays' + '.h5'
    print('writing >>> %s' % (tropFile))
    h5trop = h5py.File(tropFile, 'w')
    group_trop = h5trop.create_group('timeseries')
    print('number of acquisitions: ' + str(date_num))
    prog_bar = ptime.progress_bar(maxValue=date_num)
    for i in range(date_num):
        date = date_list[i]
        group_trop.create_dataset(date, data=trop_ts[i], compression='gzip')
        prog_bar.update(i + 1, suffix=date)
    prog_bar.close()

    # Write Attributes
    for key, value in atr.items():
        group_trop.attrs[key] = value
    h5trop.close()

    ## Write corrected Time series to HDF5
    if k == 'timeseries':
        if not inps.out_file:
            inps.out_file = os.path.splitext(
                inps.timeseries_file)[0] + '_' + 'GACOS' + '.h5'
        print('writing trop corrected timeseries file %s' % (inps.out_file))
        h5ts = h5py.File(inps.timeseries_file, 'r')
        h5tsCor = h5py.File(inps.out_file, 'w')
        group_tsCor = h5tsCor.create_group('timeseries')
        print('number of acquisitions: ' + str(date_num))
        prog_bar = ptime.progress_bar(maxValue=date_num)
        for i in range(date_num):
            date = date_list[i]
            print(date)
            ts = h5ts['timeseries'].get(date)[:]
            group_tsCor.create_dataset(date,
                                       data=ts - trop_ts[i],
                                       compression='gzip')
            prog_bar.update(i + 1, suffix=date)
        prog_bar.close()
        h5ts.close()
        # Write Attributes
        for key, value in atr.items():
            group_tsCor.attrs[key] = value
        h5tsCor.close()
        print('delays written to %s' % (inps.out_file))

    print('finished')
    return inps.out_file
Exemplo n.º 5
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, datasetName='mask')[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):
            pairs_yx = '{},{},{},{}'.format(y[2*i],
                                            x[2*i],
                                            y[2*i+1],
                                            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.progressBar(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 = deramp.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)
            for key, value in h5[k][ifgram].attrs.items():
                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)
                for key, value in h5[k][ifgram].attrs.items():
                    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 = deramp.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,
                                          out_file=ifgram_cor_file,
                                          metadata=atr)
        if save_cor_deramp_file:
            print('writing >>> '+ifgram_cor_deramp_file)
            ifgram_cor_deramp_file = writefile.write(data_derampCor,
                                                     out_file=ifgram_cor_deramp_file,
                                                     metadata=atr)

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

    return ifgram_cor_file, ifgram_cor_deramp_file