Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt_freq broken with 360_day and group 31-doy #2037

Open
1 task done
juliettelavoie opened this issue Jan 8, 2025 · 0 comments · May be fixed by #2038
Open
1 task done

adapt_freq broken with 360_day and group 31-doy #2037

juliettelavoie opened this issue Jan 8, 2025 · 0 comments · May be fixed by #2038

Comments

@juliettelavoie
Copy link
Contributor

Generic Issue

  • xclim version: 0.54
  • Python version: 3.12.7
  • Operating System: linux

Description

adapt_freq stopped working with 360_day calendar and group 31-doy

What I Did

import xarray as xr
import xclim as xc
from xclim.testing.helpers import test_timeseries as timeseries
import xscen as xs
from xclim import sdba
import numpy as np
group = {"group": "time.dayofyear", "window": 31}
group = sdba.Grouper.from_kwargs(**group)["group"]
dref = timeseries(np.ones(365 * 3), variable="tas", start="2001-01-01", freq="D", as_dataset=True)
dhist = timeseries(
    np.concatenate([np.ones(365 * 2) * 2, np.ones(365) * 3]),
    variable="tas",
    start="2001-01-01",
    freq="D",
    as_dataset=True,
)
ref=dref.convert_calendar("360_day", align_on='year')['tas']
hist=dhist.convert_calendar("360_day", align_on='year')['tas']
hist_ad, pth, dP0 = sdba.processing.adapt_freq(
    ref, hist, thresh="2 K", group=group
)

What I Received

Traceback (most recent call last):
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-5-ef5356e36a2f>", line 20, in <module>
    hist_ad, pth, dP0 = sdba.processing.adapt_freq(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<boltons.funcutils.FunctionBuilder-288>", line 2, in adapt_freq
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/core/formatting.py", line 468, in _call_and_add_history
    outs = func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/sdba/processing.py", line 103, in adapt_freq
    out = _adapt_freq(xr.Dataset({"sim": sim, "ref": ref}), group=group, thresh=thresh)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<boltons.funcutils.FunctionBuilder-280>", line 2, in _map_blocks
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/sdba/base.py", line 485, in _parse_group
    return func(*f_args, **f_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/sdba/base.py", line 639, in _map_blocks
    coords[group.prop] = group.get_coordinate(ds=ds)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/sdba/base.py", line 198, in get_coordinate
    mdoy = max(
           ^^^^
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xclim/sdba/base.py", line 199, in <genexpr>
    xr.coding.calendar_ops._days_in_year(yr, cal)
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xarray/coding/calendar_ops.py", line 244, in _days_in_year
    return full_like(years, 360)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/exec/jlavoie/.conda/xscen-dev/lib/python3.12/site-packages/xarray/core/common.py", line 1719, in full_like
    raise TypeError("Expected DataArray, Dataset, or Variable")
TypeError: Expected DataArray, Dataset, or Variable

But it works if I don't convert the calendar or if group time.

Note: this breaks a xscen test.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@aulemahal aulemahal linked a pull request Jan 8, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant