{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Global Average Annual Temperature Plot\n\nProduces a time-series plot of North American temperature forecasts for 2\ndifferent emission scenarios. Constraining data to a limited spatial area also\nfeatures in this example.\n\nThe data used comes from the HadGEM2-AO model simulations for the A1B and E1\nscenarios, both of which were derived using the IMAGE Integrated Assessment\nModel (Johns et al. 2011; Lowe et al. 2009).\n\n## References\n\n   Johns T.C., et al. (2011) Climate change under aggressive mitigation: the\n   ENSEMBLES multi-model experiment. Climate Dynamics, Vol 37, No. 9-10,\n   doi:10.1007/s00382-011-1005-5.\n\n   Lowe J.A., C.D. Hewitt, D.P. Van Vuuren, T.C. Johns, E. Stehfest, J-F.\n   Royer, and P. van der Linden, 2009. New Study For Climate Modeling,\n   Analyses, and Scenarios. Eos Trans. AGU, Vol 90, No. 21,\n   doi:10.1029/2009EO210001.\n\n.. seealso::\n\n    Further details on the aggregation functionality being used in this example\n    can be found in `cube-statistics`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport iris\nimport iris.analysis.cartography\nimport iris.plot as iplt\nimport iris.quickplot as qplt\n\n\ndef main():\n    # Load data into three Cubes, one for each set of NetCDF files.\n    e1 = iris.load_cube(iris.sample_data_path(\"E1_north_america.nc\"))\n\n    a1b = iris.load_cube(iris.sample_data_path(\"A1B_north_america.nc\"))\n\n    # load in the global pre-industrial mean temperature, and limit the domain\n    # to the same North American region that e1 and a1b are at.\n    north_america = iris.Constraint(\n        longitude=lambda v: 225 <= v <= 315, latitude=lambda v: 15 <= v <= 60\n    )\n    pre_industrial = iris.load_cube(\n        iris.sample_data_path(\"pre-industrial.pp\"), north_america\n    )\n\n    # Generate area-weights array. As e1 and a1b are on the same grid we can\n    # do this just once and re-use. This method requires bounds on lat/lon\n    # coords, so let's add some in sensible locations using the \"guess_bounds\"\n    # method.\n    e1.coord(\"latitude\").guess_bounds()\n    e1.coord(\"longitude\").guess_bounds()\n    e1_grid_areas = iris.analysis.cartography.area_weights(e1)\n    pre_industrial.coord(\"latitude\").guess_bounds()\n    pre_industrial.coord(\"longitude\").guess_bounds()\n    pre_grid_areas = iris.analysis.cartography.area_weights(pre_industrial)\n\n    # Perform the area-weighted mean for each of the datasets using the\n    # computed grid-box areas.\n    pre_industrial_mean = pre_industrial.collapsed(\n        [\"latitude\", \"longitude\"], iris.analysis.MEAN, weights=pre_grid_areas\n    )\n    e1_mean = e1.collapsed(\n        [\"latitude\", \"longitude\"], iris.analysis.MEAN, weights=e1_grid_areas\n    )\n    a1b_mean = a1b.collapsed(\n        [\"latitude\", \"longitude\"], iris.analysis.MEAN, weights=e1_grid_areas\n    )\n\n    # Plot the datasets\n    qplt.plot(e1_mean, label=\"E1 scenario\", lw=1.5, color=\"blue\")\n    qplt.plot(a1b_mean, label=\"A1B-Image scenario\", lw=1.5, color=\"red\")\n\n    # Draw a horizontal line showing the pre-industrial mean\n    plt.axhline(\n        y=pre_industrial_mean.data,\n        color=\"gray\",\n        linestyle=\"dashed\",\n        label=\"pre-industrial\",\n        lw=1.5,\n    )\n\n    # Constrain the period 1860-1999 and extract the observed data from a1b\n    constraint = iris.Constraint(\n        time=lambda cell: 1860 <= cell.point.year <= 1999\n    )\n    observed = a1b_mean.extract(constraint)\n\n    # Assert that this data set is the same as the e1 scenario:\n    # they share data up to the 1999 cut off.\n    assert np.all(np.isclose(observed.data, e1_mean.extract(constraint).data))\n\n    # Plot the observed data\n    qplt.plot(observed, label=\"observed\", color=\"black\", lw=1.5)\n\n    # Add a legend and title\n    plt.legend(loc=\"upper left\")\n    plt.title(\"North American mean air temperature\", fontsize=18)\n\n    plt.xlabel(\"Time / year\")\n    plt.grid()\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
}