Created
February 27, 2023 21:36
-
-
Save amotl/45819df4a635a79a32540935cc811e95 to your computer and use it in GitHub Desktop.
Lazily access ECMWF's ERA5 data via xarray from Planetary Computer's SpatioTemporal Asset Catalog (STAC) API. The data is physically stored in Zarr format on Azure Blob Filesystem (ABFS).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
About | |
===== | |
Lazily access ECMWF's ERA5 data via xarray from Planetary Computer's | |
SpatioTemporal Asset Catalog (STAC) API. The data is physically stored in Zarr | |
format on Azure Blob Filesystem (ABFS). | |
Setup | |
===== | |
pip install adlfs dask planetary-computer pystac-client xarray zarr | |
References | |
========== | |
- https://github.com/earthobservations/wetterdienst/issues/871#issuecomment-1447045874 | |
- https://planetarycomputer.microsoft.com/dataset/era5-pds | |
- https://github.com/davemlz/cubo | |
""" | |
import pystac_client | |
import planetary_computer | |
import xarray as xr | |
from pystac import ItemCollection, Item | |
def stac_open_collection(name: str, **kwargs) -> ItemCollection: | |
catalog = pystac_client.Client.open("https://planetarycomputer.microsoft.com/api/stac/v1/") | |
search = catalog.search(collections=[name], **kwargs) | |
return search.item_collection() | |
def stacitem_to_dataset(item: Item) -> xr.Dataset: | |
signed_item = planetary_computer.sign(item) | |
datasets = [ | |
xr.open_dataset(asset.href, **asset.extra_fields["xarray:open_kwargs"]) for asset in signed_item.assets.values() | |
] | |
return xr.combine_by_coords(datasets, join="exact") | |
if __name__ == "__main__": | |
collection = stac_open_collection("era5-pds", datetime="1980-01") | |
ds = stacitem_to_dataset(collection[0]) | |
print(ds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The shape of the
xarray.Dataset
looks like this: