我正在使用WRF输出数据绘制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
metpy版本:0.12.0
我的猜测是wrf.getvar()
返回NumPy掩码数组,与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')
此外,提醒一下MetPy0.12有点过时了,我建议您尽快更新到最新版本(目前为1.3.1(。