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

Promote band-level coords to data var attributes for stackstac #37

Merged
merged 1 commit into from
Apr 5, 2024
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
1 change: 1 addition & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_to_xarray_with_pystac_client_search_passes_kwargs_through(simple_search
def test_to_xarray_with_different_stacking_library(simple_search, stacking_library):
ds = to_xarray(simple_search, stacking_library=stacking_library)
assert isinstance(ds, xr.Dataset)
assert "band" not in ds.dims


def test_to_xarray_with_drop_variables_raises(simple_search):
Expand Down
12 changes: 11 additions & 1 deletion xpystac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ def _(
return odc_stac.load(items, **{"chunks": {"x": 1024, "y": 1024}, **kwargs})
elif stacking_library == "stackstac":
stackstac = _import_optional_dependency("stackstac")
return stackstac.stack(obj, **kwargs).to_dataset(dim="band", promote_attrs=True)
da = stackstac.stack(obj, **kwargs)
bands = {}
for band in da.band.values:
b = da.sel(band=band)
scalar_coords = {k: v.item() for k, v in b.coords.items() if v.shape == ()}
b = b.assign_attrs(
**{k: v for k, v in scalar_coords.items() if v is not None}
)
b = b.drop_vars(scalar_coords)
bands[band] = b
return xarray.Dataset(bands, attrs=da.attrs)


@to_xarray.register
Expand Down
Loading