from pathlib import Path
import datetime
import xarray as xr
import rioxarray
Geotiff-to-xarray
Generate a xarray.Dataset
from a list of geotiffs.
print(xr.__version__)
print(rioxarray.__version__)
2025.3.1
0.18.2
= Path("...") geotiff_dir
def get_date(filename: Path) -> datetime.datetime:
"""Parse date from filename - adapt to your needs"""
= filename.stem
f = f.split("_")[-1]
date_str return datetime.datetime.strptime(date_str, "%Y-%m-%d")
Get list of geotiffs
= list(geotiff_dir.glob("*.tif"))
geotiffs =get_date) geotiffs.sort(key
Perform the actual read
Important: The geotiffs themselves have to be tiled to enable chunked loading/processing (see this github issue), just setting chunks={...}
in .open_rasterio
is not sufficient.
= [
da_s
rioxarray.open_rasterio(
raster,="snow_cover",
default_name={"band": 1, "x": 1024, "y": 1024},
chunks="band", drop=True)
).squeeze(dimfor raster in geotiffs
]
= xr.Variable("time", [get_date(img) for img in geotiffs]) time_var
Concatenate aling time dimension
= xr.concat(da_s, dim=time_var).to_dataset() ds
# write source directory into attributes to preserve the origin
"base_dir"] = geotiff_dir.as_posix() ds.attrs[
Write to disk as zarr
"data.zarr") ds.to_zarr(