diff --git a/scripts/make_summary.py b/scripts/make_summary.py index 150c159e..c831fb9d 100644 --- a/scripts/make_summary.py +++ b/scripts/make_summary.py @@ -62,6 +62,13 @@ idx = pd.IndexSlice opt_name = {"Store": "e", "Line" : "s", "Transformer" : "s"} +def _add_indexed_rows(df, raw_index): + new_index = df.index|pd.MultiIndex.from_product(raw_index) + if isinstance(new_index, pd.Index): + new_index = pd.MultiIndex.from_tuples(new_index) + + return df.reindex(new_index) + def assign_carriers(n): if "carrier" not in n.loads: @@ -82,16 +89,17 @@ def assign_carriers(n): if "EU gas store" in n.stores.index and n.stores.loc["EU gas Store","carrier"] == "": n.stores.loc["EU gas Store","carrier"] = "gas Store" - def calculate_costs(n,label,costs): for c in n.iterate_components(n.branch_components|n.controllable_one_port_components^{"Load"}): capital_costs = c.df.capital_cost*c.df[opt_name.get(c.name,"p") + "_nom_opt"] capital_costs_grouped = capital_costs.groupby(c.df.carrier).sum() + + # Index tuple(s) indicating the newly to-be-added row(s) + raw_index = tuple([[c.list_name],["capital"],list(capital_costs_grouped.index)]) + costs = _add_indexed_rows(costs, raw_index) - costs = costs.reindex(costs.index|pd.MultiIndex.from_product([[c.list_name],["capital"],capital_costs_grouped.index])) - - costs.loc[idx[c.list_name,"capital",list(capital_costs_grouped.index)],label] = capital_costs_grouped.values + costs.loc[idx[raw_index],label] = capital_costs_grouped.values if c.name == "Link": p = c.pnl.p0.multiply(n.snapshot_weightings,axis=0).sum() @@ -137,8 +145,12 @@ def calculate_energy(n,label,energy): return energy def include_in_summary(summary, multiindexprefix, label, item): - summary = summary.reindex(summary.index | pd.MultiIndex.from_product([[p] for p in multiindexprefix] + [item.index])) - summary.loc[idx[tuple(multiindexprefix + [list(item.index)])], label] = item.values + + # Index tuple(s) indicating the newly to-be-added row(s) + raw_index = tuple([multiindexprefix,list(item.index)]) + summary = _add_indexed_rows(summary, raw_index) + + summary.loc[idx[raw_index], label] = item.values return summary def calculate_capacity(n,label,capacity): @@ -179,9 +191,12 @@ def calculate_supply(n,label,supply): continue s = c.pnl.p[items].max().multiply(c.df.loc[items,'sign']).groupby(c.df.loc[items,'carrier']).sum() - - supply = supply.reindex(supply.index|pd.MultiIndex.from_product([[i],[c.list_name],s.index])) - supply.loc[idx[i,c.list_name,list(s.index)],label] = s.values + + # Index tuple(s) indicating the newly to-be-added row(s) + raw_index = tuple([[i],[c.list_name],list(s.index)]) + supply = _add_indexed_rows(supply, raw_index) + + supply.loc[idx[raw_index],label] = s.values for c in n.iterate_components(n.branch_components): @@ -222,9 +237,12 @@ def calculate_supply_energy(n,label,supply_energy): continue s = c.pnl.p[items].sum().multiply(c.df.loc[items,'sign']).groupby(c.df.loc[items,'carrier']).sum() - - supply_energy = supply_energy.reindex(supply_energy.index|pd.MultiIndex.from_product([[i],[c.list_name],s.index])) - supply_energy.loc[idx[i,c.list_name,list(s.index)],label] = s.values + + # Index tuple(s) indicating the newly to-be-added row(s) + raw_index = tuple([[i],[c.list_name],list(s.index)]) + supply_energy = _add_indexed_rows(supply_energy, raw_index) + + supply_energy.loc[idx[raw_index],label] = s.values for c in n.iterate_components(n.branch_components): @@ -473,7 +491,7 @@ if __name__ == "__main__": for opts in expand_from_wildcard("opts")} print(networks_dict) - + dfs = make_summaries(networks_dict, country=snakemake.wildcards.country) to_csv(dfs)