{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Deriving Exner Pressure and Air Temperature\n\nThis example shows some processing of cubes in order to derive further related\ncubes; in this case the derived cubes are Exner pressure and air temperature\nwhich are calculated by combining air pressure, air potential temperature and\nspecific humidity. Finally, the two new cubes are presented side-by-side in a\nplot.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport matplotlib.ticker\n\nimport iris\nimport iris.coords as coords\nimport iris.iterate\nimport iris.plot as iplt\nimport iris.quickplot as qplt\n\n\ndef limit_colorbar_ticks(contour_object):\n    \"\"\"\n    Takes a contour object which has an associated colorbar and limits the\n    number of ticks on the colorbar to 4.\n\n    \"\"\"\n    colorbar = contour_object.colorbar\n    colorbar.locator = matplotlib.ticker.MaxNLocator(4)\n    colorbar.update_ticks()\n\n\ndef main():\n    fname = iris.sample_data_path(\"colpex.pp\")\n\n    # The list of phenomena of interest\n    phenomena = [\"air_potential_temperature\", \"air_pressure\"]\n\n    # Define the constraint on standard name and model level\n    constraints = [\n        iris.Constraint(phenom, model_level_number=1) for phenom in phenomena\n    ]\n\n    air_potential_temperature, air_pressure = iris.load_cubes(\n        fname, constraints\n    )\n\n    # Define a coordinate which represents 1000 hPa\n    p0 = coords.AuxCoord(1000, long_name=\"P0\", units=\"hPa\")\n    # Convert reference pressure 'p0' into the same units as 'air_pressure'\n    p0.convert_units(air_pressure.units)\n\n    # Calculate Exner pressure\n    exner_pressure = (air_pressure / p0) ** (287.05 / 1005.0)\n    # Set the name (the unit is scalar)\n    exner_pressure.rename(\"exner_pressure\")\n\n    # Calculate air_temp\n    air_temperature = exner_pressure * air_potential_temperature\n    # Set the name (the unit is K)\n    air_temperature.rename(\"air_temperature\")\n\n    # Now create an iterator which will give us lat lon slices of\n    # exner pressure and air temperature in the form\n    # (exner_slice, air_temp_slice).\n    lat_lon_slice_pairs = iris.iterate.izip(\n        exner_pressure,\n        air_temperature,\n        coords=[\"grid_latitude\", \"grid_longitude\"],\n    )\n\n    # For the purposes of this example, we only want to demonstrate the first\n    # plot.\n    lat_lon_slice_pairs = [next(lat_lon_slice_pairs)]\n\n    plt.figure(figsize=(8, 4))\n    for exner_slice, air_temp_slice in lat_lon_slice_pairs:\n        plt.subplot(121)\n        cont = qplt.contourf(exner_slice)\n\n        # The default colorbar has a few too many ticks on it, causing text to\n        # overlap. Therefore, limit the number of ticks.\n        limit_colorbar_ticks(cont)\n\n        plt.subplot(122)\n        cont = qplt.contourf(air_temp_slice)\n        limit_colorbar_ticks(cont)\n        iplt.show()\n\n\nif __name__ == \"__main__\":\n    main()"
      ]
    }
  ],
  "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.10.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}