- {
- “cells”: [
- {
“cell_type”: “markdown”, “metadata”: {}, “source”: [
“# Customizing and controlling xclimn”, “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”: [
“import xarray as xrn”, “import xclimn”, “from xclim.testing import open_dataset”
]
}, {
“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.open_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”: [
“## Checksn”, “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”: [
“## Missing valuesn”, “n”, “For example, one can 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 tasmaxn”, “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 tasmaxn”, “tx_mean.sel(time=’2013’, lat=75, lon=200)”
]
}, {
“cell_type”: “markdown”, “metadata”: {}, “source”: [
“This method checks that there is less than nm=5 invalid values in a month and that there are no consecutive runs of nc>=4 invalid values. Thus, every month is now valid.n”, “n”, “Finally, it is possible for advanced users to register their own method. Xclim’s missing methods are in fact based on class instances. Thus, to create a custom missing class, one should implement a subclass based on xclim.core.checks.MissingBase and overriding at least the is_missing method. The method should take a null argument and a count argument.n”, “n”, “- null is a DataArrayResample instance of the resampled mask of invalid values in the input dataarray.n”, “- count is the number of days in each resampled periods and any number of other keyword arguments. n”, “n”, “The is_missing method should return a boolean mask, at the same frequency as the indicator output (same as count), where True values are for elements that are considered missing and masked on the output.n”, “n”, “When registering the class with the xclim.core.checks.register_missing_method decorator, the keyword arguments will be registered as options for the missing method. One can also implement a validate static method that receives only those options and returns whether they should be considered valid or not.”
]
}, {
“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [
“from xclim.core.missing import register_missing_methodn”, “from xclim.core.missing import MissingBasen”, “from xclim.indices.run_length import longest_runn”, “n”, “@register_missing_method("consecutive")n”, “class MissingConsecutive(MissingBase):n”, ” """Any period with more than max_n consecutive missing values is considered invalid"""n”, ” def is_missing(self, null, count, max_n=5):n”, ” return null.map(longest_run, dim="time") >= max_nn”, “n”, ” @staticmethodn”, ” def validate(max_n):n”, ” return max_n > 0n”
]
}, {
“cell_type”: “markdown”, “metadata”: {}, “source”: [
“The new method is now accessible and usable with:”
]
}, {
“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [
“with xclim.set_options(check_missing="consecutive", missing_options={‘consecutive’: {‘max_n’: 2}}):n”, ” tx_mean = xclim.atmos.tx_mean(tasmax=tasmax, freq=’MS’) # compute monthly max tasmaxn”, “tx_mean.sel(time=’2013’, lat=75, lon=200)”
]
}
], “metadata”: {
- “kernelspec”: {
“display_name”: “Python 3”, “language”: “python”, “name”: “python3”
}, “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.8.8”
}
}, “nbformat”: 4, “nbformat_minor”: 4
}