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

unstack horizon #203

Merged
merged 6 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Breaking changes

Bug fixes
^^^^^^^^^
* N/A
* Fix bug in ``unstack_dates`` with seasonal climatological mean. (:issue:`202`, :pull:`202`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion xscen/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def climatological_mean(
]
else:
raise ValueError("The type of 'time' could not be understood.")
ds_rolling = ds_rolling.drop_vars({"month", "year", "time", "day"})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is to resolve the warning that keeps popping up ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exact

ds_rolling = ds_rolling.assign_coords(time=time_coord).transpose("time", ...)

concats.extend([ds_rolling])
Expand Down Expand Up @@ -680,7 +681,7 @@ def produce_horizon(
)
if period is not None:
period = standardize_periods(period, multiple=False)
ds = ds.sel(time=slice(period[0], period[1])).load()
ds = ds.sel(time=slice(period[0], period[1]))
window = int(period[1]) - int(period[0]) + 1
if to_level:
to_level = to_level.format(period0=period[0], period1=period[1])
Expand Down
9 changes: 7 additions & 2 deletions xscen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,6 @@ def unstack_dates(
calendar=calendar,
use_cftime=use_cftime,
)
new_coords = dict(ds.coords)
new_coords.update({"time": new_time, new_dim: seas_list})

def reshape_da(da):
if "time" not in da.dims:
Expand All @@ -889,6 +887,13 @@ def reshape_da(da):
da = flox.xarray.rechunk_for_blockwise(da, "time", years)
return xr.DataArray(da.data.reshape(new_shape), dims=new_dims)

new_coords = dict(ds.coords)
new_coords.update({"time": new_time, new_dim: seas_list})

# put horizon in the right time dimension
if "horizon" in new_coords:
new_coords["horizon"] = reshape_da(new_coords["horizon"])

if isinstance(ds, xr.Dataset):
dso = dsp.map(reshape_da, keep_attrs=True)
else:
Expand Down