如何在极坐标文件grib2中绘制极坐标等值线

2024-05-12 19:54:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用matplotlib绘制CMC grib2压力预测文件来绘制压力等值线。grib2网格的描述可以在这里找到:https://weather.gc.ca/grib/grib2_reg_10km_e.html。grib2文件位于以下目录:http://dd.weather.gc.ca/model_gem_regional/10km/grib2/00/000/,以CMC_reg_PRMSL_MSL_0_ps10km开头,后跟日期。这是一个包含平均海平面压力的grib文件。

我的问题是,在实际的压力等值线上,我得到了一些沿着纬度线的直线。我想这可能是因为我用PlateCarree绘制而不是大地测量,但是等高线图不允许使用大地测量。我的阴谋结果是: enter image description here

代码如下:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import cartopy
import cartopy.crs as ccrs
import Nio

gr = Nio.open_file('./data/CMC_reg_PRMSL_MSL_0_ps10km_2018111800_P000.grib2', 'r')
print(gr)
names = gr.variables.keys()
print("Variable Names:", names)
dims = gr.dimensions
print("Dimensions: ", dims)
attr = gr.attributes.keys()
print("Attributes: ", attr)

obs = gr.variables['PRMSL_P0_L101_GST0'][:]
lats = gr.variables["gridlat_0"][:]
lons = gr.variables["gridlon_0"][:]

fig = plt.figure(figsize=(15, 2))
intervals = range(95000, 105000, 400)
ax=plt.axes([0.,0.,1.,1.],projection=ccrs.PlateCarree())
obsobj = plt.contour(lons, lats, obs, intervals, cmap='jet',transform=ccrs.PlateCarree())
states_provinces = cartopy.feature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none')
ax.add_feature(cartopy.feature.BORDERS)
ax.coastlines(resolution='10m')  
ax.add_feature(states_provinces,edgecolor='gray')
obsobj.clabel()
colbar =plt.colorbar(obsobj)

如有任何建议,我们将不胜感激。

更新

对于没有PyNIO的人,可以使用comments部分中的转储文件来重新生成。

只需删除对NIO的所有引用,并将lats、lons、obs赋值替换为以下内容。

^{pr2}$

Tags: 文件importmatplotlibas绘制pltvariablesax
2条回答

问题

问题是电网绕着地球转。因此,在-180°的网格上会有一些点,其最近的邻居位于+180°处,也就是说,网格围绕着反eridian。下面将沿着两个方向绘制栅格索引。可以看到第一个网格行(黑色)出现在绘图的两侧。在

enter image description here

因此,沿着太平洋向西的等高线需要直接穿过地块,继续向地块另一侧的日本延伸。这会导致不需要的线

enter image description here

解决方案

一个解决办法是遮住板的外部点。这些发生在网格的中间。在经度大于179°或小于-179°的坐标处切割网格,并将北极排除在外

enter image description here

蓝色表示切割点。在

将其应用于等高线图可以得到:

import matplotlib.pyplot as plt
import numpy as np
import cartopy
import cartopy.crs as ccrs

lats = np.load('data/lats.dump')
lons = np.load('data/lons.dump')
obs = np.load('data/obs.dump')

intervals = range(95000, 105000, 400)

fig, ax = plt.subplots(figsize=(15,4), subplot_kw=dict(projection=ccrs.PlateCarree()))
fig.subplots_adjust(left=0.03, right=0.97, top=0.8, bottom=0.2)

mask = (lons > 179) | (lons < -179) | (lats > 89)
maskedobs = np.ma.array(obs, mask=mask)

pc = ax.contour(lons, lats, maskedobs, intervals, cmap='jet', transform=ccrs.PlateCarree())

ax.add_feature(cartopy.feature.BORDERS)
ax.coastlines(resolution='10m')  

colbar =plt.colorbar(pc)

plt.show()

enter image description here

如果要将经度加起来+180以避免负坐标,则代码应该正在运行。在我看来,坐标变换应该是合法的。在

相关问题 更多 >