{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute DCSC's TXND: the number of unusually hot days\n", "\n", "Example notebook that runs icclim.\n", "\n", "The example calculates the number of unusually hot days (TXND indicator from DCSC) for the dataset chosen by the user on C4I.\n", "\n", "We assume to have the **tasmax** variable in netCDF files in a `./data` folder for model `CMCC` and for one member `r1i1p1f1`. \n", "The data can be dowloaded using the [metalink](data/cmcc_tas.metalink) provided with this notebook. \n", "The data described in a `.metalink` file can be dowloaded with tools such as [aria2](https://aria2.github.io/) or a browser plugin such as [DownThemAll!](https://addons.mozilla.org/en-US/firefox/addon/downthemall/)\n", "If you wish to use a different dataset, you can use the [climate 4 impact portal](https://www.climate4impact.eu/c4i-frontend/) to search and select the data you wish to use and a metalink file to the [ESGF](https://esgf.llnl.gov/) data will be provided.\n", "\n", "The data is read using xarray and a plot of the time series over a specific region is generated, as well as an average spatial map. Several output types examples are shown.\n", "\n", "To keep this example fast to run, the following period is considered: 2015-01-01 to 2019-12-31, and plots are shown over European region." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Installation and preparation of the needed modules" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python: 3.11.7 | packaged by conda-forge | (main, Dec 15 2023, 08:38:37) [GCC 12.3.0]\n", "numpy: 1.26.4\n", "xarray: 2024.2.0\n", "pandas: 2.2.1\n", "icclim: 7.0.0\n", "cftime: 1.6.3\n", "xclim: 0.48.0\n" ] } ], "source": [ "import datetime\n", "import sys\n", "from pathlib import Path\n", "\n", "import cartopy.crs as ccrs\n", "import cftime\n", "import icclim\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "import xclim\n", "from xclim.core.calendar import select_time \n", "from icclim.frequency import FrequencyRegistry\n", "\n", "print(\"python: \", sys.version)\n", "print(\"numpy: \", np.__version__)\n", "print(\"xarray: \", xr.__version__)\n", "print(\"pandas: \", pd.__version__)\n", "print(\"icclim: \", icclim.__version__)\n", "print(\"cftime: \", cftime.__version__)\n", "print(\"xclim: \", xclim.__version__)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specification of the parameters" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DATA_DIR = Path(\"./data\")\n", "out_f = \"txnd_icclim.nc\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18500101-18741231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_18750101-18991231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_19000101-19241231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_19250101-19491231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_19500101-19741231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_19750101-19991231.nc',\n", " 'data/tas_day_CMCC-ESM2_historical_r1i1p1f1_gn_20000101-20141231.nc']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "historical_files = [str(f) for f in DATA_DIR.glob(\"tas*CMCC*historical*.nc\")]\n", "sorted(historical_files)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['data/tas_day_CMCC-ESM2_ssp585_r1i1p1f1_gn_20150101-20391231.nc',\n", " 'data/tas_day_CMCC-ESM2_ssp585_r1i1p1f1_gn_20400101-20641231.nc',\n", " 'data/tas_day_CMCC-ESM2_ssp585_r1i1p1f1_gn_20650101-20891231.nc',\n", " 'data/tas_day_CMCC-ESM2_ssp585_r1i1p1f1_gn_20900101-21001231.nc']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "studied_files = [str(f) for f in DATA_DIR.glob(\"tas*CMCC*ssp585*.nc\")]\n", "sorted(studied_files)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Build NormaL\n", "\n", "We build the summer `normal`, from April to September included. \n", "This normat DataArray will have one value per `lat, lon` couple, the values will be the mean of temperature of the summers within the reference periode. \n", "We will make use of xclim's `select_time` to filter the summer months.\n", "\n", "> ℹ️ Alternatively, the normal can be saved in a netCDF file and the path to this file can be used in `normal` parameter of `icclim.dcsc.txnd` function." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'tas' (lat: 192, lon: 288)> Size: 221kB\n", "dask.array<mean_agg-aggregate, shape=(192, 288), dtype=float32, chunksize=(192, 288), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * lat (lat) float64 2kB -90.0 -89.06 -88.12 -87.17 ... 88.12 89.06 90.0\n", " * lon (lon) float64 2kB 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n", " height float64 8B 2.0\n", "Attributes:\n", " standard_name: air_temperature\n", " long_name: Near-Surface Air Temperature\n", " comment: near-surface (usually, 2 meter) air temperature\n", " units: K\n", " original_name: TREFHT\n", " cell_methods: area: time: mean\n", " cell_measures: area: areacella\n", " history: 2020-12-21T16:22:42Z altered by CMOR: Treated scalar dime...
<xarray.Dataset> Size: 5MB\n", "Dimensions: (lat: 192, lon: 288, time: 11, bounds: 2)\n", "Coordinates:\n", " * lat (lat) float64 2kB -90.0 -89.06 -88.12 ... 88.12 89.06 90.0\n", " * lon (lon) float64 2kB 0.0 1.25 2.5 3.75 ... 355.0 356.2 357.5 358.8\n", " height float64 8B 2.0\n", " * time (time) object 88B 2090-06-16 00:00:00 ... 2100-06-16 00:00:00\n", " * bounds (bounds) int64 16B 0 1\n", "Data variables:\n", " TXND (time, lat, lon) float64 5MB dask.array<chunksize=(1, 192, 288), meta=np.ndarray>\n", " time_bounds (time, bounds) object 176B 2090-04-01 00:00:00 ... 2100-08-3...\n", "Attributes:\n", " title: number_of_days_when_maximum_air_temperature_is_greater_than...\n", " references: Portail DRIAS, DCSC, MeteoFrance\n", " institution: Climate impact portal (https://climate4impact.eu)\n", " history: 2021-01-18T14:11:26Z altered by CMOR: Treated scalar dimens...\n", " source: \n", " Conventions: CF-1.6
<xarray.Dataset> Size: 5MB\n", "Dimensions: (lat: 192, lon: 288, time: 11, bounds: 2)\n", "Coordinates:\n", " * lat (lat) float64 2kB -90.0 -89.06 -88.12 ... 88.12 89.06 90.0\n", " * lon (lon) float64 2kB 0.0 1.25 2.5 3.75 ... 355.0 356.2 357.5 358.8\n", " height float64 8B ...\n", " * time (time) object 88B 2090-06-16 00:00:00 ... 2100-06-16 00:00:00\n", " * bounds (bounds) int64 16B 0 1\n", "Data variables:\n", " TXND (time, lat, lon) float64 5MB ...\n", " time_bounds (time, bounds) object 176B ...\n", "Attributes:\n", " title: number_of_days_when_maximum_air_temperature_is_greater_than...\n", " references: Portail DRIAS, DCSC, MeteoFrance\n", " institution: Climate impact portal (https://climate4impact.eu)\n", " history: 2021-01-18T14:11:26Z altered by CMOR: Treated scalar dimens...\n", " source: \n", " Conventions: CF-1.6
<xarray.DataArray 'TXND' (time: 11, lat: 192, lon: 288)> Size: 5MB\n", "[608256 values with dtype=float64]\n", "Coordinates:\n", " * lat (lat) float64 2kB -90.0 -89.06 -88.12 -87.17 ... 88.12 89.06 90.0\n", " * lon (lon) float64 2kB 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n", " height float64 8B ...\n", " * time (time) object 88B 2090-06-16 00:00:00 ... 2100-06-16 00:00:00\n", "Attributes:\n", " standard_name: number_of_days_when_maximum_air_temperature_is_greater_th...\n", " long_name: Number of days when maximum air temperature is greater th...\n", " comment: near-surface (usually, 2 meter) air temperature\n", " units: d\n", " original_name: TREFHT\n", " cell_methods: time: sum over days\n", " cell_measures: area: areacella\n", " history:
\n", " | lat | \n", "lon | \n", "height | \n", "TXND | \n", "
---|---|---|---|---|
time | \n", "\n", " | \n", " | \n", " | \n", " |
2090-06-16 00:00:00 | \n", "43.82199 | \n", "3.75 | \n", "2.0 | \n", "118.0 | \n", "
2091-06-16 00:00:00 | \n", "43.82199 | \n", "3.75 | \n", "2.0 | \n", "88.0 | \n", "
2092-06-16 00:00:00 | \n", "43.82199 | \n", "3.75 | \n", "2.0 | \n", "84.0 | \n", "
2093-06-16 00:00:00 | \n", "43.82199 | \n", "3.75 | \n", "2.0 | \n", "103.0 | \n", "
2094-06-16 00:00:00 | \n", "43.82199 | \n", "3.75 | \n", "2.0 | \n", "96.0 | \n", "