Python中的一组GrADS函数。
mygrads的Python项目详细描述
我的格拉兹
{a2}
这是用python实现的函数的集合,它们在GrADS中复制它们的实现。在
计算需要Numpy。我们使用Xarray读入nc文件,Matplotlib和Cartopy进行绘图。下面是一个简单的示例,该示例使用包中包含的示例数据运行。在
安装
pip install mygrads
示例
导入
^{pr2}$其他常用进口
importnumpyasnpimportxarrayasxrimportcartopy.crsasccrsimportmatplotlib.pyplotasplt
读入一些数据
# We are using some sample data downloaded from the NCEP Reanalysis 2# Downloaded from: https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis2.html# Zonal windds=xr.open_dataset('data/u.nc')u=ds['uwnd'][0,0,:,:].valueslat=ds['lat'].valueslon=ds['lon'].values# Meridional windds=xr.open_dataset('data/v.nc')v=ds['vwnd'][0,0,:,:].values# Temperatureds=xr.open_dataset('data/t.nc')t=ds['air'][0,0,:,:].values
示例
水平散度
与渐变hdivg
(ref.)相同。在
div=mg.hdivg(u,v,lat,lon)
相对涡度
或相对涡度的垂直分量。与渐变相同hcurl
(ref.)
vort=mg.hcurl(u,v,lat,lon)
温度平流
不是在本机中实现的。重要的是,考虑到上述函数,它非常简单,并且已经描述了here。在
tadv=mg.hadv(u,v,t,lat,lon)
中心有限差分
这复制了GrADS的cdiff
函数(参见它们的docu)差异在栅格空间中进行,对于间距不相等的栅格,不执行任何调整。每个网格点的结果值是网格点的值加一减去网格点的值减去一。”
它也在hdivg
、hcurl
和hadv
实现中内部使用。numpy-like参数axis
应为0或1,以指示在哪个维度上计算导数。在
latv,lonv=np.meshgrid(lat,lon,indexing='ij')dudx=mg.cdiff(u,axis=0)/mg.cdiff(lonv*np.pi/180)
绘图
请注意,数据来自500百帕水平,所以风基本上是地转的。因此,没有太多的分歧导致射流的邻域。在
fig=plt.figure(figsize=(10,8))ax=fig.add_subplot(2,2,1,projection=ccrs.Mercator())ax.set_extent([-120,-10,-60,10],crs=ccrs.PlateCarree())ax.coastlines(resolution='50m')mesh=ax.pcolormesh(lon,lat,t-273.5,vmin=-30,vmax=0,transform=ccrs.PlateCarree(),cmap="Spectral_r")cbar=plt.colorbar(mesh,shrink=0.75,label='[°C]')q=ax.quiver(lon,lat,u,v,minlength=0.1,scale_units='xy',scale=0.0001,transform=ccrs.PlateCarree(),color='k',width=0.003)plt.title('Input Data\n wind and temperature at 500 hPa')ax=fig.add_subplot(2,2,2,projection=ccrs.Mercator())ax.set_extent([-120,-10,-60,10],crs=ccrs.PlateCarree())ax.coastlines(resolution='50m')mesh=ax.pcolormesh(lon,lat,div*100000,vmin=-1.5,vmax=1.5,transform=ccrs.PlateCarree(),cmap="RdBu_r")cbar=plt.colorbar(mesh,shrink=0.75,label='[$x10^{-5}$ s$^{-1}$]')plt.title('Horizontal Divergence')ax=fig.add_subplot(2,2,3,projection=ccrs.Mercator())ax.set_extent([-120,-10,-60,10],crs=ccrs.PlateCarree())ax.coastlines(resolution='50m')mesh=ax.pcolormesh(lon,lat,vort*100000,vmin=-5,vmax=5,transform=ccrs.PlateCarree(),cmap="RdBu_r")cbar=plt.colorbar(mesh,shrink=0.75,label='[$x10^{-5}$ s$^{-1}$]')plt.title('Relative Vorticity')ax=fig.add_subplot(2,2,4,projection=ccrs.Mercator())ax.set_extent([-120,-10,-60,10],crs=ccrs.PlateCarree())ax.coastlines(resolution='50m')mesh=ax.pcolormesh(lon,lat,tadv*84600,vmin=-5,vmax=5,transform=ccrs.PlateCarree(),cmap="RdBu_r")cbar=plt.colorbar(mesh,shrink=0.75,label='[°C day$^{-1}$]')plt.title('Advection of Temperature')plt.tight_layout()fig.savefig('example.png',dpi=300)
- 项目
标签: