ICON Training - Hands-on Session - HSURF Plot Script#
This is a Python 3 Jupyter notebook. We will be using Matplotlib, a basic plotting library for the Python programming language and its numerical mathematics extension NumPy. The Cartopy package extends the Matplotlib functionality and offers map projection definitions.
We start by loading the necessary modules.
import pathlib
import numpy as np # data structures (floating-point arrays)
import netCDF4 # NetCDF I/O library
import cartopy
from matplotlib import pyplot as plt # Base library for plotting
import matplotlib.colors as mcol # Color converter
Set file locations:
filename = "/pool/data/ICON/ICON_training/exercise_lam/grids/extpar_DOM01.nc"
Open the NetCDF grid file and load the data sites (cell circumcenters):
ds = netCDF4.Dataset(filename)
cx = np.degrees(np.asarray(ds["clon"]))
cy = np.degrees(np.asarray(ds["clat"]))
Load external parameters data set from a second file, get min/max::
src_data = np.asarray(ds["topography_c"])
plot_min = np.round(np.amin(src_data))
plot_max = np.round(np.amax(src_data))
Then we plot with the function tricontourf
:
fig = plt.figure(figsize=(7, 7))
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray')
ax.add_feature(cartopy.feature.COASTLINE,edgecolor='gray')
ax.set_aspect(1.3)
cmap = mcol.LinearSegmentedColormap.from_list("terrain",plt.get_cmap("terrain")(np.linspace(0.2, 1, 100)))
ax.tricontourf(cx, cy, src_data, transform=cartopy.crs.PlateCarree(), cmap=cmap)
plt.colorbar(plt.cm.ScalarMappable(cmap=cmap,norm=plt.Normalize(plot_min,plot_max)),ax=ax, orientation='horizontal', label="m")
plt.show()
fig.savefig(str(pathlib.Path.home()) + "/icon-training-scripts/exercise_prepare_lam/HSURF.png")
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.