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

fix incorrect time reconstruction #188

Merged
merged 10 commits into from May 3, 2023
2 changes: 1 addition & 1 deletion HISTORY.rst
Expand Up @@ -42,7 +42,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: 40 additions & 12 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,28 @@ 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 = [
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,
)
]
RondeauG marked this conversation as resolved.
Show resolved Hide resolved
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 +291,23 @@ 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 = [
pd.to_datetime(f"{y}, {m}, {d}")
for y, m, d in zip(
deltas.year.values, deltas.month.values, deltas.day.values
)
]
RondeauG marked this conversation as resolved.
Show resolved Hide resolved
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