Skip to content

Q_Vector returns wrong units #3689

@sodoesaburningbus

Description

@sodoesaburningbus

What went wrong?

mpcalc.q_vector returns values with units of m2/kg/s. Appropriate units should be kg/m2/s2.

Operating System

Windows

Version

1.6.2

Python Version

3.12

Code to Reproduce

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import metpy.calc as mpcalc
import metpy.constants as mpconstants
from metpy.units import units
import numpy as np
import xarray as xr
from datetime import datetime, timedelta, time 

e = datetime(2023, 5, 1, 12)
ds = xr.open_dataset(f'https://www.ncei.noaa.gov/thredds/dodsC/model-gfs-g4-anl-files/'
                     f'{date:%Y%m}/{date:%Y%m%d}/gfs_4_{date:%Y%m%d_%H}00_000.grb2')

# Sunset geographic extent of data to CONUS to limit how much is downloaded
lon_slice = slice(-125, -67)  
lat_slice = slice(24, 50)     

lats = ds['lat'].values  # Extract latitude
lons = ds['lon'].values  # Extract longitude

# Create meshgrid from 1D lat and lon arrays
lons_grid, lats_grid = np.meshgrid(lons, lats)

# Compute grid spacings (dx, dy) using np.gradient
dy, dx = np.gradient(lats_grid, axis=0), np.gradient(lons_grid, axis=1)

# Grab data and smooth using a nine-point filter applied 50 times to grab the synoptic signal
level = 700 * units.hPa
hght_700 = ds['Geopotential_height_isobaric'].metpy.sel(vertical=level)
tmpk_700 = ds['Temperature_isobaric'].metpy.sel(vertical=level)
uwnd_700 = ds['u-component_of_wind_isobaric'].metpy.sel(vertical=level)
vwnd_700 = ds['v-component_of_wind_isobaric'].metpy.sel(vertical=level)
w_700 = ds['Vertical_velocity_geometric_isobaric'].metpy.sel(vertical=level)

# Compute the Q-vector components
uqvect, vqvect = mpcalc.q_vector(uwnd_700, vwnd_700, tmpk_700, level)
print(uqvect)
print(uqvect.to_base_units())
# Compute the divergence of the Q-vectors calculated above (term A of the RHS according to Hoskins et al. (1978)
q_div = -2 * mpcalc.divergence(uqvect, vqvect)

Errors, Traceback, and Logs

<xarray.DataArray (time: 1, lat: 361, lon: 720)> Size: 2MB
<Quantity([[[ 2.11213126e-08 -1.56944484e-10 -1.30758030e-10 ... -9.80448728e-11
   -1.17662229e-10 -1.37315293e-10]
  [ 1.75598535e-10  1.84654699e-10  5.25826480e-09 ...  2.46977001e-10
    2.28100511e-10  2.36409042e-10]
  [ 2.77543024e-12  1.05006759e-12  1.03417809e-12 ...  1.06336328e-12
    1.83656116e-12  1.83360512e-12]
  ...
  [ 5.59318956e-12  8.84133153e-13  2.63383217e-13 ...  5.14350542e-13
    9.62793471e-13  1.80597172e-12]
  [-1.91209715e-09  4.78330514e-10  5.06398520e-10 ... -8.15684653e-10
   -7.88212573e-10 -7.62894144e-10]
  [ 2.62437847e-10 -2.68241094e-10 -2.87836800e-10 ... -2.29051163e-10
    2.82836771e-13  3.12410213e-13]]], 'meter ** 2 / kilogram / second')>

Units at end of array are incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugSomething is not working like it should

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions