From 8e21e1dc079997b4895be0c8110ebece2bc2c273 Mon Sep 17 00:00:00 2001 From: Daniele Lerede Date: Mon, 13 Jan 2025 19:19:55 +0100 Subject: [PATCH 1/3] restore modifications for correct computation of AC hydro profiles --- scripts/add_electricity.py | 6 ++-- scripts/build_renewable_profiles.py | 53 +++++------------------------ 2 files changed, 10 insertions(+), 49 deletions(-) diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 84232f4d8..49d6e2b7c 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -488,10 +488,8 @@ def attach_hydro(n, costs, ppl): ror = ppl.query('technology == "Run-Of-River"') phs = ppl.query('technology == "Pumped Storage"') hydro = ppl.query('technology == "Reservoir"') - if snakemake.params.alternative_clustering: - bus_id = ppl["region_id"] - else: - bus_id = ppl["bus"] + + bus_id = ppl["bus"] inflow_idx = ror.index.union(hydro.index) if not inflow_idx.empty: diff --git a/scripts/build_renewable_profiles.py b/scripts/build_renewable_profiles.py index 77427534d..8c16bce09 100644 --- a/scripts/build_renewable_profiles.py +++ b/scripts/build_renewable_profiles.py @@ -356,9 +356,6 @@ def rescale_hydro(plants, runoff, normalize_using_yearly, normalization_year): logger.info("No bus has installed hydro plants, ignoring normalization.") return runoff - if snakemake.params.alternative_clustering: - plants = plants.set_index("shape_id") - years_statistics = normalize_using_yearly.index if isinstance(years_statistics, pd.DatetimeIndex): years_statistics = years_statistics.year @@ -533,24 +530,6 @@ def create_scaling_factor( # the region should be restricted for non-hydro technologies, as the hydro potential is calculated across hydrobasins which may span beyond the region of the country cutout = filter_cutout_region(cutout, regions) - if snakemake.params.alternative_clustering: - regions = gpd.GeoDataFrame( - regions.reset_index() - .groupby("shape_id") - .agg( - { - "x": "mean", - "y": "mean", - "country": "first", - "geometry": "first", - "bus": "first", - } - ) - .reset_index() - .set_index("bus"), - crs=regions.crs, - ) - buses = regions.index func = getattr(cutout, resource.pop("method")) @@ -577,17 +556,10 @@ def create_scaling_factor( # select busbar whose location (p) belongs to at least one hydrobasin geometry # if extendable option is true, all buses are included # otherwise only where hydro powerplants are available are considered - if snakemake.params.alternative_clustering: - filter_bus_to_consider = regions.index.map( - lambda bus_id: config.get("extendable", False) - | (bus_id in hydro_ppls.region_id.values) - ) - ### TODO: quickfix. above case and the below case should by unified - if snakemake.params.alternative_clustering == False: - filter_bus_to_consider = regions.index.map( - lambda bus_id: config.get("extendable", False) - | (bus_id in hydro_ppls.bus.values) - ) + filter_bus_to_consider = regions.index.map( + lambda bus_id: config.get("extendable", False) + | (bus_id in hydro_ppls.bus.values) + ) bus_to_consider = regions.index[filter_bus_to_consider] # identify subset of buses within the hydrobasins @@ -605,17 +577,10 @@ def create_scaling_factor( columns={"x": "lon", "y": "lat", "country": "countries"} ).loc[bus_in_hydrobasins, ["lon", "lat", "countries", "shape_id"]] - # TODO: these cases shall be fixed by restructuring the alternative clustering procedure - if snakemake.params.alternative_clustering == False: - resource["plants"]["installed_hydro"] = [ - True if (bus_id in hydro_ppls.bus.values) else False - for bus_id in resource["plants"].index - ] - else: - resource["plants"]["installed_hydro"] = [ - True if (bus_id in hydro_ppls.region_id.values) else False - for bus_id in resource["plants"].shape_id.values - ] + resource["plants"]["installed_hydro"] = [ + True if (bus_id in hydro_ppls.bus.values) else False + for bus_id in resource["plants"].index + ] # get normalization before executing runoff normalization = None @@ -631,8 +596,6 @@ def create_scaling_factor( else: # otherwise perform the calculations inflow = correction_factor * func(capacity_factor=True, **resource) - if snakemake.params.alternative_clustering: - inflow["plant"] = regions.shape_id.loc[inflow["plant"]].values if "clip_min_inflow" in config: inflow = inflow.where(inflow >= config["clip_min_inflow"], 0) From 162033bf5727d8c3b2c9719a803e3c9c7bb9f49b Mon Sep 17 00:00:00 2001 From: yerbol-akhmetov Date: Fri, 17 Jan 2025 21:22:23 +0500 Subject: [PATCH 2/3] reassign buses for hydro powerplants --- scripts/add_electricity.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 49d6e2b7c..3c5a83592 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -470,12 +470,26 @@ def attach_hydro(n, costs, ppl): _add_missing_carriers_from_costs(n, costs, carriers) + if snakemake.params.alternative_clustering: + # bus to region mapping for hydro powerplants + bus_to_region_id = ( + ppl.drop_duplicates(subset="bus").set_index("bus")["region_id"].to_dict() + ) + ppl = ( ppl.query('carrier == "hydro"') .reset_index(drop=True) .rename(index=lambda s: str(s) + " hydro") ) + with xr.open_dataarray(snakemake.input.profile_hydro) as inflow: + if snakemake.params.alternative_clustering: + for bus in inflow.indexes["plant"]: + if bus in bus_to_region_id: + region_to_update = bus_to_region_id[bus] + # Update all rows in the DataFrame where region_id matches + ppl.loc[ppl["region_id"] == region_to_update, "bus"] = bus + # Current fix, NaN technologies set to ROR if ppl.technology.isna().any(): n_nans = ppl.technology.isna().sum() From 52e66366dbf1a52cfecd4d475e977096c0b800ac Mon Sep 17 00:00:00 2001 From: yerbol-akhmetov Date: Sun, 19 Jan 2025 22:24:17 +0500 Subject: [PATCH 3/3] restyle reassigning buses part --- scripts/add_electricity.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 3c5a83592..0ebfefac4 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -470,20 +470,19 @@ def attach_hydro(n, costs, ppl): _add_missing_carriers_from_costs(n, costs, carriers) - if snakemake.params.alternative_clustering: - # bus to region mapping for hydro powerplants - bus_to_region_id = ( - ppl.drop_duplicates(subset="bus").set_index("bus")["region_id"].to_dict() - ) - ppl = ( ppl.query('carrier == "hydro"') .reset_index(drop=True) .rename(index=lambda s: str(s) + " hydro") ) - with xr.open_dataarray(snakemake.input.profile_hydro) as inflow: - if snakemake.params.alternative_clustering: + if snakemake.params.alternative_clustering: + # bus to region mapping for hydro powerplants + bus_to_region_id = ( + ppl.drop_duplicates(subset="bus").set_index("bus")["region_id"].to_dict() + ) + + with xr.open_dataarray(snakemake.input.profile_hydro) as inflow: for bus in inflow.indexes["plant"]: if bus in bus_to_region_id: region_to_update = bus_to_region_id[bus]