Last active
August 20, 2024 06:56
-
-
Save robbibt/0301d77a0fbf62a8ab606df2d60385a3 to your computer and use it in GitHub Desktop.
Load ARCO-ERA5 data from Google Cloud
This file contains hidden or 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
# Loads cloud-friendly Zarr format Analysis-Ready, Cloud Optimized ERA5 data from Google Cloud. | |
# Available: https://github.com/google-research/arco-era5 | |
import xarray | |
import gcsfs | |
import odc.geo.xr | |
from odc.geo.geom import BoundingBox | |
def load_era5( | |
x=None, | |
y=None, | |
crs="EPSG:4326", | |
time=None, | |
bands=None, | |
chunks=None, | |
path="gs://gcp-public-data-arco-era5/ar/full_37-1h-0p25deg-chunk-1.zarr-v3", | |
): | |
# Lazily load Zarr from Google Cloud Platform | |
era5_ds = xarray.open_zarr(path, chunks=chunks, storage_options=dict(token="anon")) | |
# Select bands | |
era5_ds = era5_ds[bands] if bands is not None else era5_ds | |
# Clip to extent | |
if x is not None: | |
bbox = BoundingBox.from_xy(x, y, crs=crs).to_crs("EPSG:4326") | |
era5_ds = era5_ds.sel( | |
longitude=slice(bbox.left, bbox.right), | |
latitude=slice(bbox.top, bbox.bottom), | |
) | |
# Select time | |
if time is not None: | |
era5_ds = era5_ds.sel(time=slice(time[0], time[-1])) | |
return era5_ds.odc.assign_crs("EPSG:4326") | |
# Lazily load Zarr from Google Cloud Platform to inspect data | |
era5_ds = xarray.open_zarr( | |
"gs://gcp-public-data-arco-era5/ar/full_37-1h-0p25deg-chunk-1.zarr-v3", | |
chunks=None, | |
storage_options=dict(token="anon")) | |
era5_ds | |
# Load a small portion and time period | |
x = (100, 150) | |
y = (0, -50) | |
time = ("2024-05-01", "2024-05-02") | |
era5_subset_ds = load_era5( | |
x=x, | |
y=y, | |
crs="EPSG:4326", | |
time=time, | |
bands=["temperature"], | |
path="gs://gcp-public-data-arco-era5/ar/full_37-1h-0p25deg-chunk-1.zarr-v3", | |
) | |
era5_subset_ds | |
# Animate | |
# pip install dea-tools | |
from dea_tools.plotting import xr_animation | |
xr_animation( | |
era5_subset_ds, | |
bands=["total_precipitation"], | |
imshow_kwargs={"cmap": "viridis"}, | |
interval=50, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
animation.2.mp4