def test_gdal_python_vs_old_prepifg_prep2(self): for i in range(10): thresh = 0.5 x_looks = y_looks = 2 res = 2 extents = [10, 0, 20, 10] extents_str = [str(e) for e in extents] data = np.array(np.random.randint(0, 3, size=(10, 10)), dtype=np.float32) # create the self.temp_tiff self.manipulation(data, self.temp_tif, self.md) # only band 1 is resapled in warp_old averaged_and_resampled, _ = gdalwarp.crop_resample_average( self.temp_tif, extents, [res, -res], self.out_tif, thresh, match_pirate=True) ifg = Ifg(self.temp_tif) # only band 1 is resampled in warp_old data, self.old_prepifg_path = warp_old(ifg, x_looks, y_looks, extents_str, [res, -res], thresh=thresh, crop_out=4, verbose=False) np.testing.assert_array_almost_equal(data, averaged_and_resampled, decimal=4)
def _warp(ifg, x_looks, y_looks, extents, resolution, thresh, crop_out, write_to_disk=True): """ Convenience function for calling GDAL functionality """ if x_looks != y_looks: raise ValueError('X and Y looks mismatch') # cut, average, resample the final output layers looks_path = cf.mlooked_path(ifg.data_path, y_looks, crop_out) # # Add missing/updated metadata to resampled ifg/DEM # new_lyr = type(ifg)(looks_path) # new_lyr.open(readonly=True) # # for non-DEMs, phase bands need extra metadata & conversions # if hasattr(new_lyr, "phase_band"): # # TODO: LOS conversion to vertical/horizontal (projection) # # TODO: push out to workflow # #if params.has_key(REPROJECTION_FLAG): # # reproject() driver_type = 'GTiff' if write_to_disk else 'MEM' resampled_data, out_ds = gdalwarp.crop_resample_average( input_tif=ifg.data_path, extents=extents, new_res=resolution, output_file=looks_path, thresh=thresh, out_driver_type=driver_type) if not write_to_disk: return resampled_data, out_ds
def warp(ifg, x_looks, y_looks, extents, resolution, thresh, crop_out, write_to_disc=True): """ Resamples 'ifg' and returns a new interferogram object. :param xlooks: Factor to scale X axis by, 5 is 5x smaller, 1 is no change :param ylooks: Factor to scale Y axis by, 5 is 5x smaller, 1 is no change :param extents: Georeferenced extents for new file: (xfirst, yfirst, xlast, ylast) :param resolution: [xres, yres] or None. Sets resolution output interferogram metadata. Use *None* if raster size is not being changed :param thresh: See thresh in prepare_ifgs() :param verbose: True to print gdalwarp output to stdout :param write_to_disc: Whether to write to disc during warp :return xxxx """ if x_looks != y_looks: raise ValueError('X and Y looks mismatch') # cut, average, resample the final output layers looks_path = cf.mlooked_path(ifg.data_path, y_looks, crop_out) # # Add missing/updated metadata to resampled ifg/DEM # new_lyr = type(ifg)(looks_path) # new_lyr.open(readonly=True) # # for non-DEMs, phase bands need extra metadata & conversions # if hasattr(new_lyr, "phase_band"): # # TODO: LOS conversion to vertical/horizontal (projection) # # TODO: push out to workflow # #if params.has_key(REPROJECTION_FLAG): # # reproject() driver_type = 'GTiff' if write_to_disc else 'MEM' resampled_data, out_ds = gdalwarp.crop_resample_average( input_tif=ifg.data_path, extents=extents, new_res=resolution, output_file=looks_path, thresh=thresh, out_driver_type=driver_type) if not write_to_disc: return resampled_data, out_ds
def test_gdal_python_vs_old_prepifg_no_match_pirate(self): for ifg in self.ifgs: extents = [150.91, -34.229999976, 150.949166651, -34.17] extents_str = [str(e) for e in extents] orig_res = 0.000833333 thresh = 0.5 for looks in range(10): x_looks = y_looks = looks res = orig_res * x_looks averaged_and_resapled, out_ds = gdalwarp.crop_resample_average( ifg.data_path, extents, new_res=[res, -res], output_file=self.temp_tif, thresh=thresh, match_pirate=False) # only band 1 is resampled in warp_old data, self.old_prepifg_path = warp_old(ifg, x_looks, y_looks, extents_str, [res, -res], thresh=thresh, crop_out=4, verbose=False) yres, xres = data.shape # old_prepifg warp resample method loses one # row at the bottom if nrows % 2 == 1 averaged_and_resapled = averaged_and_resapled[:yres, :xres] nrows, ncols = averaged_and_resapled.shape np.testing.assert_array_almost_equal(data, averaged_and_resapled, decimal=4) # make sure they are the same after they are opened again # Last [yres:nrows, xres:ncols] won't match due to Pirate # dropping last few rows/columns depending on resolution/looks data_from_file = gdal.Open(self.old_prepifg_path).ReadAsArray() new_from_file = out_ds.ReadAsArray() np.testing.assert_array_almost_equal( data_from_file[:yres, :xres], new_from_file[:yres, :xres]) out_ds = None # manual close
def test_no_out_file_when_driver_type_is_mem(self): for ifg in self.ifgs: extents = [150.91, -34.229999976, 150.949166651, -34.17] extents_str = [str(e) for e in extents] orig_res = 0.000833333 thresh = 0.5 x_looks = y_looks = 6 res = orig_res * x_looks averaged_and_resampled, out_ds = gdalwarp.crop_resample_average( ifg.data_path, extents, new_res=[res, -res], output_file=self.temp_tif, thresh=thresh, out_driver_type='MEM', match_pirate=True) # only band 1 is resampled in warp_old data, self.old_prepifg_path = warp_old(ifg, x_looks, y_looks, extents_str, [res, -res], thresh=thresh, crop_out=4, verbose=False) yres, xres = data.shape # old_prepifg warp resample method loses one row at the bottom if # nrows % 2 == 1 averaged_and_resampled = averaged_and_resampled[:yres, :xres] np.testing.assert_array_almost_equal(data, averaged_and_resampled, decimal=4) # make sure they are the same after they are opened again data_from_file = gdal.Open(self.old_prepifg_path).ReadAsArray() new_from_file = out_ds.ReadAsArray() np.testing.assert_array_almost_equal(data_from_file, new_from_file) self.assertFalse(os.path.exists(self.temp_tif)) out_ds = None # manual close
def test_gdal_python_vs_old_prepifg(self): for ifg in self.ifgs: extents = [150.91, -34.229999976, 150.949166651, -34.17] extents_str = [str(e) for e in extents] orig_res = 0.000833333 thresh = 0.5 for looks in range(1, 10): x_looks = y_looks = looks res = orig_res * x_looks averaged_and_resampled = gdalwarp.crop_resample_average( ifg.data_path, extents, new_res=[res, -res], output_file=self.temp_tif, thresh=thresh, match_pirate=True)[0] # only band 1 is resampled in warp_old data, self.old_prepifg_path = warp_old(ifg, x_looks, y_looks, extents_str, [res, -res], thresh=thresh, crop_out=4, verbose=False) yres, xres = data.shape # old_prepifg warp resample method loses one row # at the bottom if nrows % 2 == 1 np.testing.assert_array_almost_equal( data, averaged_and_resampled[:yres, :xres], decimal=4) nrows, ncols = averaged_and_resampled.shape # make sure they are the same after they are opened again data_from_file = gdal.Open(self.old_prepifg_path).ReadAsArray() self.assertTrue(os.path.exists(self.temp_tif)) new_from_file = gdal.Open(self.temp_tif).ReadAsArray() np.testing.assert_array_almost_equal(data_from_file, new_from_file)