Code: Select all
import xarray as xr
from pathlib import Path
# define input file
ifile = Path('/soge-home/data/analysis/era5/0.28125x0.28125/hourly/total_precipitation/grb/era5_hourly_total_precipitation_202505.grb')
# create index directory
workdir = Path.cwd()
idx_dir = Path(f'{workdir}/idx_files')
idx_dir.mkdir(exist_ok=True, parents=True)
# read dataset
ds = xr.open_dataset(ifile,
chunks='auto',
engine='cfgrib',
backend_kwargs={
"indexpath": str(idx_dir / ifile.name) + ".{short_hash}.idx",
'time_dims': ('valid_time',) # use "valid_time" as main time dim
},
).rename({'valid_time': 'time'}) # rename "valid_time" to "time"
Code: Select all
import xarray as xr
from pathlib import Path
# create index directory
workdir = Path.cwd()
idx_dir = Path(f'{workdir}/idx_files')
idx_dir.mkdir(exist_ok=True, parents=True)
# create sorted list of input files
var_dir = Path('/soge-home/data/analysis/era5/0.28125x0.28125/hourly/total_precipitation/grb/')
ifiles = sorted(var_dir.glob(f'*_2025*.grb'))
# read data with custom index files
ds = xr.open_mfdataset(ifiles,
chunks='auto',
engine='cfgrib',
combine='nested',
concat_dim='valid_time',
parallel=True,
backend_kwargs={
'indexpath': str(idx_dir) + "/test.idx", # custom index file location
'time_dims': ('valid_time',) # use valid_time as main time dim
},
).rename({'valid_time': 'time'}) # rename valid_time to time
Code: Select all
Can't read index file '/hn01-home/worc1870/research/python/progs/grb_test/idx_files/test.idx'
Traceback (most recent call last):
File "/hn01-home/worc1870/micromamba/envs/mm313/lib/python3.13/site-packages/cfgrib/messages.py", line 551, in from_indexpath_or_filestream
self = cls.from_indexpath(indexpath)
File "/hn01-home/worc1870/micromamba/envs/mm313/lib/python3.13/site-packages/cfgrib/messages.py", line 430, in from_indexpath
index = pickle.load(file)
EOFError: Ran out of input
Ignoring index file '/hn01-home/worc1870/research/python/progs/grb_test/idx_files/test.idx' incompatible with GRIB file
Ignoring index file '/hn01-home/worc1870/research/python/progs/grb_test/idx_files/test.idx' incompatible with GRIB file
Ignoring index file '/hn01-home/worc1870/research/python/progs/grb_test/idx_files/test.idx' incompatible with GRIB file
Verwendet Python 3.13.7, Xarray 2025.10.1 und cfgrib 0.9.15.1.
Vielen Dank,
Sebastian
Mobile version