opendatacube / odc-stac

Load STAC items into xarray Datasets.
Apache License 2.0
140 stars 20 forks source link

stac2ds product_cache does not accept odc product #167

Open jmettes opened 2 months ago

jmettes commented 2 months ago

I'd like to construct an odc ds doc from stac, with an existing odc product definition. But it looks like it requires some _md attributes with RasterCollectionMetadata

>>> import datacube
>>> dc = datacube.Datacube()

>>> product_cache={"sentinel-2-c1-l2a": dc.index.products.get_by_name("s2_l2a")}
>>> product_cache
{'sentinel-2-c1-l2a': Product(name='s2_l2a', id_=1)}
>>> from odc.stac.eo3 import stac2ds
>>> from pystac import Item

>>> item = Item.from_file("https://earth-search.aws.element84.com/v1/collections/sentinel-2-c1-l2a/items/S2A_T01KAA_20240825T221936_L2A")
>>> next(stac2ds([item], product_cache=product_cache))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[134], line 7
      3 from odc.stac.eo3._eo3converter import infer_dc_product_from_item
      5 item = Item.from_file("https://earth-search.aws.element84.com/v1/collections/sentinel-2-c1-l2a/items/S2A_T01KAA_20240825T221936_L2A")
----> 7 next(stac2ds([item], product_cache=product_cache))

File ~/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/stac/eo3/_eo3converter.py:325, in stac2ds(items, cfg, product_cache)
    322     product = infer_dc_product(item, cfg)
    323     products[collection_id] = product
--> 325 yield _item_to_ds(item, product, cfg)

File [~/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/stac/eo3/_eo3converter.py:241](http://localhost:8888/home/jonathan/micromamba/envs/generic-mamba/envs/cubeenv/lib/python3.11/site-packages/odc/stac/eo3/_eo3converter.py#line=240), in _item_to_ds(item, product, cfg)
    238 if cfg is None:
    239     cfg = {}
--> 241 md: RasterCollectionMetadata = getattr(product, "_md")
    242 uuid_cfg = cfg.get("uuid", {})
    243 ds_uuid = _compute_uuid(
    244     item, mode=uuid_cfg.get("mode", "auto"), extras=uuid_cfg.get("extras", [])
    245 )

AttributeError: 'Product' object has no attribute '_md'

One way to get around this is:

>>> dataset = next(stac2ds([item]))
>>> dataset.product
Product(name='sentinel-2-c1-l2a', id_=None)
>>> dataset.product = dc.index.products.get_by_name("s2_l2a")
>>> dataset.product
Product(name='s2_l2a', id_=1)

but would be nice to pass it in from the start.