当最不稳定地块函数的输出用作输入时,MetPy 湿球温度函数返回错误
当尝试使用 MetPy 的 most_unstable_parcel 函数返回的地块的湿球温度时,我收到一个错误,该错误似乎与 最不稳定的地块压力的 Pint 属性相关。有必要通过将压力强制为 int 或 float 类型来剥离这些属性。
请参阅下面的讨论。
我非常感谢您能帮助我理解如何使用最不稳定的地块压力,而无需采取强制将其转换为需要重新分配单位的类型的技巧。谢谢你!
计算示例(来自 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
在运行一些输入排列后,我发现如果首先强制输出压力值,则可以使用输出压力值,例如,输入 float 或 int。
# 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_unstable_parcel 的压力输出(例如 MUp[0])不能直接用作湿球温度计算的输入。
When attempting to use the find the wet-bulb temperature for a parcel returned by MetPy's most_unstable_parcel function, I am getting an error that seems to be related to the Pint attributes of the most unstable parcel pressure. It is necessary to strip away those attributes by coercing the pressure to be of type int or float.
See discussion, below.
I would greatly appreciate some help in understanding how to use the most unstable parcel pressure without resorting to the artifice of coercing it to a type that requires re-assigning units. Thank you!
Example Calculation (data from OUN 20220328 00Z sounding):
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])
When this is executed, we get the following error traceback:
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
After running a few permutations of inputs, I discovered that it is possible to use the output pressure value if one first coerces it to, say, type float or int.
# 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
It's just not clear to me why the pressure output from most_unstable_parcel (e.g. MUp[0]) cannot be used directly as an input to the wet-bulb temperature calculation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是Metpy的
wet_bulb_temperature
函数中的一个错误标量,因此,感谢您确定问题! 已提交了拉动请求,这应该使它成为即将到来的Metpy V1 .3.0发行。目前,您需要以某种方式覆盖
wit_bulb_temperature
中存在的数组形状识别。正如您发现的那样,从数字标量转换为Python标量是这样做的。另一个正在转换为1D numpy阵列,如以下内容:This is a bug with MetPy's
wet_bulb_temperature
function in handling NumPy scalars, so thank you for identifying the issue! A pull request fixing this has been submitted, which should make it into the upcoming MetPy v1.3.0 release.For now, you will need to somehow override the array shape recognition that exists in
wet_bulb_temperature
. Converting from a NumPy scalar to a Python scalar as you discovered is one such way to do so. Another is converting to 1D NumPy arrays, like the following: