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

More time control #13

Merged
merged 3 commits into from
Jun 30, 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
30 changes: 18 additions & 12 deletions pyhomogenize/_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ def date_to_str(self, date, fmt=None):

def _convert_time(self, time):
"""Converts time object to ``datetime.datetime`` object"""

def cftimeindex(time_index, calendar):
cf_datetime = [
cftime.datetime(
t.year,
t.month,
t.day,
calendar=calendar,
)
for t in time_index
]
return xr.CFTimeIndex(cf_datetime)

try:
if "units" in time.attrs:
time_index = pd.to_datetime(
Expand All @@ -238,20 +251,13 @@ def _convert_time(self, time):
time.attrs["calendar"] = "standard"
if time.calendar == "proleptic_gregorian":
time.attrs["calendar"] = "standard"
cf_datetime = [
cftime.datetime(
t.year,
t.month,
t.day,
calendar=time.calendar,
)
for t in time_index
]
return xr.CFTimeIndex(cf_datetime)
return cftimeindex(time_index, calendar=time.calendar)
except Exception:
if "time" in time.indexes:
return time.indexes["time"]
return time.values[()]
t = time.indexes["time"]
else:
t = time.values[()]
return cftimeindex(t, calendar="standard")

def _equalize_time(
self,
Expand Down
3 changes: 2 additions & 1 deletion pyhomogenize/_read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
def open_xrdataset(
files,
use_cftime=True,
decode_cf=False,
decode_times=False,
parallel=False,
data_vars="minimal",
Expand Down Expand Up @@ -61,7 +62,7 @@ def drop_all_coords(ds):
decode_times=decode_times,
combine=combine,
preprocess=drop_all_coords,
decode_cf=False,
decode_cf=decode_cf,
chunks=chunks,
data_vars=data_vars,
coords=coords,
Expand Down