示例#1
0
 def test_open_ifg_from_dataset(self):
     """
     Test showing open() can not be used for Ifg created with
     gdal.Dataset object as Dataset has already been read in
     """
     paths = [self.ifg.data_path]
     mlooked_phase_data = prepifg_helper.prepare_ifgs(
         paths,
         crop_opt=prepifg_helper.ALREADY_SAME_SIZE,
         xlooks=2,
         ylooks=2,
         write_to_disc=False)
     mlooked = [Ifg(m[1]) for m in mlooked_phase_data]
     self.assertRaises(RasterException, mlooked[0].open)
示例#2
0
 def _get_r_dist(ifg_path):
     """
     Get RDIst class object
     """
     ifg = Ifg(ifg_path)
     ifg.open()
     r_dist = vcm_module.RDist(ifg)()
     ifg.close()
     return r_dist
示例#3
0
文件: prepifg.py 项目: sixy6e/PyRate
def __write_geometry_files(params: dict, exts: Tuple[float, float, float,
                                                     float], transform,
                           ifg_path: str) -> None:
    """
    Calculate geometry and save to geotiff files using the information in the
    first interferogram in the stack, i.e.:
    - rdc_azimuth.tif (azimuth radar coordinate at each pixel)
    - rdc_range.tif (range radar coordinate at each pixel)
    - azimuth_angle.tif (satellite azimuth angle at each pixel)
    - incidence_angle.tif (incidence angle at each pixel)
    - look_angle.tif (look angle at each pixel)
    """
    ifg = Ifg(ifg_path)
    ifg.open(readonly=True)

    # calculate per-pixel lon/lat
    lon, lat = geometry.get_lonlat_coords(ifg)

    # not currently implemented for ROIPAC data which breaks some tests
    # if statement can be deleted once ROIPAC is deprecated from PyRate
    if ifg.meta_data[ifc.PYRATE_INSAR_PROCESSOR] == 'ROIPAC':
        log.warning("Geometry calculations are not implemented for ROI_PAC")
        return

    # get geometry information and save radar coordinates and angles to tif files
    # using metadata of the first image in the stack
    # get pixel values of crop (needed to crop lookup table file)
    # pixel extent of cropped area (original IFG input)
    xmin, ymax = convert_geographic_coordinate_to_pixel_value(
        exts[0], exts[1], transform)
    xmax, ymin = convert_geographic_coordinate_to_pixel_value(
        exts[2], exts[3], transform)
    # xmin, xmax: columns of crop
    # ymin, ymax: rows of crop

    # calculate per-pixel radar coordinates
    az, rg = geometry.calc_radar_coords(ifg, params, xmin, xmax, ymin, ymax)

    # Read height data from DEM
    dem_file = params[C.DEM_FILE_PATH].sampled_path
    dem = DEM(dem_file)
    # calculate per-pixel look angle (also calculates and saves incidence and azimuth angles)
    lk_ang, inc_ang, az_ang, rg_dist = geometry.calc_pixel_geometry(
        ifg, rg, lon.data, lat.data, dem.data)

    # save radar coordinates and angles to geotiff files
    combinations = zip([az, rg, lk_ang, inc_ang, az_ang, rg_dist],
                       C.GEOMETRY_OUTPUT_TYPES)
    shared.iterable_split(__parallelly_write, combinations, params, ifg_path)
示例#4
0
def dem_or_ifg(data_path):
    """
    Returns an Ifg or DEM class object from input geotiff file.

    :param str data_path: file path name

    :return: Interferogram or DEM object from input file
    :rtype: Ifg or DEM class object
    """
    ds = gdal.Open(data_path)
    md = ds.GetMetadata()
    if ifc.MASTER_DATE in md:  # ifg
        return Ifg(data_path)
    else:
        return DEM(data_path)
示例#5
0
    def test_same_size_multilooking(self):
        ifgs = same_exts_ifgs()
        ifg_data_paths = [d.data_path for d in ifgs]
        xlooks = ylooks = 2
        prepare_ifgs(ifg_data_paths, ALREADY_SAME_SIZE, xlooks, ylooks)

        looks_paths = [mlooked_path(d, looks=xlooks, crop_out=ALREADY_SAME_SIZE)
                       for d in ifg_data_paths]
        mlooked = [Ifg(i) for i in looks_paths]
        for m in mlooked:
            m.open()
        self.assertEqual(len(mlooked), 2)

        for ifg in mlooked:
            self.assertAlmostEqual(ifg.x_step, xlooks * self.xs)
            self.assertAlmostEqual(ifg.x_step, ylooks * self.xs)
示例#6
0
    def _process_phase_sum(ifg_paths):
        if isinstance(ifg_paths[0], Ifg):
            proc_ifgs = ifg_paths
        else:
            proc_ifgs = [Ifg(ifg_path) for ifg_path in ifg_paths]

        for ifg in proc_ifgs:
            if not ifg.is_open:
                ifg.open(readonly=False)

        ifg_phase_data_sum = np.zeros(proc_ifgs[0].shape, dtype=np.float32)

        for ifg in proc_ifgs:
            ifg_phase_data_sum += ifg.phase_data

        return ifg_phase_data_sum
示例#7
0
def _ref_phase_estimation(ifg_paths, params, refpx, refpy):
    """
    Wrapper for reference phase estimation.
    """
    log.info("Calculating reference phase and correcting each interferogram")
    if len(ifg_paths) < 2:
        raise rpe.ReferencePhaseError(
            "At least two interferograms required for reference phase correction ({len_ifg_paths} "
            "provided).".format(len_ifg_paths=len(ifg_paths)))

    if mpiops.run_once(shared.check_correction_status, ifg_paths,
                       ifc.PYRATE_REF_PHASE):
        log.debug('Finished reference phase correction')
        return

    if params[cf.REF_EST_METHOD] == 1:
        ref_phs = rpe.est_ref_phase_method1(ifg_paths, params)
    elif params[cf.REF_EST_METHOD] == 2:
        ref_phs = rpe.est_ref_phase_method2(ifg_paths, params, refpx, refpy)
    else:
        raise rpe.ReferencePhaseError("No such option, use '1' or '2'.")

    # Save reference phase numpy arrays to disk.
    ref_phs_file = os.path.join(params[cf.TMPDIR], 'ref_phs.npy')
    if mpiops.rank == MASTER_PROCESS:
        collected_ref_phs = np.zeros(len(ifg_paths), dtype=np.float64)
        process_indices = mpiops.array_split(range(len(ifg_paths)))
        collected_ref_phs[process_indices] = ref_phs
        for r in range(1, mpiops.size):
            process_indices = mpiops.array_split(range(len(ifg_paths)), r)
            this_process_ref_phs = np.zeros(shape=len(process_indices),
                                            dtype=np.float64)
            mpiops.comm.Recv(this_process_ref_phs, source=r, tag=r)
            collected_ref_phs[process_indices] = this_process_ref_phs
        np.save(file=ref_phs_file, arr=collected_ref_phs)
    else:
        mpiops.comm.Send(ref_phs, dest=MASTER_PROCESS, tag=mpiops.rank)
    log.debug('Finished reference phase correction')

    # Preserve old return value so tests don't break.
    if isinstance(ifg_paths[0], Ifg):
        ifgs = ifg_paths
    else:
        ifgs = [Ifg(ifg_path) for ifg_path in ifg_paths]
    mpiops.comm.barrier()
    return ref_phs, ifgs
示例#8
0
文件: prepifg.py 项目: sixy6e/PyRate
def __calc_coherence_stats(params, ifg_path):
    coherence_files_multi_paths = params[C.COHERENCE_FILE_PATHS]
    sampled_paths = [c.sampled_path for c in coherence_files_multi_paths]
    ifgs = [Ifg(s) for s in sampled_paths]
    for i in ifgs:
        i.open()
    phase_data = np.stack([i.phase_data for i in ifgs])
    coh_stats = Configuration.coherence_stats(params)

    for stat_func, out_type in zip(
        [np.nanmedian, np.nanmean, np.nanstd],
        [ifg.COH_MEDIAN, ifg.COH_MEAN, ifg.COH_STD]):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            arr = stat_func(phase_data, axis=0)
        arr[arr == 0.0] = np.nan  # convert exact zeros (no-data) to NaN
        dest = coh_stats[out_type]
        __save_geom_files(ifg_path, dest, arr, out_type)
示例#9
0
文件: orbital.py 项目: wzmucas/PyRate
def remove_orbital_error(ifgs: Iterable,
                         params: dict,
                         preread_ifgs=None) -> None:
    """
    Wrapper function for PyRate orbital error removal functionality.

    NB: the ifg data is modified in situ, rather than create intermediate
    files. The network method assumes the given ifgs have already been reduced
    to a minimum spanning tree network.

    :param list ifgs: List of interferograms class objects
    :param dict params: Dictionary containing configuration parameters
    :param dict preread_ifgs: Dictionary containing information specifically
        for MPI jobs (optional)

    :return: None - interferogram phase data is updated and saved to disk
    """

    ifg_paths = [i.data_path
                 for i in ifgs] if isinstance(ifgs[0], Ifg) else ifgs

    mlooked = None
    # mlooking is not necessary for independent correction
    # can use multiple procesing if write_to_disc=True
    if params[cf.ORBITAL_FIT_METHOD] == NETWORK_METHOD:
        mlooked_dataset = prepifg_helper.prepare_ifgs(
            ifg_paths,
            crop_opt=prepifg_helper.ALREADY_SAME_SIZE,
            xlooks=params[cf.ORBITAL_FIT_LOOKS_X],
            ylooks=params[cf.ORBITAL_FIT_LOOKS_Y],
            thresh=params[cf.NO_DATA_AVERAGING_THRESHOLD],
            write_to_disc=False)
        mlooked = [Ifg(m[1]) for m in mlooked_dataset]

        for m in mlooked:
            m.initialize()
            m.nodata_value = params[cf.NO_DATA_VALUE]
            m.convert_to_nans()
            m.convert_to_mm()

    _orbital_correction(ifgs,
                        params,
                        mlooked=mlooked,
                        preread_ifgs=preread_ifgs)
示例#10
0
def small_data_setup(datafiles=None, is_dir=False):
    """Returns Ifg objs for the files in the small test dir
    input phase data is in radians; these ifgs are in radians - not converted to mm"""
    if is_dir:
        datafiles = glob.glob(join(datafiles, "*.tif"))
    else:
        if datafiles:
            for i, d in enumerate(datafiles):
                datafiles[i] = os.path.join(SML_TEST_TIF, d)
        else:
            datafiles = glob.glob(join(SML_TEST_TIF, "*.tif"))
    datafiles.sort()
    ifgs = [Ifg(i) for i in datafiles]

    for i in ifgs:
        i.open()
        i.nodata_value = 0

    return ifgs
示例#11
0
 def setUp(self):
     from tests.common import small_data_setup
     self.ifgs = small_data_setup()
     self.ifg_paths = [i.data_path for i in self.ifgs]
     params = Configuration(common.TEST_CONF_ROIPAC).__dict__
     self.headers = [
         roipac.roipac_header(i.data_path, params) for i in self.ifgs
     ]
     prepare_ifgs(self.ifg_paths,
                  crop_opt=1,
                  xlooks=1,
                  ylooks=1,
                  headers=self.headers)
     looks_paths = [
         mlooked_path(d, looks=1, crop_out=1) for d in self.ifg_paths
     ]
     self.ifgs_with_nan = [Ifg(i) for i in looks_paths]
     for ifg in self.ifgs_with_nan:
         ifg.open()
示例#12
0
 def __run_once(self):
     ref_phs_file = Configuration.ref_phs_file(self.params)
     correct._copy_mlooked(self.params)
     multi_paths = self.params[cf.INTERFEROGRAM_FILES]
     ifg_paths = [p.tmp_sampled_path for p in multi_paths]
     ifgs = [Ifg(i) for i in ifg_paths]
     self.params[cf.REFX_FOUND], self.params[
         cf.REFY_FOUND] = ref_pixel_calc_wrapper(self.params)
     correct._create_ifg_dict(self.params)
     ref_phase_est_wrapper(self.params)
     for i in ifgs:
         i.open()
     phase_prev = [i.phase_data for i in ifgs]
     # assert ref_ph_file present
     assert ref_phs_file.exists()
     time_written = os.stat(ref_phs_file).st_mtime
     for i in ifgs:
         i.close()
     return phase_prev, time_written
示例#13
0
    def test_nodata(self):
        """Verify NODATA value copied correctly (amplitude band not copied)"""
        xlooks = ylooks = 1
        prepare_ifgs(self.ifg_paths, MINIMUM_CROP, xlooks, ylooks)

        for ex in [self.exp_files[0], self.exp_files[4]]:
            ifg = Ifg(ex)
            ifg.open()
            # NB: amplitude band doesn't have a NODATA value
            self.assertTrue(
                isnan(ifg.dataset.GetRasterBand(1).GetNoDataValue()))
            ifg.close()
        for i in self.ifgs:
            i.close()
示例#14
0
def _apply_aps_correction(ifg_paths: List[str], aps_paths: List[str],
                          params: dict) -> None:
    """
    Function to read and apply (subtract) APS corrections from interferogram data.
    """
    for ifg_path, aps_path in mpiops.array_split(
            list(zip(ifg_paths, aps_paths))):
        # read the APS correction from numpy array
        aps_corr = np.load(aps_path)
        # open the Ifg object
        ifg = Ifg(ifg_path)
        ifg.open(readonly=False)
        # convert NaNs and convert to mm
        nan_and_mm_convert(ifg, params)
        # subtract the correction from the ifg phase data
        ifg.phase_data[~np.isnan(ifg.phase_data
                                 )] -= aps_corr[~np.isnan(ifg.phase_data)]
        # set meta-data tags after aps error correction
        ifg.dataset.SetMetadataItem(ifc.PYRATE_APS_ERROR, ifc.APS_REMOVED)
        # write phase data to disc and close ifg.
        ifg.write_modified_phase()
        ifg.close()
示例#15
0
def _create_ifg_dict(params):
    """
    Save the preread_ifgs dict with information about the ifgs that are
    later used for fast loading of Ifg files in IfgPart class

    :param list dest_tifs: List of destination tifs
    :param dict params: Config dictionary
    :param list tiles: List of all Tile instances

    :return: preread_ifgs: Dictionary containing information regarding
                interferograms that are used later in workflow
    :rtype: dict
    """
    dest_tifs = [ifg_path for ifg_path in params[C.INTERFEROGRAM_FILES]]
    ifgs_dict = {}
    process_tifs = mpiops.array_split(dest_tifs)
    for d in process_tifs:
        ifg = Ifg(d.tmp_sampled_path) # get the writable copy
        ifg.open()
        nan_and_mm_convert(ifg, params)
        ifgs_dict[d.tmp_sampled_path] = PrereadIfg(
            path=d.sampled_path,
            tmp_path=d.tmp_sampled_path,
            nan_fraction=ifg.nan_fraction,
            first=ifg.first,
            second=ifg.second,
            time_span=ifg.time_span,
            nrows=ifg.nrows,
            ncols=ifg.ncols,
            metadata=ifg.meta_data
        )
        ifg.write_modified_phase() # update phase converted to mm
        ifg.close()
    ifgs_dict = join_dicts(mpiops.comm.allgather(ifgs_dict))

    ifgs_dict = mpiops.run_once(__save_ifgs_dict_with_headers_and_epochs, dest_tifs, ifgs_dict, params, process_tifs)

    params[C.PREREAD_IFGS] = ifgs_dict
    return ifgs_dict
示例#16
0
def __create_ifg_edge_dict(ifg_files: List[str], params: dict) -> Dict[Edge, IndexedIfg]:
    """Returns a dictionary of indexed ifg 'edges'"""
    ifg_files.sort()
    ifgs = [Ifg(i) for i in ifg_files]

    def _func(ifg, index):
        ifg.open()
        ifg.nodata_value = params[C.NO_DATA_VALUE]
        ifg.convert_to_nans()
        ifg.convert_to_radians()
        idx_ifg = IndexedIfg(index, IfgPhase(ifg.phase_data))
        return idx_ifg

    process_ifgs = mpiops.array_split(list(enumerate(ifgs)))
    ret_combined = {}
    for idx, _ifg in process_ifgs:
        ret_combined[Edge(_ifg.first, _ifg.second)] = _func(_ifg, idx)
        _ifg.close()

    ret_combined = join_dicts(mpiops.comm.allgather(ret_combined))
    return ret_combined
示例#17
0
 def setup_class(cls):
     from tests.common import small_data_setup
     cls.ifgs = small_data_setup()
     cls.ifg_paths = [i.data_path for i in cls.ifgs]
     params = Configuration(common.TEST_CONF_ROIPAC).__dict__
     cls.headers = [
         roipac.roipac_header(i.data_path, params) for i in cls.ifgs
     ]
     params[C.IFG_LKSX], params[C.IFG_LKSY], params[
         C.IFG_CROP_OPT] = 1, 1, 1
     prepare_ifgs(cls.ifg_paths,
                  crop_opt=1,
                  xlooks=1,
                  ylooks=1,
                  headers=cls.headers,
                  params=params)
     looks_paths = [
         mlooked_path(d, params, t)
         for d, t in zip(cls.ifg_paths, [InputTypes.IFG] * len(cls.ifgs))
     ]
     cls.ifgs_with_nan = [Ifg(i) for i in looks_paths]
     for ifg in cls.ifgs_with_nan:
         ifg.open()
示例#18
0
def __create_multilooked_dataset_for_network_correction(params):
    multi_paths = params[cf.INTERFEROGRAM_FILES]
    ifg_paths = [p.tmp_sampled_path for p in multi_paths]
    headers = [find_header(p, params) for p in multi_paths]
    crop_opt = prepifg_helper.ALREADY_SAME_SIZE
    xlooks = params[cf.ORBITAL_FIT_LOOKS_X]
    ylooks = params[cf.ORBITAL_FIT_LOOKS_Y]
    thresh = params[cf.NO_DATA_AVERAGING_THRESHOLD]
    rasters = [shared.dem_or_ifg(r) for r in ifg_paths]
    exts = prepifg_helper.get_analysis_extent(crop_opt, rasters, xlooks,
                                              ylooks, None)

    out_paths = [tempfile.mktemp() for _ in ifg_paths]
    mlooked_dataset = [
        prepifg_helper.prepare_ifg(d, xlooks, ylooks, exts, thresh, crop_opt,
                                   h, False, p)
        for d, h, p in zip(ifg_paths, headers, out_paths)
    ]
    mlooked = [Ifg(m[1]) for m in mlooked_dataset]
    for m in mlooked:
        m.initialize()
        shared.nan_and_mm_convert(m, params)
    return mlooked
示例#19
0
def __plot_ifg(file, cmap, ax, num_ifgs, params):
    try:
        ifg = Ifg(file)
        ifg.open()
    except:
        raise AttributeError(f'Cannot open interferogram geotiff: {file}')

    # change nodata values to NaN for display; convert units to mm (if in radians)
    nan_and_mm_convert(ifg, params)

    im = ax.imshow(ifg.phase_data, cmap=cmap)
    text = ax.set_title(Path(ifg.data_path).stem)
    text.set_fontsize(20)
    #    text.set_fontsize(min(20, int(num_ifgs / 2)))
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(im, cax=cax, label='mm')
    ifg.close()
示例#20
0
    def test_nans(self):
        """Verify NaNs replace 0 in the multilooked phase band"""
        xlooks = ylooks = 1
        prepare_ifgs(self.ifg_paths, MINIMUM_CROP, xlooks, ylooks)

        for ex in [self.exp_files[0], self.exp_files[4]]:
            ifg = Ifg(ex)
            ifg.open()

            phase = ifg.phase_band.ReadAsArray()
            self.assertFalse((phase == 0).any())
            self.assertTrue((isnan(phase)).any())
            ifg.close()

        self.assertAlmostEqual(nanmax(phase), 4.247, 3)  # copied from gdalinfo
        self.assertAlmostEqual(nanmin(phase), 0.009, 3)  # copied from gdalinfo
        for i in self.ifgs:
            i.close()
示例#21
0
    def _inner(ifg_paths):
        if isinstance(ifg_paths[0], Ifg):
            proc_ifgs = ifg_paths
        else:
            proc_ifgs = [Ifg(ifg_path) for ifg_path in ifg_paths]

        for ifg in proc_ifgs:
            if not ifg.is_open:
                ifg.open(readonly=False)

        ifg_phase_data_sum = np.zeros(proc_ifgs[0].shape, dtype=np.float64)
        phase_data = [i.phase_data for i in proc_ifgs]
        for ifg in proc_ifgs:
            ifg_phase_data_sum += ifg.phase_data

        comp = np.isnan(ifg_phase_data_sum)
        comp = np.ravel(comp, order='F')
        if params[cf.PARALLEL]:
            log.info("Calculating ref phase using multiprocessing")
            ref_phs = Parallel(n_jobs=params[cf.PROCESSES],
                               verbose=joblib_log_level(cf.LOG_LEVEL))(
                                   delayed(_est_ref_phs_method1)(p, comp)
                                   for p in phase_data)
            for n, ifg in enumerate(proc_ifgs):
                ifg.phase_data -= ref_phs[n]
        else:
            log.info("Calculating ref phase")
            ref_phs = np.zeros(len(proc_ifgs))
            for n, ifg in enumerate(proc_ifgs):
                ref_phs[n] = _est_ref_phs_method1(ifg.phase_data, comp)
                ifg.phase_data -= ref_phs[n]

        for ifg in proc_ifgs:
            _update_phase_metadata(ifg)
            ifg.close()

        return ref_phs
示例#22
0
    def test_orbital_correction_legacy_equality(self):
        from pyrate import correct
        from pyrate.configuration import MultiplePaths

        multi_paths = [MultiplePaths(p, params=self.params) for p in self.ifg_paths]
        for m in multi_paths:  # cheat
            m.sampled_path = m.converted_path

        self.params[cf.INTERFEROGRAM_FILES] = multi_paths
        self.params['rows'], self.params['cols'] = 2, 3
        Path(self.BASE_DIR).joinpath('tmpdir').mkdir(exist_ok=True, parents=True)
        correct._copy_mlooked(self.params)
        correct._update_params_with_tiles(self.params)
        correct._create_ifg_dict(self.params)
        correct._copy_mlooked(self.params)
        pyrate.core.orbital.orb_fit_calc_wrapper(self.params)

        onlyfiles = [f for f in os.listdir(SML_TEST_LEGACY_ORBITAL_DIR)
            if os.path.isfile(os.path.join(SML_TEST_LEGACY_ORBITAL_DIR, f))
            and f.endswith('.csv') and f.__contains__('_method1_')]

        count = 0
        for i, f in enumerate(onlyfiles):
            ifg_data = np.genfromtxt(os.path.join(SML_TEST_LEGACY_ORBITAL_DIR, f), delimiter=',')
            for k, j in enumerate([m.tmp_sampled_path for m in multi_paths]):
                ifg = Ifg(j)
                ifg.open()
                print(f)
                print(j)
                if os.path.basename(j).split('_ifg.')[0] == os.path.basename(f).split(
                        '_orb_planar_1lks_method1_geo_')[1].split('.')[0]:
                    count += 1
                    # all numbers equal
                    np.testing.assert_array_almost_equal(ifg_data, ifg.phase_data, decimal=2)

                    # means must also be equal
                    assert np.nanmean(ifg_data) == pytest.approx(np.nanmean(ifg.phase_data), abs=1e-2)

                    # number of nans must equal
                    assert np.sum(np.isnan(ifg_data)) == np.sum(np.isnan(ifg.phase_data))
                ifg.close()

        # ensure that we have expected number of matches
        assert count == len(self.ifg_paths)
示例#23
0
    def test_get_epochs(self):
        def str2date(s):
            segs = s[:4], s[4:6], s[6:] # year, month, day
            return date(*[int(sg) for sg in segs])

        raw_date = ['20060619', '20060828', '20061002', '20061106', '20061211',
                    '20070115', '20070219', '20070326', '20070430', '20070604',
                    '20070709', '20070813', '20070917']

        exp_dates = [str2date(d) for d in raw_date]
        exp_repeat = [1, 1, 3, 3, 4, 3, 3, 3, 3, 3, 3, 2, 2]
        exp_spans = [0, 0.1916, 0.2875, 0.3833, 0.4791, 0.5749, 0.6708, 0.7666,
                            0.8624, 0.9582, 1.0541, 1.1499, 1.2457]

        ifms = join(SML_TEST_TIF, "ifms_17")
        ifgs = [Ifg(join(SML_TEST_TIF, p)) for p in parse_namelist(ifms)]
        for i in ifgs:
            i.open()

        epochs = get_epochs(ifgs)[0]

        self.assertTrue((exp_dates == epochs.dates).all())
        self.assertTrue((exp_repeat == epochs.repeat).all())
        assert_array_almost_equal(exp_spans, epochs.spans, decimal=4)
示例#24
0
    def test_orbital_correction_legacy_equality(self):
        from pyrate import process
        from pyrate.configuration import MultiplePaths

        multi_paths = [MultiplePaths(self.BASE_DIR, p) for p in self.ifg_paths]
        for m in multi_paths:  # cheat
            m.sampled_path = m.converted_path
        process._orb_fit_calc(multi_paths, self.params)

        onlyfiles = [
            f for f in os.listdir(SML_TEST_LEGACY_ORBITAL_DIR)
            if os.path.isfile(os.path.join(SML_TEST_LEGACY_ORBITAL_DIR, f))
            and f.endswith('.csv') and f.__contains__('_method1_')
        ]

        count = 0
        for i, f in enumerate(onlyfiles):
            ifg_data = np.genfromtxt(os.path.join(SML_TEST_LEGACY_ORBITAL_DIR,
                                                  f),
                                     delimiter=',')
            for k, j in enumerate(self.ifg_paths):
                ifg = Ifg(j)
                ifg.open()
                if os.path.basename(j).split('_unw.')[0] == os.path.basename(
                        f).split('_orb_planar_1lks_method1_')[1].split('.')[0]:
                    count += 1
                    # all numbers equal
                    np.testing.assert_array_almost_equal(ifg_data,
                                                         ifg.phase_data,
                                                         decimal=2)

                    # means must also be equal
                    self.assertAlmostEqual(np.nanmean(ifg_data),
                                           np.nanmean(ifg.phase_data),
                                           places=2)

                    # number of nans must equal
                    self.assertEqual(np.sum(np.isnan(ifg_data)),
                                     np.sum(np.isnan(ifg.phase_data)))
                ifg.close()

        # ensure that we have expected number of matches
        self.assertEqual(count, len(self.ifg_paths))
示例#25
0
def mask_pixels_with_unwrapping_errors(ifgs_breach_count: NDArray[(
    Any, Any, Any), UInt16], num_occurrences_each_ifg: NDArray[(Any, ),
                                                               UInt16],
                                       params: dict) -> None:
    """
    Find pixels in the phase data that breach closure_thr, and mask
    (assign NaNs) to those pixels in those ifgs.
    :param ifgs_breach_count: unwrapping issues at pixels in all loops
    :param num_occurrences_each_ifg:  frequency of ifgs appearing in all loops
    :param params: params dict
    """
    log.debug("Masking phase data of retained ifgs")

    for i, m_p in enumerate(params[C.INTERFEROGRAM_FILES]):
        pix_index = ifgs_breach_count[:, :, i] == num_occurrences_each_ifg[i]
        ifg = Ifg(m_p.tmp_sampled_path)
        ifg.open()
        nan_and_mm_convert(ifg, params)
        ifg.phase_data[pix_index] = np.nan
        ifg.write_modified_phase()

    log.info(f"Masked phase data of {i + 1} retained ifgs after phase closure")
    return None
示例#26
0
    def test_custom_extents(self):
        xlooks = ylooks = 1
        cext = self._custom_extents_tuple()
        prepare_ifgs(self.ifg_paths, CUSTOM_CROP, xlooks, ylooks,
                     user_exts=cext)

        ifg = Ifg(self.exp_files[2])
        ifg.open()

        gt = ifg.dataset.GetGeoTransform()
        exp_gt = (cext.xfirst, self.xs, 0, cext.yfirst, 0, self.ys)

        for i, j in zip(gt, exp_gt):
            self.assertAlmostEqual(i, j)
        self.assert_geotransform_equal([self.exp_files[2], self.exp_files[6]])

        # close ifgs
        ifg.close()
        for i in self.ifgs:
            i.close()
示例#27
0
    def test_min_extents(self):
        """Test ifgcropopt=1 crops datasets to min extents."""
        xlooks = ylooks = 1
        prepare_ifgs(self.ifg_paths, MINIMUM_CROP, xlooks, ylooks)
        ifg = Ifg(self.exp_files[0])
        ifg.open()

        # output files should have same extents
        # NB: also verifies gdalwarp correctly copies geotransform across
        # NB: expected data copied from gdalinfo output
        gt = ifg.dataset.GetGeoTransform()
        exp_gt = (150.911666666, 0.000833333, 0,
                  -34.172499999, 0, -0.000833333)
        for i, j in zip(gt, exp_gt):
            self.assertAlmostEqual(i, j)
        self.assert_geotransform_equal([self.exp_files[0], self.exp_files[4]])

        ifg.close()
        for i in self.ifgs:
            i.close()
示例#28
0
def _wrap_spatio_temporal_filter(ifg_paths, params, tiles, preread_ifgs):
    """
    A wrapper for the spatio-temporal filter so it can be tested.
    See docstring for spatio_temporal_filter.
    """
    if not params[cf.APSEST]:
        log.info('APS correction not required.')
        return

    # perform some checks on existing ifgs
    log.debug('Checking APS correction status')
    if mpiops.run_once(shared.check_correction_status, ifg_paths,
                       ifc.PYRATE_APS_ERROR):
        log.debug('Finished APS correction')
        return  # return if True condition returned

    tsincr = _calc_svd_time_series(ifg_paths, params, preread_ifgs, tiles)

    ifg = Ifg(ifg_paths[0])  # just grab any for parameters in slpfilter
    ifg.open()
    spatio_temporal_filter(tsincr, ifg, params, preread_ifgs)
    ifg.close()
示例#29
0
    def test_default_max_extents(self):
        """Test ifgcropopt=2 crops datasets to max bounding box extents."""
        xlooks = ylooks = 1
        prepare_ifgs(self.ifg_paths, MAXIMUM_CROP, xlooks, ylooks)
        for f in [self.exp_files[1], self.exp_files[5]]:
            self.assertTrue(exists(f), msg="Output files not created")

        # output files should have same extents
        # NB: also verifies gdalwarp correctly copies geotransform across
        ifg = Ifg(self.exp_files[1])
        ifg.open()
        gt = ifg.dataset.GetGeoTransform()

        # copied from gdalinfo output
        exp_gt = (150.91, 0.000833333, 0, -34.17, 0, -0.000833333)
        for i, j in zip(gt, exp_gt):
            self.assertAlmostEqual(i, j)

        self.assert_geotransform_equal([self.exp_files[1], self.exp_files[5]])

        ifg.close()
        for i in self.ifgs:
            i.close()
示例#30
0
for file in glob.glob(f'{tempml_dir}/*ifg.tif'):
    tempml_list.append(file)

# Sort
ifg_list.sort()
corr_list.sort()
tempml_list.sort()

# define colour map
cmap = cm.Spectral_r
cmap.set_bad(color='grey')

# loop over each ifg in turn
for i in range(args.FIRST_IFG - 1, args.LAST_IFG):
    # Read data
    orig_ifg = Ifg(ifg_list[i])
    orig_ifg.open()
    orig_ifg.convert_to_mm()  # force mm conversion
    ifg = orig_ifg.phase_data
    orig_ifg.close()

    corr = np.load(corr_list[i])

    corr_ifg = Ifg(tempml_list[i])
    corr_ifg.open()
    corr_ifg.convert_to_mm()  # force mm conversion
    ifg_corr = corr_ifg.phase_data
    corr_ifg.close()

    # Identify Date Pair
    date_pair_list_ifg = re.findall(r'\d{8}-\d{8}', ifg_list[i])