Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.
https://unidata.github.io/MetPy/
BSD 3-Clause "New" or "Revised" License
1.24k stars 413 forks source link

RC2 advection calculation does not get coordinate data from DataArray #1548

Closed kgoebber closed 3 years ago

kgoebber commented 3 years ago

Try to include:

# Your code here
from datetime import datetime

import metpy.calc as mpcalc
from metpy.units import units
import xarray as xr

date = datetime(2020, 10, 26, 12)

ds = xr.open_dataset('https://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_0p5deg/Best').metpy.parse_cf()

data_slice_dict = dict(time=date, vertical=850*units.hPa, lat=slice(55, 20), lon=slice(360-140, 360-60))

# First pull wind data for plotting barbs and computing advection
uwnd_850 = ds['u-component_of_wind_isobaric'].metpy.sel(data_slice_dict)
vwnd_850 = ds['v-component_of_wind_isobaric'].metpy.sel(data_slice_dict)
tmpc = (ds.Temperature_isobaric.metpy.sel(data_slice_dict)).metpy.convert_units('degC')
smooth_tmpc = mpcalc.smooth_gaussian(tmpc, 5)

t_adv_850 = mpcalc.advection(smooth_tmpc, uwnd_850, vwnd_850)

Output:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-5722b3ee1007> in <module>
      3 
      4 # Compute the temperature advection at 850 hPa
----> 5 t_adv_850 = mpcalc.advection(smooth_tmpc, uwnd_850, vwnd_850)

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/xarray.py in wrapper(*args, **kwargs)
   1158 
   1159             # Evaluate inner calculation
-> 1160             result = func(*bound_args.args, **bound_args.kwargs)
   1161 
   1162             # Wrap output based on match and match_unit

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/calc/kinematics.py in advection(scalar, u, v, w, dx, dy, dz, x_dim, y_dim, vertical_dim)
    292             (u, dx, x_dim),
    293             (v, dy, y_dim),
--> 294             (w, dz, vertical_dim)
    295         )
    296         if wind is not None

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/calc/kinematics.py in <genexpr>(.0)
    294             (w, dz, vertical_dim)
    295         )
--> 296         if wind is not None
    297     )
    298 

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/calc/tools.py in wrapper(f, **kwargs)
    894         if 'x' in kwargs or 'delta' in kwargs:
    895             # Use the usual DataArray to pint.Quantity preprocessing wrapper
--> 896             return preprocess_and_wrap()(func)(f, **kwargs)
    897         elif isinstance(f, xr.DataArray):
    898             # Get axis argument, defaulting to first dimension

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/xarray.py in wrapper(*args, **kwargs)
   1158 
   1159             # Evaluate inner calculation
-> 1160             result = func(*bound_args.args, **bound_args.kwargs)
   1161 
   1162             # Wrap output based on match and match_unit

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/calc/tools.py in first_derivative(f, axis, x, delta)
    970 
    971     """
--> 972     n, axis, delta = _process_deriv_args(f, axis, x, delta)
    973     take = make_take(n, axis)
    974 

~/miniconda3/envs/new_main/lib/python3.7/site-packages/metpy/calc/tools.py in _process_deriv_args(f, axis, x, delta)
   1273         delta = np.diff(x, axis=axis)
   1274     else:
-> 1275         raise ValueError('Must specify either "x" or "delta" for value positions.')
   1276 
   1277     return n, axis, delta

ValueError: Must specify either "x" or "delta" for value positions.
jthielen commented 3 years ago

@kgoebber I guess this is what I get for poor testing in #1490...somehow the grid arg decorator got missed on the new advection. Also, this uncovered a separate bug in the grid arg decorator when there is a vertical scalar non-dim coord, so that was a good catch too. Does this fix work for you https://github.com/jthielen/MetPy/commit/fe46d137f56aeb493b8fbf54156f115b69e7e255?

kgoebber commented 3 years ago

That does the trick! Working as intended now. Thanks!