diff --git a/src/egon/data/datasets/gas_grid.py b/src/egon/data/datasets/gas_grid.py index a956d3c8f..9a0812875 100755 --- a/src/egon/data/datasets/gas_grid.py +++ b/src/egon/data/datasets/gas_grid.py @@ -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( @@ -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" diff --git a/src/egon/data/datasets/pypsaeur/__init__.py b/src/egon/data/datasets/pypsaeur/__init__.py index c39a52a0a..c26db2566 100755 --- a/src/egon/data/datasets/pypsaeur/__init__.py +++ b/src/egon/data/datasets/pypsaeur/__init__.py @@ -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' + """ )