Skip to content

Commit

Permalink
Merge pull request #840 from openego/feature/#797-move_function_assig…
Browse files Browse the repository at this point in the history
…n_busID

Move and merge the two assigne_gas_bus_id functions
  • Loading branch information
AmeliaNadal committed Jul 27, 2022
2 parents c8c2db7 + 86ee80e commit 303730c
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 103 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ Changed
`#787 <https://github.com/openego/eGon-data/issues/787>`_
* Update pypsa-eur-sec fork and store national demand time series
`#402 <https://github.com/openego/eGon-data/issues/402>`_
* Move and merge the two assign_gas_bus_id functions to a central place
`#797 <https://github.com/openego/eGon-data/issues/797>`_
* Add coordinates to non AC buses abroad in eGon100RE
`#803 <https://github.com/openego/eGon-data/issues/803>`_

Expand Down
42 changes: 3 additions & 39 deletions src/egon/data/datasets/ch4_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,44 +232,6 @@ def load_biogas_generators(scn_name):
return biogas_generators_list


def assign_bus_id(dataframe, scn_name, carrier):
"""Assigns bus_ids (for H2 buses) to points (contained in a dataframe) according to location
Parameters
----------
dataframe : pandas.DataFrame
DataFrame cointaining points
scn_name : str
Name of the scenario
carrier : str
Name of the carrier
Returns
-------
res : pandas.DataFrame
Dataframe including bus_id
"""

voronoi = db.select_geodataframe(
f"""
SELECT bus_id, geom FROM grid.egon_gas_voronoi
WHERE scn_name = '{scn_name}' AND carrier = '{carrier}';
""",
epsg=4326,
)

res = gpd.sjoin(dataframe, voronoi)
res["bus"] = res["bus_id"]
res = res.drop(columns=["index_right"])

# Assert that all power plants have a bus_id
assert (
res.bus.notnull().all()
), f"Some points are not attached to a {carrier} bus."

return res


def import_gas_generators(scn_name="eGon2035"):
"""Insert list of gas production units in database
Expand Down Expand Up @@ -306,7 +268,9 @@ def import_gas_generators(scn_name="eGon2035"):
CH4_generators_list = CH4_generators_list.assign(**c)

# Match to associated CH4 bus
CH4_generators_list = assign_bus_id(CH4_generators_list, scn_name, "CH4")
CH4_generators_list = db.assign_gas_bus_id(
CH4_generators_list, scn_name, "CH4"
)

# Remove useless columns
CH4_generators_list = CH4_generators_list.drop(columns=["geom", "bus_id"])
Expand Down
5 changes: 3 additions & 2 deletions src/egon/data/datasets/ch4_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from egon.data import config, db
from egon.data.config import settings
from egon.data.datasets import Dataset
from egon.data.datasets.ch4_prod import assign_bus_id
from egon.data.datasets.gas_grid import (
ch4_nodes_number_G,
define_gas_nodes_list,
Expand Down Expand Up @@ -125,7 +124,9 @@ def import_installed_ch4_storages(scn_name):

# Match to associated gas bus
Gas_storages_list = Gas_storages_list.reset_index(drop=True)
Gas_storages_list = assign_bus_id(Gas_storages_list, scn_name, "CH4")
Gas_storages_list = db.assign_gas_bus_id(
Gas_storages_list, scn_name, "CH4"
)

# Add missing columns
c = {"scn_name": scn_name, "carrier": "CH4"}
Expand Down
5 changes: 3 additions & 2 deletions src/egon/data/datasets/chp/match_nep.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from egon.data.datasets.chp.small_chp import assign_use_case
from egon.data.datasets.power_plants import (
assign_bus_id,
assign_gas_bus_id,
assign_voltage_level,
filter_mastr_geometry,
select_target,
Expand Down Expand Up @@ -503,7 +502,9 @@ def insert_large_chp(sources, target, EgonChp):
).bus_id

# Assign gas bus_id
insert_chp["gas_bus_id"] = assign_gas_bus_id(insert_chp_c).gas_bus_id
insert_chp["gas_bus_id"] = db.assign_gas_bus_id(
insert_chp_c, "eGon2035", "CH4"
).bus

insert_chp = assign_use_case(insert_chp, sources)

Expand Down
11 changes: 6 additions & 5 deletions src/egon/data/datasets/chp/small_chp.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from egon.data import config, db
from egon.data.datasets.power_plants import (
assign_bus_id,
assign_gas_bus_id,
filter_mastr_geometry,
select_target,
)
Expand Down Expand Up @@ -87,7 +86,9 @@ def existing_chp_smaller_10mw(sources, MaStR_konv, EgonChp):

# Assign gas bus_id
mastr_chp_c = mastr_chp.copy()
mastr_chp["gas_bus_id"] = assign_gas_bus_id(mastr_chp_c).gas_bus_id
mastr_chp["gas_bus_id"] = db.assign_gas_bus_id(
mastr_chp_c, "eGon2035", "CH4"
).bus

# Assign bus_id
mastr_chp["bus_id"] = assign_bus_id(
Expand Down Expand Up @@ -175,9 +176,9 @@ def extension_to_areas(

selected_areas = selected_areas.to_crs(4326)
# Assign gas bus_id
selected_areas["gas_bus_id"] = assign_gas_bus_id(
selected_areas.copy()
).gas_bus_id
selected_areas["gas_bus_id"] = db.assign_gas_bus_id(
selected_areas.copy(), "eGon2035", "CH4"
).bus

# Select randomly one area from the list of possible areas
# weighted by the share of demand
Expand Down
3 changes: 1 addition & 2 deletions src/egon/data/datasets/hydrogen_etrago/h2_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import geopandas as gpd

from egon.data import db
from egon.data.datasets.ch4_prod import assign_bus_id
from egon.data.datasets.etrago_setup import link_geom_from_buses
from egon.data.datasets.scenario_parameters import get_sector_parameters

Expand Down Expand Up @@ -74,7 +73,7 @@ def insert_h2_pipelines():
new_pipelines.set_crs(epsg=4326, inplace=True)

# find bus in H2_grid voronoi
new_pipelines = assign_bus_id(new_pipelines, "eGon2035", "H2_grid")
new_pipelines = db.assign_gas_bus_id(new_pipelines, "eGon2035", "H2_grid")
new_pipelines = new_pipelines.rename(columns={"bus_id": "bus1"}).drop(
columns=["bus"]
)
Expand Down
7 changes: 3 additions & 4 deletions src/egon/data/datasets/industrial_gas_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from egon.data import db
from egon.data.config import settings
from egon.data.datasets import Dataset
from egon.data.datasets.ch4_prod import assign_bus_id
from egon.data.datasets.etrago_helpers import (
finalize_bus_insertion,
initialise_bus_insertion,
Expand Down Expand Up @@ -169,7 +168,7 @@ def read_industrial_CH4_demand(scn_name="eGon2035"):
industrial_loads_list = read_industrial_demand(scn_name, carrier)

# Match to associated gas bus
industrial_loads_list = assign_bus_id(
industrial_loads_list = db.assign_gas_bus_id(
industrial_loads_list, scn_name, carrier
)

Expand Down Expand Up @@ -201,10 +200,10 @@ def read_industrial_H2_demand(scn_name="eGon2035"):
industrial_loads_list = read_industrial_demand(scn_name, "H2")
industrial_loads_list_copy = industrial_loads_list.copy()
# Match to associated gas bus
industrial_loads_list = assign_bus_id(
industrial_loads_list = db.assign_gas_bus_id(
industrial_loads_list, scn_name, "H2_grid"
)
industrial_loads_saltcavern = assign_bus_id(
industrial_loads_saltcavern = db.assign_gas_bus_id(
industrial_loads_list_copy, scn_name, "H2_saltcavern"
)

Expand Down
33 changes: 0 additions & 33 deletions src/egon/data/datasets/power_plants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,39 +537,6 @@ def assign_bus_id(power_plants, cfg):
return power_plants


def assign_gas_bus_id(power_plants):
"""Assigns gas_bus_ids to power plants according to location
Parameters
----------
power_plants : pandas.DataFrame
Power plants (including voltage level)
Returns
-------
power_plants : pandas.DataFrame
Power plants (including voltage level) and gas_bus_id
"""

gas_voronoi = db.select_geodataframe(
"""
SELECT * FROM grid.egon_gas_voronoi WHERE scn_name = 'eGon2035' AND
carrier = 'CH4'
""",
epsg=4326,
)

res = gpd.sjoin(power_plants, gas_voronoi)
res["gas_bus_id"] = res["bus_id"]

# Assert that all power plants have a gas_bus_id
assert res.gas_bus_id.notnull().all(), f"""Some power plants are
not attached to a gas bus: {res[res.gas_bus_id.isnull()]}"""

return res


def insert_hydro_biomass():
"""Insert hydro and biomass power plants in database
Expand Down
27 changes: 13 additions & 14 deletions src/egon/data/datasets/storages/pumped_hydro.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
The module containing code allocating pumped hydro plants based on
data from MaStR and NEP.
"""

import pandas as pd

from geopy.geocoders import Nominatim
from sqlalchemy.orm import sessionmaker
import geopandas as gpd
import egon.data.config
from egon.data import db, config
import pandas as pd

from egon.data import db
from egon.data.datasets.chp.match_nep import match_nep_chp
from egon.data.datasets.chp.small_chp import assign_use_case
from egon.data.datasets.power_plants import (
assign_voltage_level,
assign_bus_id,
assign_gas_bus_id,
assign_voltage_level,
filter_mastr_geometry,
select_target,
)
from egon.data.datasets.chp.match_nep import match_nep_chp
from egon.data.datasets.chp.small_chp import assign_use_case
from geopy.geocoders import Nominatim
from sqlalchemy.orm import sessionmaker


def select_nep_pumped_hydro():
""" Select pumped hydro plants from NEP power plants list
"""Select pumped hydro plants from NEP power plants list
Returns
Expand Down Expand Up @@ -58,7 +57,7 @@ def select_nep_pumped_hydro():


def select_mastr_pumped_hydro():
""" Select pumped hydro plants from MaStR
"""Select pumped hydro plants from MaStR
Returns
Expand Down Expand Up @@ -140,7 +139,7 @@ def match_storage_units(
consider_carrier=True,
consider_capacity=True,
):
""" Match storage_units (in this case only pumped hydro) from MaStR
"""Match storage_units (in this case only pumped hydro) from MaStR
to list of power plants from NEP
Parameters
Expand Down Expand Up @@ -255,7 +254,7 @@ def match_storage_units(


def get_location(unmatched):
""" Gets a geolocation for units which couldn't be matched using MaStR data.
"""Gets a geolocation for units which couldn't be matched using MaStR data.
Uses geolocator and the city name from NEP data to create longitude and
latitude for a list of unmatched units.
Expand Down
42 changes: 40 additions & 2 deletions src/egon/data/db.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import time

from psycopg2.errors import UniqueViolation, DeadlockDetected
from psycopg2.errors import DeadlockDetected, UniqueViolation
from sqlalchemy import create_engine, text
from sqlalchemy.exc import IntegrityError, OperationalError
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -163,7 +163,7 @@ def wrapped(*xs, **ks):


def select_dataframe(sql, index_col=None, warning=True):
""" Select data from local database as pandas.DataFrame
"""Select data from local database as pandas.DataFrame
Parameters
----------
Expand Down Expand Up @@ -351,3 +351,41 @@ def commit(*args, **kwargs):
return ret

return commit


def assign_gas_bus_id(dataframe, scn_name, carrier):
"""Assigns bus_ids to points (contained in a dataframe) according to location
Parameters
----------
dataframe : pandas.DataFrame
DataFrame cointaining points
scn_name : str
Name of the scenario
carrier : str
Name of the carrier
Returns
-------
res : pandas.DataFrame
Dataframe including bus_id
"""

voronoi = select_geodataframe(
f"""
SELECT bus_id, geom FROM grid.egon_gas_voronoi
WHERE scn_name = '{scn_name}' AND carrier = '{carrier}';
""",
epsg=4326,
)

res = gpd.sjoin(dataframe, voronoi)
res["bus"] = res["bus_id"]
res = res.drop(columns=["index_right"])

# Assert that all power plants have a bus_id
assert (
res.bus.notnull().all()
), f"Some points are not attached to a {carrier} bus."

return res

0 comments on commit 303730c

Please sign in to comment.