MetPy湿球温度函数在使用最不稳定地块函数的输出作为输入时返回错误



当尝试使用由MetPy的most_unstatible_parcel函数返回的包裹的查找湿球温度时,我遇到了一个错误,该错误似乎与最不稳定包裹压力的Pint属性有关。有必要通过强制压力为intfloat类型来去除这些属性。

请参阅下面的讨论。

我非常感谢你帮助我理解如何使用最不稳定的包裹压力,而不必诉诸于将其强制为需要重新分配单位的类型的技巧。非常感谢。

示例计算(来自OUN 20220328 00Z探测的数据(:

from metpy.units import units
import metpy.calc as mpcalc
#  Construct a sample sounding:
p = [972.0, 942.8, 925.0, 910.1, 878.2, 850.0, 847.3, 826.0, 817.0, 787.5]*units('hPa')
T = [25.4, 22.4, 20.6, 19.3, 16.4, 13.8, 13.6, 11.6, 11.1, 9.4]*units('degC')
Td = [6.4, 5.3, 4.6, 4.1, 2.9, 1.8, 1.7, 0.6, -0.2, -2.9]*units('degC')  
h = [345,  610,  776,  914, 1219, 1497, 1524, 1737, 1829, 2134]*units('m')
#  Find the Most Unstable Parcel
MUp = mpcalc.most_unstable_parcel(p,T,Td,h)
# MUp
# (972.0 <Unit('hectopascal')>,
#  25.4 <Unit('degree_Celsius')>,
#  6.4 <Unit('degree_Celsius')>,
#  0)
#  Next compute the wet-bulb-temperature for this parcel:
Twb = mpcalc.wet_bulb_temperature(MUp[0],MUp[1],MUp[2])

当执行此操作时,我们会得到以下错误回溯:

IndexError                                Traceback (most recent call last)
Input In [4], in <module>
----> 1 Twb = mpcalc.wet_bulb_temperature(MUp[0],MUp[1],MUp[2])
File ~/miniconda3/envs/MetPy/lib/python3.8/site-packages/metpy/xarray.py:1230, in preprocess_and_wrap.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
1227     _mutate_arguments(bound_args, units.Quantity, lambda arg, _: arg.m)
1229 # Evaluate inner calculation
-> 1230 result = func(*bound_args.args, **bound_args.kwargs)
1232 # Wrap output based on match and match_unit
1233 if match is None:
File ~/miniconda3/envs/MetPy/lib/python3.8/site-packages/metpy/units.py:288, in check_units.<locals>.dec.<locals>.wrapper(*args, **kwargs)
285 @functools.wraps(func)
286 def wrapper(*args, **kwargs):
287     _check_units_inner_helper(func, sig, defaults, dims, *args, **kwargs)
--> 288     return func(*args, **kwargs)
File ~/miniconda3/envs/MetPy/lib/python3.8/site-packages/metpy/calc/thermo.py:3073, in wet_bulb_temperature(pressure, temperature, dewpoint)
3071 ret = it.operands[3]
3072 if ret.size == 1:
-> 3073     ret = ret[0]
3074 return units.Quantity(ret, temperature.units)
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

在运行了一些输入排列后,我发现如果首先强制输出压力值,比如输入floatint,就可以使用输出压力值。

#  Reformat the pressure value as input to
#  wet_bulb_temperature()
d = float(MUp[0].magnitude)*units('hPa')
Twb1 = mpcalc.wet_bulb_temperature(d,MUp[1],MUp[2])
print(Twb1)
#  output is 14.209821596989343 degree_Celsius
# As far as Python is concerned, the two instances of pressure are
# identical:

print(MUp[0]==d)
#  Output:  True

我只是不清楚为什么most_unstatible_parcel的压力输出(例如MUp[0](不能直接用作湿球温度计算的输入。

这是MetPy的wet_bulb_temperature函数在处理NumPy标量时的一个错误,所以感谢您发现这个问题!已经提交了一个修复此问题的pull请求,该请求将被纳入即将发布的MetPy v1.3.0版本中。

目前,您需要以某种方式覆盖wet_bulb_temperature中存在的阵列形状识别。将NumPy标量转换为您发现的Python标量就是这样一种方法。另一种方法是转换为1D NumPy数组,如下所示:

Twb = mpcalc.wet_bulb_temperature(
*(np.atleast_1d(arg) for arg in (MUp[0], MUp[1], MUp[2]))
)

最新更新