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

Allow xr_kwargs #32

Merged
merged 13 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Intake-thredds v2021.x.xx

### Features

- Allow `xarray_kwargs` for :py:class:`~intake_thredds.THREDDSMergedSource` [#32](https://github.com/NCAR/intake-thredds/pull/32) ([@aaronspring](https://github.com/aaronspring))

## Intake-thredds v2021.2.17

([full changelog](https://github.com/NCAR/intake-thredds/compare/792fdc08e7fbbf66455fe554ca9a0f1f8a14ae32...ccb3c469a07cc7adf058ce0f8ba41197ebc5b7c7))
Expand Down
12 changes: 9 additions & 3 deletions intake_thredds/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ class THREDDSMergedSource(DataSourceMixin):
name = 'thredds_merged'
partition_access = True

def __init__(self, url, path, driver='opendap', progressbar=True, metadata=None):
def __init__(
self, url, path, driver='opendap', progressbar=True, xarray_kwargs={}, metadata=None
):

super(THREDDSMergedSource, self).__init__(metadata=metadata)
self.urlpath = url
if 'simplecache::' in url:
self.metadata.update({'fsspec_pre_url': 'simplecache::'})
self.path = path
self.driver = driver
self.xarray_kwargs = xarray_kwargs
self._ds = None
self.progressbar = progressbar
if self.progressbar and tqdm is None:
Expand All @@ -87,9 +90,12 @@ def _open_dataset(self):
break
path = self.path[i:]
if self.progressbar:
data = [ds.to_dask() for ds in tqdm(_match(cat, path), desc='Dataset(s)', ncols=79)]
data = [
ds(xarray_kwargs=self.xarray_kwargs).to_dask()
for ds in tqdm(_match(cat, path), desc='Dataset(s)', ncols=79)
]
else:
data = [ds.to_dask() for ds in _match(cat, path)]
data = [ds(xarray_kwargs=self.xarray_kwargs).to_dask() for ds in _match(cat, path)]
self._ds = xr.combine_by_coords(data, combine_attrs='override')


Expand Down
22 changes: 22 additions & 0 deletions tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,25 @@ def test_THREDDSMergedSource_simplecache_fails_opendap(THREDDSMergedSource_cat_s
intake.open_thredds_cat(
f'simplecache::{THREDDSMergedSource_cat_short_url}', driver='opendap'
)


@pytest.mark.parametrize('driver', ['netcdf', 'opendap'])
@pytest.mark.parametrize('decode_times', [True, False])
def test_THREDDSMergedSource_xarray_kwargs(THREDDSMergedSource_cat_short_url, driver, decode_times):
"""Test xarray_kwargs."""
ds = intake.open_thredds_merged(
'https://psl.noaa.gov/thredds/catalog.xml',
[
'Datasets',
'ncep.reanalysis.dailyavgs',
'surface',
'air.sig995.194*.nc',
],
driver=driver,
xarray_kwargs={'decode_times': decode_times},
).to_dask()
# check xarray_kwargs
if decode_times:
assert 'units' not in ds.time.attrs
else:
assert 'units' in ds.time.attrs