def daily_temperature_range(tasmax, tasmin, freq: str = "YS") -> xarray.DataArray: r"""Mean of daily temperature range. The mean difference between the daily maximum temperature and the daily minimum temperature. Parameters ---------- tasmax : xarray.DataArray Maximum daily temperature values [℃] or [K] tasmin : xarray.DataArray Minimum daily temperature values [℃] or [K] freq : str Resampling frequency; Defaults to "YS". Returns ------- xarray.DataArray The average variation in daily temperature range for the given time period. Notes ----- Let :math:`TX_{ij}` and :math:`TN_{ij}` be the daily maximum and minimum temperature at day :math:`i` of period :math:`j`. Then the mean diurnal temperature range in period :math:`j` is: .. math:: DTR_j = \frac{ \sum_{i=1}^I (TX_{ij} - TN_{ij}) }{I} """ q = 1 * utils.units2pint(tasmax) - 0 * utils.units2pint(tasmin) dtr = tasmax - tasmin out = dtr.resample(time=freq).mean(dim="time", keep_attrs=True) out.attrs["units"] = f"{q.units}" return out
def test_cfunits2pint(self, pr_series): u = units2pint(pr_series([1, 2])) assert (str(u)) == "kilogram / meter ** 2 / second" assert pint2cfunits(u) == "kg m-2 s-1" u = units2pint("m^3 s-1") assert str(u) == "meter ** 3 / second" assert pint2cfunits(u) == "m^3 s-1"
def test_cfunits2pint(self, pr_series): u = units2pint(pr_series([1, 2])) assert (str(u)) == 'kilogram / meter ** 2 / second' assert pint2cfunits(u) == 'kg m-2 s-1' u = units2pint('m^3 s-1') assert str(u) == 'meter ** 3 / second' assert pint2cfunits(u) == 'm^3 s-1'
def extreme_temperature_range(tasmax: xarray.DataArray, tasmin: xarray.DataArray, freq: str = "YS") -> xarray.DataArray: r"""Extreme intra-period temperature range. The maximum of max temperature (TXx) minus the minimum of min temperature (TNn) for the given time period. Parameters ---------- tasmax : xarray.DataArray Maximum daily temperature values [℃] or [K] tasmin : xarray.DataArray Minimum daily temperature values [℃] or [K] freq : Optional[str[ Resampling frequency; Defaults to "YS". Returns ------- xarray.DataArray Extreme intra-period temperature range for the given time period. Notes ----- Let :math:`TX_{ij}` and :math:`TN_{ij}` be the daily maximum and minimum temperature at day :math:`i` of period :math:`j`. Then the extreme temperature range in period :math:`j` is: .. math:: ETR_j = max(TX_{ij}) - min(TN_{ij}) """ q = 1 * utils.units2pint(tasmax) - 0 * utils.units2pint(tasmin) tx_max = tasmax.resample(time=freq).max(dim="time") tn_min = tasmin.resample(time=freq).min(dim="time") out = tx_max - tn_min out.attrs["units"] = f"{q.units}" return out
def daily_temperature_range_variability(tasmax: xarray.DataArray, tasmin: xarray.DataArray, freq: str = "YS") -> xarray.DataArray: r"""Mean absolute day-to-day variation in daily temperature range. Mean absolute day-to-day variation in daily temperature range. Parameters ---------- tasmax : xarray.DataArray Maximum daily temperature values [℃] or [K] tasmin : xarray.DataArray Minimum daily temperature values [℃] or [K] freq : str Resampling frequency; Defaults to "YS". Returns ------- xarray.DataArray The average day-to-day variation in daily temperature range for the given time period. Notes ----- Let :math:`TX_{ij}` and :math:`TN_{ij}` be the daily maximum and minimum temperature at day :math:`i` of period :math:`j`. Then calculated is the absolute day-to-day differences in period :math:`j` is: .. math:: vDTR_j = \frac{ \sum_{i=2}^{I} |(TX_{ij}-TN_{ij})-(TX_{i-1,j}-TN_{i-1,j})| }{I} """ q = 1 * utils.units2pint(tasmax) - 0 * utils.units2pint(tasmin) vdtr = abs((tasmax - tasmin).diff(dim="time")) out = vdtr.resample(time=freq).mean(dim="time") out.attrs["units"] = f"{q.units}" return out
def ts_fit_graph(ts, params): """Create graphic showing an histogram of the data and the distribution fitted to it. Parameters ---------- ts : str Path to netCDF file storing the time series. params : str Path to netCDF file storing the distribution parameters. Returns ------- fig """ from xclim.generic import get_dist n = ts.nbasins.size dist = params.attrs['long_name'].split(' ')[0] fig, axes = plt.subplots(n, figsize=(10, 6), squeeze=False) for i in range(n): ax = axes.flat[i] ax2 = plt.twinx(ax) p = params.isel(nbasins=i) # Plot histogram of time series as density then as a normal count. density, bins, patches = ax.hist(ts.isel(nbasins=i).dropna(dim='time'), alpha=.5, density=True, bins='auto', label="__nolabel__") ax2.hist( ts.isel(nbasins=i).dropna(dim='time'), bins=bins, facecolor=(1, 1, 1, 0.01), edgecolor='gray', linewidth=1, ) # Plot pdf of distribution dc = get_dist(dist)(*params.isel(nbasins=i)) mn = dc.ppf(.01) mx = dc.ppf(.99) q = np.linspace(mn, mx, 200) pdf = dc.pdf(q) ps = ', '.join(["{:.1f}".format(x) for x in p.values]) ax.plot(q, pdf, '-', label="{}({})".format(params.attrs['scipy_dist'], ps)) # Labels ax.set_xlabel("{} (${:~P}$)".format(ts.long_name, units2pint(ts.units))) ax.set_ylabel("Probability density") ax2.set_ylabel("Histogram count") ax.legend(frameon=False) plt.tight_layout() return fig