from pystac_client import Client
import planetary_computer
from odc.stac import stac_load
from bounding_box import *
odc.stack.stac_load
Query Sentinel-2 data via odc.stac.stac_load into xarray.Dataset
Important
Set modifier
in client. Otherwise only the search will succeed, but data access will fail
= Client.open(
catalog "https://planetarycomputer.microsoft.com/api/stac/v1",
=planetary_computer.sign_inplace,
modifier
)
= catalog.search(
query =["sentinel-2-l2a"],
collections=[lon_min, lat_min, lon_max, lat_max],
bbox="2024-07-01",
datetime
)
= list(query.items())
items print(f"Found: {len(items):d} datasets")
Important
Remember to set chunks
, otherwise the data will be loaded eagerly!
= 1024
chunk_size
= stac_load(
ds
items,=("B04", "B03", "B02", "B08"),
bands=10,
resolution={"time": 1, "x": chunk_size, "y": chunk_size} # this prevents eager loading of data
chunks
) ds
= ds.sel(x=slice(x_min, x_max), y=slice(y_max, y_min)) ds
= ds.isel(time=[0]) # just take first timestep to reduce amount of data ds
# load data into memory if desired
ds.load()
"B04"].squeeze().plot.imshow() ds[
… write to disk, if necessary …