def filter_scene(self, scene: Scene): """Create a new Scene with filtered DataArrays removed.""" _cache = {} remaining_ids = [] filtered_ids = [] for data_id in scene.keys(): logger.debug("Analyzing '{}' for filtering...".format(data_id)) if not self._filter_data_array(scene[data_id], _cache): remaining_ids.append(data_id) else: logger.debug(self.FILTER_MSG.format(data_id)) filtered_ids.append(data_id) if not remaining_ids: return None new_scn = scene.copy(remaining_ids) new_scn._wishlist = scene.wishlist.copy() return new_scn
def resample_scene( input_scene: Scene, areas_to_resample: ListOfAreas, grid_configs: list[str, ...], resampler: Optional[str], preserve_resolution: bool = True, grid_coverage: Optional[float] = None, is_polar2grid: bool = True, **resample_kwargs, ) -> list[tuple[Scene, set]]: """Resample a single Scene to multiple target areas.""" area_resolver = AreaDefResolver(input_scene, grid_configs) resampling_dtree = ResamplerDecisionTree.from_configs() if resampler is None: resampling_groups = _create_resampling_groups(input_scene, resampling_dtree, is_polar2grid) else: default_target = _default_grid(resampler, is_polar2grid) rs_kwargs = _hashable_kwargs(resample_kwargs) resampling_groups = {(resampler, rs_kwargs, default_target): None} wishlist = input_scene.wishlist.copy() scenes_to_save = [] for (resampler, _resample_kwargs, default_target), data_ids in resampling_groups.items(): areas = _areas_to_resample(areas_to_resample, resampler, default_target) scene_to_resample = input_scene.copy(datasets=data_ids) preserve_resolution = _get_preserve_resolution(preserve_resolution, resampler, areas) preserved_products = _products_to_preserve_resolution( preserve_resolution, wishlist, scene_to_resample) if preserved_products: scenes_to_save.append((scene_to_resample, preserved_products)) logger.debug("Products to preserve resolution for: {}".format( preserved_products)) logger.debug("Products to use new resolution for: {}".format( set(wishlist) - preserved_products)) # convert hashable tuple to dict _resample_kwargs = _redict_hashable_kwargs(_resample_kwargs) _grid_cov = _resample_kwargs.get("grid_coverage", grid_coverage) if _grid_cov is None: _grid_cov = 0.1 for area_name in areas: area_def = area_resolver[area_name] rs = _get_default_resampler(resampler, area_name, area_def, input_scene) if area_def is not None: this_area_scene = scene_to_resample if resampler != "native" and _grid_cov > 0: logger.info( "Checking products for sufficient output grid coverage (grid: '%s')...", area_name) filter = ResampleCoverageFilter( target_area=area_def, coverage_fraction=_grid_cov) this_area_scene = filter.filter_scene(scene_to_resample) if this_area_scene is None: logger.warning( "No products were found to overlap with '%s' grid.", area_name) continue if data_ids is not None: data_ids = list( set(data_ids) & set(this_area_scene.keys())) logger.info("Resampling to '%s' using '%s' resampling...", area_name, rs) logger.debug("Resampling to '%s' using resampler '%s' with %s", area_name, rs, _resample_kwargs) new_scn = this_area_scene.resample(area_def, resampler=rs, datasets=data_ids, **_resample_kwargs) elif not preserve_resolution: # the user didn't want to resample to any areas # the user also requested that we don't preserve resolution # which means we have to save this Scene's datasets # because they won't be saved new_scn = scene_to_resample else: # No resampling and any preserved resolution datasets were saved earlier continue # we only want to try to save products that we asked for and that # we were actually able to generate. Composite generation may have # modified the original DataID so we can't use # 'resampled_products'. _resampled_products = (new_scn.wishlist & set(new_scn.keys())) - preserved_products if _resampled_products: scenes_to_save.append((new_scn, _resampled_products)) return scenes_to_save