![](/img/trans.png)
[英]Cause of TypeError while plotting wind barbs on Metpy SkewT
[英]Getting error message while ploting metpy SkewT
我正在使用 WRF output 數據到 plot SkweT,這里是代碼:
import wrf
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np
import metpy.calc as mpcalc
from metpy.plots import SkewT
from metpy.units import units
wrfin = Dataset(r'wrfout_d02_2022-06-19_00_00_00')
lat_lon = [25.0803, 121.2183]
x_y = wrf.ll_to_xy(wrfin, lat_lon[0], lat_lon[1])
p1 = wrf.getvar(wrfin,"pressure",timeidx=0)
T1 = wrf.getvar(wrfin,"tc",timeidx=0)
Td1 = wrf.getvar(wrfin,"td",timeidx=0)
u1 = wrf.getvar(wrfin,"ua",timeidx=0)
v1 = wrf.getvar(wrfin,"va",timeidx=0)
p = p1[:,x_y[0],x_y[1]] * units.hPa
T = T1[:,x_y[0],x_y[1]] * units.degC
Td = Td1[:,x_y[0],x_y[1]] * units.degC
u = v1[:,x_y[0],x_y[1]] * units('m/s')
v = u1[:,x_y[0],x_y[1]] * units('m/s')
skew = SkewT()
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
my_interval = np.arange(100, 1000, 50) * units('mbar')
ix = mpcalc.resample_nn_1d(p, my_interval)
skew.plot_barbs(p[ix], u[ix], v[ix])
skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
skew.plot_mixing_lines()
skew.ax.set_ylim(1000, 100)
skew.ax.set_xlim(-60, 40)
skew.ax.set_xlabel('Temperature ($^\circ$C)')
skew.ax.set_ylabel('Pressure (hPa)')
plt.savefig('SkewT.png', bbox_inches='tight')
但運行錯誤消息,尤其是在塊中:
**raise DimensionalityError(
pint.errors.DimensionalityError: Cannot convert from 'dimensionless' (dimensionless) to 'millibar' ([mass] / [length] / [time] ** 2)**
似乎mpcalc.resample_nn_1d
不起作用。
我該如何解決?
python 版本:3.9.7
版本:0.12.0
我的猜測是wrf.getvar()
正在返回 Z3B7F949B2343F9E5390E29F6EF5E1778Z 屏蔽 arrays,它與 Pint Quantity
實例的行為有點奇怪(與普通數組相比)。 我建議嘗試使用這種語法來“附加”單元:
p = units.Quantity(p1[:,x_y[0],x_y[1]], 'hPa')
T = units.Quantity(T1[:,x_y[0],x_y[1]], 'degC')
Td = units.Quantity(Td1[:,x_y[0],x_y[1]], 'degC')
u = units.Quantity(v1[:,x_y[0],x_y[1]], 'm/s')
v = units.Quantity(u1[:,x_y[0],x_y[1]], 'm/s')
另外,提醒一下 MetPy 0.12 有點過時了,我建議盡快更新到最新版本(當前為 1.3.1)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.