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 intake_xarray_kwargs to ThreddsCatalog #52

Merged
merged 8 commits into from
Mar 18, 2022
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
12 changes: 10 additions & 2 deletions intake_thredds/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ThreddsCatalog(Catalog):
Location of thredds catalog.
driver : str
Select driver to access data. Choose from 'netcdf' and 'opendap'.
intake_xarray_kwargs : dict
Keyword arguments to pass to intake_xarray DataSource.
**kwargs :
Additional keyword arguments are passed through to the
:py:class:`~intake.catalog.Catalog` base class.
Expand All @@ -24,9 +26,10 @@ class ThreddsCatalog(Catalog):

name = 'thredds_cat'

def __init__(self, url: str, driver: str = 'opendap', **kwargs):
def __init__(self, url: str, driver: str = 'opendap', intake_xarray_kwargs=None, **kwargs):
self.url = url
self.driver = driver
self.intake_xarray_kwargs = intake_xarray_kwargs or {'chunks': {}}
super().__init__(**kwargs)

def _load(self):
Expand Down Expand Up @@ -77,14 +80,19 @@ def access_urls(ds, self):
url = f'{self.metadata["fsspec_pre_url"]}{url}'
return url

def _update_args(ds):
args = self.intake_xarray_kwargs.copy()
args.update({'urlpath': access_urls(ds, self)})
return args

self._entries.update(
{
ds.name: LocalCatalogEntry(
ds.name,
'THREDDS data',
self.driver,
True,
{'urlpath': access_urls(ds, self), 'chunks': {}},
_update_args(ds),
[],
[],
{},
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ def test_ThreddsCatalog_simplecache_fails_opendap(thredds_cat_url):
"""Test that ThreddsCatalog simplecache:: in url with opendap."""
with pytest.raises(ValueError, match=r'simplecache requires driver="netcdf"'):
intake.open_thredds_cat(f'simplecache::{thredds_cat_url}', driver='opendap')


def test_ThreddsCatalog_intake_xarray_kwargs():
"""Test that ThreddsCatalog allows intake_xarray kwargs."""
cat = intake.open_thredds_cat(
'https://psl.noaa.gov/thredds/catalog/Datasets/noaa.ersst/catalog.xml',
driver='netcdf',
intake_xarray_kwargs={
'xarray_kwargs': {'decode_times': False, 'engine': 'h5netcdf'},
'chunks': {},
},
)
entry = cat['sst.mon.19712000.ltm.v3.nc']
ds = entry(chunks={}).to_dask()
assert isinstance(ds, xr.Dataset)