{ "cells": [ { "cell_type": "markdown", "id": "d157bce7-ea0e-45ad-9c4e-7ee254b54d92", "metadata": { "tags": [] }, "source": [ "
\n", "\n", "# ICON Training - Hands-on Session - Basic Plot Script for Ex 5 - Allocating Additional Fields\n", "---" ] }, { "cell_type": "markdown", "id": "38c20862-5e83-4989-b72a-47da1e8483d3", "metadata": {}, "source": [ "This is a Python 3 Jupyter notebook. We create a plot with Matplotlib and Cartopy and start by loading the necessary modules." ] }, { "cell_type": "code", "execution_count": null, "id": "2e0f9f82-1eb9-4e3c-b7ef-72115a70c0d8", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import numpy as np\n", "import xarray as xr\n", "import cartopy\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "c11082ca-9043-499e-9774-461180eda54f", "metadata": {}, "source": [ "Define the directory and filenames." ] }, { "cell_type": "code", "execution_count": null, "id": "8a2d2642-a044-4848-8e51-b52b1bdc5cbb", "metadata": {}, "outputs": [], "source": [ "user = getpass.getuser()\n", "\n", "# absolute path to directory with plenty of space:\n", "EXPDIR=f\"/scratch/{user[0]}/{user}/exercise_programming/\"\n", "\n", "# file names\n", "filename_ll = f\"{EXPDIR}/Ex5_Diagnostic_DOM01_20210714T120000Z.nc\"\n", "print(f\"Filename: {filename_ll=}\")" ] }, { "cell_type": "markdown", "id": "59e7225f-9361-4ea5-a838-0b6969eec772", "metadata": {}, "source": [ "
\n", " Exercise: \n", " Specify the variable you have implemented into ICON for plotting.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "d1c95799-bbae-4f61-9c6a-0b54f71602fe", "metadata": {}, "outputs": [], "source": [ "plotvar=????????" ] }, { "cell_type": "markdown", "id": "bc054efe-5e31-496f-8e7a-154e7448abe7", "metadata": { "tags": [] }, "source": [ "
\n", "Solution\n", "\n", "```\n", "plotvar='process_id' # for the Fortran exercise part F1\n", "#plotvar='comin_process_id' # for the ComIn exercise part\n", "```\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "d5618de4-4d41-4797-8305-e5fdf652d2aa", "metadata": { "tags": [] }, "source": [ "### Create the plot" ] }, { "cell_type": "code", "execution_count": null, "id": "e61df059-244d-484d-8238-5b954bb9ae17", "metadata": {}, "outputs": [], "source": [ "#%matplotlib widget\n", "\n", "# Open the dataset from the specified filename\n", "ds_ll = xr.open_dataset(filename_ll)\n", "\n", "# Extract longitude and latitude from the dataset for the specified plot variable\n", "lon = ds_ll[plotvar].lon\n", "lat = ds_ll[plotvar].lat\n", "\n", "# Create a figure with a specified size\n", "fig = plt.figure(figsize=(9, 9))\n", "\n", "# Select the first time step of the dataset for plotting\n", "dataset = ds_ll[plotvar][0,:,:]\n", "\n", "# Define the color map and the minimum and maximum values for the plot\n", "plot_color = \"flag\"\n", "plot_min = np.round(np.amin(dataset))\n", "plot_max = np.round(np.amax(dataset))\n", "\n", "# Add a subplot with a PlateCarree projection and no frame\n", "ax = fig.add_subplot(projection=cartopy.crs.PlateCarree(), frameon=False)\n", "\n", "# Set the title of the plot\n", "ax.set_title(\"ICON Domain Decomposition\", y=1.05)\n", "\n", "# Add country borders to the plot\n", "ax.add_feature(cartopy.feature.BORDERS, edgecolor='gray')\n", "\n", "# Create a filled contour plot with the specified color map and transform\n", "ax.contourf(lon, lat, dataset, int(plot_max-plot_min), cmap=plot_color, transform=cartopy.crs.PlateCarree())\n", "\n", "# Add gridlines with labels in degrees, minutes, and seconds\n", "ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)\n", "\n", "# Add a color bar to the plot with a horizontal orientation and a label\n", "plt.colorbar(plt.cm.ScalarMappable(cmap=plot_color, norm=plt.Normalize(plot_min, plot_max)), ax=ax, orientation='horizontal', label=\"process ID\")\n", "\n", "# Display the plot\n", "plt.show()\n" ] }, { "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": "0 Python 3 (based on the module python3/unstable", "language": "python", "name": "python3_unstable" }, "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 }