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

storm_relative_helicity,The example code for this function cannot run #3594

Open pll001 opened 1 month ago

pll001 commented 1 month ago

What went wrong?

I copied the example code of this function, but when it runs, an error occurs. What is the problem 1723278732507

Operating System

Windows

Version

1.6.2

Python Version

3.9

Code to Reproduce

from metpy.calc import storm_relative_helicity, wind_components
from metpy.units import units
# set needed values of pressure, height, wind direction/speed
p = [1000, 925, 850, 700, 500, 400] * units.hPa
h = [250, 700, 1500, 3100, 5720, 7120] * units.meters
wdir = [165, 180, 190, 210, 220, 250] * units.degree
sped = [5, 15, 20, 30, 50, 60] * units.knots
# compute wind components
u, v = wind_components(sped, wdir)
# compute SRH with a storm vector
storm_relative_helicity(h, u, v, depth=1 * units.km,
                        storm_u=7 * units('m/s'), storm_v=7 * units('m/s'))

Errors, Traceback, and Logs

Traceback (most recent call last):
  File "D:\python\3.10 fog\5.py", line 11, in <module>
    storm_relative_helicity(h, u, v, depth=1 * units.km,
  File "E:\anaconda\envs\python\lib\site-packages\metpy\xarray.py", line 1330, in wrapper
    result = func(*bound_args.args, **bound_args.kwargs)
  File "E:\anaconda\envs\python\lib\site-packages\metpy\units.py", line 333, in wrapper
    return func(*args, **kwargs)
  File "E:\anaconda\envs\python\lib\site-packages\metpy\calc\kinematics.py", line 848, in storm_relative_helicity
    _, u, v = get_layer_heights(height, depth, u, v, with_agl=True, bottom=bottom)
  File "E:\anaconda\envs\python\lib\site-packages\metpy\xarray.py", line 1330, in wrapper
    result = func(*bound_args.args, **bound_args.kwargs)
  File "E:\anaconda\envs\python\lib\site-packages\metpy\units.py", line 333, in wrapper
    return func(*args, **kwargs)
  File "E:\anaconda\envs\python\lib\site-packages\metpy\calc\tools.py", line 476, in get_layer_heights
    sfc_height = np.min(height)
TypeError: no implementation found for 'numpy.min' on types that implement __array_function__: [<class 'pint.util.Quantity'>]
DWesl commented 4 weeks ago

It looks like the code here implements min properly, what versions of NumPy and Pint are you using?

pll001 commented 4 weeks ago

看起来这里的代码实现min正确,您使用的是哪个版本的 NumPy 和 Pint?

Numpy 1.21.2 Pint 0.20.1

DWesl commented 4 weeks ago

Interesting. Works fine for me on Python 3.9.16 with NumPy 1.26.4 and pint 0.23

>>> np.min(pint.Quantity(np.array([250, 700, 1500, 3100, 5720, 7120]), "meter"))
<Quantity(250, 'meter')>

Is there something keeping you from upgrading NumPy or Pint?

dopplershift commented 3 weeks ago

I'm unable to reproduce this on my macOS machine with Python 3.9 + Pint 0.20.1 and Numpy 1.21.2 with current MetPy main. I don't think we've changed anything relevant since 1.6.2, so I'm guessing this is an issue with your environment.

I would double check what versions of pint and numpy are being picked up with python -c 'import pint; print(pint.__version__); import numpy; print(numpy.__version__)', and consider rebuilding your environment.

DWesl commented 2 weeks ago

Does conda env create -n metpy_test metpy numpy pint && conda activate metpy_test && python "D:\python\3.10 fog\5.py" run into the same problem? conda env create -n metpy_test2 metpy==1.6.2 numpy==1.21.2 pint==0.20.1 python==3.9 && conda activate metpy_test2 && python "D:\python\3.10 fog\5.py"?

If those fail with conda: No program or batch file with that name or similar errors ("Program not found" is also likely), replacing conda env create -n with pip install --force-reinstall and skipping the conda activate part should work.

I think getting rid of those would be conda env remove -n metpy_test && conda env remove -n metpy_test2