ecmwf/cfgrib

Issue reading different variables from a grib file that have different stepType

McNomarr opened this issue · 2 comments

In the code below, I'm trying to open a grib file and read several variabls (t,prate,soilw,tmin,tmax,dswrf). Because they all have have different stepType from one another the code crashes:

stepType: 'instant' variables: t, sde, t2m, prate
stepType: 'avg' variables: prate,dsrwf
stepType: 'min' variable: tmin
stepType: 'max' variable: tmax
Because 'prate' appears to be in 'avg' and 'instant' the code gets confused and gives the following traceback:

cfgrib.dataset.DatasetBuildError: multiple values for unique key, try re-open the file with one of:
filter_by_keys={'stepType': 'instant', 'shortName': 'prate'}
filter_by_keys={'stepType': 'avg', 'shortName': 'prate'}

Has anyone managed to overcome this issue and read all variables using xr.opendataset properly?

Here's the excerpt of the code I'm running:

if fh == 0:
    vars_to_extract = ['t']
else:
    vars_to_extract = ['prate','soilw','sde','tmin','tmax','dswrf']

push = []
for var in vars_to_extract:
    logging.info('Extract vars {}'.format(var))
    logging.info('fh: {} - var: {}'.format(fh, var))
    **arr = xr.open_dataset(target_file,
                        engine='cfgrib',
                            backend_kwargs={'filter_by_keys': {'shortName': '{}'.format(var)}}**
                        )

Hi @McNomarr,

You should be able to also specify the stepType in the filter_by_keys argument, making your code a bit more complicated, but at least for variable 'prate', you would need to add {'stepType': 'instant'}. Unfortunately, different step types do not make for consistent xarrays, so they really need to be split like this in order for the dataset to make sense.

I hope this helps a little,
Iain

Hi @McNomarr,

Do you really need to use xarray ?

Maybe you could try with cfgrib.open_datasets() instead, It would do the job.

No ? Am I wrong ?

Christophe