Skip to content

Commit 58e229a

Browse files
committed
Add check before plotting wind barbs
Fixes Unidata#2785. Currently, BarbPlot is happy to attempt to plot very large wind barbs, but if the size becomes too large, memory usage becomes an issue, leading to system hangs. This commit adds a check to make sure the wind barbs will not consist of so many pennants that memory could be an issue.
1 parent e262ca6 commit 58e229a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/metpy/plots/declarative.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,19 @@ def _build(self):
16601660

16611661
wind_slice = (slice(None, None, self.skip[0]), slice(None, None, self.skip[1]))
16621662

1663+
# If too many of the winds/vectors to plot have excessively high magnitudes,
1664+
# the call to ax.barbs will cause matplotlib to draw so many pennants that all
1665+
# memory will be consumed, possibly hanging the system. Thus, here we check
1666+
# what we are about to plot to avoid the hang.
1667+
u_vals = u.values[wind_slice]
1668+
v_vals = v.values[wind_slice]
1669+
speeds = np.hypot(u_vals, v_vals)
1670+
if np.median(speeds) > 50000:
1671+
msg = "Too many large wind barbs to plot. Perhaps 'scale' is too high?"
1672+
raise ValueError(msg)
1673+
16631674
self.handle = self.parent.ax.barbs(
1664-
x_like[wind_slice], y_like[wind_slice],
1665-
u.values[wind_slice], v.values[wind_slice],
1675+
x_like[wind_slice], y_like[wind_slice], u_vals, v_vals,
16661676
color=self.color, pivot=self.pivot, length=self.barblength, zorder=2, **kwargs)
16671677

16681678

0 commit comments

Comments
 (0)