WeatherEd
WeatherEd

Reputation: 13

Cause of TypeError while plotting wind barbs on Metpy SkewT

Metpy.plots.skewT.plot_barbs throws this error:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

when given 3 Numpy arrays: pressure, u, and v.

Other users who reported this error message either did not use the correct dtype or used np as a variable name. I have verified that neither of these is the cause in my case. I have also tried removing the units from the u and v arrays.

import numpy as np
import metpy.plots as mpplots

# units comes from another file

pres = np.array([nan, 96950.26215278, 96877.55755208, 96763.37230603, 96652.54882812]) * units.pascal
u = np.array([nan,  0.36735288,  0.44829027,  1.29182593, -0.94374102]) * units.meter / units.second
v = np.array([nan, 4.61110612, 5.74110696, 6.01459714, 5.5652721]) * units.meter / units.second

rotation = 30
fig = mpplots.SkewT(rotation=rotation, aspect=80.5)
fig.ax.yaxis.set_major_locator(ticker.MultipleLocator(5))
fig.plot(pres, temp, 'r', label="Temperature")
fig.plot(pres, t_d, 'g', label="Dewpoint")
fig.ax.set_ylim(np.nanmax(pres.to(units.hPa).magnitude) + 10,
                np.nanmin(pres.to(units.hPa).magnitude) - 20)
fig.ax.set_xlim(np.nanmin(t_d.to(units.degC).magnitude) - 5,
                np.nanmax(temp.to(units.degC).magnitude) + 10)
fig.plot_dry_adiabats(linewidth=0.5, label="Dry Adiabats")
fig.plot_moist_adiabats(linewidth=0.5, label="Moist Adiabats")
fig.plot_mixing_lines(linewidth=0.5, label="Mixing Ratio")
fig.plot_barbs(np.array(pres.magnitude, dtype='float64') * pres.units,
               np.array(u.magnitude, dtype='float64') * u.units,
               np.array(v.magnitude, dtype='float64') * v.units)
Traceback (most recent call last):
  File "/snap/pycharm-professional/167/helpers/pydev/pydevd.py", line 1415, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-professional/167/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/.../sample_analysis.py", line 43, in <module>
    fig = plotting.plot_skewT(temp=temp, pres=pres, t_d=t_d, u=u, v=v, units=units)
  File "/home/.../plotting.py", line 248, in plot_skewT
    np.array(v.magnitude, dtype='float64') * v.units)
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/metpy/plots/skewt.py", line 440, in plot_barbs
    clip_on=True, zorder=2, **kwargs)
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/matplotlib/__init__.py", line 1785, in inner
    return func(ax, *args, **kwargs)
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4874, in barbs
    b = mquiver.Barbs(self, *args, **kw)
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/matplotlib/quiver.py", line 965, in __init__
    self.set_UVC(u, v, c)
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/matplotlib/quiver.py", line 1145, in set_UVC
    self.u = ma.masked_invalid(U, copy=False).ravel()
  File "/home/.../miniconda3/envs/Profiles/lib/python3.7/site-packages/numpy/ma/core.py", line 2366, in masked_invalid
    condition = ~(np.isfinite(a))
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Process finished with exit code 1

Upvotes: 0

Views: 261

Answers (1)

DopplerShift
DopplerShift

Reputation: 5853

I was able to make it work with this:

import numpy as np
import metpy.plots as mpplots
from metpy.units import units

pres = np.array([np.nan, 96950.26215278, 96877.55755208, 96763.37230603, 96652.54882812]) * units.pascal
u = np.array([np.nan,  0.36735288,  0.44829027,  1.29182593, -0.94374102]) * units.meter / units.second
v = np.array([np.nan, 4.61110612, 5.74110696, 6.01459714, 5.5652721]) * units.meter / units.second

fig = mpplots.SkewT(rotation=30, aspect=80.5)
fig.plot_dry_adiabats(linewidth=0.5, label="Dry Adiabats")
fig.plot_moist_adiabats(linewidth=0.5, label="Moist Adiabats")
fig.plot_mixing_lines(linewidth=0.5, label="Mixing Ratio")
fig.plot_barbs(pres.astype('float64'), u.astype('float64'), v.astype('float64'))

I suspect there may be something that needs to be done in the process of reading the data to generate the nans properly.

Upvotes: 0

Related Questions