I would like to create weather maps from models. For this purpose the German Weather Service provides its forecast data in GRIB2 format.
With xarray I read the grib-file like this:
ds = od('path/to/grib', backend_kwargs={'filter_by_keys': {'typeOfLevel': 'heightAboveGround', 'level': 2}})
After that I want to plot this data:
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())
But those GRIB files are for whole Europe. I only want to plot for example the Data for Germany with its map in background? So how can I restrict the coordinates (latitude, longitude), so that the plotted map will only show for example Germany?
What you can do is to mask the data with the where function:
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))
To get only germany you need shapefiles to check whether a coordinates is in the german polygon or not. But this is not very clear if you need such a solution.