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

Add xarray open kwargs to cesm timeseries and cesm history #81

Merged
merged 6 commits into from
Sep 15, 2021
Merged
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
23 changes: 18 additions & 5 deletions ecgtools/parsers/cesm.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ def _join(a):
return date


def parse_cesm_history(file, user_streams_dict={}):
def parse_cesm_history(file, user_streams_dict={}, xarray_open_kwargs=None):
"""Parser for CESM history files"""
_default_kwargs = {'engine': 'netcdf4', 'chunks': {}, 'decode_times': False}
if xarray_open_kwargs is None:
xarray_open_kwargs = _default_kwargs
else:
_default_kwargs.update(xarray_open_kwargs)

file = pathlib.Path(file)
info = {}
# If there are entries for user_streams, edit the dictionary
Expand All @@ -126,7 +133,7 @@ def parse_cesm_history(file, user_streams_dict={}):
except:
info['member_id'] = None
break
with xr.open_dataset(file, chunks={}, decode_times=False) as ds:
with xr.open_dataset(file, **xarray_open_kwargs) as ds:
try:
time = ds.cf['time'].name
except KeyError:
Expand Down Expand Up @@ -161,8 +168,14 @@ def parse_cesm_history(file, user_streams_dict={}):
return {INVALID_ASSET: file, TRACEBACK: traceback.format_exc()}


def parse_cesm_timeseries(file, user_streams_dict={}):
"""Parser for CESM Timeseries files"""
def parse_cesm_timeseries(file, user_streams_dict={}, xarray_open_kwargs=None):
"""Parser for CESM timeseries files"""
_default_kwargs = {'engine': 'netcdf4', 'chunks': {}, 'decode_times': False}
if xarray_open_kwargs is None:
xarray_open_kwargs = _default_kwargs
else:
_default_kwargs.update(xarray_open_kwargs)

file = pathlib.Path(file)
info = {}

Expand Down Expand Up @@ -204,7 +217,7 @@ def parse_cesm_timeseries(file, user_streams_dict={}):
info['end_time'] = parse_date(end_time)
info['time_range'] = date_range
break
with xr.open_dataset(file, chunks={}, decode_times=False) as ds:
with xr.open_dataset(file, **xarray_open_kwargs) as ds:

# Get the long name from dataset
info['long_name'] = ds[info['variable']].attrs.get('long_name')
Expand Down