Skip to content

Commit

Permalink
initial work on #282
Browse files Browse the repository at this point in the history
  • Loading branch information
pochedls committed Jul 28, 2022
1 parent 8a55bf5 commit d274306
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions conda-env/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies:
- pandas=1.4.3
- xarray=2022.3.0
- xesmf=0.6.2
- python-dateutil=2.8.2
- types-python-dateutil=2.8.19
# Quality Assurance
# ==================
# If versions are updated, also update 'rev' in `.pre-commit.config.yaml`
Expand Down
35 changes: 26 additions & 9 deletions xcdat/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from glob import glob
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union

import cftime
import pandas as pd
import xarray as xr
from dateutil import relativedelta as rd

from xcdat import bounds # noqa: F401
from xcdat.axis import center_times as center_times_func
Expand Down Expand Up @@ -315,9 +317,15 @@ def decode_non_cf_time(dataset: xr.Dataset) -> xr.Dataset:
except ValueError:
return ds

ref_date = pd.to_datetime(ref_date)
ref_date = pd.to_datetime(ref_date).to_pydatetime()

if units == "months":
data = [ref_date + rd.relativedelta(months=offset) for offset in time.data]
data = [cftime.datetime(t.year, t.month, t.day) for t in data]
elif units == "years":
data = [ref_date + rd.relativedelta(years=offset) for offset in time.data]
data = [cftime.datetime(t.year, t.month, t.day) for t in data]

data = [ref_date + pd.DateOffset(**{units: offset}) for offset in time.data]
decoded_time = xr.DataArray(
name=time.name,
data=data,
Expand All @@ -335,13 +343,22 @@ def decode_non_cf_time(dataset: xr.Dataset) -> xr.Dataset:
ds = ds.assign_coords({time.name: decoded_time})

if time_bounds is not None:
data_bounds = [
[
ref_date + pd.DateOffset(**{units: lower}),
ref_date + pd.DateOffset(**{units: upper}),
if units == "months":
data_bounds = [
[
ref_date + rd.relativedelta(months=lower),
ref_date + rd.relativedelta(months=upper),
]
for [lower, upper] in time_bounds.data
]
elif units == "years":
data_bounds = [
[
ref_date + rd.relativedelta(years=lower),
ref_date + rd.relativedelta(years=upper),
]
for [lower, upper] in time_bounds.data
]
for [lower, upper] in time_bounds.data
]
decoded_time_bnds = xr.DataArray(
name=time_bounds.name,
data=data_bounds,
Expand Down Expand Up @@ -429,7 +446,7 @@ def _postprocess_dataset(
dataset: xr.Dataset,
data_var: Optional[str] = None,
center_times: bool = False,
add_bounds: bool = False,
add_bounds: bool = True,
lon_orient: Optional[Tuple[float, float]] = None,
) -> xr.Dataset:
"""Post-processes a Dataset object.
Expand Down

0 comments on commit d274306

Please sign in to comment.