Skip to content

Commit

Permalink
Merge branch 'features/#1014-load-areas' into continuous-integration/…
Browse files Browse the repository at this point in the history
…run-everything-2022-11-10
  • Loading branch information
nesnoj committed Nov 30, 2022
2 parents 2ea3727 + 8579276 commit 2fafe22
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/egon/data/datasets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ distributed_industrial_demand:
demandregio:
schema: 'demand'
table: 'egon_demandregio_cts_ind'
scenarios: ["eGon2035", "eGon100RE"]
scenarios: ["eGon2021", "eGon2035", "eGon100RE"]
wz:
schema: 'demand'
table: 'egon_demandregio_wz'
Expand Down
63 changes: 37 additions & 26 deletions src/egon/data/datasets/industry/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

from sqlalchemy import ARRAY, Column, Float, Integer, String
from sqlalchemy.ext.declarative import declarative_base
import geopandas as gpd
import numpy as np
Expand All @@ -18,8 +17,8 @@

def identify_voltage_level(df):

"""Identify the voltage_level of a grid component based on its peak load and
defined thresholds.
"""Identify the voltage_level of a grid component based on its peak load
and defined thresholds.
Parameters
Expand All @@ -37,7 +36,8 @@ def identify_voltage_level(df):

df["voltage_level"] = np.nan

# Identify voltage_level for every demand area taking thresholds into account which were defined in the eGon project
# Identify voltage_level for every demand area taking thresholds into
# account which were defined in the eGon project
df.loc[df["peak_load"] <= 0.1, "voltage_level"] = 7
df.loc[df["peak_load"] > 0.1, "voltage_level"] = 6
df.loc[df["peak_load"] > 0.2, "voltage_level"] = 5
Expand All @@ -49,15 +49,16 @@ def identify_voltage_level(df):


def identify_bus(load_curves, demand_area):
"""Identify the grid connection point for a consumer by determining its grid level
based on the time series' peak load and the spatial intersection to mv
grid districts or ehv voronoi cells.
"""Identify the grid connection point for a consumer by determining its
grid level based on the time series' peak load and the spatial
intersection to mv grid districts or ehv voronoi cells.
Parameters
----------
load_curves : pandas.DataFrame
Demand timeseries per demand area (e.g. osm landuse area, industrial site)
Demand timeseries per demand area (e.g. osm landuse area, industrial
site)
demand_area: pandas.DataFrame
Dataframe with id and geometry of areas where an industrial demand
Expand All @@ -83,7 +84,8 @@ def identify_bus(load_curves, demand_area):
epsg=3035,
)

# Initialize dataframe to identify peak load per demand area (e.g. osm landuse area or industrial site)
# Initialize dataframe to identify peak load per demand area (e.g. osm
# landuse area or industrial site)
peak = pd.DataFrame(columns=["id", "peak_load"])
peak["id"] = load_curves.max(axis=0).index
peak["peak_load"] = load_curves.max(axis=0).values
Expand All @@ -96,12 +98,14 @@ def identify_bus(load_curves, demand_area):
# Identify all demand areas connected to HVMV buses
peak_hv = peak[peak["voltage_level"] > 1]

# Perform a spatial join between the centroid of the demand area and mv grid districts to identify grid connection point
# Perform a spatial join between the centroid of the demand area and mv
# grid districts to identify grid connection point
peak_hv["centroid"] = peak_hv["geom"].centroid
peak_hv = peak_hv.set_geometry("centroid")
peak_hv_c = gpd.sjoin(peak_hv, griddistrict, how="inner", op="intersects")

# Perform a spatial join between the polygon of the demand area and mv grid districts to ensure every area got assign to a bus
# Perform a spatial join between the polygon of the demand area and mv
# grid districts to ensure every area got assign to a bus
peak_hv_p = peak_hv[~peak_hv.isin(peak_hv_c)].dropna().set_geometry("geom")
peak_hv_p = gpd.sjoin(
peak_hv_p, griddistrict, how="inner", op="intersects"
Expand All @@ -122,7 +126,8 @@ def identify_bus(load_curves, demand_area):
# Identify all demand areas connected to EHV buses
peak_ehv = peak[peak["voltage_level"] == 1]

# Perform a spatial join between the centroid of the demand area and ehv voronoi to identify grid connection point
# Perform a spatial join between the centroid of the demand area and ehv
# voronoi to identify grid connection point
peak_ehv["centroid"] = peak_ehv["geom"].centroid
peak_ehv = peak_ehv.set_geometry("centroid")
peak_ehv = gpd.sjoin(peak_ehv, ehv_voronoi, how="inner", op="intersects")
Expand All @@ -142,7 +147,8 @@ def identify_bus(load_curves, demand_area):


def calc_load_curves_ind_osm(scenario):
"""Temporal disaggregate electrical demand per osm industrial landuse area.
"""Temporal disaggregate electrical demand per osm industrial landuse
area.
Parameters
Expand All @@ -153,8 +159,8 @@ def calc_load_curves_ind_osm(scenario):
Returns
-------
pandas.DataFrame
Demand timeseries of industry allocated to osm landuse areas and aggregated
per substation id
Demand timeseries of industry allocated to osm landuse areas and
aggregated per substation id
"""

Expand Down Expand Up @@ -237,7 +243,8 @@ def calc_load_curves_ind_osm(scenario):


def insert_osm_ind_load():
"""Inserts electrical industry loads assigned to osm landuse areas to the database
"""Inserts electrical industry loads assigned to osm landuse areas to the
database.
Returns
-------
Expand All @@ -249,7 +256,7 @@ def insert_osm_ind_load():
"targets"
]

for scenario in ["eGon2035", "eGon100RE"]:
for scenario in ["eGon2021", "eGon2035", "eGon100RE"]:

# Delete existing data from database
db.execute_sql(
Expand All @@ -263,7 +270,8 @@ def insert_osm_ind_load():
db.execute_sql(
f"""
DELETE FROM
{targets['osm_load_individual']['schema']}.{targets['osm_load_individual']['table']}
{targets['osm_load_individual']['schema']}.
{targets['osm_load_individual']['table']}
WHERE scn_name = '{scenario}'
"""
)
Expand Down Expand Up @@ -300,7 +308,8 @@ def insert_osm_ind_load():


def calc_load_curves_ind_sites(scenario):
"""Temporal disaggregation of load curves per industrial site and industrial subsector.
"""Temporal disaggregation of load curves per industrial site and
industrial subsector.
Parameters
Expand All @@ -311,8 +320,8 @@ def calc_load_curves_ind_sites(scenario):
Returns
-------
pandas.DataFrame
Demand timeseries of industry allocated to industrial sites and aggregated
per substation id and industrial subsector
Demand timeseries of industry allocated to industrial sites and
aggregated per substation id and industrial subsector
"""
sources = egon.data.config.datasets()["electrical_load_curves_industry"][
Expand Down Expand Up @@ -340,12 +349,13 @@ def calc_load_curves_ind_sites(scenario):
epsg=3035,
)

# Replace entries to bring it in line with demandregio's subsector definitions
# Replace entries to bring it in line with demandregio's subsector
# definitions
demands_ind_sites.replace(1718, 17, inplace=True)
share_wz_sites = demands_ind_sites.copy()

# Create additional df on wz_share per industrial site, which is always set to one
# as the industrial demand per site is subsector specific
# Create additional df on wz_share per industrial site, which is always
# set to one as the industrial demand per site is subsector specific

share_wz_sites.demand = 1
share_wz_sites.reset_index(inplace=True)
Expand Down Expand Up @@ -402,7 +412,8 @@ def calc_load_curves_ind_sites(scenario):


def insert_sites_ind_load():
"""Inserts electrical industry loads assigned to osm landuse areas to the database
"""Inserts electrical industry loads assigned to osm landuse areas to the
database.
Returns
-------
Expand All @@ -414,7 +425,7 @@ def insert_sites_ind_load():
"targets"
]

for scenario in ["eGon2035", "eGon100RE"]:
for scenario in ["eGon2021", "eGon2035", "eGon100RE"]:

# Delete existing data from database
db.execute_sql(
Expand Down
4 changes: 4 additions & 0 deletions src/egon/data/datasets/loadarea/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
**Load Areas**
TBD
Note: industrial demand contains:
* voltage levels 4-7
* only demand from ind. sites+osm located in LA!
"""

import os
Expand Down
115 changes: 63 additions & 52 deletions src/egon/data/datasets/loadarea/loadareas_add_demand_ind.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,69 @@ __url__ = "https://github.com/openego/eGon-data/blob/main/LICENSE"
__author__ = "nesnoj"
*/

------------------------
-- Scenario: eGon2021 --
------------------------

-- Add industrial consumption and peak load
-- 1) Industry from OSM landuse areas
UPDATE demand.egon_loadarea AS t1
SET
sector_peakload_industrial = t2.peak_load,
sector_consumption_industrial = t2.demand
FROM (
SELECT a.id AS id,
SUM(b.demand)::float AS demand,
SUM(b.peak_load)::float AS peak_load
FROM demand.egon_loadarea AS a,
(
SELECT
sum(ind_osm.demand) as demand,
sum(ind_osm.peak_load) as peak_load,
ST_PointOnSurface(landuse.geom) AS geom_surfacepoint
FROM
openstreetmap.osm_landuse as landuse,
demand.egon_osm_ind_load_curves_individual as ind_osm
WHERE
ind_osm.scn_name = 'eGon2021' AND
ind_osm.osm_id = landuse.id
GROUP BY landuse.id
) AS b
WHERE a.geom && b.geom_surfacepoint AND
ST_CONTAINS(a.geom, b.geom_surfacepoint)
GROUP BY a.id
) AS t2
WHERE t1.id = t2.id;

-- 2) Industry from industrial sites
UPDATE demand.egon_loadarea AS t1
SET
sector_peakload_industrial = sector_peakload_industrial + t2.peak_load,
sector_consumption_industrial = sector_consumption_industrial + t2.demand
FROM (
SELECT a.id AS id,
SUM(b.demand)::float AS demand,
SUM(b.peak_load)::float AS peak_load
FROM demand.egon_loadarea AS a,
(
SELECT
ind_sites.id,
ind_loads.demand,
ind_loads.peak_load,
ST_TRANSFORM(ind_sites.geom, 3035) as geom
FROM
demand.egon_industrial_sites as ind_sites,
demand.egon_sites_ind_load_curves_individual as ind_loads
WHERE
ind_loads.scn_name = 'eGon2021' AND
ind_loads.site_id = ind_sites.id
) AS b
WHERE a.geom && b.geom AND
ST_CONTAINS(a.geom, b.geom)
GROUP BY a.id
) AS t2
WHERE t1.id = t2.id;

------------------------
-- Scenario: eGon2035 --
------------------------
Expand Down Expand Up @@ -132,55 +195,3 @@ UPDATE demand.egon_loadarea AS t1
GROUP BY a.id
) AS t2
WHERE t1.id = t2.id;

------------------------
-- Scenario: eGon2021 --
------------------------
-- Update values for status quo scenario

UPDATE demand.egon_loadarea AS t1
SET
sector_consumption_industrial = sector_consumption_industrial_2035 * t2.scaling_factor,
sector_peakload_industrial = sector_peakload_industrial_2035 * t2.scaling_factor
FROM (
SELECT
la.id,
dr.scaling_factor
FROM
demand.egon_loadarea as la
LEFT JOIN
(
SELECT
a.nuts3 as nuts,
a.demand / b.demand as scaling_factor
FROM (
SELECT
nuts3,
sum(demand) as demand
FROM demand.egon_demandregio_cts_ind
WHERE
wz in (
SELECT wz FROM demand.egon_demandregio_wz WHERE sector = 'industry'
) AND
scenario = 'eGon2021'
GROUP BY nuts3
ORDER BY nuts3
) AS a,
(
SELECT
nuts3,
sum(demand) as demand
FROM demand.egon_demandregio_cts_ind
WHERE
wz in (
SELECT wz FROM demand.egon_demandregio_wz WHERE sector = 'industry'
) AND
scenario = 'eGon2035'
GROUP BY nuts3
ORDER BY nuts3
) AS b
WHERE a.nuts3 = b.nuts3
) as dr
ON la.nuts = dr.nuts
) as t2
WHERE t1.id = t2.id;

0 comments on commit 2fafe22

Please sign in to comment.