0

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
Kevin
  • 16,549
  • 8
  • 60
  • 74
WeatherEd
  • 13
  • 4
  • Have you checked that you have no np.inf or NaN values in your input matrices? – G. Anderson Nov 06 '19 at 18:28
  • Have you tried making your nans into `np.nan`? If I do that, the plot returns wind barbs (Python 3.7, MetPy 0.11.1) – zbruick Nov 06 '19 at 20:12
  • @zbruick I'm not sure how to go about that. The current dtype is float64. As I understand it, arrays with units have to be np arrays and not lists. This forces everything to the same dtype. How do you get around this? – WeatherEd Nov 08 '19 at 15:14

1 Answers1

0

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.

DopplerShift
  • 5,472
  • 1
  • 21
  • 20
  • I took a very long break from this problem and came back to it today. I'm still stumped. My most recent attempt involved a data set with no holes - there are no NaNs to cause problems. The Advanced_Sounding.py file from the MetPy web page gives the same error message. This leads me to believe that the issue is with my setup. Any idea what I might have done wrong? – WeatherEd Jul 23 '20 at 17:17
  • I'm not sure what's going wrong. If `Advanced_Sounding.py` from the examples in the MetPy docs doesn't run in your environment, then it sounds like your environment is to blame. Can you create a new environment with MetPy installed and see if that works? – DopplerShift Jul 23 '20 at 22:38
  • Downgrading to pint==0.9 fixed the problem - finally! – WeatherEd Aug 09 '20 at 15:28
  • I wouldn't call that a fix, considering MetPy 0.12 requires Pint 0.10.1 – DopplerShift Aug 09 '20 at 19:18