Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 31, 2023
1 parent 0e8ae8f commit b888bcf
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions docs/notebooks/partitioning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
"metadata": {},
"outputs": [],
"source": [
"from __future__ import annotations\n",
"\n",
"import matplotlib as mpl\n",
"import numpy as np\n",
"import xarray as xr\n",
"import pandas as pd\n",
"import matplotlib as mpl\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",
Expand All @@ -44,10 +47,10 @@
"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",
"# 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",
"# 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",
Expand All @@ -57,11 +60,15 @@
"for model, member in zip(models, members):\n",
" for scenario in scenarios:\n",
" url = host + pat.format(model=model, scenario=scenario, member=member)\n",
" \n",
"\n",
" # Fetch data using pandas\n",
" df = pd.read_csv(url, index_col=0, comment='#', parse_dates=True)[\"world\"]\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",
" da = (\n",
" xr.DataArray(df)\n",
" .expand_dims(model=[model], scenario=[scenario])\n",
" .rename(date=\"time\")\n",
" )\n",
" data.append(da)"
]
},
Expand Down Expand Up @@ -93,16 +100,23 @@
}
],
"source": [
"# Combine DataArrays from the different models and scenarios into one. \n",
"# Combine DataArrays from the different models and scenarios into one.\n",
"ens_mon = xr.combine_by_coords(data)[\"world\"]\n",
"\n",
"# Then resample the monthly time series at the annual frequency\n",
"ens = ens_mon.resample(time=\"Y\").mean()\n",
"ssp_col = {'ssp126': '#1d3354', 'ssp245': '#eadd3d', 'ssp370': '#f21111', 'ssp585': '#840b22'}\n",
"ssp_col = {\n",
" \"ssp126\": \"#1d3354\",\n",
" \"ssp245\": \"#eadd3d\",\n",
" \"ssp370\": \"#f21111\",\n",
" \"ssp585\": \"#840b22\",\n",
"}\n",
"plt.figure(figsize=(10, 4))\n",
"for scenario in ens.scenario:\n",
" for model in ens.model:\n",
" ens.sel(scenario=scenario, model=model).plot(color=ssp_col[str(scenario.data)], alpha=.8, lw=1)\n",
" ens.sel(scenario=scenario, model=model).plot(\n",
" color=ssp_col[str(scenario.data)], alpha=0.8, lw=1\n",
" )\n",
"plt.title(\"Projected mean global temperature\");"
]
},
Expand Down Expand Up @@ -155,9 +169,15 @@
"metadata": {},
"outputs": [],
"source": [
"colors = {\"variability\": \"#DC551A\", \"model\": \"#2B2B8B\", \"scenario\": \"#275620\", \"total\": \"k\"}\n",
"colors = {\n",
" \"variability\": \"#DC551A\",\n",
" \"model\": \"#2B2B8B\",\n",
" \"scenario\": \"#275620\",\n",
" \"total\": \"k\",\n",
"}\n",
"names = {\"variability\": \"Internal variability\"}\n",
"\n",
"\n",
"def graph_fraction_of_total_variance(variance, ref=\"2000\", ax=None):\n",
" \"\"\"Figure from Hawkins and Sutton (2009) showing fraction of total variance.\n",
" Parameters\n",
Expand Down

0 comments on commit b888bcf

Please sign in to comment.