ICON Training - Hands-on Session - Basic Plot Script for Ex 5 - Allocating Additional Fields#
This is a Python 3 Jupyter notebook. We create a plot with Matplotlib and Cartopy and start by loading the necessary modules.
import getpass
import numpy as np
import xarray as xr
import cartopy
import matplotlib.pyplot as plt
Define the directory and filenames.
user = getpass.getuser()
# absolute path to directory with plenty of space:
EXPDIR=f"/scratch/{user[0]}/{user}/exercise_programming/"
# file names
filename_ll = f"{EXPDIR}/Ex5_Diagnostic_DOM01_20210714T120000Z.nc"
print(f"Filename: {filename_ll=}")
Exercise:
Specify the variable you have implemented into ICON for plotting.
plotvar=????????
Solution
plotvar='process_id' # for the Fortran exercise part F1
#plotvar='comin_process_id' # for the ComIn exercise part
Create the plot#
#%matplotlib widget
# Open the dataset from the specified filename
ds_ll = xr.open_dataset(filename_ll)
# Extract longitude and latitude from the dataset for the specified plot variable
lon = ds_ll[plotvar].lon
lat = ds_ll[plotvar].lat
# Create a figure with a specified size
fig = plt.figure(figsize=(9, 9))
# Select the first time step of the dataset for plotting
dataset = ds_ll[plotvar][0,:,:]
# Define the color map and the minimum and maximum values for the plot
plot_color = "flag"
plot_min = np.round(np.amin(dataset))
plot_max = np.round(np.amax(dataset))
# Add a subplot with a PlateCarree projection and no frame
ax = fig.add_subplot(projection=cartopy.crs.PlateCarree(), frameon=False)
# Set the title of the plot
ax.set_title("ICON Domain Decomposition", y=1.05)
# Add country borders to the plot
ax.add_feature(cartopy.feature.BORDERS, edgecolor='gray')
# Create a filled contour plot with the specified color map and transform
ax.contourf(lon, lat, dataset, int(plot_max-plot_min), cmap=plot_color, transform=cartopy.crs.PlateCarree())
# Add gridlines with labels in degrees, minutes, and seconds
ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)
# Add a color bar to the plot with a horizontal orientation and a label
plt.colorbar(plt.cm.ScalarMappable(cmap=plot_color, norm=plt.Normalize(plot_min, plot_max)), ax=ax, orientation='horizontal', label="process ID")
# Display the plot
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.