Skip to content

Commit

Permalink
Merge pull request #188 from Ouranosinc/fix-timeagg
Browse files Browse the repository at this point in the history
fix incorrect time reconstruction
  • Loading branch information
RondeauG committed May 3, 2023
2 parents fc09cf4 + 0aa2167 commit b2b705b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Expand Up @@ -44,7 +44,7 @@ Bug fixes
* `xscen.utils.unstack_fill_nan`` can now handle datasets that have non dimension coordinates. (:issue:`156`, :pull:`175`).
* `extract_dataset` now skips a simulation way earlier if the frequency doesn't match. (:pull:`170`).
* `extract_dataset` now correctly tries to extract in reverse timedelta order. (:pull:`170`).

* `compute_deltas` no longer creates all NaN values if the input dataset is in a non-standard calendar. (:pull:`188`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
52 changes: 42 additions & 10 deletions xscen/aggregate.py
Expand Up @@ -7,6 +7,7 @@
from types import ModuleType
from typing import Sequence, Tuple, Union

import cftime
import geopandas as gpd
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -122,12 +123,29 @@ def climatological_mean(
# get back to 1D time
ds_rolling = ds_rolling.stack(time=("year", "month", "day"))
# rebuild time coord
time_coord = [
pd.to_datetime(f"{y - window + 1}, {m}, {d}")
for y, m, d in zip(
ds_rolling.year.values, ds_rolling.month.values, ds_rolling.day.values
if isinstance(ds.indexes["time"], pd.core.indexes.datetimes.DatetimeIndex):
time_coord = list(
pd.to_datetime(
{
"year": ds_rolling.year.values - window + 1,
"month": ds_rolling.month.values,
"day": ds_rolling.day.values,
}
).values
)
]
elif isinstance(ds.indexes["time"], xr.coding.cftimeindex.CFTimeIndex):
time_coord = [
cftime.datetime(
y - window + 1, m, d, calendar=ds.indexes["time"].calendar
)
for y, m, d in zip(
ds_rolling.year.values,
ds_rolling.month.values,
ds_rolling.day.values,
)
]
else:
raise ValueError("The type of 'time' could not be understood.")
ds_rolling = ds_rolling.assign_coords(time=time_coord).transpose("time", ...)

concats.extend([ds_rolling])
Expand Down Expand Up @@ -274,12 +292,26 @@ def compute_deltas(
# get back to 1D time
deltas = deltas.stack(time=("year", "month", "day"))
# rebuild time coord
time_coord = [
pd.to_datetime(f"{y}, {m}, {d}")
for y, m, d in zip(
deltas.year.values, deltas.month.values, deltas.day.values
if isinstance(ds.indexes["time"], pd.core.indexes.datetimes.DatetimeIndex):
time_coord = list(
pd.to_datetime(
{
"year": deltas.year.values,
"month": deltas.month.values,
"day": deltas.day.values,
}
).values
)
]
elif isinstance(ds.indexes["time"], xr.coding.cftimeindex.CFTimeIndex):
time_coord = [
cftime.datetime(y, m, d, calendar=ds.indexes["time"].calendar)
for y, m, d in zip(
deltas.year.values, deltas.month.values, deltas.day.values
)
]
else:
raise ValueError("The type of 'time' could not be understood.")

deltas = deltas.assign(time=time_coord).transpose("time", ...)
deltas = deltas.reindex_like(ds)

Expand Down

0 comments on commit b2b705b

Please sign in to comment.