Skip to content

Commit

Permalink
store indexer as a tuple, and reuse in spi
Browse files Browse the repository at this point in the history
  • Loading branch information
coxipi committed Nov 30, 2023
1 parent 35fcb30 commit 0ec03b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 4 additions & 1 deletion xclim/indices/_agro.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,10 @@ def standardized_precipitation_index(
:cite:cts:`mckee_relationship_1993`
"""
if params is not None and pr_cal is None:
freq, window = (params.attrs[s] for s in ["freq", "window"])
freq, window, indexer = (params.attrs[s] for s in ["freq", "window", "indexer"])
# Unpack attrs to None and {} if needed
freq = None if freq == "" else freq
indexer = {} if indexer[0] == "" else {indexer[0]: indexer[1:]}
if cal_start or cal_end:
warnings.warn(
"Expected either `cal_{start|end}` or `params`, got both. The `params` input overrides other inputs."
Expand Down
13 changes: 8 additions & 5 deletions xclim/indices/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def standardized_index_fit_params(
window: int,
dist: str,
method: str,
offset: Quantified = "",
offset: Quantified | None = None,
**indexer,
) -> xr.DataArray:
r"""Standardized Index fitting parameters.
Expand Down Expand Up @@ -712,7 +712,7 @@ def standardized_index_fit_params(
f"The method `{method}` is not supported for distribution `{dist}`."
)

if offset != "":
if offset is not None:
with xr.set_options(keep_attrs=True):
da = da + convert_units_to(offset, da, context="hydro")

Expand All @@ -724,16 +724,19 @@ def standardized_index_fit_params(
)
params.attrs = {
"calibration_period": cal_range,
"freq": freq,
"freq": freq or "",
"window": window,
"scipy_dist": dist,
"method": method,
"group": group,
"units": "",
"offset": offset,
"offset": offset or "",
}
if indexer != {}:
params.attrs["time_indexer"] = str(indexer)
method, args = indexer.popitem()
else:
method, args = "", []
params.attrs["time_indexer"] = (method, *args)

return params

Expand Down

0 comments on commit 0ec03b4

Please sign in to comment.