map_to_admin_2
01_prep_maps
batch_process_all_covariates(output_dir=None, skip_existing=True)
Process all covariates in the COVARIATE_DICT and convert them to netCDF.
Parameters:
output_dir : str or Path, optional Directory to save output files. If None, uses default directory. skip_existing : bool, default=True If True, skip processing covariates that already have output files.
Returns:
dict: Dictionary with covariate names as keys and output paths as values
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
create_multiyear_netcdf(input_path_template, years, output_path, variable_name)
Create a multi-year netCDF file from individual GeoTIFF files.
Parameters:
input_path_template : str Template path with {year} to be replaced for each year years : list of int List of years to process output_path : str Path to save the combined netCDF file variable_name : str Name of the variable in the output file
Returns:
xr.Dataset: The created dataset with time dimension
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
geotiff_to_netcdf(input_path, output_path, variable_name, resolution=None, year=None, encoding=None, global_extent=True, fill_value=0.0)
Convert a GeoTIFF file to netCDF format.
Parameters:
input_path : str Path to the input GeoTIFF file output_path : str Path to save the output netCDF file variable_name : str Name of the variable to use in the netCDF file resolution : float, optional Resolution of the raster in degrees. If None, will be calculated from the transform. year : int, optional Year for the data (for time dimension) encoding : dict, optional Encoding options for xarray.to_netcdf() global_extent : bool, default=False If True, extend the raster to global lat/lon coverage (-180 to 180, -90 to 90) fill_value : float, default=0.0 Value to fill extended areas with
Returns:
xr.Dataset: The created dataset
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
main()
Main function to run when script is executed directly.
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
process_multiyear_variable(covariate_key, output_dir)
Process a multi-year variable and convert to a single netCDF with time dimension.
Parameters:
covariate_key : str Key of the covariate in the COVARIATE_DICT
Returns:
str: Path to the output netCDF file
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
process_synoptic_variable(covariate_key, output_dir)
Process a synoptic (time-invariant) variable and convert to netCDF.
Parameters:
covariate_key : str Key of the covariate in the COVARIATE_DICT
Returns:
str: Path to the output netCDF file
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
set_file_permissions(file_path)
Set 775 permissions (rwxrwxr-x) on the specified file.
Source code in src/idd_forecast_mbp/map_to_admin_2/01_prep_maps.py
pixel_hierarchy
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/idd_forecast_mbp/map_to_admin_2/pixel_hierarchy.py
load_subset_hierarchy(subset_hierarchy: str) -> pd.DataFrame
Load a subset location hierarchy.
The subset hierarchy might be equal to the full aggregation hierarchy, but it might also be a subset of the full aggregation hierarchy. These hierarchies are used to provide different views of aggregated climate data.
Parameters
subset_hierarchy The administrative hierarchy to load (e.g. "gbd_2021")
Returns
pd.DataFrame The hierarchy data with parent-child relationships
Source code in src/idd_forecast_mbp/map_to_admin_2/pixel_hierarchy.py
post_process(df: pd.DataFrame, pop_df: pd.DataFrame) -> pd.DataFrame
Rename 000 to {summary_covariate}_per_capita Merge in population Create {summary_covariate}_capita*population -> {summary_covariate}
Source code in src/idd_forecast_mbp/map_to_admin_2/pixel_hierarchy.py
pixel_main
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/idd_forecast_mbp/map_to_admin_2/pixel_main.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.
Source code in src/idd_forecast_mbp/map_to_admin_2/pixel_main.py
load_raking_shapes(full_aggregation_hierarchy: str, bounds: tuple[float, float, float, float]) -> gpd.GeoDataFrame
Load shapes for a full aggregation hierarchy within given bounds.
Parameters
full_aggregation_hierarchy The full aggregation hierarchy to load (e.g. "gbd_2021") bounds The bounds to load (xmin, ymin, xmax, ymax)
Returns
gpd.GeoDataFrame The shapes for the given hierarchy and bounds
Source code in src/idd_forecast_mbp/map_to_admin_2/pixel_main.py
to_raster(ds: xr.DataArray, no_data_value: float | int, lat_col: str = 'lat', lon_col: str = 'lon', crs: str = 'EPSG:4326') -> rt.RasterArray
Convert an xarray DataArray to a RasterArray.
Parameters
ds The xarray DataArray to convert. no_data_value The value to use for missing data. This should be consistent with the dtype of the data. lat_col The name of the latitude coordinate in the dataset. lon_col The name of the longitude coordinate in the dataset. crs The coordinate reference system of the data.
Returns
rt.RasterArray The RasterArray representation of the input data.