diff --git a/etrago/tools/plot.py b/etrago/tools/plot.py index 11bee6b3..184744ff 100644 --- a/etrago/tools/plot.py +++ b/etrago/tools/plot.py @@ -25,7 +25,6 @@ import logging import os -from etrago.tools.execute import import_gen_from_links from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse @@ -35,6 +34,8 @@ import numpy as np import pandas as pd +from etrago.tools.execute import import_gen_from_links + cartopy_present = True try: import cartopy.crs as ccrs @@ -115,7 +116,7 @@ def plot_osm(x, y, zoom, alpha=0.4): ) plotter.plot(ax, alpha=alpha) # ax.plot(x, y, "ro-") - return fig, ax + return fig, ax, extent.xrange, extent.yrange def coloring(): @@ -1123,7 +1124,7 @@ def nodal_gen_dispatch( if osm is not False: if set_epsg_network.counter == 0: set_epsg_network(network) - fig, ax = plot_osm(osm["x"], osm["y"], osm["zoom"]) + fig, ax, xrange, yrange = plot_osm(osm["x"], osm["y"], osm["zoom"]) elif (osm is False) and cartopy_present: fig, ax = plt.subplots( subplot_kw={"projection": ccrs.PlateCarree()}, figsize=(5, 5) @@ -1584,54 +1585,24 @@ def calc_dc_loading(network, timesteps): DC line loading in MW """ - # Aviod covering of bidirectional links - network.links["linked_to"] = 0 - for i, row in network.links.iterrows(): - if not ( - network.links.index[ - (network.links.bus0 == row["bus1"]) - & (network.links.bus1 == row["bus0"]) - & (network.links.length == row["length"]) - ] - ).empty: - links_df = network.links.index[ - (network.links.bus0 == row["bus1"]) - & (network.links.bus1 == row["bus0"]) - & (network.links.length == row["length"]) - ] - - network.links.at[i, "linked_to"] = links_df.values[0] + dc_links = network.links.loc[network.links.carrier == "DC", :] - network.links.linked_to = network.links.linked_to.astype(str) - # Set p_nom_max and line_loading for one directional links link_load = network.links_t.p0[ - network.links.index[network.links.linked_to == "0"] + network.links.index[network.links.carrier == "DC"] ] - p_nom_opt_max = network.links.p_nom_opt[network.links.linked_to == "0"] - - # Set p_nom_max and line_loading for bidirectional links - for i, row in network.links[network.links.linked_to != "0"].iterrows(): - load = pd.DataFrame( - index=network.links_t.p0.index, columns=["to", "from"] - ) - load["to"] = network.links_t.p0[row["linked_to"]] - load["from"] = network.links_t.p0[i] - link_load[i] = load.abs().max(axis=1) - p_nom_opt_max[i] = max( - row.p_nom_opt, - network.links.p_nom_opt[ - network.links.index == row["linked_to"] - ].values[0], + dc_load = pd.Series(index=network.links.index, data=0.0) + dc_load.loc[dc_links.index] = ( + ( + mul_weighting(network, link_load) + .loc[network.snapshots[timesteps]] + .abs() + .sum()[dc_links.index] + / dc_links.p_nom_opt ) - dc_load = ( - mul_weighting(network, link_load) - .loc[network.snapshots[timesteps]] - .abs() - .sum()[network.links.index] - / p_nom_opt_max - ).dropna() - dc_load.loc[network.links.carrier != "DC"] = 0 + .fillna(0) + .values + ) return dc_load @@ -1750,7 +1721,7 @@ def calc_network_expansion(network, method="abs", ext_min=0.1): return network, extension_lines, extension_links -def plot_background_grid(network, ax, geographical_boundaries): +def plot_background_grid(network, ax, geographical_boundaries, osm): """Plots grid topology in background of other network.plot Parameters @@ -1761,6 +1732,9 @@ def plot_background_grid(network, ax, geographical_boundaries): axes of plot geographical_boundaries : list Set georaphical boundaries for the plots + osm : False or dict. + False if not osm background map is required or dictionary with + x, y and zoom information. Returns ------- @@ -1770,7 +1744,7 @@ def plot_background_grid(network, ax, geographical_boundaries): link_widths = pd.Series(index=network.links.index, data=0) link_widths.loc[network.links.carrier == "DC"] = 0.3 - if cartopy_present: + if osm is not False: network.plot( ax=ax, line_colors="grey", @@ -1778,21 +1752,33 @@ def plot_background_grid(network, ax, geographical_boundaries): bus_sizes=0, line_widths=0.5, link_widths=link_widths, - geomap=True, - projection=ccrs.PlateCarree(), - color_geomap=True, + geomap=False, boundaries=geographical_boundaries, ) else: - network.plot( - ax=ax, - line_colors="grey", - link_colors="grey", - bus_sizes=0, - line_widths=0.5, - link_widths=link_widths, - geomap=False, - ) + if cartopy_present: + network.plot( + ax=ax, + line_colors="grey", + link_colors="grey", + bus_sizes=0, + line_widths=0.5, + link_widths=link_widths, + geomap=True, + projection=ccrs.PlateCarree(), + color_geomap=True, + boundaries=geographical_boundaries, + ) + else: + network.plot( + ax=ax, + line_colors="grey", + link_colors="grey", + bus_sizes=0, + line_widths=0.5, + link_widths=link_widths, + geomap=False, + ) def demand_side_management(self, buses, snapshots, agg="5h", used=False): @@ -2385,8 +2371,8 @@ def plot_grid( False, it could be assinged like this: {"H2": 50, "heat": 0.1, "battery": 10} geographical_boundaries : list, optional - Set georaphical boundaries for the plots The default is - [-2.5, 16, 46.8, 58] + Set georaphical boundaries for the plots. This parameter is overwritten + when osm is used. The default is [-2.5, 16, 46.8, 58] Returns ------- @@ -2412,7 +2398,8 @@ def plot_grid( if osm is not False: if network.srid == 4326: set_epsg_network(network) - fig, ax = plot_osm(osm["x"], osm["y"], osm["zoom"]) + fig, ax, xrange, yrange = plot_osm(osm["x"], osm["y"], osm["zoom"]) + geographical_boundaries = [xrange[0], xrange[1], yrange[0], yrange[1]] elif (osm is False) and cartopy_present: fig, ax = plt.subplots( @@ -2441,7 +2428,7 @@ def plot_grid( link_widths = link_colors.apply(lambda x: 10 if x != 0 else 0) line_widths = 10 label = "line loading in p.u." - plot_background_grid(network, ax, geographical_boundaries) + plot_background_grid(network, ax, geographical_boundaries, osm) # Only active flow direction is displayed! flow = pd.Series(1, index=network.branches().index, dtype="float64") flow.iloc[flow.index.get_level_values("component") == "Line"] = ( @@ -2480,14 +2467,14 @@ def plot_grid( label = "v_nom in kV" line_colors = network.lines.v_nom link_colors = pd.Series(data=0, index=network.links.index) - plot_background_grid(network, ax, geographical_boundaries) + plot_background_grid(network, ax, geographical_boundaries, osm) elif line_colors == "expansion_abs": title = "Network expansion" label = "network expansion in GVA" all_network, line_colors, link_colors = calc_network_expansion( network, method="abs", ext_min=ext_min ) - plot_background_grid(all_network, ax, geographical_boundaries) + plot_background_grid(all_network, ax, geographical_boundaries, osm) if ext_width is not False: line_widths = line_colors / ext_width @@ -2509,7 +2496,7 @@ def plot_grid( all_network, line_colors, link_colors = calc_network_expansion( network, method="rel", ext_min=ext_min ) - plot_background_grid(all_network, ax, geographical_boundaries) + plot_background_grid(all_network, ax, geographical_boundaries, osm) if ext_width is not False: line_widths = 0.5 + (line_colors / ext_width) link_widths = link_colors.apply( @@ -2529,11 +2516,11 @@ def plot_grid( if ext_width is not False: line_widths = 0.5 + (line_colors / ext_width) link_colors = pd.Series(data=0, index=network.links.index) - plot_background_grid(network, ax, geographical_boundaries) + plot_background_grid(network, ax, geographical_boundaries, osm) elif line_colors == "dlr": title = "Dynamic line rating" label = "TWh above nominal capacity" - plot_background_grid(network, ax, geographical_boundaries) + plot_background_grid(network, ax, geographical_boundaries, osm) # calc min capacity per line in the given period: Since lines with # different original voltage level could be aggregated during the @@ -2566,7 +2553,7 @@ def plot_grid( label = "" line_colors = "grey" link_colors = "grey" - plot_background_grid(network, ax, geographical_boundaries) + plot_background_grid(network, ax, geographical_boundaries, osm) link_widths = 0 line_widths = 0 @@ -2702,7 +2689,7 @@ def plot_grid( else: logger.warning("bus_color {} undefined".format(bus_colors)) - if cartopy_present: + if cartopy_present & (osm is False): ll = network.plot( line_colors=line_colors, link_colors=link_colors,