Skip to content

Commit

Permalink
✅ Unit test for a Zarr-backed STAC asset
Browse files Browse the repository at this point in the history
Ensure that a STAC asset pointing to a Zarr file can be loaded using XpySTACAssetReader. Using the Daymet Annual Hawaii STAC Collection at https://planetarycomputer.microsoft.com/dataset/daymet-annual-hi for this unit test. Also edited previous unit test to specify that it is for a Cloud-Optimized GeoTIFF STAC Asset.
  • Loading branch information
weiji14 committed Mar 31, 2023
1 parent 178eca0 commit f8650e5
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions zen3geo/tests/test_datapipes_xpystac.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
pystac = pytest.importorskip("pystac")
xpystac = pytest.importorskip("xpystac")


# %%
def test_xpystac_asset_reader():
def test_xpystac_asset_reader_cog():
"""
Ensure that XpySTACAssetReader works to read in a pystac.Asset object and
output to an xarray.Dataset object.
Ensure that XpySTACAssetReader works to read in a pystac.Asset object
stored as a Cloud-Optimized GeoTIFF and output to an xarray.Dataset object.
"""
item_url: str = "https://github.com/stac-utils/pystac/raw/v1.7.0/tests/data-files/raster/raster-sentinel2-example.json"
item_url: str = "https://github.com/stac-utils/pystac/raw/v1.7.1/tests/data-files/raster/raster-sentinel2-example.json"
asset: pystac.Asset = pystac.Item.from_file(href=item_url).assets["overview"]
assert asset.media_type == pystac.MediaType.COG

dp = IterableWrapper(iterable=[asset])

Expand All @@ -34,3 +36,32 @@ def test_xpystac_asset_reader():
assert dataset.rio.bounds() == (399960.0, 4090240.0, 509720.0, 4200000.0)
assert dataset.rio.resolution() == (320.0, -320.0)
assert dataset.rio.crs == "EPSG:32633"


def test_xpystac_asset_reader_zarr():
"""
Ensure that XpySTACAssetReader works to read in a pystac.Asset object
stored as a Zarr file and output to an xarray.Dataset object.
"""
collection_url: str = "https://planetarycomputer.microsoft.com/api/stac/v1/collections/daymet-daily-hi"
asset: pystac.Asset = pystac.Collection.from_file(href=collection_url).assets[
"zarr-https"
]
assert asset.media_type == "application/vnd+zarr"

dp = IterableWrapper(iterable=[asset])

# Using class constructors
dp_xpystac = XpySTACAssetReader(source_datapipe=dp)
# Using functional form (recommended)
dp_xpystac = dp.read_from_xpystac()

assert len(dp_xpystac) == 1
it = iter(dp_xpystac)
dataset = next(it)

assert dataset.sizes == {"time": 14965, "y": 584, "x": 284, "nv": 2}
assert dataset.prcp.dtype == "float32"
assert dataset.rio.bounds() == (-5802750.0, -622500.0, -5518750.0, -38500.0)
assert dataset.rio.resolution() == (1000.0, -1000.0)
assert dataset.rio.grid_mapping == "lambert_conformal_conic"

0 comments on commit f8650e5

Please sign in to comment.