From d27430621b06ad30424d97674200e79ee626a480 Mon Sep 17 00:00:00 2001 From: Stephen Po-Chedley Date: Wed, 27 Jul 2022 18:54:38 -0700 Subject: [PATCH] initial work on #282 --- conda-env/dev.yml | 2 ++ xcdat/dataset.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/conda-env/dev.yml b/conda-env/dev.yml index fa33caad..98f76a1b 100644 --- a/conda-env/dev.yml +++ b/conda-env/dev.yml @@ -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` diff --git a/xcdat/dataset.py b/xcdat/dataset.py index d4806498..205ba9e5 100644 --- a/xcdat/dataset.py +++ b/xcdat/dataset.py @@ -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 @@ -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, @@ -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, @@ -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.