Skip to content

Commit

Permalink
Merge pull request #310 from openego/fixes/#280-overwritten-ch4-forei…
Browse files Browse the repository at this point in the history
…gn-buses

Fixes/#280 overwritten ch4 foreign buses
  • Loading branch information
CarlosEpia authored Sep 12, 2024
2 parents 8a774f3 + 0fc2839 commit 56ad045
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 81 deletions.
171 changes: 95 additions & 76 deletions src/egon/data/datasets/gas_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,101 +293,120 @@ def insert_gas_buses_abroad(scn_name="eGon2035"):
gas_carrier = "CH4"
# Connect to local database
engine = db.engine()

db.execute_sql(
f"""
DELETE FROM grid.egon_etrago_bus WHERE "carrier" = '{gas_carrier}' AND
scn_name = '{scn_name}' AND country != 'DE';
"""
)

# Select the foreign buses
gdf_abroad_buses = central_buses_pypsaeur(sources, scenario=scn_name)
gdf_abroad_buses = gdf_abroad_buses.drop_duplicates(subset=["country"])
# for the eGon100RE scenario the CH4 buses are created by electrical_neighbours_egon100()
# therefore instead of created the buses, for this scenario the buses are just read.
if scn_name == "eGon100RE":
gdf_abroad_buses = geopandas.read_postgis(
f"""
SELECT * FROM grid.egon_etrago_bus WHERE "carrier" = '{gas_carrier}' AND
scn_name = '{scn_name}' AND country != 'DE';
""",
con=engine,
crs=4326,
)
gdf_abroad_buses.drop_duplicates(
subset="country", keep="first", inplace=True
)
return gdf_abroad_buses

# Select next id value
new_id = db.next_etrago_id("bus")
else:
db.execute_sql(
f"""
DELETE FROM grid.egon_etrago_bus WHERE "carrier" = '{gas_carrier}' AND
scn_name = '{scn_name}' AND country != 'DE';
"""
)

gdf_abroad_buses = gdf_abroad_buses.drop(
columns=[
"v_nom",
"v_mag_pu_set",
"v_mag_pu_min",
"v_mag_pu_max",
"geom",
"control",
"generator",
"location",
"unit",
"sub_network"
],
errors = "ignore"
)
gdf_abroad_buses["scn_name"] = scn_name
gdf_abroad_buses["carrier"] = gas_carrier
gdf_abroad_buses["bus_id"] = range(new_id, new_id + len(gdf_abroad_buses))
# Select the foreign buses
gdf_abroad_buses = central_buses_pypsaeur(sources, scenario=scn_name)
gdf_abroad_buses = gdf_abroad_buses.drop_duplicates(subset=["country"])

#Add central bus in Russia
gdf_abroad_buses = pd.concat(
[
gdf_abroad_buses,
pd.DataFrame(
index=["RU"],
data={
"scn_name": scn_name,
"bus_id": (new_id + len(gdf_abroad_buses) + 1),
"x": 41,
"y": 55,
"country": "RU",
"carrier": gas_carrier,
},
),
],
ignore_index=True,
)
# if in test mode, add bus in center of Germany
boundary = settings()["egon-data"]["--dataset-boundary"]
# Select next id value
new_id = db.next_etrago_id("bus")

if boundary != "Everything":
gdf_abroad_buses = gdf_abroad_buses.drop(
columns=[
"v_nom",
"v_mag_pu_set",
"v_mag_pu_min",
"v_mag_pu_max",
"geom",
"control",
"generator",
"location",
"unit",
"sub_network",
],
errors="ignore",
)
gdf_abroad_buses["scn_name"] = scn_name
gdf_abroad_buses["carrier"] = gas_carrier
gdf_abroad_buses["bus_id"] = range(
new_id, new_id + len(gdf_abroad_buses)
)

# Add central bus in Russia
gdf_abroad_buses = pd.concat(
[
gdf_abroad_buses,
pd.DataFrame(
index=[gdf_abroad_buses.index.max() + 1],
index=["RU"],
data={
"scn_name": scn_name,
"bus_id": (new_id + len(gdf_abroad_buses) + 1),
"x": 10.4234469,
"y": 51.0834196,
"country": "DE",
"x": 41,
"y": 55,
"country": "RU",
"carrier": gas_carrier,
},
),
],
ignore_index=True,
)
# if in test mode, add bus in center of Germany
boundary = settings()["egon-data"]["--dataset-boundary"]

if boundary != "Everything":
gdf_abroad_buses = pd.concat(
[
gdf_abroad_buses,
pd.DataFrame(
index=[gdf_abroad_buses.index.max() + 1],
data={
"scn_name": scn_name,
"bus_id": (new_id + len(gdf_abroad_buses) + 1),
"x": 10.4234469,
"y": 51.0834196,
"country": "DE",
"carrier": gas_carrier,
},
),
],
ignore_index=True,
)

gdf_abroad_buses = geopandas.GeoDataFrame(
gdf_abroad_buses,
geometry=geopandas.points_from_xy(
gdf_abroad_buses["x"], gdf_abroad_buses["y"]
),
)
gdf_abroad_buses = gdf_abroad_buses.rename(
columns={"geometry": "geom"}
).set_geometry("geom", crs=4326)
gdf_abroad_buses = geopandas.GeoDataFrame(
gdf_abroad_buses,
geometry=geopandas.points_from_xy(
gdf_abroad_buses["x"], gdf_abroad_buses["y"]
),
)
gdf_abroad_buses = gdf_abroad_buses.rename(
columns={"geometry": "geom"}
).set_geometry("geom", crs=4326)

# Insert to db
print(gdf_abroad_buses)
gdf_abroad_buses.to_postgis(
"egon_etrago_bus",
engine,
schema="grid",
index=False,
if_exists="append",
dtype={"geom": Geometry()},
)
return gdf_abroad_buses
# Insert to db
print(gdf_abroad_buses)
gdf_abroad_buses.to_postgis(
"egon_etrago_bus",
engine,
schema="grid",
index=False,
if_exists="append",
dtype={"geom": Geometry()},
)
return gdf_abroad_buses


def insert_gas_pipeline_list(
Expand Down Expand Up @@ -636,7 +655,7 @@ def insert_gas_pipeline_list(
gas_pipelines_list.loc[
gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0"
] = "NL" # bus "SEQ_10608_p" DE -> NL

if scn_name == "eGon100RE":
gas_pipelines_list = gas_pipelines_list[
gas_pipelines_list["country_1"] != "RU"
Expand Down
11 changes: 6 additions & 5 deletions src/egon/data/datasets/pypsaeur/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,12 @@ def clean_database():
;"""
)

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


Expand Down

0 comments on commit 56ad045

Please sign in to comment.