Skip to content

Commit f60e77b

Browse files
committed
Add curtailment to plot
1 parent 6ed6a8f commit f60e77b

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

switch_model/generators/core/dispatch.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -796,14 +796,23 @@ def graph_total_dispatch(tools):
796796
)
797797
def energy_balance(tools):
798798
# Get dispatch dataframe
799-
cols = ["timestamp", "gen_tech", "gen_energy_source", "DispatchGen_MW", "scenario_name", "scenario_index"]
799+
cols = ["timestamp", "gen_tech", "gen_energy_source", "DispatchGen_MW", "scenario_name", "scenario_index",
800+
"Curtailment_MW"]
800801
df = tools.get_dataframe("dispatch.csv", drop_scenario_info=False)[cols]
801802
df = tools.transform.gen_type(df)
803+
804+
# Rename and add needed columns
805+
df["Dispatch Limit"] = df["DispatchGen_MW"] + df["Curtailment_MW"]
806+
df = df.drop("Curtailment_MW", axis=1)
807+
df = df.rename({"DispatchGen_MW": "Dispatch"}, axis=1)
802808
# Sum dispatch across all the projects of the same type and timepoint
803-
df = df.groupby(["timestamp", "gen_type", "scenario_name", "scenario_index"], as_index=False).sum()
804-
df = df.rename({"gen_type": "Type", "DispatchGen_MW": "value"}, axis=1)
809+
key_columns = ["timestamp", "gen_type", "scenario_name", "scenario_index"]
810+
df = df.groupby(key_columns, as_index=False).sum()
811+
df = df.melt(id_vars=key_columns, value_vars=["Dispatch", "Dispatch Limit"], var_name="Type")
812+
df = df.rename({"gen_type": "Source"}, axis=1)
805813

806-
discharge = df[df["Type"] == "Storage"].drop("Type", axis=1).rename({"value": "discharge"}, axis=1)
814+
discharge = df[(df["Source"] == "Storage") & (df["Type"] == "Dispatch")].drop(["Source", "Type"], axis=1).rename(
815+
{"value": "discharge"}, axis=1)
807816

808817
# Get load dataframe
809818
load = tools.get_dataframe("load_balance.csv", drop_scenario_info=False)
@@ -831,7 +840,8 @@ def energy_balance(tools):
831840
"StorageNetCharge": "Storage Net Flow",
832841
"zone_demand_mw": "Demand",
833842
}, axis=1).sort_index(axis=1)
834-
load = load.melt(id_vars=key_columns, var_name="Type")
843+
load = load.melt(id_vars=key_columns, var_name="Source")
844+
load["Type"] = "Dispatch"
835845

836846
# Merge dispatch contributions with load contributions
837847
df = pd.concat([load, df])
@@ -845,9 +855,13 @@ def energy_balance(tools):
845855
FREQUENCY = "1W"
846856

847857
def groupby_time(df):
848-
return df.groupby(
849-
["scenario_name", "period", "Type", tools.pd.Grouper(key="datetime", freq=FREQUENCY, origin="start")])[
850-
"value"]
858+
return df.groupby([
859+
"scenario_name",
860+
"period",
861+
"Source",
862+
"Type",
863+
tools.pd.Grouper(key="datetime", freq=FREQUENCY, origin="start")
864+
])["value"]
851865

852866
df = groupby_time(df).sum().reset_index()
853867

@@ -856,11 +870,12 @@ def groupby_time(df):
856870
soc = soc.rename({"STORAGE_GEN_TPS_2": "timepoint", "StateOfCharge": "value"}, axis=1)
857871
# Sum over all the projects that are in the same scenario with the same timepoint
858872
soc = soc.groupby(["timepoint", "scenario_name"], as_index=False).sum()
859-
soc["Type"] = "State Of Charge"
873+
soc["Source"] = "State Of Charge"
860874
soc["value"] /= 1e6 # Convert to TWh
861875

862876
# Group by time
863877
soc = tools.transform.timestamp(soc, use_timepoint=True, key_col="timepoint").astype({"period": str})
878+
soc["Type"] = "Dispatch"
864879
soc = groupby_time(soc).mean().reset_index()
865880

866881
# Add state of charge to dataframe
@@ -883,7 +898,7 @@ def groupby_time(df):
883898
num_periods = df["period"].nunique()
884899
pn = tools.pn
885900
plot = pn.ggplot(df) + \
886-
pn.geom_line(pn.aes(x="day", y="value", color="Type")) + \
901+
pn.geom_line(pn.aes(x="day", y="value", color="Source", linetype="Type")) + \
887902
pn.facet_grid("period ~ scenario_name") + \
888903
pn.labs(y="Contribution to Energy Balance (TWh)") + \
889904
pn.scales.scale_color_manual(values=colors, aesthetics="color", na_value=colors["Other"]) + \
@@ -892,6 +907,9 @@ def groupby_time(df):
892907
labels=["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
893908
breaks=(15, 46, 76, 106, 137, 167, 198, 228, 259, 289, 319, 350),
894909
limits=(0, 366)) + \
910+
pn.scales.scale_linetype_manual(
911+
values={"Dispatch Limit": "dotted", "Dispatch": "solid"}
912+
) + \
895913
pn.theme(
896914
figure_size=(pn.options.figure_size[0] * tools.num_scenarios, pn.options.figure_size[1] * num_periods))
897915

0 commit comments

Comments
 (0)