Skip to content

Commit

Permalink
odc-stac added back
Browse files Browse the repository at this point in the history
  • Loading branch information
konstntokas committed Nov 28, 2024
1 parent 33b1b95 commit bf6f0d2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies:
- dask
- s3fs
- numpy
- odc-stac
- rasterio
- rioxarray
- requests
Expand Down
18 changes: 14 additions & 4 deletions xcube_stac/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
JsonNumberSchema,
JsonObjectSchema,
JsonStringSchema,
JsonComplexSchema,
)

# general stac constants
Expand Down Expand Up @@ -92,10 +93,19 @@
# parameter schemas
STAC_STORE_PARAMETERS = dict(
url=JsonStringSchema(title="URL to STAC catalog"),
stack_mode=JsonBooleanSchema(
title="Decide if stacking of STAC items is applied",
description="If True, 'odc-stac' is used as a default backend.",
default=False,
stack_mode=JsonComplexSchema(
one_of=[
JsonStringSchema(
title="Backend for stacking STAC items",
description="So far, only 'odc-stac' is supported as a backend.",
const="odc-stac",
),
JsonBooleanSchema(
title="Decide if stacking of STAC items is applied",
description="If True, 'odc-stac' is used as a default backend.",
default=False,
),
],
),
)
STAC_STORE_PARAMETERS.update(S3FsAccessor.get_storage_options_schema().properties)
Expand Down
10 changes: 9 additions & 1 deletion xcube_stac/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,21 @@ def __init__(self, url: str, stack_mode: bool = False, **storage_options_s3):
self._storage_options_s3,
self._helper,
)
elif stack_mode is True:
elif stack_mode is True or stack_mode == "odc-stac":
if stack_mode is True:
stack_mode = "xcube"
self._impl = StackStoreMode(
self._catalog,
self._url_mod,
self._searchable,
self._storage_options_s3,
self._helper,
stack_mode,
)
else:
raise DataStoreError(
"Invalid parameterization detected: a boolean or"
" 'odc-stac', was expected"
)

@classmethod
Expand Down
51 changes: 39 additions & 12 deletions xcube_stac/store_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Iterator, Union

import numpy as np
import odc.stac
import pystac
import pystac_client.client
import requests
Expand Down Expand Up @@ -404,6 +405,18 @@ def _get_open_params_data_opener(
class StackStoreMode(SingleStoreMode):
"""Implementations to access stacked STAC items within one collection"""

def __init__(
self,
catalog: Union[pystac.Catalog, pystac_client.client.Client],
url_mod: str,
searchable: bool,
storage_options_s3: dict,
helper: Helper,
stack_mode: str,
):
super().__init__(catalog, url_mod, searchable, storage_options_s3, helper)
self._stack_mode = stack_mode

def access_collection(self, data_id: str) -> pystac.Collection:
"""Access collection for a given data ID.
Expand Down Expand Up @@ -496,29 +509,43 @@ def open_data(
return None
items = sorted(items, key=lambda item: item.properties.get("datetime"))

# group items by date
grouped_items = groupby_solar_day(items)

if opener_id is None:
opener_id = ""
if "asset_names" not in open_params:
assets = list_assets_from_item(
next(iter(grouped_items.values()))[0],
items[0],
supported_format_ids=self._helper.supported_format_ids,
)
open_params["asset_names"] = [asset.extra_fields["id"] for asset in assets]

if is_valid_ml_data_type(data_type) or opener_id.split(":")[0] == "mldataset":
raise NotImplementedError("mldataset not supported in stacking mode")
else:
ds = self.stack_items(grouped_items, **open_params)
ds.attrs["stac_catalog_url"] = self._catalog.get_self_href()
ds.attrs["stac_item_ids"] = dict(
{
date.isoformat(): [item.id for item in items]
for date, items in grouped_items.items()
}
)
if self._stack_mode == "odc-stac":
bbox = open_params["bbox"]
odc_stac_params = dict(
bands=open_params.get("asset_names"),
groupby="solar_day",
chunks=dict(time=1, x=1024, y=1024),
crs=open_params.get("crs"),
resolution=open_params.get("asset_names"),
x=(bbox[0], bbox[2]),
y=(bbox[1], bbox[3]),
)
ds = odc.stac.load(
items,
**odc_stac_params,
)
else:
grouped_items = groupby_solar_day(items)
ds = self.stack_items(grouped_items, **open_params)
ds.attrs["stac_catalog_url"] = self._catalog.get_self_href()
ds.attrs["stac_item_ids"] = dict(
{
date.isoformat(): [item.id for item in items]
for date, items in grouped_items.items()
}
)
return ds

def stack_items(
Expand Down

0 comments on commit bf6f0d2

Please sign in to comment.