Skip to content

Commit 839c57d

Browse files
authored
Implement Zarr STAC Extension handling (#51)
Implement reading a collection-level asset with zarr stac extension metadata.
1 parent 3ad1bdd commit 839c57d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

tests/test_core.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ def test_to_xarray_zarr_with_open_kwargs_engine():
127127
to_xarray(zarr_asset)
128128

129129

130+
@requires_planetary_computer
131+
def test_to_xarray_zarr_with_zarr_extension():
132+
import planetary_computer as pc
133+
134+
catalog = pystac_client.Client.open(
135+
STAC_URLS["PLANETARY-COMPUTER"], modifier=pc.sign_inplace
136+
)
137+
collection = catalog.get_collection("daymet-daily-hi")
138+
assert collection is not None
139+
zarr_asset = collection.assets["zarr-abfs"]
140+
141+
# pop off the xarray-assets extension fields
142+
zarr_asset.extra_fields.pop("xarray:open_kwargs")
143+
zarr_asset.extra_fields["zarr:consolidated"] = True
144+
145+
to_xarray(zarr_asset)
146+
147+
130148
@pytest.mark.skip(reason="not yet supported with kerchunk >=0.2.8")
131149
def test_to_xarray_with_item_collection_with_kerchunk_attrs_in_data_cube(
132150
data_cube_kerchunk,

xpystac/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ def _(
175175
if obj.media_type == pystac.MediaType.COG:
176176
_import_optional_dependency("rioxarray")
177177
default_kwargs = {**default_kwargs, "engine": "rasterio"}
178-
elif obj.media_type == "application/vnd+zarr":
178+
elif obj.media_type in ["application/vnd+zarr", "application/vnd.zarr"]:
179179
_import_optional_dependency("zarr")
180-
default_kwargs = {**default_kwargs, "engine": "zarr"}
180+
zarr_kwargs = {}
181+
if "zarr:consolidated" in obj.extra_fields:
182+
zarr_kwargs["consolidated"] = obj.extra_fields["zarr:consolidated"]
183+
if "zarr:zarr_format" in obj.extra_fields:
184+
zarr_kwargs["zarr_format"] = obj.extra_fields["zarr:zarr_format"]
185+
186+
default_kwargs = {**default_kwargs, **zarr_kwargs, "engine": "zarr"}
181187
elif obj.media_type == "application/vnd.zarr+icechunk":
182188
from xpystac._icechunk import read_icechunk
183189

0 commit comments

Comments
 (0)