Skip to content

Commit

Permalink
BUG: Fix q_vector units w/ default static stability (Fixes Unidata#3689)
Browse files Browse the repository at this point in the history
There's no reason the units should change in this case, just assign
proper units to the default unity value of static stability.
  • Loading branch information
dopplershift committed Nov 14, 2024
1 parent ab1b956 commit 19065c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/metpy/calc/kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,15 +1266,16 @@ 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,
temperature,
pressure,
dx=None,
dy=None,
static_stability=1,
static_stability=None,
x_dim=-1,
y_dim=-2,
*,
Expand Down Expand Up @@ -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`.
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/calc/test_kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 19065c5

Please sign in to comment.