在气象家园论坛中,有用户对WRF模式输出的散度计算感到困惑。本项目旨在介绍WRF散度的计算方法,并提供Python可视化实现。在后续研究中发现不使用循环也可以完成计算,因此进行了测试比较。
import numpy as np
import xarray as xr
import metpy.calc as mpcalc
from metpy.interpolate import cross_section
from wrf import getvar, interplevel, to_np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from netCDF4 import Dataset
from metpy.units import units
import metpy
# 读取WRF输出文件
file_path = "/home/mw/input/typhoon9537/wrfout_d01_2019-08-08_19_00_00"
ncfile = Dataset(file_path)
# 获取需要的变量
u = getvar(ncfile, "ua") # 纬向风 (m/s)
v = getvar(ncfile, "va") # 经向风 (m/s)
p = getvar(ncfile, "pressure") # 气压 (hPa)
z = getvar(ncfile, "z")
lat = getvar(ncfile, "lat")
lon = getvar(ncfile, "lon")
%%timeit
# 计算网格间距
dx, dy = mpcalc.lat_lon_grid_deltas(lon.values, lat.values)
# 初始化散度数组
div = np.zeros_like(u.values)
# 逐层计算散度
for k in range(u.shape[0]):
div[k, :, :] = mpcalc.divergence(
u[k, :, :] * units('m/s'),
v[k, :, :] * units('m/s'),
dx=dx,
dy=dy
)
# 将散度数组转为xarray.DataArray
div_da = xr.DataArray(
div,
dims=u.dims,
coords=u.coords,
attrs={'units': 's^-1', 'description': 'divergence'}
)
<magic-timeit>:9: UserWarning: More than one time coordinate present for variable "ua".
<magic-timeit>:9: UserWarning: Horizontal dimension numbers not found. Defaulting to (..., Y, X) order.
737 ms ± 15.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
# 计算网格间距
dx, dy = mpcalc.lat_lon_grid_deltas(lon.values, lat.values)
# 扩展dx和dy的维度
dx = dx[None, :]
dy = dy[None, :]
# 直接计算散度
div = mpcalc.divergence(u, v, dx=dx, dy=dy)
<magic-timeit>:8: UserWarning: More than one time coordinate present for variable "ua".
<magic-timeit>:8: UserWarning: Horizontal dimension numbers not found. Defaulting to (..., Y, X) order.
454 ms ± 7.29 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
# 直接从数据数组计算网格间距
dx, dy = metpy.xarray.grid_deltas_from_dataarray(u)
# 计算散度
div = mpcalc.divergence(u, v, dx=dx, dy=dy)
<magic-timeit>:2: UserWarning: More than one time coordinate present for variable "XLAT".
<magic-timeit>:2: UserWarning: y and x dimensions unable to be identified. Assuming [..., y, x] dimension order.
<magic-timeit>:5: UserWarning: More than one time coordinate present for variable "ua".
<magic-timeit>:5: UserWarning: Horizontal dimension numbers not found. Defaulting to (..., Y, X) order.
513 ms ± 4.88 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 选择某一层进行可视化
level = 10# 选择第10层
div_slice = div.isel(bottom_top=level)
# 创建地图投影
proj = ccrs.PlateCarree()
# 创建图形
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection=proj)
# 绘制散度
contour = ax.contourf(
lon.values, lat.values, div_slice.values*1e5,
levels=np.linspace(-50, 50, 21),
cmap='RdBu_r',
transform=proj
)
# 添加地图要素
ax.coastlines()
ax.gridlines()
plt.colorbar(contour, ax=ax, label='Divergence (s$^{-1}$)')
# 添加标题
plt.title(f'WRF Divergence at Level {level}')
plt.show()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有