{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"[](https://colab.research.google.com/github/ClimateMatchAcademy/course-content/blob/main/tutorials/W2D1_FutureClimate-IPCCIPhysicalBasis/student/W2D1_Tutorial2.ipynb)
"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Tutorial 2: Time Series, Global Averages, and Scenario Comparison\n",
"\n",
"**Week 2, Day 1, Future Climate: The Physical Basis**\n",
"\n",
"**Content creators:** Brodie Pearson, Julius Busecke, Tom Nicholas\n",
"\n",
"**Content reviewers:** Younkap Nina Duplex, Zahra Khodakaramimaghsoud, Sloane Garelick, Peter Ohue, Jenna Pearson, Derick Temfack, Peizhen Yang, Cheng Zhang, Chi Zhang, Ohad Zivan\n",
"\n",
"**Content editors:** Jenna Pearson, Ohad Zivan, Chi Zhang\n",
"\n",
"**Production editors:** Wesley Banfield, Jenna Pearson, Chi Zhang, Ohad Zivan\n",
"\n",
"**Our 2023 Sponsors:** NASA TOPS, Google DeepMind, and CMIP"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Tutorial Objectives\n",
"In this tutorial, we will expand to look at data from **three** CMIP6/ScenarioMIP experiments (*historical*, *SSP1-2.6* and *SSP5-8.5*). Our aim will be to calculate the global mean SST for these 3 experiments, taking into account the spatially-varying size of the model's grid cells (i.e., calculating a *weighted* mean).\n",
"\n",
"By the end of this tutorial, you'll be able to:\n",
"\n",
"- Load and analyze CMIP6 SST data from different experiments.\n",
"- Understand the difference between historical and future emission scenarios.\n",
"- Calculate the global mean SST from gridded model data.\n",
"- Apply the concept of weighted mean to account for varying grid cell sizes in Earth System Models."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Setup\n",
"\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {},
"executionInfo": {
"elapsed": 90386,
"status": "ok",
"timestamp": 1683927956198,
"user": {
"displayName": "Brodie Pearson",
"userId": "05269028596972519847"
},
"user_tz": 420
},
"tags": [
"colab"
]
},
"outputs": [],
"source": [
"# installations ( uncomment and run this cell ONLY when using google colab or kaggle )\n",
"\n",
"# !pip install condacolab &> /dev/null\n",
"# import condacolab\n",
"# condacolab.install()\n",
"\n",
"# # Install all packages in one call (+ use mamba instead of conda), this must in one line or code will fail\n",
"# !mamba install xarray-datatree intake-esm gcsfs xmip aiohttp nc-time-axis cf_xarray xmip xarrayutils &> /dev/null"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {},
"tags": []
},
"outputs": [],
"source": [
"# imports\n",
"import time\n",
"tic = time.time()\n",
"\n",
"import intake\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import xarray as xr\n",
"\n",
"from xmip.preprocessing import combined_preprocessing\n",
"from xarrayutils.plotting import shaded_line_plot\n",
"\n",
"from datatree import DataTree\n",
"from xmip.postprocessing import _parse_metric"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": []
},
"outputs": [],
"source": [
"# @title Figure settings\n",
"import ipywidgets as widgets # interactive display\n",
"\n",
"plt.style.use(\n",
" \"https://raw.githubusercontent.com/ClimateMatchAcademy/course-content/main/cma.mplstyle\"\n",
")\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": []
},
"outputs": [],
"source": [
"# @title Video 1: Future Climate Scenarios\n",
"\n",
"from ipywidgets import widgets\n",
"from IPython.display import YouTubeVideo\n",
"from IPython.display import IFrame\n",
"from IPython.display import display\n",
"\n",
"\n",
"class PlayVideo(IFrame):\n",
" def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n",
" self.id = id\n",
" if source == 'Bilibili':\n",
" src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n",
" elif source == 'Osf':\n",
" src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n",
" super(PlayVideo, self).__init__(src, width, height, **kwargs)\n",
"\n",
"\n",
"def display_videos(video_ids, W=400, H=300, fs=1):\n",
" tab_contents = []\n",
" for i, video_id in enumerate(video_ids):\n",
" out = widgets.Output()\n",
" with out:\n",
" if video_ids[i][0] == 'Youtube':\n",
" video = YouTubeVideo(id=video_ids[i][1], width=W,\n",
" height=H, fs=fs, rel=0)\n",
" print(f'Video available at https://youtube.com/watch?v={video.id}')\n",
" else:\n",
" video = PlayVideo(id=video_ids[i][1], source=video_ids[i][0], width=W,\n",
" height=H, fs=fs, autoplay=False)\n",
" if video_ids[i][0] == 'Bilibili':\n",
" print(f'Video available at https://www.bilibili.com/video/{video.id}')\n",
" elif video_ids[i][0] == 'Osf':\n",
" print(f'Video available at https://osf.io/{video.id}')\n",
" display(video)\n",
" tab_contents.append(out)\n",
" return tab_contents\n",
"\n",
"\n",
"video_ids = [('Youtube', 'XJppKGY0w0I'), ('Bilibili', 'BV1Jk4y1P7xf')]\n",
"tab_contents = display_videos(video_ids, W=730, H=410)\n",
"tabs = widgets.Tab()\n",
"tabs.children = tab_contents\n",
"for i in range(len(tab_contents)):\n",
" tabs.set_title(i, video_ids[i][0])\n",
"display(tabs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"pycharm": {
"name": "#%%\n"
},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# @markdown\n",
"from ipywidgets import widgets\n",
"from IPython.display import IFrame\n",
"\n",
"link_id = \"m4pd9\"\n",
"\n",
"download_link = f\"https://osf.io/download/{link_id}/\"\n",
"render_link = f\"https://mfr.ca-1.osf.io/render?url=https://osf.io/{link_id}/?direct%26mode=render%26action=download%26mode=render\"\n",
"# @markdown\n",
"out = widgets.Output()\n",
"with out:\n",
" print(f\"If you want to download the slides: {download_link}\")\n",
" display(IFrame(src=f\"{render_link}\", width=730, height=410))\n",
"display(out)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Section 1: Load CMIP6 SST Data from Several Experiments Using `xarray`\n",
"\n",
"In the last tutorial we loaded data from the *SSP5-8.5* (high-emissions projection) experiment of one CMIP6 model called *TaiESM1*. \n",
"\n",
"Let's expand on this by using data from **three** experiments \n",
"* *historical*: a simulation of 1850-2015 using observed forcing, \n",
"* *SSP1-2.6*: a future, low-emissions scenario, and \n",
"* *SSP5-8.5*: a future, high-emissions scenario.\n",
"\n",
"Due to the uncertainty in how future emissions of greenhouse gases and aerosols will change, it is useful to use a range of different emission scenarios as inputs, or forcings to climate models. These scenarios (SSPs) are datasets which are based on different socio-economic conditions in the future, such as population growth, energy consumption, energy sources, and climate policies. [Learn more about forcing scenarios here](https://climate-scenarios.canada.ca/?page=forcing-scenarios), and [learn more about the CMIP6 scenarios here](https://climate-scenarios.canada.ca/?page=cmip6-overview-notes).\n",
"\n",
"To learn more about CMIP, including the different experiments/scenarios, please see our [CMIP Resource Bank](https://github.com/ClimateMatchAcademy/course-content/blob/main/tutorials/CMIP/CMIP_resource_bank.md) and the [CMIP website](https://wcrp-cmip.org/).\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {},
"executionInfo": {
"elapsed": 12915,
"status": "ok",
"timestamp": 1683927997892,
"user": {
"displayName": "Brodie Pearson",
"userId": "05269028596972519847"
},
"user_tz": 420
},
"tags": []
},
"outputs": [],
"source": [
"# open an intake catalog containing the Pangeo CMIP cloud data\n",
"col = intake.open_esm_datastore(\n",
" \"https://storage.googleapis.com/cmip6/pangeo-cmip6.json\"\n",
")\n",
"\n",
"# pick the experiments you require\n",
"experiment_ids = [\"historical\", \"ssp126\", \"ssp585\"]\n",
"\n",
"# from the full `col` object, create a subset using facet search\n",
"cat = col.search(\n",
" source_id=\"TaiESM1\",\n",
" variable_id=\"tos\",\n",
" member_id=\"r1i1p1f1\",\n",
" table_id=\"Omon\",\n",
" grid_label=\"gn\",\n",
" experiment_id=experiment_ids,\n",
" require_all_on=[\n",
" \"source_id\"\n",
" ], # make sure that we only get models which have all of the above experiments\n",
")\n",
"\n",
"# convert the sub-catalog into a datatree object, by opening each dataset into an xarray.Dataset (without loading the data)\n",
"kwargs = dict(\n",
" preprocess=combined_preprocessing, # apply xMIP fixes to each dataset\n",
" xarray_open_kwargs=dict(\n",
" use_cftime=True\n",
" ), # ensure all datasets use the same time index\n",
" storage_options={\n",
" \"token\": \"anon\"\n",
" }, # anonymous/public authentication to google cloud storage\n",
")\n",
"\n",
"cat.esmcat.aggregation_control.groupby_attrs = [\"source_id\", \"experiment_id\"]\n",
"dt = cat.to_datatree(**kwargs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Coding Exercise 1.1\n",
"\n",
"In this tutorial and the following tutorials we will be looking at the global mean sea surface temperature. To calculate this global mean, we need to know the horizontal area of every ocean grid cell in all the models we are using. \n",
"\n",
"Write code to load this ocean-grid area data using the previously shown method for SST data, noting that:\n",
"* We now need a variable called *areacello* (area of cells in the ocean) \n",
"* This variable is stored in table_id *Ofx* (it is from the ocean model and is fixed/constant in time) \n",
"* A model's grid does not change between experiments so you only need to get grid data from the *historical* experiment for each model\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {},
"executionInfo": {
"elapsed": 313,
"status": "error",
"timestamp": 1683928000559,
"user": {
"displayName": "Brodie Pearson",
"userId": "05269028596972519847"
},
"user_tz": 420
}
},
"outputs": [],
"source": [
"cat_area = col.search(\n",
" source_id=\"TaiESM1\",\n",
" # Add the appropriate variable_id\n",
" variable_id=...,\n",
" member_id=\"r1i1p1f1\",\n",
" # Add the appropriate table_id\n",
" table_id=...,\n",
" grid_label=\"gn\",\n",
" # Add the appropriate experiment_id\n",
" experiment_id=[...],\n",
" require_all_on=[\"source_id\"],\n",
")\n",
"cat_area.esmcat.aggregation_control.groupby_attrs = [\"source_id\", \"experiment_id\"]\n",
"dt_area = cat_area.to_datatree(**kwargs)\n",
"\n",
"dt_with_area = DataTree()\n",
"\n",
"for model, subtree in dt.items():\n",
" metric = dt_area[model][\"historical\"].ds[\"areacello\"]\n",
" dt_with_area[model] = subtree.map_over_subtree(_parse_metric, metric)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Section 2: Global Mean Sea Surface Temperature (GMSST)\n",
"\n",
"The data files above contain spatial maps of the sea surface temperature for every month of each experiment's time period. For the rest of today's tutorials, we're going to focus on the global mean sea surface temperature, rather than maps, as a way to visualize the ocean's changing temperature at a global scale$^*$.\n",
"\n",
"The global mean of a property can be calculated by integrating that variable over the surface area of Earth covered by the system (ocean, atmosphere etc.) and dividing by the total surface area of that system. For Sea Surface Temperature, $SST(x,y)$, the global mean ($GMSST$) can be written as an integral over the surface of the ocean ($S_{ocean}$):\n",
"\n",
"\\begin{equation}\n",
"GMSST = \\frac{\\iint_{S_{ocean}}SST(x,y) dxdy}{\\iint_{S_{ocean}} dxdy}\n",
"\\end{equation}\n",
"\n",
"where $x$ and $y$ are horizontal coordinates (i.e. longitude and latitude). This formulation works if $SST(x,y)$ is a [spatially-continuous function](https://en.wikipedia.org/wiki/Continuous_or_discrete_variable), but in a global model we only know the SST of *discrete* grid cells rather than a *continuous* SST field. Integrals are only defined for continuous variables, we must instead use a summation over the grid cells (summation is the discrete equivalent of integration):\n",
"\n",
"\\begin{equation}\n",
"GMSST = \\frac{ \\sum_{i,j} SST(i,j) A(i,j)}{\\sum_{i,j} A(i,j)}\n",
"\\end{equation}\n",
"\n",
"where $(i,j)$ represent the indices of the 2D spatial SST data from a CMIP6 model, and $A$ denotes the area of each ocean grid cell, which can vary between cells/locations, as you saw in the last tutorial where *TaiESM1* had irregularly-gridded output. This calculation is essentially a *weighted mean* of the SST across the model cells, where the weighting accounts for the varying area of cells - that is, larger cells should contribute more the global mean than smaller cells.\n",
"\n",
"$^*$*Note: we could alternatively look at ocean heat content, which depends on temperature at all depths, but it is a more intensive computation that would take too long to calculate in these tutorials.*"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Coding Exercise 2.1\n",
"\n",
"Complete the following code so that it calculates and plots a timeseries of global mean sea surface temperature from the *TaiESM1* model for both the *historical* experiment and the two future projection experiments, *SSP1-2.6* (low emissions) and *SSP5-8.5* (high emissions). \n",
"\n",
"As you complete this exercise this, consider the following questions:\n",
"* In the first function, what `xarray` operation is the following line doing, and why is it neccessary?\n",
"```\n",
"return ds.weighted(ds.areacello.fillna(0)).mean(['x', 'y'], keep_attrs=True)\n",
"```\n",
"* How would your time series plot might change if you instead used took a simple mean of all the sea surface temperatures across all grid cells? (Perhaps your previous maps could provide some help here) \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {},
"executionInfo": {
"elapsed": 296,
"status": "error",
"timestamp": 1683928013844,
"user": {
"displayName": "Brodie Pearson",
"userId": "05269028596972519847"
},
"user_tz": 420
},
"id": "oFpi2G1j2ghU",
"outputId": "d20c571c-982f-4120-b063-4b023c05771e"
},
"outputs": [],
"source": [
"def global_mean(ds: xr.Dataset) -> xr.Dataset:\n",
" \"\"\"Global average, weighted by the cell area\"\"\"\n",
" return ds.weighted(ds.areacello.fillna(0)).mean([\"x\", \"y\"], keep_attrs=True)\n",
"\n",
"\n",
"# average every dataset in the tree globally\n",
"dt_gm = ...\n",
"fig, ax = plt.subplots()\n",
"for experiment in [\"historical\", \"ssp126\", \"ssp585\"]:\n",
" da = ...\n",
" ...\n",
"ax.set_title(\"Global Mean SST from TaiESM1\")\n",
"ax.set_ylabel(\"Global Mean SST [$^\\circ$C]\")\n",
"ax.set_xlabel(\"Year\")\n",
"ax.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Question 1.1: Climate Connection\n",
"\n",
"1. Is this plot what you expected? If so, explain what you expected, and why, from the historical experiment, and the SSP1-2.6 and SSP5-8.5 scenarios (see below for a potentially useful figure).\n",
"\n",
"For context, here is [Figure TS.4](https://www.ipcc.ch/report/ar6/wg1/downloads/figures/IPCC_AR6_WGI_TS_Figure_4.png) from the Technical Summary of the [IPCC Sixth Assessment Report](https://www.ipcc.ch/assessment-report/ar6/), which shows how several elements of forcing differ between experiments (including *historical* and *SSP* experiments). In the video above we saw the $CO_2$ panel of this figure:\n",
"\n",
"
\n",
"\n",
"Figure TS.4 | The climate change cause–effect chain: The intent of this figure is to illustrate the process chain starting from anthropogenic emissions, to changes in atmospheric concentration, to changes in Earth’s energy balance (‘forcing’), to changes in global climate and ultimately regional climate and climatic impact-drivers. Shown is the core set of five Shared Socio-economic Pathway (SSP) scenarios as well as emissions and concentration ranges for the previous Representative Concentration Pathway (RCP) scenarios in year 2100; carbon dioxide (CO2) emissions (GtCO2yr–1), panel top left; methane (CH4) emissions (middle) and sulphur dioxide (SO2), nitrogen oxide (NOx) emissions (all in Mt yr–1), top right; concentrations of atmospheric CO2(ppm) and CH4 (ppb), second row left and right; effective radiative forcing for both anthropogenic and natural forcings (W m–2), third row; changes in global surface air temperature (°C) relative to 1850–1900, fourth row; maps of projected temperature change (°C) (left) and changes in annual-mean precipitation (%) (right) at a global warming level (GWL) of 2°C relative to 1850–1900 (see also Figure TS.5), bottom row. Carbon cycle and non-CO2 biogeochemical feedbacks will also influence the ultimate response to anthropogenic emissions (arrows on the left). {1.6.1, Cross-Chapter Box 1.4, 4.2.2, 4.3.1, 4.6.1, 4.6.2}\n",
"\n",
"Credit: [IPCC](https://www.ipcc.ch/report/ar6/wg1/downloads/figures/IPCC_AR6_WGI_TS_Figure_4.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Summary\n",
"\n",
"In tutorial 2, you diagnosed changes at a global scale by calculating global mean timeseries with CMIP6 model mapped data. You then synthesized and compared global mean SST evolution in various CMIP6 experiments, spanning Earth's recent past and several future scenarios. \n",
"\n",
"We started by loading CMIP6 SST data from three different scenarios: *historical*, *SSP1-2.6* (low-emissions future), and *SSP5-8.5* (high-emissions future). This process expanded our understanding of model outputs. We then focused on calculating global mean SST, by taking into account the spatially-discrete and irregularly-gridded nature of this model's grid cells through a weighted mean. This weighted mean approach yielded the global mean SST, providing a holistic view of the Earth's changing sea surface temperatures under multiple future climate scenarios.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"# Resources\n",
"\n",
"This tutorial uses data from the simulations conducted as part of the [CMIP6](https://wcrp-cmip.org/) multi-model ensemble. \n",
"\n",
"For examples on how to access and analyze data, please visit the [Pangeo Cloud CMIP6 Gallery](https://gallery.pangeo.io/repos/pangeo-gallery/cmip6/index.html) \n",
"\n",
"For more information on what CMIP is and how to access the data, please see this [page](https://github.com/ClimateMatchAcademy/course-content/blob/main/tutorials/CMIP/CMIP_resource_bank.md)."
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"machine_shape": "hm",
"name": "W2D1_Tutorial_2",
"provenance": [
{
"file_id": "1WfT8oN22xywtecNriLptqi1SuGUSoIlc",
"timestamp": 1680298239014
}
],
"toc_visible": true
},
"gpuClass": "standard",
"kernel": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}