{ "cells": [ { "cell_type": "markdown", "id": "d157bce7-ea0e-45ad-9c4e-7ee254b54d92", "metadata": { "tags": [] }, "source": [ "# ICON Training - Hands-on Session - Basic plot script using Matplotlib and Cartopy\n", "---" ] }, { "cell_type": "markdown", "id": "82416258-d6c8-4e5f-ab72-f32245cd9da7", "metadata": { "tags": [] }, "source": [ "This is a Python 3 Jupyter notebook. We start by loading the necessary modules." ] }, { "cell_type": "code", "execution_count": null, "id": "e1a1867c-bd4a-4a3c-bfc5-8e43ec7e53fa", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import numpy as np\n", "import xarray as xr\n", "import cartopy\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "bae0f2f2-86ff-45b5-88ae-67d13caa3a63", "metadata": {}, "source": [ "Define the directory and filenames." ] }, { "cell_type": "code", "execution_count": null, "id": "56c07887-ca1f-4bd7-8c1c-bc1e7751c68d", "metadata": {}, "outputs": [], "source": [ "user = getpass.getuser()\n", "EXPDIR = f\"/scratch/{user[0]}/{user}/exercise_lam/\"\n", "\n", "# file name\n", "filename_ll = f\"{EXPDIR}/ilfff01000000ML.nc\"\n", "\n", "# output file\n", "output_filename1 = \"./TQV_regular.png\"\n", "output_filename2 = \"./TQC_regular.png\"\n", "\n", "# colorbar and label\n", "plot_color = \"gist_rainbow\"\n", "plot_label = \"kg m-2\"\n", "\n", "#select the timestep to be plotted (note that we have only 1 timestep per file)\n", "timestep = 0" ] }, { "cell_type": "markdown", "id": "d6ab0b55-cca8-4ac7-a671-f5654f156960", "metadata": {}, "source": [ "Then open the **regular (lat/lon)** datafile and preload coordinate arrays:" ] }, { "cell_type": "code", "execution_count": null, "id": "13beebb9-6952-455c-bdb3-5394248bae16", "metadata": {}, "outputs": [], "source": [ "ds_ll = xr.open_dataset(filename_ll)\n", "lon = ds_ll[\"tqv_dia\"].lon\n", "lat = ds_ll[\"tqv_dia\"].lat" ] }, { "cell_type": "markdown", "id": "b1aa04af-f4d1-4064-957a-8d13b26e8d0d", "metadata": {}, "source": [ "Create 2 plots. One for tqv and one for tqc" ] }, { "cell_type": "code", "execution_count": null, "id": "12ba7619-d13c-4102-8160-306c24822748", "metadata": {}, "outputs": [], "source": [ "# process tqv_dia\n", "#\n", "fig1 = plt.figure(figsize=(9, 9))\n", "tqv = ds_ll[\"tqv_dia\"][timestep,:,:]\n", "\n", "plot_min = np.round(np.amin(tqv))\n", "plot_max = np.round(np.amax(tqv))\n", "\n", "ax = plt.axes(projection=cartopy.crs.PlateCarree())\n", "ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray')\n", "ax.contourf(lon, lat, tqv, 60, cmap = plot_color, transform = cartopy.crs.PlateCarree())\n", "ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)\n", "plt.colorbar(plt.cm.ScalarMappable(cmap=plot_color,norm=plt.Normalize(plot_min,plot_max)),ax=ax, orientation='horizontal', label=plot_label)\n", "\n", "plt.show()\n", "\n", "# process tqc_dia\n", "#\n", "fig2 = plt.figure(figsize=(9, 9))\n", "\n", "tqc = ds_ll[\"tqc_dia\"][timestep,:,:]\n", "\n", "plot_min = np.round(np.amin(tqc))\n", "plot_max = np.round(np.amax(tqc))\n", "\n", "ax = plt.axes(projection=cartopy.crs.PlateCarree())\n", "ax.add_feature(cartopy.feature.BORDERS,edgecolor='gray')\n", "ax.contourf(lon, lat, tqc, 60, cmap = plot_color, transform = cartopy.crs.PlateCarree())\n", "ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)\n", "plt.colorbar(plt.cm.ScalarMappable(cmap=plot_color,norm=plt.Normalize(plot_min,plot_max)),ax=ax, orientation='horizontal', label=plot_label)\n", "\n", "plt.show()\n", "\n", "fig1.savefig(output_filename1, dpi=200)\n", "fig2.savefig(output_filename2, dpi=200)" ] }, { "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 }