From 19065c57874c7abe378973cd8205b4c601184574 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Wed, 13 Nov 2024 16:12:04 -0700 Subject: [PATCH] BUG: Fix q_vector units w/ default static stability (Fixes #3689) There's no reason the units should change in this case, just assign proper units to the default unity value of static stability. --- src/metpy/calc/kinematics.py | 12 ++++++++---- tests/calc/test_kinematics.py | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/metpy/calc/kinematics.py b/src/metpy/calc/kinematics.py index 73f8d7a64c4..6253b9c5d23 100644 --- a/src/metpy/calc/kinematics.py +++ b/src/metpy/calc/kinematics.py @@ -1266,7 +1266,8 @@ def inertial_advective_wind( broadcast=('u', 'v', 'temperature', 'pressure', 'static_stability', 'parallel_scale', 'meridional_scale') ) -@check_units('[speed]', '[speed]', '[temperature]', '[pressure]', '[length]', '[length]') +@check_units('[speed]', '[speed]', '[temperature]', '[pressure]', '[length]', '[length]', + '[energy] / [mass] / [pressure]**2') def q_vector( u, v, @@ -1274,7 +1275,7 @@ def q_vector( pressure, dx=None, dy=None, - static_stability=1, + static_stability=None, x_dim=-1, y_dim=-2, *, @@ -1325,8 +1326,8 @@ def q_vector( the size of `u` along the applicable axis. Optional if `xarray.DataArray` with latitude/longitude coordinates used as input. static_stability : `pint.Quantity`, optional - The static stability at the pressure level. Defaults to 1 if not given to calculate - the Q-vector without factoring in static stability. + The static stability at the pressure level. Defaults to 1 J/ kg / Pa^2 if not given + to calculate the Q-vector without factoring in static stability. x_dim : int, optional Axis number of x dimension. Defaults to -1 (implying [..., Y, X] order). Automatically parsed from input if using `xarray.DataArray`. @@ -1353,6 +1354,9 @@ def q_vector( static_stability """ + if static_stability is None: + static_stability = units.Quantity(1, 'J kg^-1 Pa^-2') + dudx, dudy, dvdx, dvdy = vector_derivative( u, v, dx=dx, dy=dy, x_dim=x_dim, y_dim=y_dim, parallel_scale=parallel_scale, meridional_scale=meridional_scale) diff --git a/tests/calc/test_kinematics.py b/tests/calc/test_kinematics.py index f71a75bc530..08366ecae6a 100644 --- a/tests/calc/test_kinematics.py +++ b/tests/calc/test_kinematics.py @@ -1262,12 +1262,12 @@ def test_q_vector_without_static_stability(q_vector_data): [-1.8952185e-13, -2.2269905e-14, -2.2269905e-14, -1.8952185e-13], [-1.9918390e-13, -2.3370829e-14, -2.3370829e-14, -1.9918390e-13], [-5.6160772e-14, -3.5145951e-13, -3.5145951e-13, -5.6160772e-14]]) - * units('m^2 kg^-1 s^-1')) + * units('kg m^-2 s^-3')) q2_truth = (np.array([[-4.4976059e-14, -4.3582378e-13, 4.3582378e-13, 4.4976059e-14], [-3.0124244e-13, -3.5724617e-14, 3.5724617e-14, 3.0124244e-13], [3.1216232e-13, 3.6662900e-14, -3.6662900e-14, -3.1216232e-13], [8.6038280e-14, 4.6968342e-13, -4.6968342e-13, -8.6038280e-14]]) - * units('m^2 kg^-1 s^-1')) + * units('kg m^-2 s^-3')) assert_almost_equal(q1, q1_truth, 16) assert_almost_equal(q2, q2_truth, 16) @@ -1840,7 +1840,7 @@ def test_q_vector_4d(data_4d): [2.50227841e-13, 2.70855069e-13, 4.03362348e-13, 5.22065702e-13], [3.37119836e-13, 3.17667714e-13, 2.25387106e-13, 6.46265259e-13], [2.05548507e-13, 3.55426850e-13, -1.74728156e-14, - 5.04028133e-13]]]]) * units('m^2 kg^-1 s^-1') + 5.04028133e-13]]]]) * units('kg m^-2 s^-3') q2_truth = np.array([[[[3.34318820e-12, -1.32561232e-13, 1.01510711e-12, 6.03331800e-12], [2.51737448e-13, -1.71044158e-13, -8.25290924e-13, 1.68843717e-13], [-3.50533924e-12, -1.68864979e-12, 7.74026063e-13, 1.53811977e-12], @@ -1884,7 +1884,7 @@ def test_q_vector_4d(data_4d): [-1.39578146e-13, -1.36744814e-13, 3.12352497e-14, 4.55339789e-13], [-1.06614836e-13, -2.19878930e-13, -8.37992151e-14, 1.87868902e-13], [-2.27057581e-13, -2.74474045e-13, -1.10759455e-13, - -3.90242255e-13]]]]) * units('m^2 kg^-1 s^-1') + -3.90242255e-13]]]]) * units('kg m^-2 s^-3') assert_array_almost_equal(q1.data, q1_truth, 15) assert_array_almost_equal(q2.data, q2_truth, 15)