Skip to content

Commit

Permalink
Add locate_bus to power model
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-f committed Nov 14, 2024
1 parent e155d5f commit e475981
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
12 changes: 8 additions & 4 deletions scripts/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ def locate_bus(
path_to_gadm=None,
gadm_clustering=False,
dropnull=True,
col_out=None,
):
"""
Function to locate the points of the dataframe df into the GADM shapefile.
Expand All @@ -1244,15 +1245,18 @@ def locate_bus(
List of target countries
gadm_level: int
GADM level to be used
path_to_gadm: str
path_to_gadm: str (default None)
Path to the GADM shapefile
gadm_clustering: bool
gadm_clustering: bool (default False)
True if gadm clustering is adopted
dropnull: bool
dropnull: bool (default True)
True if the rows with null values should be dropped
col_out: str (default gadm_{gadm_level})
Name of the output column
"""
if col_out is None:
col_out = "gadm_{}".format(gadm_level)
df = df[df.country.isin(countries)]
col_out = "gadm_{}".format(gadm_level)
df[col_out] = None
for co in countries:
gdf_shape, col = _get_shape_col_gdf(
Expand Down
32 changes: 9 additions & 23 deletions scripts/build_powerplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
from _helpers import (
configure_logging,
create_logger,
locate_bus,
read_csv_nafix,
to_csv_nafix,
two_digits_2_name_country,
Expand Down Expand Up @@ -371,28 +372,13 @@ def replace_natural_gas_technology(df: pd.DataFrame):
country_list = snakemake.params.countries
geo_crs = snakemake.params.geo_crs

gdf = gpd.read_file(snakemake.input.gadm_shapes)

def locate_bus(coords, co):
gdf_co = gdf[gdf["GADM_ID"].str.contains(co)]

point = Point(coords["lon"], coords["lat"])

try:
return gdf_co[gdf_co.contains(point)][
"GADM_ID"
].item() # filter gdf_co which contains point and returns the bus

except ValueError:
return gdf_co[
gdf_co.geometry == min(gdf_co.geometry, key=(point.distance))
][
"GADM_ID"
].item() # looks for closest one shape=node
# fixing https://github.com/pypsa-meets-earth/pypsa-earth/pull/670

ppl["region_id"] = ppl[["lon", "lat", "Country"]].apply(
lambda pp: locate_bus(pp[["lon", "lat"]], pp["Country"]), axis=1
)
ppl = locate_bus(
ppl.rename(columns={"lon": "x", "lat": "y", "Country": "country"}),
country_list,
gadm_layer_id,
snakemake.input.gadm_shapes,
snakemake.params.alternative_clustering,
col_out="region_id",
).rename(columns={"x": "lon", "y": "lat", "country": "Country"})

ppl.to_csv(snakemake.output.powerplants)
28 changes: 11 additions & 17 deletions scripts/cluster_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
configure_logging,
create_logger,
get_aggregation_strategies,
locate_bus,
update_p_nom_max,
)
from add_electricity import load_costs
Expand Down Expand Up @@ -379,23 +380,13 @@ def n_bounds(model, *n_id):


def busmap_for_gadm_clusters(inputs, n, gadm_level, geo_crs, country_list):
gdf = gpd.read_file(inputs.gadm_shapes)

def locate_bus(coords, co):
gdf_co = gdf[gdf["GADM_ID"].str.contains(co)]
point = Point(coords["x"], coords["y"])

try:
return gdf_co[gdf_co.contains(point)]["GADM_ID"].item()

except ValueError:
return gdf_co[
gdf_co.geometry == min(gdf_co.geometry, key=(point.distance))
]["GADM_ID"].item()

buses = n.buses
buses["gadm_{}".format(gadm_level)] = buses[["x", "y", "country"]].apply(
lambda bus: locate_bus(bus[["x", "y"]], bus["country"]), axis=1
buses = locate_bus(
n.buses,
country_list,
gadm_level,
inputs.gadm_shapes,
gadm_clustering=True,
)

buses["gadm_subnetwork"] = (
Expand Down Expand Up @@ -657,7 +648,10 @@ def cluster_regions(busmaps, inputs, output):
from _helpers import mock_snakemake

snakemake = mock_snakemake(
"cluster_network", network="elec", simpl="", clusters="min"
"cluster_network",
network="elec",
simpl="",
clusters="min",
)
configure_logging(snakemake)

Expand Down

0 comments on commit e475981

Please sign in to comment.