在 python 中绘制 grib 文件时是否可以限制坐标?

Is there the possibility to restrict the coordinates while plotting a grib file in python?

我想根据模型创建天气图。为此,德国气象局以 GRIB2 格式提供预报数据。

我用 xarray 读取 grib 文件是这样的:

ds = od('path/to/grib', backend_kwargs={'filter_by_keys': {'typeOfLevel': 'heightAboveGround', 'level': 2}})

之后我想绘制这些数据:

fig = plt.figure(figsize=(10,10))
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='10m')
ax.gridlines()

states_provinces = cfeature.NaturalEarthFeature(
    category='cultural',
    name='admin_0_boundary_lines_land',
    scale='10m',
    facecolor='none')
provinces_provinces = cfeature.NaturalEarthFeature(
    category='cultural',
    name='admin_1_states_provinces_lines',
    scale='10m',
    facecolor='none')


ax.add_feature(cfeature.COASTLINE)
ax.add_feature(states_provinces, edgecolor='gray', zorder=1)
ax.add_feature(provinces_provinces, edgecolor='gray', zorder=1)

plot = ds.t2m.plot(cmap=plt.cm.coolwarm, transform=ccrs.PlateCarree())

但是那些 GRIB 文件是针对整个欧洲的。我只想绘制例如德国的数据及其背景地图?那么我怎样才能限制坐标(纬度,经度),以便绘制的地图只显示例如德国?

您可以做的是使用 where 函数屏蔽数据:

ds = ds.where(np.logical_and(ds.latitude > lat_bounds_min, ds.latitude < lat_bounds_max))
ds = ds.where(np.logical_and(ds.longitude > lon_bounds_min, ds.longitude < lon_bounds_max))

要仅获取德国,您需要 shapefile 来检查坐标是否在德国多边形中。但是如果你需要这样的解决方案,这不是很清楚。