def test_pint2cfunits(self): u = units("mm/d") assert pint2cfunits(u.units) == "mm d-1" u = units("percent") assert pint2cfunits(u.units) == "%" u = units("pct") assert pint2cfunits(u.units) == "%"
def precip_seasonality(pr: xarray.DataArray, freq: str = "YS") -> xarray.DataArray: r"""ANUCLIM Precipitation Seasonality (C of V). The annual precipitation Coefficient of Variation (C of V) expressed in percent. Calculated as the standard deviation of precipitation values for a given year expressed as a percentage of the mean of those values. Parameters ---------- pr : xarray.DataArray Total precipitation rate at daily, weekly, or monthly frequency. Units need to be defined as a rate (e.g. mm d-1, mm week-1). freq : str Resampling frequency. Returns ------- xarray.DataArray, [%] Precipitation coefficient of variation Examples -------- The following would compute for each grid cell of file `pr.day.nc` the annual precipitation seasonality: >>> import xclim.indices as xci >>> p = xr.open_dataset(path_to_pr_file).pr >>> pday_seasonality = xci.precip_seasonality(p) >>> p_weekly = xci.precip_accumulation(p, freq='7D') # Input units need to be a rate >>> p_weekly.attrs['units'] = "mm/week" >>> pweek_seasonality = xci.precip_seasonality(p_weekly) Notes ----- According to the ANUCLIM user-guide https://fennerschool.anu.edu.au/files/anuclim61.pdf (ch. 6), input values should be at a weekly (or monthly) frequency. However, the xclim.indices implementation here will calculate the result with input data with daily frequency as well. As such weekly or monthly input values, if desired, should be calculated prior to calling the function. If input units are in mm s-1 (or equivalent) values are converted to mm/day to avoid potentially small denominator values. """ # If units in mm/sec convert to mm/days to avoid potentially small denominator if units2pint(pr) == units("mm / s"): pr = convert_units_to(pr, "mm d-1") with xarray.set_options(keep_attrs=True): seas = 100 * _anuclim_coeff_var(pr, freq=freq) seas.attrs["units"] = "%" return seas