{ "cells": [ { "cell_type": "markdown", "id": "d157bce7-ea0e-45ad-9c4e-7ee254b54d92", "metadata": { "tags": [] }, "source": [ "# ICON Training - Hands-on Session - HSURF Plot Script\n", "---" ] }, { "cell_type": "markdown", "id": "82416258-d6c8-4e5f-ab72-f32245cd9da7", "metadata": {}, "source": [ "This is a Python 3 Jupyter notebook. \n", "We will be using [Matplotlib](https://matplotlib.org/), a basic plotting library for the Python programming language and its numerical mathematics extension NumPy. The\n", "[Cartopy](https://github.com/SciTools/cartopy) package extends the Matplotlib functionality and offers map projection definitions.\n", "\n", "We start by loading the necessary modules." ] }, { "cell_type": "code", "execution_count": null, "id": "3cc8ce8a-d88e-4367-9aaf-16ea0cc907df", "metadata": {}, "outputs": [], "source": [ "import pathlib\n", "import numpy as np # data structures (floating-point arrays)\n", "import netCDF4 # NetCDF I/O library\n", "import cartopy\n", "from matplotlib import pyplot as plt # Base library for plotting\n", "import matplotlib.colors as mcol # Color converter" ] }, { "cell_type": "markdown", "id": "5171ae24-1fd0-40b3-9262-a32b47d2b3de", "metadata": {}, "source": [ "Set file locations:" ] }, { "cell_type": "code", "execution_count": null, "id": "c365c5ac-0553-4990-934d-087499164a57", "metadata": {}, "outputs": [], "source": [ "filename = \"/pool/data/ICON/ICON_training/exercise_lam/grids/extpar_DOM01.nc\"" ] }, { "cell_type": "markdown", "id": "7f0952c7-14f8-4cd6-969b-9e1f5296cc67", "metadata": {}, "source": [ "Open the NetCDF grid file and load the data sites (cell circumcenters):" ] }, { "cell_type": "code", "execution_count": null, "id": "0dd83efd-240a-4bfa-b193-8fa1f4bee235", "metadata": {}, "outputs": [], "source": [ "ds = netCDF4.Dataset(filename)\n", "cx = np.degrees(np.asarray(ds[\"clon\"]))\n", "cy = np.degrees(np.asarray(ds[\"clat\"]))" ] }, { "cell_type": "markdown", "id": "ede22306-6f4b-4813-bc28-5ee8900fe1fb", "metadata": {}, "source": [ "Load external parameters data set from a second file, get min/max::" ] }, { "cell_type": "code", "execution_count": null, "id": "269a30cb-9bd6-40be-b969-90dabe952d68", "metadata": {}, "outputs": [], "source": [ "src_data = np.asarray(ds[\"topography_c\"])\n", "\n", "plot_min = np.round(np.amin(src_data))\n", "plot_max = np.round(np.amax(src_data))" ] }, { "cell_type": "markdown", "id": "af50f9c1-2819-4ba1-9558-8e0f34a0f90e", "metadata": {}, "source": [ "Then we plot with the function `tricontourf`:" ] }, { "cell_type": "code", "execution_count": null, "id": "cf0eb192-ae02-48bc-9b8b-4756ff26f3ce", "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(7, 7))\n", "\n", "ax = plt.axes(projection=cartopy.crs.PlateCarree())\n", "ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray')\n", "ax.add_feature(cartopy.feature.COASTLINE,edgecolor='gray')\n", "ax.set_aspect(1.3)\n", "\n", "cmap = mcol.LinearSegmentedColormap.from_list(\"terrain\",plt.get_cmap(\"terrain\")(np.linspace(0.2, 1, 100)))\n", "\n", "ax.tricontourf(cx, cy, src_data, transform=cartopy.crs.PlateCarree(), cmap=cmap)\n", "\n", "plt.colorbar(plt.cm.ScalarMappable(cmap=cmap,norm=plt.Normalize(plot_min,plot_max)),ax=ax, orientation='horizontal', label=\"m\")\n", "\n", "plt.show()\n", "fig.savefig(str(pathlib.Path.home()) + \"/icon-training-scripts/exercise_prepare_lam/HSURF.png\")" ] }, { "cell_type": "markdown", "id": "57398f8f-f9b9-4241-9afc-313dd4c24b55", "metadata": {}, "source": [ "---\n", "\n", "*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.*" ] } ], "metadata": { "kernelspec": { "display_name": "1 Python 3 (based on the module python3/2023.01)", "language": "python", "name": "python3_2023_01" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }