{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Customizing and controlling xclim\n", "\n", "xclim's behaviour can be controlled globally or contextually through `xclim.set_options`, which acts the same way as `xarray.set_options`. For the extension of xclim with the addition of indicators, see the [Extending xclim](extendxclim.ipynb) notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import annotations\n", "\n", "import xarray as xr\n", "\n", "import xclim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create fake data with some missing values and mask every 10th, 20th and 30th of the month. This represents 9.6-10% of masked data for all months except February, where it is 7.1%." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tasmax = xr.tutorial.load_dataset(\"air_temperature\").air.resample(time=\"D\").max(keep_attrs=True)\n", "tasmax = tasmax.where(tasmax.time.dt.day % 10 != 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Checks\n", "Above, we created fake temperature data from a xarray tutorial dataset that doesn't have all the standard CF attributes. By default, when triggering a computation with an Indicator from xclim, warnings will be raised:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tx_mean = xclim.atmos.tx_mean(tasmax=tasmax, freq=\"MS\") # compute monthly max tasmax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setting `cf_compliance` to `'log'` mutes those warnings and sends them to the log instead." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xclim.set_options(cf_compliance=\"log\")\n", "\n", "tx_mean = xclim.atmos.tx_mean(tasmax=tasmax, freq=\"MS\") # compute monthly max tasmax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding translated metadata\n", "\n", "With the help of its internationalization module (`xclim.core.locales`), xclim can add translated metadata to the output of the indicators. The metadata is _not_ translated on-the-fly, but translations are manually written for each indicator and metadata field. Currently, all indicators have a French translation, but users can freely add more languages. See [Internationalization](../internationalization.rst) and [Extending xclim](extendxclim.ipynb).\n", "\n", "In the example below, notice the added `long_name_fr` and `description_fr` attributes. Also, the use of `set_options` as a context makes this configuration transient, only valid within the context." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(metadata_locales=[\"fr\"]):\n", " out = xclim.atmos.tx_max(tasmax=tasmax)\n", "out.attrs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Missing values\n", "\n", "One can also globally change the missing method.\n", "\n", "Change the default missing method to \"pct\" and set its tolerance to 8%:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xclim.set_options(check_missing=\"pct\", missing_options={\"pct\": {\"tolerance\": 0.08}})\n", "\n", "tx_mean = xclim.atmos.tx_mean(tasmax=tasmax, freq=\"MS\") # compute monthly max tasmax\n", "tx_mean.sel(time=\"2013\", lat=75, lon=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only February has non-masked data. Let's say we want to use the ``\"wmo\"`` method (and its default options), but only once, we can do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with xclim.set_options(check_missing=\"wmo\"):\n", " tx_mean = xclim.atmos.tx_mean(tasmax=tasmax, freq=\"MS\") # compute monthly max tasmax\n", "tx_mean.sel(time=\"2013\", lat=75, lon=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This method checks that there are less than `nm=11` invalid values in a month and that there are no consecutive runs of `nc>=5` invalid values. Thus, every month is now valid.\n", "\n", "