From ab1d52911bcbb90ad95c47b68d6eebef28392d3a Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Fri, 29 May 2026 13:19:22 +0200 Subject: [PATCH] Strip inherited attributes from absolute_momentum result absolute_momentum builds its result as norm_wind + f * distance, where distance is derived from the cross-section x-coordinate. Depending on xarray's keep_attrs default, the arithmetic propagates attributes that do not describe absolute momentum -- the x-coordinate's metadata (long_name 'x coordinate of projection', standard_name, _metpy_axis, grid_spacing, _CoordinateAxisType) picked up through np.hypot, plus the input wind's attributes. With recent xarray these survive the operation, so the returned DataArray carried stray coordinate metadata as its own attrs. This surfaced as the test_absolute_momentum_given_xy and test_absolute_momentum_xarray_units_attr failures in the nightly build (#3933); both assert the result has clean attributes. Explicitly clear the derived quantity's attributes so the behavior is robust to xarray's keep_attrs default, matching the existing test contract and the historical behavior these tests were written against. --- src/metpy/calc/cross_sections.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/metpy/calc/cross_sections.py b/src/metpy/calc/cross_sections.py index 1b33330a389..e7e0b608b59 100644 --- a/src/metpy/calc/cross_sections.py +++ b/src/metpy/calc/cross_sections.py @@ -325,4 +325,14 @@ def absolute_momentum(u, v, index='index'): _, x, y = xr.broadcast(norm_wind, x, y) distance = np.hypot(x.metpy.quantify(), y.metpy.quantify()) - return (norm_wind + f * distance).metpy.convert_units('m/s') + momentum = (norm_wind + f * distance).metpy.convert_units('m/s') + + # The arithmetic above can carry through attributes that do not describe + # absolute momentum (e.g. the cross-section's x-coordinate metadata picked + # up via ``distance``, or the input wind's attributes), and whether they + # propagate at all depends on xarray's ``keep_attrs`` default. Absolute + # momentum is a newly derived quantity, so return it without these + # inherited attributes. + momentum.attrs = {} + + return momentum