Skip to content

Commit

Permalink
grab attrs from ds.time.encoding instead of reattaching (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmims authored May 30, 2024
1 parent 78aa4d6 commit a35ee1b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,21 @@ def execute(self, var, ds, **kwargs):
objects so that they can be compared with the model data's time axis.
"""
tv_name = var.name_in_model
t_coord = ds.cf.dim_axes(tv_name).get('T', None)
# t_coord = ds.cf.dim_axes(tv_name).get('T', None)
t_coord = ds.time
if t_coord is None:
var.log.debug("Exit %s for %s: time-independent.",
self.__class__.__name__, var.full_name)
return ds
# time coordinate will be a list if variable has
# multiple coordinates/coordinate attributes
if isinstance(t_coord, list):
cal = t_coord[0].attrs['calendar']
cal = t_coord[0].encoding['calendar']
t_start = t_coord[0].values[0]
t_end = t_coord[0].values[-1]
t_size = t_coord[0].size
else:
cal = t_coord.attrs['calendar']
cal = t_coord.encoding['calendar']
t_start = t_coord.values[0]
t_end = t_coord.values[-1]
t_size = t_coord.size
Expand Down
6 changes: 2 additions & 4 deletions src/xr_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,21 +1278,19 @@ def parse(self, var, ds: xr.Dataset):
"""

self.normalize_pre_decode(ds)
cal = ds['time'].attrs['calendar']
ds = xr.decode_cf(ds,
decode_coords=True, # parse coords attr
decode_times=True,
use_cftime=True # use cftime instead of np.datetime64
)
ds['time'].attrs['calendar'] = cal
# ds = ds.cf.guess_coord_axis() # may not need this
# self.restore_attrs_backup(ds)
# self.normalize_metadata(var, ds)
self.check_calendar(ds)
# self._post_normalize_hook(var, ds)

#if self.disable:
# return ds # stop here; don't attempt to reconcile
if self.disable:
return ds # stop here; don't attempt to reconcile
#if var is not None:
# self.reconcile_variable(var, ds)
# self.check_ds_attrs(var, ds)
Expand Down

0 comments on commit a35ee1b

Please sign in to comment.