Commit 4e1f1975 authored by Fabian Wachsmann's avatar Fabian Wachsmann
Browse files

Delete use-case_analyse-time-series_xarray_cmip6.ipynb

parent daaa1fcf
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot the time series of a simulated variable from CMIP6 data for 1850-2100\n",
"We will plot the __anomaly__ of a variable with respect to a reference period __1851-1880__ from the historical simulation.\n",
"\n",
"For understanding the code we recoomend to be familiar with\n",
"- intake-esm <br>\n",
"check the notebook `tutorial_find-data_catalogs_intake-esm.ipynb`\n",
"- pandas\n",
"- xarray\n",
"\n",
"We use three different experiments from the CMIP6 data pool:\n",
"- historical <br>\n",
"simulation of the time span 1850-2015\n",
"- ssp245\n",
"- ssp585 <br>\n",
"projections for the future 2015-2100\n",
"\n",
"We focus on the German Earth System Models (ESM)\n",
"- MPI-ESM1-2-HR <br>\n",
"Mainly developed by Max-Planck-Institut\n",
"- AWI-CM-1-1-MR <br>\n",
"Mainly developed by Alfred-Wegener-Institut\n",
"\n",
"See also:\n",
"https://www.dkrz.de/projekte-und-partner/HLRE-Projekte/focus/neues-klimamodell\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import intake\n",
"import pandas as pd\n",
"import hvplot.pandas\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Choose one of\n",
"# pr, psl, tas, tasmax, tasmin, clt\n",
"variable_id = \"tas\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Finding data\n",
"We open the CMIP6 catalog with intake"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"col_url = \"https://swift.dkrz.de/v1/dkrz_a44962e3ba914c309a7421573a6949a6/intake-esm/mistral-cmip6.json\"\n",
"col = intake.open_esm_datastore(col_url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Subsetting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define a query and specify values for some columns.\n",
"In the following case, we look for the specified variable in monthly resolution for the 3 different experiments.\n",
"\n",
"_Search for the ESMs by `source_id` and not for its corresponding institutions by `institution_id` because some experiments may be conducted by other institutions._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"query = dict(\n",
" variable_id=variable_id,\n",
" table_id=\"Amon\",\n",
" experiment_id=[\"historical\", \"ssp245\", \"ssp585\"],\n",
" source_id=[\"MPI-ESM1-2-HR\", \"AWI-CM-1-1-MR\"],\n",
")\n",
"# piControl = pre-industrial control, simulation to represent a stable climate from 1850 for >100 years.\n",
"# historical = historical Simulation, 1850-2014\n",
"# ssp370 = Shared Socioeconomic Pathways (SSPs) are scenarios of projected socioeconomic global changes. Simulation covers 2015-2100\n",
"cat = col.search(**query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accessing the data\n",
"\n",
"The catalog can give us a dictionary of xarray datasets. They are aggregated over time and member. The keys of the dictionary are concatenated directory names."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xrdsetdict = cat.to_dataset_dict()\n",
"'''Keys of the dataset dictionary are concatinations of 6 column labels:'''\n",
"for key in xrdsetdict.keys():\n",
" print(key)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Post-process the data\n",
"\n",
"We define a function to calculate the __global and yearly mean__ of the variable. <br>\n",
"We assume that the grid is _rectangular_ (Not valid for other sources). That implies that grid cell area is proportional to the cosinus of the latitude and we can use these cosinuses as the _weights_ for the global mean.\n",
"\n",
"With these weights we can calculate a global mean.\n",
"The yearly mean is calculated with a groupby."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def global_yearly_mean(dfirst):\n",
" # Get weights\n",
" weights = np.cos(np.deg2rad(dfirst.lat))\n",
" # Weighted Variable\n",
" varset = dfirst.get(variable_id)\n",
" wvar = varset.weighted(weights)\n",
" # Tas global mean:\n",
" wvargm = wvar.mean((\"lon\", \"lat\"))\n",
" # Tas yearly mean:\n",
" wvargmym = wvargm.groupby(\"time.year\").mean(\"time\")\n",
" return wvargmym.values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We calculate the historical reference of the variable from the years 1851-1880 in _one_ historical simulation of _one_ ESM.\n",
"With that we can afterwards calculate anomalies."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"historical = [key for key in xrdsetdict.keys() if \"historical\" in key][0]\n",
"dshist = xrdsetdict[historical]\n",
"tashist = dshist.sel(time=dshist.time.dt.year.isin(range(1851, 1881)))\n",
"# 10member\n",
"tashistgmym = global_yearly_mean(tashist)\n",
"tashistgmymm = tashistgmym.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We retrieve attributes from the variable to label axes in the plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lname = dshist.get(variable_id).attrs[\"long_name\"]\n",
"units = dshist.get(variable_id).attrs[\"units\"]\n",
"label = \"Delta \" + lname + \"[\" + units + \"]\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot is generated by a function on top of a __pandas dataframe__. \n",
"\n",
"We define one and write the results of each global yearly mean into that dataframe. We use parts of the keys of the dataset dictionary as column names of the dataframe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lxr = list(xrdsetdict.keys())\n",
"columns = [\".\".join(elem.split(\".\")[1:4]) for elem in lxr]\n",
"print(columns)\n",
"tasgmympd = pd.DataFrame(index=range(1850, 2101), columns=columns)\n",
"for key in xrdsetdict.keys():\n",
" print([\".\".join(key.split(\".\")[1:4])])\n",
" datatoappend = global_yearly_mean(xrdsetdict[key])[0, :] - tashistgmymm\n",
" years = list(xrdsetdict[key].get(variable_id).groupby(\"time.year\").groups.keys())\n",
" tasgmympd.loc[years, \".\".join(key.split(\".\")[1:4])] = datatoappend"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tasgmympd.hvplot.line(\n",
" xlabel=\"Year\",\n",
" ylabel=label,\n",
" value_label=label,\n",
" legend=\"top_left\",\n",
" ncol=3,\n",
" title=\"Global and yearly mean anomaly in comparison with\" \"1851-1880 \",\n",
" grid=True,\n",
" height=600,\n",
" width=820,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:root] *",
"language": "python",
"name": "conda-root-py"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment