# Accessing Metadata as Variables in Xarray Datasets Using odc-stac?

**URL:** <https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460>\
**Category:** Data\
**Created:** [August 28, 2024, 7:51am UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460 "2024-08-28T07:51:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fran\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/fran_martin/32/2898_2.png) [@Fran\_Martin](https://discourse.pangeo.io/u/Fran_Martin)\
**Post date:** [August 28, 2024, 7:51am UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460/1 "2024-08-28T07:51:15Z")

</div>

Hi everyone,

I’m currently working with the `odc-stac` library to access and process geospatial data, particularly Sentinel-1 RTC datasets. I’ve been able to load the data successfully into an Xarray Dataset, but I’m interested in retrieving and incorporating metadata associated with the images—such as orbit information, instrument mode, acquisition date, and other relevant details—directly as variables within the Xarray Dataset.

So far, I haven’t found a clear way to achieve this using `odc-stac`. Ideally, I would like these metadata elements to be loaded alongside the data itself, making it easier to analyze and utilize them programmatically without having to manually extract or merge this information from separate sources.

My questions are:

- Is there a built-in way in `odc-stac` to load these metadata fields as variables within the Xarray Dataset?
- If not, what would be the best approach to access and integrate this kind of metadata? Are there recommended workarounds, such as modifying the STAC item properties or using additional libraries?
- Any tips on handling metadata from Sentinel-1 RTC or similar datasets would be greatly appreciated!

I’ve gone through the documentation, but the information on metadata handling seems limited, so any pointers or examples would be super helpful.

Thanks in advance for your guidance!

---

<div class="post-metadata">

**Author:** ![maawoo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/maawoo/32/1972_2.png) [@maawoo](https://discourse.pangeo.io/u/maawoo)\
**Post date:** [August 28, 2024, 6:10pm UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460/2 "2024-08-28T18:10:17Z")

</div>

There is another package that is similar to `odc-stac`, which is called [`stackstac`](https://github.com/gjoseph92/stackstac). It should load the STAC Item properties without issues.  
[Here](https://github.com/opendatacube/odc-stac/issues/54) is an interesting discussion about the differences between both packages, with a great breakdown by @kirill.kzb (maintainer of `odc-stac`) in [this comment](https://github.com/opendatacube/odc-stac/issues/54#issuecomment-1103313511). It also includes the following section about this topic:

> - Access to the original STAC metadata
> - `odc-stac` doesn’t really expose any of that, and there is a fundamental design choice that makes it impossible to do in a general case, but we can certainly add it for special case data loading in the future.
> - `stackstac` exposes all the metadata fields in the returned xarray, combined with delayed computation enabled by Dask this can be very handy as you can leverage all the xarray conveniences to filter out unwanted data.

---

<div class="post-metadata">

**Author:** ![Fran\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/fran_martin/32/2898_2.png) [@Fran\_Martin](https://discourse.pangeo.io/u/Fran_Martin)\
**Post date:** [August 28, 2024, 6:28pm UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460/3 "2024-08-28T18:28:04Z")

</div>

Thank you very much for the response! The truth is that I have visited that discussion quite a few times and I have never noticed that point. It’s certainly a problem for me, because that associated metadata is important. I would like to know more about the decision about not exposing them by @kirill.kzb

---

<div class="post-metadata">

**Author:** ![scottyhq](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/scottyhq/32/162_2.png) [@scottyhq](https://discourse.pangeo.io/u/scottyhq)\
**Post date:** [August 30, 2024, 6:35am UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460/4 "2024-08-30T06:35:22Z")

</div>

@Fran_Martin You can add STAC metadata fields via [ds.assign\_coords](https://docs.xarray.dev/en/stable/generated/xarray.Dataset.assign_coords.html), just take care with any grouping that your doing with odc.stac.load.

The basic approach is 1. create a dataframe from your STAC items, 2. match the time index of the dataframe with the xr.Dataset time index from odc.stac 3. optionally do groupby operations on the dataframe and 4. use ds.assign\_coords to apply various metadata fields as coordinates

If you’re working with RTC check out this repository, in particular this function:

> <https://github.com/egagli/sar_snowmelt_timing/blob/43a8e55138a8da2b99dc0021194a44543211295d/sar_snowmelt_timing/s1_rtc_bs_utils.py#L107-L136>

Here is another example with landsat [CoCoLessons/10\_rioxarray.ipynb at main · CodeToCommunicate/CoCoLessons · GitHub](https://github.com/CodeToCommunicate/CoCoLessons/blob/main/10_rioxarray.ipynb)

---

<div class="post-metadata">

**Author:** ![Fran\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/fran_martin/32/2898_2.png) [@Fran\_Martin](https://discourse.pangeo.io/u/Fran_Martin)\
**Post date:** [September 4, 2024, 6:21am UTC](https://discourse.pangeo.io/t/accessing-metadata-as-variables-in-xarray-datasets-using-odc-stac/4460/5 "2024-09-04T06:21:51Z")

</div>

Hello @scottyhq ,

First of all, I highly appreciate your explanation. I managed to get all the metadata I need with your how-to, following your exact steps, so if it helps anyone, It works 🙂
