diff --git a/setup.py b/setup.py index df5ea09c8..b6e6af804 100755 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ "scipy>=1.2", "statsmodels", "xarray>=0.17", + "Jinja2", ] dev_requirements = [] diff --git a/xclim/core/formatting.py b/xclim/core/formatting.py index db1c339e1..106fe70fb 100644 --- a/xclim/core/formatting.py +++ b/xclim/core/formatting.py @@ -14,8 +14,10 @@ from inspect import _empty, signature # noqa from typing import Any, Mapping, Sequence +import numpy as np import xarray as xr from boltons.funcutils import wraps +from jinja2 import Environment from xclim.core.utils import InputKind, PercentileDataArray @@ -34,6 +36,8 @@ "pr_per_period": "{unkown}", } +jinja_env = Environment(autoescape=True) + class AttrFormatter(string.Formatter): """A formatter for frequently used attribute values. @@ -77,7 +81,9 @@ def format(self, format_string: str, /, *args: Any, **kwargs: dict) -> str: for k, v in DEFAULT_FORMAT_PARAMS.items(): if k not in kwargs: kwargs.update({k: v}) - return super().format(format_string, *args, **kwargs) + kwargs.update({"np": np}) # noqa + intermediary_res = jinja_env.from_string(format_string, globals=kwargs).render() + return super().format(intermediary_res, *args, **kwargs) def format_field(self, value, format_spec): """Format a value given a formatting spec. diff --git a/xclim/indicators/atmos/_temperature.py b/xclim/indicators/atmos/_temperature.py index 968e1914a..3993df048 100644 --- a/xclim/indicators/atmos/_temperature.py +++ b/xclim/indicators/atmos/_temperature.py @@ -172,7 +172,11 @@ class TempWithIndexing(ResamplingIndicatorWithIndexing): description="{freq} number of heat wave events over a given period. " "An event occurs when the minimum and maximum daily " "temperature both exceeds specific thresholds : " + "{% if np.isscalar(thresh_tasmin) and np.isscalar(thresh_tasmax)%}" "(Tmin > {thresh_tasmin} and Tmax > {thresh_tasmax}) " + "{% else %}" + "(Tmin > per_gridcell_tmin_thresholds and Tmax > per_gridcell_tmax_thresholds) " + "{% endif %}" "over a minimum number of days ({window}).", cell_methods="", keywords="health,",