utils
aggregate_climate_to_hierarchy(data: pd.DataFrame, hierarchy: pd.DataFrame) -> pd.DataFrame
Create all aggregate climate values for a given hierarchy from most-detailed data.
Parameters
data The most-detailed climate data to aggregate. hierarchy The hierarchy to aggregate the data to.
Returns
pd.DataFrame The climate data with values for all levels of the hierarchy.
Source code in src/climate_data/aggregate/utils.py
blocks_with_shapefile_intersections(hierarchy: str, pm_data: PopulationModelData, modeling_frame: gpd.GeoDataFrame | None = None) -> set[str]
Return the block_keys whose footprint intersects the hierarchy's raking shapes.
Blocks whose (dissolved) modeling-frame geometry intersects no raking polygon
contribute only empty rows to the sum/sum aggregation, so the pipeline can skip
them. The shapefile lookup is delegated to
PopulationModelData.load_raking_shapes, which routes to the right file for
whichever hierarchy is in use.
.. note::
This uses the block *geometry* vs the full shape set, whereas the production
emptiness gate in ``build_location_masks`` uses the raster *bounding box* vs
bbox-filtered shapes. Equivalence was validated empirically for ``lsae_1285``
(bit-for-bit identical output) but is frame/hierarchy-dependent — re-validate
if the modeling frame or a hierarchy's shapes change.
Parameters
hierarchy
The full aggregation hierarchy whose raking shapes gate the blocks.
pm_data
PopulationModelData used to load the raking shapes (and the modeling frame
when one is not supplied).
modeling_frame
Optional pre-loaded modeling frame. Loaded via
pm_data.load_modeling_frame() when None; pass the already-loaded
frame to avoid a redundant re-read.
Returns
set[str] The block_keys with at least one intersecting raking polygon.
Raises
ValueError If no block intersects any raking shape while blocks exist — a likely CRS/shapefile misconfiguration that would otherwise silently skip everything.
Source code in src/climate_data/aggregate/utils.py
build_bounds_map(raster_template: rt.RasterArray, shape_values: list[tuple[Polygon | MultiPolygon, int]]) -> dict[int, tuple[slice, slice]]
Build a map of location IDs to buffered slices of the raster template.
Parameters
raster_template The raster template to build the bounds map for. shape_values A list of tuples where the first element is a shapely Polygon or MultiPolygon in the CRS of the raster template and the second element is the location ID of the shape.
Returns
dict[int, tuple[slice, slice]] A dictionary mapping location IDs to a tuple of slices representing the bounds of the location in the raster template. The slices are buffered by 10 pixels to ensure that the entire shape is included in the mask.
Source code in src/climate_data/aggregate/utils.py
build_location_masks(hierarchy: str, block_key: str, pm_data: PopulationModelData) -> tuple[dict[str, slice], dict[int, tuple[slice, slice, npt.NDArray[np.bool_]]], npt.NDArray[np.uint32]]
Build location masks for each location in the hierarchy.
Parameters
hierarchy The name of the hierarchy to build location masks for. Must be one of of the keys of the HIERARCHY_MAP constant. pm_data PopulationModelData object to load the population model data.
Returns
tuple[dict[str, slice], dict[int, tuple[slice, slice, npt.NDArray[np.bool_]]], npt.NDArray[np.uint32]] A three-tuple of: - climate_slice: a dict mapping "longitude"/"latitude" to slices bounding the location block, for subsetting climate rasters before processing (downstream operations scale with the number of pixels in the mask). - bounds_map: a dict mapping each location ID to (row_slice, col_slice, mask), where mask is a boolean array selecting that location's pixels within the sliced window. - location_mask: a 2D uint32 array where each location ID is written as a unique integer value.
Source code in src/climate_data/aggregate/utils.py
get_bbox(raster: rt.RasterArray, crs: str | None = None) -> shapely.Polygon
Get the bounding box of a raster array.
Parameters
raster The raster array to get the bounding box of. crs The CRS to return the bounding box in. If None, the bounding box is returned in the CRS of the raster.
Returns
shapely.Polybon The bounding box of the raster in the CRS specified by the crs parameter.