33import geopandas as gpd
44import pandas as pd
55import rioxarray as rxr
6- import script_utils
76import xarray as xr
87from resample import _rasterize_regions
98
109
11- def report (shapes , area_potentials , csv_path , html_path , png_path ):
10+ def report (shapes , area_potentials , csv_path , html_path ):
1211 """Generate a report summarizing area potentials for different technologies."""
1312 shapes = gpd .read_parquet (shapes )
1413
15- ds_inputs = xr .Dataset ()
16-
17- # Collect the area potentials from the input files
18- for area_potential in area_potentials :
19- ds_inputs [area_potential ] = rxr .open_rasterio (
20- area_potential , mask_and_scale = True
21- )
22-
23- ds_inputs ["regions" ] = (
24- ("y" , "x" ),
25- _rasterize_regions (shapes , ds_inputs [area_potential ]),
14+ print ("Generating reference raster and rasterizing regions..." )
15+ reference_raster = rxr .open_rasterio (area_potentials [0 ])
16+ regions = xr .DataArray (
17+ _rasterize_regions (shapes , reference_raster ),
18+ dims = ("y" , "x" ),
19+ coords = {"y" : reference_raster .y , "x" : reference_raster .x },
2620 )
21+ # regions = xr.DataArray(("y", "x"), _rasterize_regions(shapes, reference_raster))
22+ del reference_raster
2723
28- script_utils .plot_all_dataset_variables (
29- ds_inputs , ncols = 2 , savefig = png_path , categorical_vars = ["regions" ]
30- )
31-
32- ds_inputs = ds_inputs .squeeze ().drop_vars (["band" , "spatial_ref" ])
33-
24+ # Collect the area potentials from the input files
3425 # Group the area potentials by regions, sum them up, and collect the resulting Series
3526 # into a DataFrame, where each column corresponds to a technology's area potential,
3627 # and the index corresponds to the regions.
3728 dataframes = []
38- for area_potential in area_potentials :
39- dataframes .append (
40- ds_inputs [area_potential ].groupby (ds_inputs ["regions" ]).sum ().to_pandas ()
29+ for area_potential_file in area_potentials :
30+ print (f"Processing area potential file: { area_potential_file } " )
31+ da_area_potential = (
32+ rxr .open_rasterio (area_potential_file , mask_and_scale = True )
33+ .squeeze ()
34+ .drop_vars (["band" , "spatial_ref" ])
4135 )
36+ dataframes .append (da_area_potential .groupby (regions ).sum ().to_pandas ())
37+ del da_area_potential
38+
4239 df = pd .concat (dataframes , axis = 1 )
4340
4441 # Add metadata columns from shapes in front of the data columns
@@ -64,5 +61,4 @@ def report(shapes, area_potentials, csv_path, html_path, png_path):
6461 snakemake .input .area_potentials ,
6562 snakemake .output .csv ,
6663 snakemake .output .html ,
67- snakemake .output .png ,
6864 )
0 commit comments