Skip to content

Commit

Permalink
Merge pull request pypsa-meets-earth#757 from ekatef/fix_in_runoff
Browse files Browse the repository at this point in the history
Fix runoff in case runoff data are missed for some hydropower plants
  • Loading branch information
davide-f authored Jul 3, 2023
2 parents d0d709d + 0a41411 commit 7900bc7
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,32 +461,40 @@ def attach_hydro(n, costs, ppl):
missing_plants = pd.Index(inflow_buses.unique()).difference(
inflow.indexes["plant"]
)
intersection_plants = pd.Index(
inflow_buses[inflow_buses.isin(inflow.indexes["plant"])]
)
# map power plants index (regions_onshore) into power plants ids (powerplants.csv)
# plants_to_keep correspond to "plant" index of regions_onshore
# plants_to_keep.index corresponds to bus_id of PyPSA network
plants_with_data = inflow_buses[inflow_buses.isin(inflow.indexes["plant"])]
plants_to_keep = plants_with_data.to_numpy()

# if missing time series are found, notify the user and exclude missing hydro plants
if not missing_plants.empty:
# original total p_nom
total_p_nom = ror.p_nom.sum() + hydro.p_nom.sum()
idxs_to_keep = inflow_buses[
inflow_buses.isin(intersection_plants)
].index
ror = ror.loc[ror.index.intersection(idxs_to_keep)]
hydro = hydro.loc[hydro.index.intersection(idxs_to_keep)]
# update plants_with_data to ensure proper match between "plant" index and bus_id
plants_with_data = inflow_buses[inflow_buses.isin(plants_to_keep)]
network_buses_to_keep = plants_with_data.index
plants_to_keep = plants_with_data.to_numpy()

ror = ror.loc[ror.index.intersection(network_buses_to_keep)]
hydro = hydro.loc[hydro.index.intersection(network_buses_to_keep)]
# loss of p_nom
loss_p_nom = ror.p_nom.sum() + hydro.p_nom.sum() - total_p_nom

logger.warning(
f"'{snakemake.input.profile_hydro}' is missing inflow time-series for at least one bus: {', '.join(missing_plants)}."
f"Corresponding hydro plants are dropped, corresponding to a total loss of {loss_p_nom}MW out of {total_p_nom}MW."
f"Corresponding hydro plants are dropped, corresponding to a total loss of {loss_p_nom:.2f}MW out of {total_p_nom:.2f}MW."
)

if not intersection_plants.empty:
# if there are any plants for which runoff data are available
if not plants_with_data.empty:
network_buses_to_keep = plants_with_data.index
plants_to_keep = plants_with_data.to_numpy()

inflow_t = (
inflow.sel(plant=intersection_plants)
inflow.sel(plant=plants_to_keep)
.rename({"plant": "name"})
.assign_coords(name=inflow_idx)
.assign_coords(name=network_buses_to_keep)
.transpose("time", "name")
.to_pandas()
)
Expand Down

0 comments on commit 7900bc7

Please sign in to comment.