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

Experiment with stacking for kerchunk #38

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
38 changes: 29 additions & 9 deletions xpystac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@functools.singledispatch
def to_xarray(
obj,
stacking_library: Union[Literal["odc.stac", "stackstac"], None] = None,
stacking_library: Union[Literal["odc.stac", "stackstac", "xpystac"], None] = None,
**kwargs,
) -> xarray.Dataset:
"""Given a PySTAC object return an xarray dataset.
Expand Down Expand Up @@ -42,10 +42,11 @@ def to_xarray(

@to_xarray.register(pystac.Item)
@to_xarray.register(pystac.ItemCollection)
@to_xarray.register(list)
def _(
obj: Union[pystac.Item, pystac.ItemCollection],
drop_variables: Union[str, List[str], None] = None,
stacking_library: Union[Literal["odc.stac", "stackstac"], None] = None,
stacking_library: Union[Literal["odc.stac", "stackstac", "xpystac"], None] = None,
**kwargs,
) -> xarray.Dataset:
if drop_variables is not None:
Expand All @@ -56,17 +57,21 @@ def _(
_import_optional_dependency("odc.stac")
stacking_library = "odc.stac"
except ImportError:
_import_optional_dependency("stackstac")
stacking_library = "stackstac"
elif stacking_library not in ["odc.stac", "stackstac"]:
try:
_import_optional_dependency("stackstac")
stacking_library = "stackstac"
except ImportError:
stacking_library = "xpystac"
elif stacking_library not in ["odc.stac", "stackstac", "xpystac"]:
raise ValueError(f"{stacking_library=} is not a valid option")

if isinstance(obj, pystac.Item):
items = [obj]
else:
items = [i for i in obj]

if stacking_library == "odc.stac":
odc_stac = _import_optional_dependency("odc.stac")
if isinstance(obj, pystac.Item):
items = [obj]
else:
items = [i for i in obj]
return odc_stac.load(items, **{"chunks": {"x": 1024, "y": 1024}, **kwargs})
elif stacking_library == "stackstac":
stackstac = _import_optional_dependency("stackstac")
Expand All @@ -81,6 +86,21 @@ def _(
b = b.drop_vars(scalar_coords)
bands[band] = b
return xarray.Dataset(bands, attrs=da.attrs)
else:
media_type = kwargs.get("media_type")
role = kwargs.get("role")
combine_kwargs = {
"combine_attrs": "drop_conflicts",
**kwargs.pop("combine_kwargs", {}),
}
asset_kwargs = {**kwargs.pop("asset_kwargs", {}), **kwargs}

dataset_list = []
for item in items:
for asset in item.get_assets(media_type=media_type, role=role).values():
dataset_list.append(to_xarray(asset, **asset_kwargs))

return xarray.combine_by_coords(dataset_list, **combine_kwargs)


@to_xarray.register
Expand Down
Loading