MetPy 1.6+散度计算逻辑变更咨询:原导数求和方法失效
MetPy 1.6及以上版本散度计算差异问题
- 发现MetPy 1.6及以上版本中散度计算存在潜在问题,需明确其计算逻辑以排查问题。2019年使用时,
mpcalc.divergence(u,v)的结果与mpcalc.first_derivative(u, axis=1) + mpcalc.first_derivative(v, axis=0)一致,但当前两者结果存在明显差异。 - 官方文档仅给出散度公式:$$\nabla \cdot U = \frac{\partial u}{\partial x} + \frac{\partial v}{\partial y}$$,作为非软件工程师,难以梳理冗长的源码。
- 本次测试使用
from metpy.cbook import example_data获取的示例数据,基于官方文档示例代码进行验证,并通过Cartopy绘制了误差对比图,测试代码如下:
import numpy as np import cartopy.crs as ccrs import cartopy.feature as cfeature import shapely.geometry as sgeom import cartopy.io.shapereader as shpreader import matplotlib.pyplot as plt plt.rcParams['text.usetex'] = True import matplotlib.ticker as mticker import metpy.calc as mpcalc from metpy.cbook import example_data # load example data ds = example_data() u = ds.uwind;v = ds.vwind; lon = ds.lon.values; lat = ds.lat.values # Calculate the total deformation of the flow div = mpcalc.divergence(ds.uwind, ds.vwind).values div3 = mpcalc.first_derivative(u,axis = 1).values+mpcalc.first_derivative(v,axis = 0).values error = (div3-div)/div*100 ax3 = plt.axes(projection=ccrs.Orthographic(265-360,35)) shapename = 'admin_1_states_provinces_lakes' states_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename) cf = ax3.contourf(lon, lat, error,range(-100, 100, 10), cmap=plt.cm.bwr_r,transform=ccrs.PlateCarree()) plt.colorbar(cf, pad=.15, aspect=50, label = '%error', orientation = 'vertical') ax3.set_title('Error between Divergence and Computed Divergence') ax3.add_feature(cfeature.STATES, edgecolor='black', linewidth=2) ax3.coastlines() gl = ax3.gridlines(crs = ccrs.PlateCarree(),draw_labels = True) gl.xlocator = mticker.FixedLocator([270-360,265-360,260-360]) gl.ylocator = mticker.FixedLocator([30,32.5,35,37.5]) plt.show()
- 需求:获取mpcalc库中散度计算的具体实现代码;此前参考的导数计算相关文章目前无法访问,需替代信息。
内容的提问来源于stack exchange,提问作者Researcher R
相关产品推荐
相关产品推荐

