{ "cells": [ { "cell_type": "markdown", "id": "33070195-2d5f-4ee3-b6cb-a658209b1f4d", "metadata": {}, "source": [ "# Basic Plotting\n" ] }, { "cell_type": "markdown", "id": "8f2a265a-5258-47df-9da2-86afb69cf660", "metadata": {}, "source": [ "**BEFORE BEGINNING THIS EXERCISE** - Check that your kernel (upper right corner, above) is `NPL 2023b`. This should be the default kernel, but if it is not, click on that button and select `NPL 2023b`." ] }, { "cell_type": "markdown", "id": "4ef5d835-37b7-4a4a-84c1-8053093f1500", "metadata": {}, "source": [ "_______________\n", "This activity was developed primarily by Cecile Hannay and Jesse Nusbaumer." ] }, { "cell_type": "markdown", "id": "b35face4-1542-4ab0-8f04-8220a00b0086", "metadata": {}, "source": [ "_______________\n", "\n", "For the atmospheric data, we will look at common variables in the atmospheric diagnostics. This notebook covers 3 basic plotting examples:\n", "\n", "Exercise 1: Global lat/lon of surface temperature\n", "\n", "Exercise 2: Zonal mean of short wave cloud forcing\n", "\n", "Exercise 3: Temperature zonal mean with vertical levels\n", "\n", "Some of the plotting in these examples are based on the AMWG diagnostic framework (ADF) and some are natively from the `xarray` functionality. `xarray` will be used for the data I/O, analysis, and some plotting, `matplotlib` and `cartopy` will aid in plotting, and `numpy` for calculations" ] }, { "cell_type": "code", "execution_count": null, "id": "72e6f251-c6ff-4478-adab-783ac1795e34", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import cartopy.crs as ccrs\n", "import cartopy.feature as cfeature\n", "import cftime\n", "import matplotlib as mpl\n", "import matplotlib.path as mpath\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import xarray as xr\n", "from matplotlib.gridspec import GridSpec\n", "from matplotlib.lines import Line2D\n", "from mpl_toolkits.axes_grid1 import make_axes_locatable" ] }, { "cell_type": "markdown", "id": "3d56ba02-c652-4a97-a466-d3633e53aeb1", "metadata": {}, "source": [ "The first step is to grab an atmosphere (CAM) history file from your CESM model run" ] }, { "cell_type": "code", "execution_count": null, "id": "20d4eec8-5332-44a5-bd5c-910048c5d95f", "metadata": {}, "outputs": [], "source": [ "# Set your username here:\n", "username = \"PUT_USER_NAME_HERE\"\n", "\n", "# Here we point to the archive directory from your b.day2.1 simulation\n", "monthly_output_path = f\"/glade/scratch/{username}/archive/b.day2.1/atm/hist\"\n", "\n", "# If you were unable to successfully run the b.day2.1 simulation, then feel free to use\n", "# this provided simulation data instead:\n", "#monthly_output_path = \"/glade/p/cesm/tutorial/tutorial_2023_archive/b.day2.1/atm/hist\"\n", "\n", "# Name of history file to plot\n", "file_name = \"b.day2.1.cam.h0.0003-07.nc\"\n", "\n", "files = os.path.join(monthly_output_path, file_name)\n", "files" ] }, { "cell_type": "code", "execution_count": null, "id": "fa978aa8-bf89-4971-a59b-ae2fafb9e0a5", "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset(files)\n", "ds" ] }, { "cell_type": "markdown", "id": "39d93758-aedb-42ab-810c-257a37dae487", "metadata": {}, "source": [ "_______________\n", "## Exercise 1: Make a lat-lon plot of TS\n", "\n", "To highlight plotting the variables from the CESM atmosphere (CAM) file, the first example will plot a simple global lat/lon plot of surface temperature `TS`" ] }, { "cell_type": "markdown", "id": "1e6b83ad", "metadata": {}, "source": [ "### Grab data from first time stamp\n", "\n", "NOTE: This dataset has only one time" ] }, { "cell_type": "code", "execution_count": null, "id": "feb6d347-cbcb-407c-bb4d-6d89c69fcf32", "metadata": {}, "outputs": [], "source": [ "ts_0 = ds.TS.sel({\"time\": ds.TS.time.values[0]}).squeeze()\n", "ts_0" ] }, { "cell_type": "markdown", "id": "cf1a965c", "metadata": {}, "source": [ "Next is to set up the map. Since we are plotting a global lat/lon, we will use the Plate Carree projection. " ] }, { "cell_type": "code", "execution_count": null, "id": "7b183aba-6eae-48ad-9545-f3c918fb99ff", "metadata": {}, "outputs": [], "source": [ "# define the colormap\n", "# cmap = plt.cm.get_cmap('jet')\n", "cmap = mpl.colormaps[\"jet\"]\n", "\n", "# set up the figure with a Plate Carree projection\n", "fig = plt.figure(figsize=(15, 10))\n", "\n", "ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())\n", "\n", "# Plot the first timeslice of TS\n", "img = ax.pcolormesh(ds.lon, ds.lat, ts_0, cmap=cmap, transform=ccrs.PlateCarree())\n", "\n", "plt.title(\"Surface Temperature\", fontsize=20)\n", "\n", "# Set up colorbar\n", "plt.colorbar(img, orientation=\"vertical\", fraction=0.0242, pad=0.01)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "ccd1f942-a16d-490b-98e6-5f57e92a1cce", "metadata": {}, "source": [ "
Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "Figure: Plotting solution.
*\n", " \n", "