Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 27, 2024
1 parent f81dc56 commit ed324df
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions scripts/base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ def get_country(df):

def _find_closest_links(links, new_links, distance_upper_bound=1.5):
treecoords = np.asarray(
[np.asarray(shapely.wkt.loads(s))[[0, -1]].flatten()
for s in links.geometry]
[np.asarray(shapely.wkt.loads(s))[[0, -1]].flatten() for s in links.geometry]
)
querycoords = np.vstack(
[new_links[["x1", "y1", "x2", "y2"]], new_links[["x2", "y2", "x1", "y1"]]]
)
tree = sp.spatial.KDTree(treecoords)
dist, ind = tree.query(
querycoords, distance_upper_bound=distance_upper_bound)
dist, ind = tree.query(querycoords, distance_upper_bound=distance_upper_bound)
found_b = ind < len(links)
found_i = np.arange(len(new_links) * 2)[found_b] % len(new_links)

Expand All @@ -120,8 +118,7 @@ def _load_buses_from_osm(fp_buses):
buses = buses.loc[:, ~buses.columns.str.contains("^Unnamed")]
buses["v_nom"] /= 1e3
buses["carrier"] = buses.pop("dc").map({True: "DC", False: "AC"})
buses["under_construction"] = buses["under_construction"].fillna(
False).astype(bool)
buses["under_construction"] = buses["under_construction"].fillna(False).astype(bool)
buses["x"] = buses["lon"]
buses["y"] = buses["lat"]
# TODO: Drop NAN maybe somewhere else?
Expand All @@ -138,8 +135,7 @@ def add_underwater_links(n, fp_offshore_shapes):
if offshore_shape is None or offshore_shape.is_empty:
n.links["underwater_fraction"] = 0.0
else:
links = gpd.GeoSeries(
n.links.geometry.dropna().map(shapely.wkt.loads))
links = gpd.GeoSeries(n.links.geometry.dropna().map(shapely.wkt.loads))
n.links["underwater_fraction"] = (
links.intersection(offshore_shape).length / links.length
)
Expand Down Expand Up @@ -197,8 +193,7 @@ def _load_lines_from_osm(fp_osm_lines):

lines["length"] /= 1e3 # m to km conversion
lines["v_nom"] /= 1e3 # V to kV conversion
lines = lines.loc[:, ~lines.columns.str.contains(
"^Unnamed")] # remove unnamed col
lines = lines.loc[:, ~lines.columns.str.contains("^Unnamed")] # remove unnamed col
# lines = _remove_dangling_branches(lines, buses) # TODO: add dangling branch removal?

return lines
Expand Down Expand Up @@ -228,8 +223,7 @@ def _load_links_from_osm(fp_osm_converters, base_network_config, voltages_config

links["length"] /= 1e3 # m to km conversion
links["v_nom"] /= 1e3 # V to kV conversion
links = links.loc[:, ~links.columns.str.contains(
"^Unnamed")] # remove unnamed col
links = links.loc[:, ~links.columns.str.contains("^Unnamed")] # remove unnamed col
# links = _remove_dangling_branches(links, buses) # TODO: add dangling branch removal?

return links
Expand Down Expand Up @@ -407,18 +401,15 @@ def _set_lines_s_nom_from_linetypes(n):

def _remove_dangling_branches(branches, buses):
return pd.DataFrame(
branches.loc[branches.bus0.isin(
buses.index) & branches.bus1.isin(buses.index)]
branches.loc[branches.bus0.isin(buses.index) & branches.bus1.isin(buses.index)]
)


def _set_countries_and_substations(inputs, base_network_config, countries_config, n):
countries = countries_config
country_shapes = gpd.read_file(
inputs.country_shapes).set_index("name")["geometry"]
country_shapes = gpd.read_file(inputs.country_shapes).set_index("name")["geometry"]

offshore_shapes = unary_union(
gpd.read_file(inputs.offshore_shapes)["geometry"])
offshore_shapes = unary_union(gpd.read_file(inputs.offshore_shapes)["geometry"])

buses = n.buses
bus_locations = buses
Expand Down Expand Up @@ -469,8 +460,7 @@ def _set_countries_and_substations(inputs, base_network_config, countries_config
assert (
not df.empty
), "No buses with defined country within 200km of bus `{}`".format(b)
n.buses.at[b, "country"] = df.loc[df.pathlength.idxmin(),
"country"]
n.buses.at[b, "country"] = df.loc[df.pathlength.idxmin(), "country"]

logger.warning(
"{} buses are not in any country or offshore shape,"
Expand Down Expand Up @@ -502,8 +492,7 @@ def base_network(
lines_ac = lines[lines.tag_frequency.astype(float) != 0].copy()
lines_dc = lines[lines.tag_frequency.astype(float) == 0].copy()

lines_ac = _set_electrical_parameters_lines(
lines_config, voltages_config, lines_ac)
lines_ac = _set_electrical_parameters_lines(lines_config, voltages_config, lines_ac)

lines_dc = _set_electrical_parameters_dc_lines(
lines_config, voltages_config, lines_dc
Expand All @@ -512,8 +501,7 @@ def base_network(
transformers = _set_electrical_parameters_transformers(
transformers_config, transformers
)
converters = _set_electrical_parameters_converters(
links_config, converters)
converters = _set_electrical_parameters_converters(links_config, converters)

n = pypsa.Network()
n.name = "PyPSA-Earth"
Expand Down Expand Up @@ -556,8 +544,7 @@ def base_network(

_set_lines_s_nom_from_linetypes(n)

_set_countries_and_substations(
inputs, base_network_config, countries_config, n)
_set_countries_and_substations(inputs, base_network_config, countries_config, n)

_set_dc_underwater_fraction(n.lines, inputs.offshore_shapes)
_set_dc_underwater_fraction(n.links, inputs.offshore_shapes)
Expand Down

0 comments on commit ed324df

Please sign in to comment.