ICON-LAM Total Precipitation Plot#
Load the necessary Python packages:
%env NUMEXPR_MAX_THREADS=2
import getpass
import numpy as np
import xarray as xr
import matplotlib.colors
import matplotlib.pyplot as plt
import cartopy
user = getpass.getuser()
Specify the data file that contains the tot_prec
field for the date 2021-07-16 00UTC:
datafile = f"/scratch/{user[0]}/{user}/exercise_lam/ilfff02000000ML.nc"
ds = xr.open_dataset(datafile)
tot_prec = ds["tot_prec"][0,:,:]
lon = ds["tot_prec"].lon
lat = ds["tot_prec"].lat
Then, create a discrete color map:
levels=[0.0, 0.1, 1.0, 5.0, 10.0, 15.0, 20.0, 30.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0]
colors=[ '#dfdfdf', '#fcffc4', '#fcff6d', '#defc4c', '#a1d442', '#50be78', '#35cfd5', '#319cd0', '#2000f6', '#8e39b2', '#d33ac0', '#e23632', '#832724', '#4f201f']
cmap, norm = matplotlib.colors.from_levels_and_colors(levels, colors)
Finally, create the plot:
fig1 = plt.figure(figsize=(9, 9))
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray')
ax.coastlines(color='gray')
ax.contourf(lon, lat, tot_prec, cmap = cmap, norm=norm, levels=levels, transform = cartopy.crs.PlateCarree())
#ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)
plt.colorbar(plt.cm.ScalarMappable(cmap=cmap,norm=norm),ax=ax, orientation='horizontal')#, label=plot_label)
clon=[7.066] # Ahr valley
clat=[50.533]
plt.plot(clon,clat, color='red', linewidth=2, marker='o', transform=cartopy.crs.Geodetic())
plt.text(clon[0],clat[0], 'Ahr valley', color='black', horizontalalignment='left', transform=cartopy.crs.Geodetic())
clon=[6.953101] # Cologne
clat=[50.935173]
plt.plot(clon,clat, color='red', linewidth=2, marker='o', transform=cartopy.crs.Geodetic())
plt.text(clon[0],clat[0], 'Cologne', color='black', horizontalalignment='left', transform=cartopy.crs.Geodetic())
plt.show()
Author info: Deutscher Wetterdienst (DWD) 2025 :: icon@dwd.de. For a full list of contributors, see CONTRIBUTING in the root directory. License info: see LICENSE file.