예제 #1
0
    def test_ref_pixel_multiple_runs_reuse_from_disc(self, ref_pixel):
        params = self.params
        params[C.REFX], params[C.REFY] = ref_pixel
        params[C.REF_PIXEL_FILE] = Configuration.ref_pixel_path(params)
        ref_pixel_calc_wrapper(params)

        ref_pixel_file = self.params[C.REF_PIXEL_FILE]
        time_written = os.stat(ref_pixel_file).st_mtime
        assert self.params[C.REFX_FOUND] == 38
        assert self.params[C.REFY_FOUND] == 58
        # run again
        ref_pixel_calc_wrapper(self.params)
        ref_pixel_file = self.params[C.REF_PIXEL_FILE]
        time_written_1 = os.stat(ref_pixel_file).st_mtime
        assert self.params[C.REFX_FOUND] == 38
        assert self.params[C.REFY_FOUND] == 58

        # run a third time
        ref_pixel_calc_wrapper(self.params)
        ref_pixel_file = self.params[C.REF_PIXEL_FILE]
        time_written_2 = os.stat(ref_pixel_file).st_mtime
        assert time_written == time_written_2 == time_written_1
        assert self.params[C.REFX], self.params[C.REFY] == ref_pixel
        assert self.params[C.REFX_FOUND] == 38
        assert self.params[C.REFY_FOUND] == 58
예제 #2
0
    def setup_method(self):
        self.params = Configuration(common.TEST_CONF_GAMMA).__dict__
        self.tmp_dir = tempfile.mkdtemp()
        self.params[cf.OUT_DIR] = self.tmp_dir
        self.params[cf.REF_EST_METHOD] = 1
        self.params[cf.PARALLEL] = False
        self.params[cf.TMPDIR] = self.tmp_dir
        common.copytree(common.SML_TEST_TIF, self.tmp_dir)
        self.small_tifs = glob.glob(os.path.join(self.tmp_dir, "*.tif"))
        for s in self.small_tifs:
            os.chmod(s, 0o644)
        self.ifgs = common.small_data_setup(self.tmp_dir, is_dir=True)
        self.params[cf.INTERFEROGRAM_FILES] = [
            MultiplePaths(p, self.params) for p in self.small_tifs
        ]
        for p in self.params[cf.INTERFEROGRAM_FILES]:
            p.sampled_path = p.converted_path
            p.tmp_sampled_path = p.sampled_path
        for ifg in self.ifgs:
            ifg.close()

        self.params[cf.REFX], self.params[cf.REFY] = -1, -1
        self.params[cf.REFNX], self.params[cf.REFNY] = 10, 10
        self.params[cf.REF_CHIP_SIZE], self.params[cf.REF_MIN_FRAC] = 21, 0.5
        self.params['rows'], self.params['cols'] = 3, 2
        self.params[cf.REF_PIXEL_FILE] = Configuration.ref_pixel_path(
            self.params)
        correct._update_params_with_tiles(self.params)
        correct.ref_pixel_calc_wrapper(self.params)
예제 #3
0
    def setup_class(cls):

        # testing constants2
        cls.BASE_DIR = tempfile.mkdtemp()
        cls.BASE_OUT_DIR = join(cls.BASE_DIR, 'out')
        cls.BASE_DEM_DIR = join(cls.BASE_DIR, 'dem')
        cls.BASE_DEM_FILE = join(cls.BASE_DEM_DIR, 'roipac_test_trimmed.tif')

        try:
            # copy source data (treat as prepifg already run)
            os.makedirs(cls.BASE_OUT_DIR)
            for path in glob.glob(join(common.SML_TEST_TIF, '*')):
                dest = join(cls.BASE_OUT_DIR, os.path.basename(path))
                shutil.copy(path, dest)
                os.chmod(dest, 0o660)

            os.makedirs(cls.BASE_DEM_DIR)
            orig_dem = common.SML_TEST_DEM_TIF
            os.symlink(orig_dem, cls.BASE_DEM_FILE)
            os.chdir(cls.BASE_DIR)

            # Turn off validation because we're in a different working dir
            #  and relative paths in config won't be work.
            params = config.get_config_params(common.TEST_CONF_ROIPAC)
            params['correct'] = [
                'orbfit', 'refphase', 'mst', 'apscorrect', 'maxvar'
            ]
            params[cf.OUT_DIR] = cls.BASE_OUT_DIR
            params[cf.PROCESSOR] = 0  # roipac
            params[cf.APS_CORRECTION] = 0
            paths = glob.glob(join(cls.BASE_OUT_DIR, 'geo_*-*.tif'))
            paths = sorted(paths)
            params[cf.PARALLEL] = False
            params[cf.ORBFIT_OFFSET] = True
            params[cf.TEMP_MLOOKED_DIR] = cls.BASE_OUT_DIR.join(
                cf.TEMP_MLOOKED_DIR)
            params[cf.INTERFEROGRAM_FILES] = [
                MultiplePaths(p, params) for p in paths
            ]
            for p in params[cf.INTERFEROGRAM_FILES]:  # cheat
                p.sampled_path = p.converted_path
                p.tmp_sampled_path = p.converted_path
            params["rows"], params["cols"] = 2, 2
            params[cf.REF_PIXEL_FILE] = Configuration.ref_pixel_path(params)
            Path(params[cf.OUT_DIR]).joinpath(cf.APS_ERROR_DIR).mkdir(
                exist_ok=True, parents=True)
            Path(params[cf.OUT_DIR]).joinpath(cf.MST_DIR).mkdir(exist_ok=True,
                                                                parents=True)
            correct.correct_ifgs(params)

            if not hasattr(cls, 'ifgs'):
                cls.ifgs = get_ifgs(out_dir=cls.BASE_OUT_DIR)
        except:
            # revert working dir & avoid paths busting other tests
            os.chdir(CURRENT_DIR)
            raise
예제 #4
0
def ref_pixel_calc_wrapper(params: dict) -> Tuple[int, int]:
    """
    Wrapper for reference pixel calculation
    """
    __validate_supplied_lat_lon(params)
    ifg_paths = [ifg_path.tmp_sampled_path for ifg_path in params[C.INTERFEROGRAM_FILES]]
    lon = params[C.REFX]
    lat = params[C.REFY]

    ifg = Ifg(ifg_paths[0])
    ifg.open(readonly=True)
    # assume all interferograms have same projection and will share the same transform
    transform = ifg.dataset.GetGeoTransform()

    ref_pixel_file = Configuration.ref_pixel_path(params)

    def __reuse_ref_pixel_file_if_exists():
        if ref_pixel_file.exists():
            refx, refy = np.load(ref_pixel_file)
            log.info('Reusing pre-calculated ref-pixel values: ({}, {}) from file {}'.format(
                refx, refy, ref_pixel_file.as_posix()))
            log.warning("Reusing ref-pixel values from previous run!!!")
            params[C.REFX_FOUND], params[C.REFY_FOUND] = int(refx), int(refy)
            return int(refx), int(refy)
        else:
            return None, None

    # read and return
    refx, refy = mpiops.run_once(__reuse_ref_pixel_file_if_exists)
    if (refx is not None) and (refy is not None):
        update_refpix_metadata(ifg_paths, int(refx), int(refy), transform, params)
        return refx, refy

    if lon == -1 or lat == -1:
        log.info('Searching for best reference pixel location')

        half_patch_size, thresh, grid = ref_pixel_setup(ifg_paths, params)
        process_grid = mpiops.array_split(grid)
        save_ref_pixel_blocks(process_grid, half_patch_size, ifg_paths, params)
        mean_sds = _ref_pixel_mpi(process_grid, half_patch_size, ifg_paths, thresh, params)
        mean_sds = mpiops.comm.gather(mean_sds, root=0)
        if mpiops.rank == MAIN_PROCESS:
            mean_sds = np.hstack(mean_sds)

        refpixel_returned = mpiops.run_once(find_min_mean, mean_sds, grid)

        if isinstance(refpixel_returned, ValueError):
            raise RefPixelError(
                "Reference pixel calculation returned an all nan slice!\n"
                "Cannot continue downstream computation. Please change reference pixel algorithm used before "
                "continuing.")
        refy, refx = refpixel_returned   # row first means first value is latitude
        log.info('Selected reference pixel coordinate (x, y): ({}, {})'.format(refx, refy))
        lon, lat = convert_pixel_value_to_geographic_coordinate(refx, refy, transform)
        log.info('Selected reference pixel coordinate (lon, lat): ({}, {})'.format(lon, lat))
    else:
        log.info('Using reference pixel from config file (lon, lat): ({}, {})'.format(lon, lat))
        log.warning("Ensure user supplied reference pixel values are in lon/lat")
        refx, refy = convert_geographic_coordinate_to_pixel_value(lon, lat, transform)
        log.info('Converted reference pixel coordinate (x, y): ({}, {})'.format(refx, refy))

    np.save(file=ref_pixel_file, arr=[int(refx), int(refy)])
    update_refpix_metadata(ifg_paths, refx, refy, transform, params)

    log.debug("refpx, refpy: "+str(refx) + " " + str(refy))
    ifg.close()
    params[C.REFX_FOUND], params[C.REFY_FOUND] = int(refx), int(refy)
    return int(refx), int(refy)