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

Load collection-level assets into xarray #90

Merged
merged 19 commits into from
Oct 20, 2021
9 changes: 6 additions & 3 deletions docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ Using xarray-assets

Intake-stac uses the `xarray-assets`_ STAC extension to automatically use the appropriate keywords to load a STAC asset into a data container.
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved

Intake-stac will automatically use the keywords from the `xarray-assets`_ STAC extension, if present, when loading data into a container.
For example, the STAC collection at <https://planetarycomputer.microsoft.com/api/stac/v1/collections/daymet-annual-hi> defines an
asset ``zarr-https`` with the metadata ``"xarray:open_kwargs": {"consolidated": true}"`` to indicate that this dataset should be
opened with the ``consolidated=True`` keyword argument. This will be used automatically by ``.to_dask()``


.. code-block:: python

>>> collection = intake.open_stac_collection(
Expand Down Expand Up @@ -175,7 +181,4 @@ Intake-stac uses the `xarray-assets`_ STAC extension to automatically use the ap
source: Daymet Software Version 4.0
start_year: 1980

In that example, the STAC catalog indicates that the Zarr dataset should be opened with ``consolidated=True``,
so intake-stac automatically forwards that keyword argument through to xarray.

.. _xarray-assets: https://github.com/stac-extensions/xarray-assets
17 changes: 10 additions & 7 deletions intake_stac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class StacCollection(StacCatalog):

def get_asset(
self,
asset,
key,
storage_options=None,
merge_asset_storage_options=True,
merge_asset_open_kwargs=True,
Expand All @@ -180,13 +180,16 @@ def get_asset(

Parameters
----------
asset : str, optional
key : str, optional
The asset key to use if multiple Zarr assets are provided.
storage_options : dict, optional
Additional arguments for the backend fsspec filesystem.
merge_asset_storage_option : bool, default True
Whether to merge the storage options provided by the asset under the
``xarray:storage_options`` key with `storage_options`.
merge_asset_open_kwargs : bool, default True
Whether to merge the keywords provided by the asset under the
``xarray:open_kwargs`` key with ``**kwargs``.
**kwargs
Additional keyword options are provided to the loader, for example ``consolidated=True``
to pass to :meth:`xarray.open_zarr`.
Expand All @@ -201,22 +204,22 @@ def get_asset(
The dataset described by the asset loaded into a dask-backed object.
"""
try:
asset_ = self._stac_obj.assets[asset]
asset = self._stac_obj.assets[key]
except KeyError:
raise KeyError(
f'No asset named {asset}. Should be one of {list(self._stac_obj.assets)}'
f'No asset named {key}. Should be one of {list(self._stac_obj.assets)}'
) from None

storage_options = storage_options or {}
if merge_asset_storage_options:
asset_storage_options = asset_.extra_fields.get('xarray:storage_options', {})
asset_storage_options = asset.extra_fields.get('xarray:storage_options', {})
storage_options.update(asset_storage_options)

if merge_asset_open_kwargs:
asset_open_kwargs = asset_.extra_fields.get('xarray:open_kwargs', {})
asset_open_kwargs = asset.extra_fields.get('xarray:open_kwargs', {})
kwargs.update(asset_open_kwargs)

return StacAsset(asset, asset_)(storage_options=storage_options, **kwargs)
return StacAsset(asset, asset)(storage_options=storage_options, **kwargs)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved


class StacItemCollection(AbstractStacCatalog):
Expand Down