diff --git a/intake_thredds/cat.py b/intake_thredds/cat.py index 126f8a8..c0a308b 100644 --- a/intake_thredds/cat.py +++ b/intake_thredds/cat.py @@ -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. @@ -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): @@ -77,6 +80,11 @@ 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( @@ -84,7 +92,7 @@ def access_urls(ds, self): 'THREDDS data', self.driver, True, - {'urlpath': access_urls(ds, self), 'chunks': {}}, + _update_args(ds), [], [], {}, diff --git a/tests/test_cat.py b/tests/test_cat.py index ef8743a..c508a8c 100644 --- a/tests/test_cat.py +++ b/tests/test_cat.py @@ -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)