{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Uncertainty partitioning\n", "\n", "Here we estimate the sources of uncertainty for an ensemble of climate model projections. The data is the same as used in the [IPCC WGI AR6 Atlas](https://github.com/IPCC-WG1/Atlas).\n", "\n", "## Fetch data\n", "We'll only fetch a small sample of the full ensemble to illustrate the logic and data structure expected by the partitioning algorithm." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "from __future__ import annotations\n", "\n", "import matplotlib as mpl\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "from matplotlib import pyplot as plt\n", "\n", "import xclim.ensembles\n", "\n", "# The directory in the Atlas repo where the data is stored\n", "# host = \"https://github.com/IPCC-WG1/Atlas/raw/main/datasets-aggregated-regionally/data/CMIP6/CMIP6_tas_land/\"\n", "host = \"https://raw.githubusercontent.com/IPCC-WG1/Atlas/main/datasets-aggregated-regionally/data/CMIP6/CMIP6_tas_land/\"\n", "\n", "# The file pattern, e.g. CMIP6_ACCESS-CM2_ssp245_r1i1p1f1.csv\n", "pat = \"CMIP6_{model}_{scenario}_{member}.csv\"" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# Here we'll download data only for a very small demo sample of models and scenarios.\n", "\n", "# Download data for a few models and scenarios.\n", "models = [\"ACCESS-CM2\", \"CMCC-CM2-SR5\", \"CanESM5\"]\n", "\n", "# The variant label is not always r1i1p1f1, so we need to match it to the model.\n", "members = [\"r1i1p1f1\", \"r1i1p1f1\", \"r1i1p1f1\"]\n", "\n", "# GHG concentrations and land-use scenarios\n", "scenarios = [\"ssp245\", \"ssp370\", \"ssp585\"]\n", "\n", "data = []\n", "for model, member in zip(models, members, strict=False):\n", " for scenario in scenarios:\n", " url = host + pat.format(model=model, scenario=scenario, member=member)\n", "\n", " # Fetch data using pandas\n", " df = pd.read_csv(url, index_col=0, comment=\"#\", parse_dates=True)[\"world\"]\n", " # Convert to a DataArray, complete with coordinates.\n", " da = xr.DataArray(df).expand_dims(model=[model], scenario=[scenario]).rename(date=\"time\")\n", " data.append(da)" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Create an ensemble\n", "\n", "Here we combine the different models and scenarios into a single DataArray with dimensions `model` and `scenario`. Note that the names of those dimensions are important for the uncertainty partitioning algorithm to work.\n", "\n", "