{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Usage\n", "\n", "## Climate indicator computations\n", "\n", "`xclim` is a library of climate indicators that operate on [xarray](https://docs.xarray.dev/en/stable/) objects. Indicators perform health checks on input data, converts units as needed, assign nans when input data is missing, and format outputs according to the Climate and Forecast (CF) convention. As the list of indicators has grown quite large, indicators are accessed through their *realm* (`xclim.indicators.atmos`, `xclim.indicators.land` and `xclim.indicators.seaIce`, plus the special `xclim.indicators.convert` and `xclim.indicators.generic`) to help browsing indicators by the domain they apply to. Some more indicators are in project/institution-specific collections : `xclim.indicators.cf` holds the indicators defined by [Clix-Meta](https://github.com/clix-meta/clix-meta/), `xclim.indicators.icclim` for [ECAD](https://www.ecad.eu)/[ICCLIM](https://icclim.readthedocs.io/) and `xclim.indicators.anuclim` for [Anuclim](https://fennerschool.anu.edu.au/research/products/anuclim-version-6-1). See [Climate Indicators](../indicators.rst) for a list of available indicators.\n", " \n", "**Indicators should not be confused with compute functions**, which define the algorithmic layer of each indicator. Those functions perform no checks beyond units compliance, and should be considered as low-level functions. See the respective documentation on [indicators](../indicators.rst) and [compute functions](../user_api/compute.rst#compute-functions) for more information. These \"compute functions\" were previously named \"indices\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import annotations\n", "\n", "import xarray as xr\n", "\n", "import xclim\n", "from xclim.testing import open_dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indicators are calculated by opening a NetCDF-like file, accessing the variable of interest, and calling the appropriate indicator function, which returns a new `xarray.Dataset` with the result.\n", "\n", "For this example, we'll first open a demonstration dataset storing surface air temperature and compute the number of growing degree days (the sum of degrees above a certain threshold) at the yearly frequency. The time frequency parameter, here `YS`, is specified using [pandas offset aliases](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases). Note that some offsets might not be supported for non-standard calendars (e.g. 360_day), see the [xarray.cftime_range documentation](https://docs.xarray.dev/en/stable/generated/xarray.cftime_range.html) for details." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Normally, we would use xarray to open a dataset, e.g.:\n", "# ds = xr.open_dataset(\"your_file.nc\")\n", "\n", "# For this example, let's use a test dataset from xclim:\n", "ds = open_dataset(\"ERA5/daily_surface_cancities_1990-1993.nc\")\n", "ds.tas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indicators are called as if they were functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdd = xclim.atmos.growing_degree_days(tas=ds.tas, thresh=\"10.0 degC\", freq=\"YS\")\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`growing_degree_days` makes most sense with **daily input**, but could theoretically accept other source frequencies.\n", "\n", "This indicator has one argument `thresh` that represents a temperature threshold. When a single (scalar) value must be given, xclim accepts a string of the value with units. It will not accept a simple number as units can't be inferred. Usually, xclim will also accept DataArrays with units for this kind of argument, as long as xarray is able to broadcast and align it with the input variable. \n", "\n", "Finally, like almost all indicators, the function takes a `freq` argument to specify over what time period it is computed. These are called \"Offset Aliases\" by Pandas and are the same as the resampling string arguments (in `xr.DataArray.resample`). Valid arguments are detailed in [pandas docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases) (note that aliases involving \"business\" notions are not supported by `xarray` and thus could raise issues in xclim).\n", "\n", "**Attributes:** `xclim` added a global `history` attribute to the dataset, as well as other attributes to the output variable: `standard_name`, `cell_methods`, `long_name` , `description`, with the latter two formatted to include some information about the inputs. Of course, `xclim` updated the `units`, but also added `units_metadata`, required by the CF-Conventions to specify how temperature units should be interpreted. Finally, it added an `xclim_description` attribute that summarizes the computation done, similar to the `history` but without all the datetime, version, and provenance tracking.\n", "\n", "### Units handling paradigm\n", "\n", "Indicators and compute functions are written in order to be flexible as to the sampling frequency and units of the data. You _can_ use `growing_degree_days` on, for example, the 6-hourly data, but the output will then be in degree-hour units (``\"K h\"``). Moreover, all units, even when untouched by the calculation, will be reformatted into a CF-compliant symbol format. This behaviour was chosen to ensure consistency between all compute functions.\n", "\n", "Indicators will always convert their output data into a specific unit, the one listed in `[indicator].outputs`, a list of metadata stores. For example here:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xclim.atmos.growing_degree_days.outputs[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This doesn't apply to the **compute functions**. Very few compute functions will convert their output to specific units; Rather, it is the dimensionality that will be consistent on output.\n", "\n", "### Conventions\n", "\n", "As you may have noticed, the `growing_degree_days` function above was not told along which dimension to operate. In xclim, the temporal dimension is _always_ assumed to be named `\"time\"`. All functions which reduce or compute over that dimension will expect that name. If you ever have another name in your data, you can simply rename it like:\n", "\n", "```\n", "ds = ds.rename(T=\"time\")\n", "```\n", "\n", "For other names and attributes, xclim tries to follow different sets of conventions. In particular, input data should follow the [CF conventions](https://cfconventions.org/) whenever possible for variable attributes. Variable names are usually the ones used in CMIP, when they exist. When a function needs to read the spatial coordinates, it will usually rely on CF conventions, through [cf-xarray](https://cf-xarray.readthedocs.io). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indicator objects\n", "\n", "Indicators are the core class of xclim. They act as compute functions with health checks and metadata attributes. Some checks involve:\n", "\n", "* Warning on missing input metadata\n", "* Identifying periods where missing data impacts the calculation and omit calculations for those periods. Those are called \"missing methods\" and are detailed in section [Health checks](../user_api/checks.rst).\n", "* Appending process history and maintaining the historical provenance of file metadata.\n", "* Writing [Climate and Forecast Convention](https://cfconventions.org/) compliant metadata based on the variables and indicators calculated.\n", "\n", "If we run the `growing_degree_days` indicator over a non-daily dataset, we'll be warned that the input data is not daily. That is, running ```xclim.atmos.growing_degree_days(ds.air, thresh='10.0 degC', freq='MS')``` will fail with a `ValidationError`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "# Show that data is not at a daily time frequency\n", "\n", "ds6h = xr.tutorial.load_dataset(\"air_temperature\")\n", "xr.infer_freq(ds6h.time)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "gdd = xclim.atmos.growing_degree_days(tas=ds6h.air, thresh=\"10.0 degC\", freq=\"MS\")\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Resampling to a daily frequency and running the same indicator succeeds, but we will still get warnings from the CF metadata checks." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "daily_ds = ds6h.resample(time=\"D\").mean(keep_attrs=True)\n", "gdd = xclim.atmos.growing_degree_days(daily_ds.air, thresh=\"10.0 degC\", freq=\"YS\")\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To suppress the CF validation warnings, we can set xclim to send these warnings to the log instead of raising a warning or an error. We also could set `data_validation='warn'` to be able to run the indicator on non-daily data. These options can be set globally or within a context manager with [set_options](../user_api/indicator.rst#xclim.core.options.set_options).\n", "\n", "The missing method which determines if a period should be considered missing or not can be controlled through the `check_missing` option, globally or contextually. The main missing methods also have options that can be modified." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(\n", " check_missing=\"pct\",\n", " missing_options={\"pct\": dict(tolerance=0.1)},\n", " cf_compliance=\"log\",\n", "):\n", " # Change the missing method to \"percent\", instead of the default \"any\"\n", " # Set the tolerance to 10%, periods with more than 10% of missing data\n", " # in the input will be masked in the output.\n", " gdd = xclim.atmos.growing_degree_days(daily_ds.air, thresh=\"10.0 degC\", freq=\"MS\")\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some indicators also expose time-selection arguments as `**indexer` keywords. This allows to compute on a subset of the time coordinates, for example only on a specific season, month, or between two dates in every year. It relies on the [select_time](../user_api/units_calendar.rst#xclim.core.calendar.select_time) function. Some indicators will simply select the time period and run the calculations, while others will smartly perform the selection at the right time, when the order of operation makes a difference. All will pass the `indexer` kwargs to the missing value handling, ensuring that the missing values _outside_ the valid time period are **not** considered.\n", "\n", "The next example computes the annual sum of growing degree days over 10 °C, but only considering days from the 1st of April to the 30th of September." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(cf_compliance=\"log\"):\n", " gdd = xclim.atmos.growing_degree_days(tas=daily_ds.air, thresh=\"10 degC\", freq=\"YS\", date_bounds=(\"04-01\", \"09-30\"))\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`xclim` also allows us to call indicators using datasets and variable names. `xarray`'s `keep_attrs` option is used to decide if dataset (or node) attributes should be preserved and passed to the output." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(cf_compliance=\"log\"):\n", " gdd = xclim.atmos.growing_degree_days(tas=\"air\", thresh=\"10.0 degC\", freq=\"MS\", ds=daily_ds)\n", "\n", " # variable names default to xclim names, so we can even do this:\n", " renamed_daily_ds = daily_ds.rename(air=\"tas\")\n", " gdd = xclim.atmos.growing_degree_days(thresh=\"10.0 degC\", freq=\"MS\", ds=renamed_daily_ds)\n", "gdd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also get DataArrays as an output by disabling the `as_dataset` option." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(as_dataset=False, cf_compliance=\"log\"):\n", " gdd_da = xclim.atmos.growing_degree_days(\n", " tas=daily_ds.air, thresh=\"10 degC\", freq=\"YS\", date_bounds=(\"04-01\", \"09-30\")\n", " )\n", "gdd_da" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, when passed a DataTree, xclim will map the computation over all nodes. It will skip empty nodes, but it requires that all non-empty nodes have all needed variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DT = xr.DataTree.from_dict({\"/daily\": daily_ds, \"/daily_too\": daily_ds, \"/daily/again\": daily_ds})\n", "with xclim.set_options(cf_compliance=\"log\"):\n", " gdd_dt = xclim.atmos.growing_degree_days(tas=\"air\", thresh=\"10 degC\", freq=\"YS\", ds=DT)\n", "gdd_dt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Graphics\n", "\n", "`xclim` does not have specific functions to create graphics. However, it is built to ensure that indicators always have appropriate axis-related metadata that libraries like [Matplotlib](https://matplotlib.org) depend on to generate detailed and informative graphics.\n", "\n", "This graphical functionality is entirely thanks to `xarray`, so the following examples are applicable to generic `xarray.DataArray` objects. For more examples, see the directions suggested by [xarray's plotting documentation](https://docs.xarray.dev/en/stable/user-guide/plotting.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `xarray` plot functions creates a histogram when the `DataArray` has 3 or more dimensions. In previous steps, `xclim` automatically filled the `long_name` and `units` attributes, which `xarray` uses to label the x-axis." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "print(\"long_name:\", gdd.growing_degree_days.attrs[\"long_name\"])\n", "print(\"units:\", gdd.growing_degree_days.attrs[\"units\"])\n", "\n", "gdd.growing_degree_days.plot()\n", "plt.suptitle(\"Summary Statistics Histogram\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the DataArray only has a `time` dimension, xarray plots a timeseries. In this case, xarray uses the `long_name` and `units` attributes provided by xclim to label the y-axis." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdd.growing_degree_days.isel(lon=20, lat=10).plot()\n", "plt.suptitle(\"Time Series at a Given Geographical Coordinate\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the DataArray only has 2 dimensions, xarray plots a heatmap. In this case, xarray uses the `long_name` and `units` attributes provided by xclim to label the colorbar." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdd.growing_degree_days.sel(time=\"2013-07-01\").plot()\n", "plt.suptitle(\"Spatial Pattern at a Specific Time Period\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Writing DataArrays and Datasets to disk\n", "\n", "To save the data as a new NetCDF, use `to_netcdf`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdd.to_netcdf(\"monthly_growing_degree_days_data.nc\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "It's possible to save Dataset objects to other file formats. For more information see: [xarray's documentation](https://docs.xarray.dev/en/stable/generated/xarray.Dataset.html)\n", "\n", "
\n" ] } ], "metadata": { "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.14.2" } }, "nbformat": 4, "nbformat_minor": 4 }