Skip to content

Commit

Permalink
Merge pull request #848 from openego/features/#575_carrier-names
Browse files Browse the repository at this point in the history
Features/#575 carrier names
  • Loading branch information
AmeliaNadal committed Aug 24, 2022
2 parents 6bcae7c + 07ee278 commit 2facea2
Showing 1 changed file with 134 additions and 13 deletions.
147 changes: 134 additions & 13 deletions src/egon/data/datasets/pypsaeursec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,90 @@ def read_network():
return pypsa.Network(str(target_file))


def clean_database():
"""Remove all components abroad for eGon100RE of the database
Remove all components abroad and their associated time series of
the datase for the scenario 'eGon100RE'.
Parameters
----------
None
Returns
-------
None
"""
scn_name = "eGon100RE"

comp_one_port = ["load", "generator", "store", "storage"]

# delete existing components and associated timeseries
for comp in comp_one_port:
db.execute_sql(
f"""
DELETE FROM {"grid.egon_etrago_" + comp + "_timeseries"}
WHERE {comp + "_id"} IN (
SELECT {comp + "_id"} FROM {"grid.egon_etrago_" + comp}
WHERE bus IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
AND scn_name = '{scn_name}'
);
DELETE FROM {"grid.egon_etrago_" + comp}
WHERE bus IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
AND scn_name = '{scn_name}';"""
)

comp_2_ports = [
"line",
"transformer",
"link",
]

for comp, id in zip(comp_2_ports, ["line_id", "trafo_id", "link_id"]):
db.execute_sql(
f"""
DELETE FROM {"grid.egon_etrago_" + comp + "_timeseries"}
WHERE scn_name = '{scn_name}'
AND {id} IN (
SELECT {id} FROM {"grid.egon_etrago_" + comp}
WHERE "bus0" IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
AND "bus1" IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
);
DELETE FROM {"grid.egon_etrago_" + comp}
WHERE scn_name = '{scn_name}'
AND "bus0" IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
AND "bus1" IN (
SELECT bus_id FROM grid.egon_etrago_bus
WHERE country != 'DE'
AND scn_name = '{scn_name}')
;"""
)

db.execute_sql(
"DELETE FROM grid.egon_etrago_bus "
"WHERE scn_name = '{scn_name}' "
"AND country <> 'DE'"
)


def neighbor_reduction():

network = read_network()
Expand Down Expand Up @@ -481,12 +565,6 @@ def neighbor_reduction():
# Connect to local database
engine = db.engine()

db.execute_sql(
"DELETE FROM grid.egon_etrago_bus "
"WHERE scn_name = 'eGon100RE' "
"AND country <> 'DE'"
)

neighbors["scn_name"] = "eGon100RE"
neighbors.index = neighbors["new_index"]

Expand All @@ -513,7 +591,6 @@ def neighbor_reduction():
neighbors = neighbors.drop(i, axis=1)

# Add geometry column

neighbors = (
gpd.GeoDataFrame(
neighbors, geometry=gpd.points_from_xy(neighbors.x, neighbors.y)
Expand All @@ -522,6 +599,16 @@ def neighbor_reduction():
.set_crs(4326)
)

# Unify carrier names
neighbors.carrier = neighbors.carrier.str.replace(" ", "_")
neighbors.carrier.replace(
{
"gas": "CH4",
"gas_for_industry": "CH4_for_industry",
},
inplace=True,
)

neighbors.to_postgis(
"egon_etrago_bus",
engine,
Expand Down Expand Up @@ -621,6 +708,21 @@ def links_to_etrago(neighbor_links, scn="eGon100RE"):
# Unify carrier names
neighbor_links.carrier = neighbor_links.carrier.str.replace(" ", "_")

neighbor_links.carrier.replace(
{
"H2_Electrolysis": "power_to_H2",
"H2_Fuel_Cell": "H2_to_power",
"H2_pipeline_retrofitted": "H2_retrofit",
"SMR": "CH4_to_H2",
"SMR_CC": "CH4_to_H2_CC",
"Sabatier": "H2_to_CH4",
"gas_for_industry": "CH4_for_industry",
"gas_for_industry_CC": "CH4_for_industry_CC",
"gas_pipeline": "CH4",
},
inplace=True,
)

neighbor_links.to_postgis(
"egon_etrago_link",
engine,
Expand All @@ -639,7 +741,6 @@ def links_to_etrago(neighbor_links, scn="eGon100RE"):
neighbor_gens["p_nom_extendable"] = False

# Unify carrier names

neighbor_gens.carrier = neighbor_gens.carrier.str.replace(" ", "_")

neighbor_gens.carrier.replace(
Expand Down Expand Up @@ -679,6 +780,7 @@ def links_to_etrago(neighbor_links, scn="eGon100RE"):
"DC": "AC",
"industry_electricity": "AC",
"H2_pipeline": "H2_system_boundary",
"gas_for_industry": "CH4_for_industry",
},
inplace=True,
)
Expand All @@ -699,10 +801,29 @@ def links_to_etrago(neighbor_links, scn="eGon100RE"):
neighbor_stores["scn_name"] = "eGon100RE"

# Unify carrier names

neighbor_stores.carrier = neighbor_stores.carrier.str.replace(" ", "_")

neighbor_stores.carrier.replace({"Li_ion": "battery"}, inplace=True)
neighbor_stores.carrier.replace(
{
"Li_ion": "battery",
"gas": "CH4",
},
inplace=True,
)
neighbor_stores.loc[
(
(neighbor_stores.e_nom_max <= 1e9)
& (neighbor_stores.carrier == "H2")
),
"carrier",
] = "H2_underground"
neighbor_stores.loc[
(
(neighbor_stores.e_nom_max > 1e9)
& (neighbor_stores.carrier == "H2")
),
"carrier",
] = "H2_overground"

for i in ["name", "p_set", "q_set", "e_nom_opt", "lifetime"]:
neighbor_stores = neighbor_stores.drop(i, axis=1)
Expand Down Expand Up @@ -849,16 +970,16 @@ def links_to_etrago(neighbor_links, scn="eGon100RE"):
execute_pypsa_eur_sec = False

if execute_pypsa_eur_sec:
tasks = (run_pypsa_eur_sec, neighbor_reduction)
tasks = (run_pypsa_eur_sec, clean_database, neighbor_reduction)
else:
tasks = neighbor_reduction
tasks = (clean_database, neighbor_reduction)


class PypsaEurSec(Dataset):
def __init__(self, dependencies):
super().__init__(
name="PypsaEurSec",
version="0.0.6",
version="0.0.7",
dependencies=dependencies,
tasks=tasks,
)

0 comments on commit 2facea2

Please sign in to comment.