{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# ENV/ATM 415: Climate Laboratory\n", "\n", "[Brian E. J. Rose](http://www.atmos.albany.edu/facstaff/brose/index.html), University at Albany\n", "\n", "# Lecture 4: Introducing the Community Earth System Model (CESM)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "### What is it?\n", "\n", "- CESM is one of a handful of complex coupled GCMs that are used as part of the IPCC process.\n", "- Developed and maintained at NCAR (Boulder CO) by a group of climate scientists and software engineers.\n", "- “Community” refers to the fact that the code is open-source, with new pieces contributed by a wide variety of users. \n", "\n", "I use CESM in my own research. We are going to be using CESM in this course. Everyone should visit the website and learn about it.\n", "\n", "http://www2.cesm.ucar.edu" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Key components of CESM:\n", "\n", "see http://www.cesm.ucar.edu/models/cesm1.2/ for more info\n", " \n", " - Atmospheric model (AGCM)\n", " - Community Atmsophere Model (CAM)\n", " - Ocean model (OGCM)\n", " - Parallel Ocean Program (POP)\n", " - Land surface model\n", " - Community Land Model (CLM)\n", " - Sea ice model\n", " - Community Ice CodE (CICE)\n", " \n", "The software is somewhat modular, so different submodels can be combined together depending on the nature of the scientific problem at hand and the available computer power." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### The Slab Ocean Model\n", "\n", "Our experiments will use CESM in the so-called **Slab Ocean Model** mode, in which **the ocean is represented by a static layer of water with some fixed heat capacity** but no motion." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Recall that we saw this schematic of different ways to represent the ocean in climate models:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "![Figure 2.9: ocean model hierarchy](http://www.atmos.albany.edu/facstaff/brose/classes/ENV415_Spring2018/images/Primer_Figure2.9.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Using the slab ocean greatly reduced the *time required for the model to reach equilibrium*. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The net effect heat transport by ocean currents is prescribed through a so-called **q-flux**, which really just means we prescribe sources and sinks of heat at different locations. \n", "\n", "For (lots of) details, see " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The key is that we **allow the sea surface temperature to change**, but we fix (prescribe) the net effect of ocean currents on the transport of energy.\n", "\n", "Why do this? \n", "- Because it takes thousands of years for the full ocean model to come into equilibrium!\n", "\n", "Why should we believe the results of the slab ocean model?\n", "- We shouldn’t! \n", "\n", "But experience with coupled models (meaning interactive ocean circulation) has shown that the circulation does not change radically under $2\\times CO_2$. \n", "\n", "So the slab ocean model gives us a decent first guess at climate sensitivity.\n", "And it makes it possible to do a lot of experimentation that we wouldn’t be able to do otherwise." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "____________\n", "\n", "## Our numerical experiments with CESM\n", "____________" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Atmosphere\n", "\n", " - Horizontal resolution about 2º lat/lon\n", " - AGCM solves the fundamental equations:\n", " - Conservation of momentum, mass, energy, water, equation of state\n", " - At 2º we resolve the **synoptic-scale dynamics**\n", " - storm tracks and cyclones. \n", " - We do NOT resolve the mesoscale and smaller\n", " - thunderstorms, individual convective events, clouds\n", " - These all must be parameterized.\n", " - Model also solves equations of radiative transfer. This takes account of\n", " - composition of the atmosphere and the absorption properties of different gases\n", " - radiative effects of clouds." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Sea ice\n", "\n", "- Resolution of 1º.\n", "- Thermodynamics (conservation of energy, water and salt)\n", " - determines freezing and melting\n", "- Dynamics (momentum equations) \n", " - determine ice motion and deformation.\n", "- Complex! Sea ice is sort of a mixture of a fluid and a solid." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Land surface model\n", "\n", "- Same resolution as atmosphere. \n", "- Determines surface fluxes of heat, water, momentum (friction) based on prescribed vegetation types.\n", "- Don’t actually know much about how it works!\n", "- Great topic for someone to dig in to for their term project." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Ocean\n", "\n", "- Same grid as sea ice, 1º.\n", "- Sea surface temperature evolves based on:\n", " - heat exchange with atmosphere\n", " - prescribed “q-flux”." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Experimental setup\n", "\n", "Model is given realistic atmospheric composition, realistic solar radiation, etc.\n", "\n", "We perform a **control run** to get a baseline simulation, and take **averages of several years** (because the model has internal variability – every year is a little bit different)\n", "\n", "We then change something, e.g. $2\\times CO_2$!\n", "\n", "And allow the model to adjust to a new equilibrium, just as we did with the toy energy balance model.\n", "\n", "Once it has gotten close to its new equilibrium, we run it for several more years again to get the new climatology.\n", "\n", "Then we can look at the **differences in the climatologies before and after the perturbation**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Description of input\n", "\n", "First, let's take a look at some of the ingredients that go into the control run. **All of the necessary data will be served up by a special data server sitting in the department**, so you should be able to run this code to interact with the data on any computer that is connected to the internet." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### You need to be connected to the internet to run the code in this notebook ###\n", "\n", "You can browse the available data through a web interface here:\n", "\n", "\n", "\n", "Within this folder called `CESM runs`, you will find another folder called `som_input` which contains all the input files.\n", "\n", "The data are all stored in `NetCDF` files, a standard file format for self-describing gridded data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import xarray as xr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to use a package called [xarray](http://xarray.pydata.org) (abbreviated here as `xr`) to work with the datasets." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Boundary conditions: continents and topography\n", "\n", "Here we are going to load the input topography file and take a look at what's inside. \n", "\n", "In this case we are passing it a URL to our online dataserver. We'll put the URL in a string variable called `datapath` to simplify things later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datapath = \"http://ramadda.atmos.albany.edu:8080/repository/opendap/latest/Top/Users/BrianRose/CESM_runs/\"\n", "endstr = \"/entry.das\"\n", "\n", "# Notice that in Python we can easily concatenate strings together just by `adding` them\n", "fullURL = datapath + 'som_input/USGS-gtopo30_1.9x2.5_remap_c050602.nc' + endstr\n", "print( fullURL)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now we actually open the dataset\n", "topo = xr.open_dataset( fullURL )\n", "print(topo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `Dataset` object has several important attributes. Much of this should look familiar if you have worked with `netCDF` data before. The `xarray` package gives a very powerful and easy to use interface to the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access individual variables within the `xarray.Dataset` object as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "topo.PHIS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting the topography\n", "\n", "We will now read the geopotential and make a plot of the topography of the Earth's surface as represented on the 2º grid. The code below makes a colorful plot of the topography. We also use the land-sea mask in order to plot nothing at grid points that are entirely ocean-covered. \n", "\n", "Execute this code exactly as written first, and then play around with it to see how you might customize the graph. \n", "\n", "Note that the function `pcolormesh` does most of the work here. It's a function that makes color 2D plots of an array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "g = 9.8 # gravity in m/s2\n", "meters_per_kilometer = 1E3 \n", "height = topo.PHIS / g / meters_per_kilometer # in kilometers\n", "# Note that we have just created a new xarray.DataArray object that preserves the axis labels\n", "# Let's go ahead and give it some useful metadata:\n", "height.attrs['units'] = 'km'\n", "height.name = 'height'\n", "height" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make a plot! `xarray` is able to automatically generate labeled plots. This is very handy for \"quick and dirty\" investigation of the data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "height.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want more control over the appearance of the plot, we can use features of `matplotlib`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A filled contour plot of topography with contours every 500 m\n", "lev = np.arange(0., 6., 0.5)\n", "fig1, ax1 = plt.subplots(figsize=(8,4))\n", "# Here we are masking the data to exclude points where the land fraction is zero (water only)\n", "cax1 = ax1.contourf( height.lon, height.lat, \n", " height.where(topo.LANDFRAC>0), levels=lev)\n", "ax1.set_title('Topography (km) and land-sea mask in CESM')\n", "ax1.set_xlabel('Longitude')\n", "ax1.set_ylabel('Latitude')\n", "cbar1 = fig1.colorbar(cax1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that at 2º resolution we can see many smaller features (e.g. Pacific islands). The model is given a fractional land cover for each grid point. \n", "\n", "Here let's plot the land-sea mask itself so we can see where there is at least \"some\" water:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig2, ax2 = plt.subplots()\n", "cax2 = ax2.pcolormesh( topo.lon, topo.lat, topo.LANDFRAC )\n", "ax2.set_title('Ocean mask in CESM')\n", "ax2.set_xlabel('Longitude'); ax2.set_ylabel('Latitude')\n", "cbar2 = fig2.colorbar(cax2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Making nicer maps\n", "\n", "Notice that to make these plots we've just plotted the lat-lon array without using any map projection.\n", "\n", "There are nice tools available to make better maps. We'll leave that as a topic for another day. But if you're keen to read ahead, check out:\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ocean boundary conditions\n", "\n", "Another important input file contains information about the slab ocean. You can see this file in the data catalog here:\n", "\n", "\n", "\n", "Let's load it and take a look." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "som_input = xr.open_dataset( datapath + 'som_input/pop_frc.1x1d.090130.nc' + endstr, decode_times=False )\n", "print(som_input)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ocean / sea ice models exist on different grids than the atmosphere (1º instead of 2º resolution)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are going to look at the **annual mean heat flux out of the ocean**, which is the prescribed 'q-flux' that we give to the slab ocean model.\n", "\n", "It is stored in the field `qdp` in the input file. \n", "\n", "The sign convention in CESM is that `qdp > 0` where **heat is going IN to the ocean**. We will change the sign to plot heat going OUT of the ocean INTO the atmosphere (a more atmosphere-centric viewpoint). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "som_input.qdp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unfortunately, here is a case in which the metadata are not very useful. There is no text description of what variable `qdp` actually is, or what its units are. (It is actually in units of W/m2)\n", "\n", "We can see that there are 12 x 180 x 360 data points. One 180 x 360 grid for each calendar month!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are going to take the average over the year at each point. \n", "\n", "We will use the power of `xarray` here to take the average over the time dimension, leaving us with a single grid on 180 latitude points by 360 longitude points:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(-som_input.qdp.mean(dim='time')).plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now make a nice plot of the annual mean q-flux. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# We can always set a non-standard size for our figure window\n", "fig3, ax3 = plt.subplots(figsize=(10, 6))\n", "lev = np.arange(-700., 750., 50.)\n", "cax3 = ax3.contourf(som_input.xc, som_input.yc, \n", " -som_input.qdp.mean(dim='time'), \n", " levels=lev, cmap=plt.cm.bwr)\n", "cbar3 = fig3.colorbar(cax3)\n", "ax3.set_title( 'CESM: Prescribed heat flux out of ocean (W m$^{-2}$), annual mean', \n", " fontsize=14 )\n", "ax3.set_xlabel('Longitude', fontsize=14)\n", "ax3.set_ylabel('Latitude', fontsize=14)\n", "ax3.text(65, 50, 'Annual', fontsize=16 )\n", "ax3.contour(topo.lon, topo.lat, topo.LANDFRAC, levels=[0.5], colors='k');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice all the spatial structure here: \n", "\n", "- Lots of heat is going in to the oceans at the equator, particularly in the eastern Pacific Ocean.\n", "- The red hot spots show where lots of heat is coming out of the ocean.\n", "- Hot spots include the mid-latitudes off the eastern coasts of Asia and North America\n", "- And also the northern North Atlantic. \n", "\n", "**All this structure is determined by ocean circulation, which we are not modeling here.** Instead, we are prescribing these heat flux patterns as an input to the atmosphere.\n", "\n", "This pattern changes throughout the year. Recall that we just averaged over all months to make this plot. We might want to look at just one month:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# select by month index (0 through 11)\n", "som_input.qdp.isel(time=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# select by array slicing (but for this you have to know the axis order!)\n", "som_input.qdp[0,:,:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we got just the first month (January) by specifying `[0,:,:]` after the variable name. This is called *slicing* or *indexing* an array. We are saying \"give me everything for month number 0\". Now make the plot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig4, ax4 = plt.subplots(figsize=(10,4)) \n", "cax4 = ax4.contourf( som_input.xc, som_input.yc, \n", " -som_input.qdp.isel(time=0), \n", " levels=lev, cmap=plt.cm.bwr)\n", "cbar4 = plt.colorbar(cax4)\n", "ax4.set_title( 'CESM: Prescribed heat flux out of ocean (W m$^{-2}$)', \n", " fontsize=14 )\n", "ax3.set_xlabel('Longitude', fontsize=14)\n", "ax3.set_ylabel('Latitude', fontsize=14)\n", "ax4.text(65, 50, 'January', fontsize=12 );\n", "ax4.contour(topo.lon, topo.lat, topo.LANDFRAC, levels=[0.5], colors='k');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Just for fun: some interactive plotting\n", "\n", "`IPython` provides some really neat and easy-to-use tools to set up interactive graphics in your notebook.\n", "\n", "Here we're going to create a figure with a slider that lets of step through each month of the q-flux data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A list of text labels for each month\n", "months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', \n", " 'Sep', 'Oct', 'Nov', 'Dec']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# an example of slicing this list:\n", "months[-2:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A function that takes a month index (0 - 11) and creates a plot just like above\n", "def sh(month):\n", " fig, ax = plt.subplots(figsize=(10,4)) \n", " cax = ax.contourf( som_input.xc, som_input.yc, \n", " -som_input.qdp.isel(time=month), \n", " levels=lev, cmap=plt.cm.bwr)\n", " cbar = plt.colorbar(cax)\n", " ax.set_title( 'CESM: Prescribed heat flux out of ocean (W m$^{-2}$)', \n", " fontsize=14 )\n", " ax.set_xlabel('Longitude', fontsize=14)\n", " ax.set_ylabel('Latitude', fontsize=14)\n", " ax.text(65, 50, months[month], fontsize=12 );\n", " ax.contour(topo.lon, topo.lat, topo.LANDFRAC, levels=[0.5], colors='k');" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Calling this function with a single month index gives us a single plot:\n", "sh(6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you execute the next cell, you should get a figure with a slider above it. Go ahead and play with it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact\n", "interact(sh, month=(0,11,1));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The \"pre-industrial\" control run\n", "\n", "Our control run is set up to simulate the climate of the \"pre-industrial era\", meaning before significant human-induced changes to the composition of the atmosphere, nominally the year 1850.\n", "\n", "Output from the control run is available on the same data server as above. Look in the folder called `som_1850_f19` (Here `som` stands for \"slab ocean model\", 1850 indicated pre-industrial conditions, and `f19` is a code for the horizontal grid resolution).\n", "\n", "There are climatology files for each active model component:\n", "\n", "- atmosphere, \n", "- sea ice\n", "- land surface \n", "\n", "I created these files by **averaging over the last 10 years of the simulation**. Let's take a look at the atmosphere file. The file is called\n", "\n", "`som_1850_f19.cam.h0.clim.nc`\n", "\n", "(the file extension `.nc` is used to indicate NetCDF format)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control = xr.open_dataset( datapath + 'som_1850_f19/som_1850_f19.cam.h0.clim.nc' + endstr )\n", "\n", "print(atm_control)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lots of different stuff! These are all the different quantities that are calculated as part of the model simulation. **Every quantity represents a long-term average for a particular month**. \n", "\n", "Want to get more information about a particular variable?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control.co2vmr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the (prescribed) amount of CO2 in the atmosphere (about 285 parts per million by volume).\n", "\n", "One nice thing about `xarray.DataArray` objects is that we can do simple arithmetic with them (already seen several examples of this in the notes above). For example, change the units of CO2 amount to ppm:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control.co2vmr * 1E6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's another variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control.SOLIN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apparently this is the incoming solar radiation or **insolation**, with shape (12,96,144) meaning it's got 12 months, 96 latitude points and 144 longitude points. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___________________________\n", "## Exercise\n", "\n", "**Make two well-labeled plots of the insolation:** \n", "\n", "1. The annual mean\n", "2. The (June - December) difference\n", "____________________________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparing the control run with the observed energy budget\n", "\n", "Recall that our investigations so far have been guided by this figure of the observed **annual, global mean energy budget**:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Observed global energy budget](http://www.atmos.albany.edu/facstaff/brose/classes/ENV415_Spring2018/images/GlobalEnergyBudget.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The global average\n", "\n", "In order to compare these numbers with the control run, **we need to take global averages** of the data.\n", "\n", "A global average **must be weighted by the area of each grid cell**. We cannot simply average over each data point on a latitude-longitude grid.\n", "\n", "**WHY?**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Weighting for global average\n", "\n", "The global average needs to weighted by the **cosine of latitude** (do you understand why?)\n", "\n", "We can implement this in `xarray` as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# functions available in xarray for standard mathematical operations\n", "# (but still preserving DataArray attributes and axes)\n", "from xarray.ufuncs import cos, deg2rad" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Take the cosine of latitude (first converting to radians)\n", "coslat = cos(deg2rad(atm_control.lat))\n", "# And divide by its mean value\n", "weight_factor = coslat / coslat.mean(dim='lat') " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Want to see what we just created?\n", "weight_factor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An alternative: use weights already provided in the dataset\n", "\n", "You will find that many gridded datasets already provide a field that gives accurate area weighting.\n", "\n", "In the case of the CESM output, the field is called `gw`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "weight_factor2 = atm_control.gw / atm_control.gw.mean(dim='lat')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compare our two weights\n", "print( (atm_control.FLNT * weight_factor).mean(dim=('time', 'lon', 'lat')))\n", "print( (atm_control.FLNT * weight_factor2).mean(dim=('time', 'lon', 'lat')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These numbers should be very close to each other." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___________________________\n", "## Exercise\n", "\n", "Make sure you can take a global average, testing on **surface temperature** in the control run.\n", "\n", "Surface temperature is called `'TS'` in the control run data file.\n", "\n", "**Calculate annual, global average `'TS'`**\n", "\n", "**Verify that you get something close to `289.57`**\n", "\n", "If you don't, try to find and fix the errors.\n", "____________________________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Finding the radiative fluxes in the model output\n", "\n", "Now that you have a working function to take global averages, we can compare some energy budget values against observations.\n", "\n", "The model output contains lots of diagnostics about the radiative fluxes. Here some CESM naming conventions to help you find the appropriate output fields:\n", "\n", "- All variables whose names being with `'F'` are an **energy flux** of some kind. \n", "- Most have a four-letter code, e.g. `'FLNT'`\n", "- `'FL'` means **longwave flux** (i.e. terrestrial)\n", "- `'FS'` means **shortwave flux** (i.e. solar)\n", "- The third letter indicates **direction** of the flux:\n", " - `'U'` = up\n", " - `'D'` = down\n", " - `'N'` = net\n", "- The fourth letter indicates the **location** of the flux:\n", " - `'T'` = top of atmosphere\n", " - `'S'` = surface\n", "- So `'FLNT'` means 'net longwave flux at the top of atmosphere', i.e. the outgoing longwave radiation.\n", "\n", "You wil see that these are all 12 x 96 x 144 -- i.e. a two-dimensional grid for every calendar month." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control.FLNT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__________________________\n", "## Exercise\n", "\n", "Compute annual, global averages of the following four quantities. \n", "\n", "1. Incoming solar radiation (or insolation)\n", "2. Absorbed solar radiation\n", "3. Planetary albedo\n", "4. Outgoing longwave radiation\n", "\n", "Compare your results briefly to the observations.\n", "____________________________" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A few more tidbits\n", "\n", "Feel free to keep exploring the data!\n", "\n", "Many other fields are four-dimensional (time, level, latitude, longitude). \n", "\n", "For example, here is the shape of the array that hold the **air temperature** at every point and every month:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control['T'].shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An important *gotcha* with `xarray`\n", "\n", "Normally we can access a variable with the notation `Dataset.variable_name`\n", "\n", "But in `xarray` (and also in `numpy` and other packages), the notation `object.T` actually represents the **transpose** operator:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print( atm_control.FLNT.shape)\n", "print( atm_control.FLNT.T.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here there is a name conflict because the air temperature variable is called `T`. One solution is to access it through the dictionary method, as I did above:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control['T']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another solution is just to rename the variable (here going from `T` to `Ta`):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "atm_control.rename({'T': 'Ta'}, inplace=True)\n", "atm_control.Ta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here is some code to plot the average sounding (temperature as function of pressure) at a particular point in the month of January." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot( atm_control.Ta[0,:,70,115], atm_control.lev )\n", "plt.gca().invert_yaxis()\n", "plt.ylabel('Pressure (hPa)')\n", "plt.xlabel('Temperature (K)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What was the location we just used for that plot? Let's check by indexing the latitude and longitude arrays:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print( atm_control.lat[70].values)\n", "print( atm_control.lon[115].values)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are actually the coordinates of the Albany area (read longitude in degrees east)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### So go ahead and mess around with the model output and see what you can do with it. And have fun.\n", "\n", "Thanks for playing!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }