def view_water_map_results(self, name: str, **kwargs ): from geoproc.plot.animation import SliceAnimation interp_water_class = kwargs.get( 'interp_water_class', 4 ) water_classes = kwargs.get('water_classes', [2,4] ) patched_water_maps = self.get_patched_water_maps( name, **kwargs ) water_counts, class_proportion = self.get_class_proportion( patched_water_maps, interp_water_class, water_classes ) animation = SliceAnimation( patched_water_maps, metrics=dict(blue=class_proportion) ) animation.show()
def view_input_data(self, name: str, **kwargs ): from geoproc.plot.animation import SliceAnimation agg = kwargs.get( "agg", False ) opspec = self.get_opspec( name.lower() ) self.get_roi_bounds(opspec) water_mapping_data = self.get_mpw_data( **opspec, cache="update" ) result = water_mapping_data if not agg else self.get_water_maps( water_mapping_data, opspec ) result.attrs['cmap'] = dict( colors=self.get_input_colors() ) animation = SliceAnimation( result ) animation.show()
def show(self, data: xr.DataArray, name: str ): data.name = name if len(data.dims) == 2: data.name = name data.plot.imshow(cmap=data.attrs.get("cmap","jet")) plt.show() elif len(data.dims) == 3: animation = SliceAnimation(data) animation.show() else: print( f"Error, can't show '{name}' data with {len(data.dims)} dimensions")
def show_roi( xbounds, ybounds, year, time_range ): dat_url = "https://floodmap.modaps.eosdis.nasa.gov/Products" dataMgr = MWPDataManager( DATA_DIR, dat_url ) dataMgr.setDefaults(product="2D2OT", year=year, start_day=time_range[0], end_day=time_range[1]) location = TileLocator.get_tile( *xbounds, *ybounds ) file_paths = dataMgr.get_tile(location) roi_bounds = xbounds + ybounds time_values = np.array([ get_date_from_filename(os.path.basename(path)) for path in file_paths], dtype='datetime64[ns]') cropped_data: xr.DataArray = XRio.load(file_paths, mask=roi_bounds, band=0, index=time_values) cropped_data.attrs['cmap'] = dict(colors=colors3) animation = SliceAnimation( cropped_data ) animation.show()
import xarray as xa from geoproc.plot.animation import SliceAnimation import os DATA_DIR = "/Users/tpmaxwel/Dropbox/Tom/Data/Aviris" aviris_tile = "ang20170714t213741" target_file = os.path.join(DATA_DIR, "results", f"{aviris_tile}_Avg-Chl.nc") n_components = 6 components_file = DATA_DIR + f"/pca-rfl-{n_components}.nc" cmap_range = [-2, 2] pca_dataset: xa.Dataset = xa.open_dataset(components_file) component_data: xa.DataArray = pca_dataset.component_data reduced_data: xa.DataArray = pca_dataset.reduced_data scale: xa.DataArray = pca_dataset.scale explained_variance: xa.DataArray = pca_dataset.explained_variance reduced_data.attrs['cmap'] = dict(range=cmap_range) metrics = dict(blue=explained_variance) target_dataset: xa.DataArray = xa.open_dataset(target_file) target_band: xa.DataArray = target_dataset.band_data target_band.attrs['cmap'] = dict(range=[0, 5]) animation = SliceAnimation(reduced_data, auxplot=target_band, metrics=metrics) animation.invert_yaxis() animation.show()
def animate(self, **kwargs): vars = kwargs.get("vars", self.get_data_vars()) data_arrays = [self._dset[vname] for vname in vars] animation = SliceAnimation(data_arrays, **kwargs) animation.show()
def animate(self, **kwargs): animation = SliceAnimation(self._obj, **kwargs) animation.show()
data_dir= "/Users/tpmaxwel/Dropbox/Tom/Data/Birkitt" mask_value = 5 water_map_colors = dict( colors = [ (0, 'nodata', (0, 0, 0)), (1, 'land', (0, 1, 0)), (2, 'water', (0, 0, 1)), (3, 'int-land', (0, 0.6, 0)), (4, 'int-water', (0, 0, 0.6)), (mask_value, 'mask', (1, 1, 0.7)) ] ) water_maps_file = os.path.join( data_dir, "saltlakemasked_water_maps.nc") water_prob_file = os.path.join( data_dir, "SaltLake_water_probability.nc") SHAPEFILE = os.path.join( data_dir, "saltLake/GreatSalt.shp" ) water_maps: xa.DataArray = xa.open_dataset( water_maps_file ).water_maps water_maps.name = "water_maps" water_maps.attrs['cmap'] = water_map_colors water_probability: xa.DataArray = xa.open_dataset( water_prob_file ).water_probability permanent_water = np.logical_and( water_probability < 1.0, water_probability > 0.95 ) mismatches: xa.DataArray = water_maps.where( permanent_water, 0 ) == 1 mismatch_count = mismatches.sum( dim=["x","y"] ) plt.show() lake_mask: gpd.GeoSeries = gpd.read_file( SHAPEFILE ) animation = SliceAnimation( water_maps, overlays=dict(red=lake_mask.boundary), metrics=dict(blue=mismatch_count) ) animation.show()
from geoproc.plot.animation import SliceAnimation from geoproc.surfaceMapping.lakeExtentMapping import WaterMapGenerator import yaml, os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) opspec_file = os.path.join(BASEDIR, "specs", "lakes1.yml") with open(opspec_file) as f: opspecs = yaml.load(f, Loader=yaml.FullLoader) waterMapGenerator = WaterMapGenerator(opspecs) patched_water_maps = waterMapGenerator.repatch_water_maps("Lake1295") roi = patched_water_maps.attrs.get("roi") kwargs = dict(overlays=dict(red=roi.boundary)) if (roi and not isinstance(roi, list)) else {} animation = SliceAnimation([waterMapGenerator.water_maps, patched_water_maps, waterMapGenerator.persistent_classes], **kwargs) animation.show()
water_probability_file) water_probability: xr.DataArray = water_probability_dataset.water_probability else: water_probability = get_water_prob(cropped_data, yearly) result = xr.Dataset( dict(water_probability=sanitize(water_probability))) result.to_netcdf(water_probability_file) print(f"Saved water_probability to {water_probability_file}") persistent_classes = get_persistent_classes(water_probability, land_water_thresholds) persistent_classes.attrs['cmap'] = dict(colors=colors3) water_probability.attrs['cmap'] = dict(colors=jet_colors2) if show_water_probability: animation = SliceAnimation([water_probability, persistent_classes], overlays=dict(red=lake_mask.boundary)) animation.show() if use_existing_water_masks is not None: if use_existing_water_masks: result_file = DATA_DIR + f"/SaltLake_water_masks.nc" water_masks_dataset: xr.Dataset = xr.open_dataset(result_file) water_masks: xr.DataArray = water_masks_dataset.water_masks else: waterMapGenerator = WaterMapGenerator() masking_results = waterMapGenerator.get_water_masks( cropped_data, binSize, threshold, mask_value) water_masks: xr.DataArray = masking_results["mask"] result = xr.Dataset(dict(water_masks=sanitize(water_masks))) result_file = DATA_DIR + f"/SaltLake_water_masks.nc"
dataset = xr.open_dataset( result_file ) water_prob = dataset['probabilitiy'] masked = water_prob > 1.0 thresholded_map: xr.DataArray = xr.where( masked, mask_value, xr.where( water_prob > land_water_thresholds[1], 2, (water_prob > land_water_thresholds[0]) ) ) thresholded_map.attrs['cmap'] = dict( colors = colors3 ) water_prob.attrs['cmap'] = dict( colors = jet_colors2 ) #water_prob_class = dataset['classification'] #water_prob_class.attrs['cmap'] = dict( colors = colors3 ) #water_prob_map.attrs['cmap'] = dict( cmap = "jet" ) animation = SliceAnimation( [ water_prob, thresholded_map ], overlays=dict(red=lake_mask.boundary) ) animation.show() # prob_file = DATA_DIR + f"/WaterProbabilityMap.tif" # water_prob_map: xr.DataArray = xr.open_rasterio( prob_file ) # # class_file = DATA_DIR + f"/WaterProbabilityClasses.tif" # water_prob_class: xr.DataArray = xr.open_rasterio( class_file ) # # # # sample the colormaps that you want to use. Use 128 from each so we get 256 # # colors in total #
t0 = time.time() if use_netcdf: yearly_lake_masks: xr.DataArray = read_lake_mask_netcdf( f"{DATA_DIR}/SaltLake_fill_masks.nc", precision=spatial_precision ) else: yearly_lake_masks: xr.DataArray = read_lake_mask_geotifs( f"{DATA_DIR}/MOD44W/*", precision=spatial_precision ) yearly_lake_masks.attrs['cmap'] = dict(colors=colors) if view_masks: lake_extent = (yearly_lake_masks == 1).sum( dim=yearly_lake_masks.dims[1:] ) lake_extent.name = "Water Extent" land_extent = (yearly_lake_masks == 0).sum( dim=yearly_lake_masks.dims[1:] ) land_extent.name = "Land Extent" animation = SliceAnimation( yearly_lake_masks, overlays=dict(red=lake_mask.boundary), metrics=dict( blue=lake_extent, green=land_extent ) ) animation.show() else: water_probability = get_water_prob( yearly_lake_masks ) water_probability.attrs['cmap'] = dict(colors=jet_colors2) persistent_classes = get_persistent_classes( yearly_lake_masks, water_probability, precision=spatial_precision ) persistent_classes.attrs['cmap'] = dict(colors=colors3) persistent_classes.name = "YearlyInterpClasses" result_file = DATA_DIR + f"/SaltLake_YearlyInterpClasses.nc" sanitize(persistent_classes).to_netcdf(result_file) animation = SliceAnimation( [ yearly_lake_masks, persistent_classes ], overlays=dict(red=lake_mask.boundary) ) animation.show()
def get_date_from_year(self, year: int): from datetime import datetime result = datetime(year, 1, 1) return np.datetime64(result) def set_spatial_precision(self, array: xr.DataArray, precision: int) -> xr.DataArray: if precision is None: return array sdims = [array.dims[-2], array.dims[-1]] rounded_coords = { dim: array.coords[dim].round(precision) for dim in sdims } return array.assign_coords(rounded_coords) if __name__ == '__main__': from geoproc.plot.animation import SliceAnimation import yaml CURDIR = os.path.dirname(os.path.abspath(__file__)) opspec_file = os.path.join(CURDIR, "specs", "lakes-test.yml") reproject_inputs = False with open(opspec_file) as f: opspecs = yaml.load(f, Loader=yaml.FullLoader) lakeMaskProcessor = LakeMaskProcessor(opspecs) results = lakeMaskProcessor.process_lakes(reproject_inputs, return_results=True) animation = SliceAnimation(list(results.values())) animation.show()