From 101599a33be63bb5e70a155b872af2fde1c62e58 Mon Sep 17 00:00:00 2001 From: nesnoj Date: Wed, 6 Sep 2023 15:47:35 +0200 Subject: [PATCH 1/3] Fix RES share calculation for SQ --- digiplan/map/calculations.py | 23 +++++++++++++++++++++-- digiplan/map/charts.py | 4 ++-- digiplan/map/popups.py | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/digiplan/map/calculations.py b/digiplan/map/calculations.py index 207693e0..a1733522 100644 --- a/digiplan/map/calculations.py +++ b/digiplan/map/calculations.py @@ -202,6 +202,25 @@ def energy_shares_per_municipality() -> pd.DataFrame: """ Calculate energy shares of renewables from electric demand per municipality. + Returns + ------- + pd.DataFrame + Energy share per municipality (index) and technology (column) + """ + energies = energies_per_municipality() + demands = datapackage.get_power_demand() + total_demand = pd.concat([d["2022"] for d in demands.values()], axis=1).sum(axis=1) + energy_shares = energies.mul(1e3).div(total_demand, axis=0) + return energy_shares.mul(1e2) + + +def energy_shares_region() -> pd.DataFrame: + """ + Calculate energy shares of renewables from electric demand for region. + + Like energy_shares_per_municipality() but with weighted demand for correct + totals. + Returns ------- pd.DataFrame @@ -211,8 +230,8 @@ def energy_shares_per_municipality() -> pd.DataFrame: demands = datapackage.get_power_demand() total_demand = pd.concat([d["2022"] for d in demands.values()], axis=1).sum(axis=1) total_demand_share = total_demand / total_demand.sum() - energies = energies.reindex(range(20)) - return energies.mul(total_demand_share, axis=0) + energy_shares = energies.mul(1e3).div(total_demand, axis=0).mul(total_demand_share, axis=0).sum(axis=0) + return energy_shares.mul(1e2) def electricity_demand_per_municipality(year: int = 2022) -> pd.DataFrame: diff --git a/digiplan/map/charts.py b/digiplan/map/charts.py index 592bbef9..adf082dc 100644 --- a/digiplan/map/charts.py +++ b/digiplan/map/charts.py @@ -487,13 +487,13 @@ def get_chart_options(self) -> dict: class EnergyShareRegionChart(Chart): - """Chart for regional energy shares.""" + """Calculate RES energy shares for whole region.""" lookup = "capacity" def get_chart_data(self) -> None: """Calculate capacities for whole region.""" - return calculations.energy_shares_per_municipality().sum().round(1) + return calculations.energy_shares_region().round(1) def get_chart_options(self) -> dict: """Overwrite title and unit.""" diff --git a/digiplan/map/popups.py b/digiplan/map/popups.py index 80f71c29..3cceadc2 100644 --- a/digiplan/map/popups.py +++ b/digiplan/map/popups.py @@ -336,7 +336,7 @@ class EnergySharePopup(RegionPopup): title = _("Anteil Energie aus EE") def get_detailed_data(self) -> pd.DataFrame: # noqa: D102 - return calculations.energy_shares_per_municipality() + return calculations.energy_shares_per_municipality().round(1) def get_chart_options(self) -> dict: """Overwrite title and unit.""" From 51150b0e43c5b603ba29a78ff753c5cf7203a63b Mon Sep 17 00:00:00 2001 From: nesnoj Date: Wed, 6 Sep 2023 15:49:09 +0200 Subject: [PATCH 2/3] Add RES share for user scenario: calculation, charts, popup, choropleth --- config/settings/base.py | 7 ++++ digiplan/map/calculations.py | 35 +++++++++++++++++++ digiplan/map/charts.py | 21 +++++++++++ digiplan/map/choropleths.py | 8 ++++- digiplan/map/popups.py | 25 +++++++++++++ .../templates/components/panel_3_results.html | 2 +- 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 81277adb..6e71e874 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -404,6 +404,12 @@ def __getitem__(self, item): # noqa: D105, ANN001, ANN204 title=_("Anteil Erneuerbare Energien am Strombedarf"), unit="%", ), + setup.Choropleth( + "energy_share_2045", + layers=["municipality"], + title=_("Anteil Erneuerbare Energien am Strombedarf"), + unit="%", + ), setup.Choropleth( "energy_capita_statusquo", layers=["municipality"], @@ -510,6 +516,7 @@ def __getitem__(self, item): # noqa: D105, ANN001, ANN204 "energy_statusquo", "energy_2045", "energy_share_statusquo", + "energy_share_2045", "energy_capita_statusquo", "energy_capita_2045", "energy_square_statusquo", diff --git a/digiplan/map/calculations.py b/digiplan/map/calculations.py index a1733522..8afccef9 100644 --- a/digiplan/map/calculations.py +++ b/digiplan/map/calculations.py @@ -253,6 +253,41 @@ def electricity_demand_per_municipality(year: int = 2022) -> pd.DataFrame: return demands_per_sector * 1e-3 +def energy_shares_2045_per_municipality(simulation_id: int) -> pd.DataFrame: + """ + Calculate energy shares of renewables from electric demand per municipality in 2045. + + Returns + ------- + pd.DataFrame + Energy share per municipality (index) and technology (column) + """ + energies = energies_per_municipality_2045(simulation_id).mul(1e-3) + demands = electricity_demand_per_municipality_2045(simulation_id).sum(axis=1) + energy_shares = energies.div(demands, axis=0) + return energy_shares.astype(float).mul(1e2) + + +def energy_shares_2045_region(simulation_id: int) -> pd.DataFrame: + """ + Calculate energy shares of renewables from electric demand for region in 2045. + + Like energy_shares_2045_per_municipality() but with weighted demand for + correct totals. + + Returns + ------- + pd.DataFrame + Energy share per municipality (index) and technology (column) + """ + energies = energies_per_municipality_2045(simulation_id) + demands = electricity_demand_per_municipality_2045(simulation_id).sum(axis=1).mul(1e3) + + demand_share = demands / demands.sum() + energy_shares = energies.div(demands, axis=0).mul(demand_share, axis=0).sum(axis=0) + return energy_shares.astype(float).mul(1e2) + + def electricity_demand_per_municipality_2045(simulation_id: int) -> pd.DataFrame: """ Calculate electricity demand per sector per municipality in GWh in 2045. diff --git a/digiplan/map/charts.py b/digiplan/map/charts.py index adf082dc..1154b61b 100644 --- a/digiplan/map/charts.py +++ b/digiplan/map/charts.py @@ -503,6 +503,26 @@ def get_chart_options(self) -> dict: return chart_options +class EnergyShare2045RegionChart(SimulationChart): + """Chart for regional energy shares.""" + + lookup = "capacity" + + def get_chart_data(self) -> None: + """Calculate RES energy shares for whole region.""" + status_quo_data = calculations.energy_shares_region().round(1) + future_data = calculations.energy_shares_2045_region(self.simulation_id).round(1) + return list(zip(status_quo_data, future_data)) + + def get_chart_options(self) -> dict: + """Overwrite title and unit.""" + chart_options = super().get_chart_options() + del chart_options["title"]["text"] + chart_options["yAxis"]["name"] = _("%") + chart_options["xAxis"]["data"] = ["2022", "Dein\nSzenario"] + return chart_options + + class EnergyCapitaRegionChart(Chart): """Chart for regional energy shares per capita.""" @@ -956,6 +976,7 @@ def get_chart_options(self) -> dict: "energy_statusquo_region": EnergyRegionChart, "energy_2045_region": Energy2045RegionChart, "energy_share_statusquo_region": EnergyShareRegionChart, + "energy_share_2045_region": EnergyShare2045RegionChart, "energy_capita_statusquo_region": EnergyCapitaRegionChart, "energy_capita_2045_region": EnergyCapita2045RegionChart, "energy_square_statusquo_region": EnergySquareRegionChart, diff --git a/digiplan/map/choropleths.py b/digiplan/map/choropleths.py index 9409d07a..3cb6f62e 100644 --- a/digiplan/map/choropleths.py +++ b/digiplan/map/choropleths.py @@ -79,7 +79,12 @@ def render(self) -> JsonResponse: class EnergyShareChoropleth(Choropleth): # noqa: D101 def get_values_per_feature(self) -> dict[int, float]: # noqa: D102 - return calculations.energy_shares_per_municipality().sum(axis=1).to_dict() + return calculations.energy_shares_per_municipality().sum(axis=1).round().to_dict() + + +class EnergyShare2045Choropleth(Choropleth): # noqa: D101 + def get_values_per_feature(self) -> dict[int, float]: # noqa: D102 + return calculations.energy_shares_2045_per_municipality(self.map_state["simulation_id"]).sum(axis=1).to_dict() class EnergyChoropleth(Choropleth): # noqa: D101 @@ -271,6 +276,7 @@ def get_values_per_feature(self) -> dict[int, float]: # noqa: D102 "energy_statusquo": EnergyChoropleth, "energy_2045": Energy2045Choropleth, "energy_share_statusquo": EnergyShareChoropleth, + "energy_share_2045": EnergyShare2045Choropleth, "energy_capita_statusquo": EnergyCapitaChoropleth, "energy_capita_2045": EnergyCapita2045Choropleth, "energy_square_statusquo": EnergySquareChoropleth, diff --git a/digiplan/map/popups.py b/digiplan/map/popups.py index 3cceadc2..e4c635e5 100644 --- a/digiplan/map/popups.py +++ b/digiplan/map/popups.py @@ -346,6 +346,30 @@ def get_chart_options(self) -> dict: return chart_options +class EnergyShare2045Popup(RegionPopup): + """Popup to show energy shares.""" + + lookup = "capacity" + title = _("Anteil Energie aus EE") + + def get_detailed_data(self) -> pd.DataFrame: # noqa: D102 + return calculations.energy_shares_2045_per_municipality(self.map_state["simulation_id"]) + + def get_chart_options(self) -> dict: + """Overwrite title and unit.""" + chart_options = super().get_chart_options() + chart_options["title"]["text"] = _("Energieanteile pro Technologie") + chart_options["yAxis"]["name"] = _("%") + chart_options["xAxis"]["data"] = ["2022", "Dein Szenario"] + return chart_options + + def get_chart_data(self) -> Iterable: + """Create capacity chart data for SQ and future scenario.""" + status_quo_data = calculations.energy_shares_per_municipality().loc[self.selected_id].round(1) + future_data = super().get_chart_data().round(1) + return list(zip(status_quo_data, future_data)) + + class EnergyCapitaPopup(RegionPopup): """Popup to show energy shares per population.""" @@ -885,6 +909,7 @@ def get_chart_options(self) -> dict: "energy_statusquo": EnergyPopup, "energy_2045": Energy2045Popup, "energy_share_statusquo": EnergySharePopup, + "energy_share_2045": EnergyShare2045Popup, "energy_capita_statusquo": EnergyCapitaPopup, "energy_capita_2045": EnergyCapita2045Popup, "energy_square_statusquo": EnergySquarePopup, diff --git a/digiplan/templates/components/panel_3_results.html b/digiplan/templates/components/panel_3_results.html index 7edbfe2d..9afef3b7 100644 --- a/digiplan/templates/components/panel_3_results.html +++ b/digiplan/templates/components/panel_3_results.html @@ -32,7 +32,7 @@