Skip to content

Commit

Permalink
added load method for timeseries files
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpm committed Jul 11, 2024
1 parent e36585f commit febddf5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/adf_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def get_timeseries_file(self, case, field):
ts_files = sorted(ts_loc.glob(ts_filenames))
return ts_files


def get_ref_timeseries_file(self, field):
if self.adf.compare_obs:
return None
Expand All @@ -175,6 +176,33 @@ def get_ref_timeseries_file(self, field):
return ts_files


def load_timeseries_dataset(self, fils):
if (len(fils) == 0):
warnings.warn("Input file list is empty.")
return None
elif (len(fils) > 1):
ds = xr.open_mfdataset(fils, decode_times=False)
else:
sfil = str(fils[0])
if not Path(sfil).is_file():
warnings.warn(f"Expecting to find file: {sfil}")
return None
ds = xr.open_dataset(sfil, decode_times=False)
if ds is None:
warnings.warn(f"invalid data on load_dataset")
# assign time to midpoint of interval (even if it is already)
if 'time_bnds' in ds:
t = ds['time_bnds'].mean(dim='nbnd')
t.attrs = ds['time'].attrs
ds = ds.assign_coords({'time':t})
elif 'time_bounds' in ds:
t = ds['time_bounds'].mean(dim='nbnd')
t.attrs = ds['time'].attrs
ds = ds.assign_coords({'time':t})
else:
warnings.warn("Timeseries file does not have time bounds info.")
return xr.decode_cf(ds)

def get_ref_regrid_file(self, case, field):
model_rg_loc = Path(self.adf.get_basic_info("cam_regrid_loc", required=True))
return sorted(model_rg_loc.glob(f"{case}_{field}_*.nc"))
Expand Down

0 comments on commit febddf5

Please sign in to comment.