Closed matteodefelice closed 1 year ago
Hi @matteodefelice,
I've downloaded a similar GRIB file and can see what the problem is. With a grib_ls, we can see the messages that are being filtered:
grib_ls -ptypeOfLevel,level,shortName ./gfs.t18z.pgrb2.0p25.f027 | grep heightAboveGround
heightAboveGround 4000 refd
heightAboveGround 1000 refd
heightAboveGround 2 2t
heightAboveGround 2 2sh
heightAboveGround 2 2d
heightAboveGround 2 2r
heightAboveGround 2 aptmp
heightAboveGround 2 tmax
heightAboveGround 2 tmin
heightAboveGround 10 10u
heightAboveGround 10 10v
heightAboveGroundLayer 3000 hlcy
heightAboveGroundLayer 6000 ustm
heightAboveGroundLayer 6000 vstm
heightAboveGround 20 u
heightAboveGround 20 v
heightAboveGround 30 u
heightAboveGround 30 v
heightAboveGround 40 u
heightAboveGround 40 v
heightAboveGround 50 u
heightAboveGround 50 v
heightAboveGround 80 t
heightAboveGround 80 q
heightAboveGround 80 pres
heightAboveGround 80 u
heightAboveGround 80 v
heightAboveGround 100 t
heightAboveGround 100 100u
heightAboveGround 100 100v
Now, cfgrib does not like variables with different coordinates (see also #13). In this case, it will first read variable 'refd' and take the level coordinates to be 1000 and 4000. Then it hits variable '2t' with a level of 2. This will not work - see the note in the readme: https://github.com/ecmwf/cfgrib#filter-heterogeneous-grib-files
So you will need to also filter by variable to get only those that have the same levels. Before you ask, I'm not sure how to handle the awkward case of u/u10/u100 having different names and paramIds!
I do hope that this helps though.
Best regards, Iain
Thanks a lot, actually I have solved the issue doing some pre-processing with wgrib2
but I'd have loved using only python.
Ok, no problem. Metview's Python interface can also be used for pre-processing, but it requires some binaries to be installed (usually through conda). We are, however, discussing some equivalent pure Python features that could help in this sort of situation, because its interface for GRIB handling is very good.
I'll close this issue now though.
Other Option is filter by levels The problem is that there are too many levels. So you can obtain the data by filter Here is an example :
For the 2m height in Python2 looks like this:
data_2maboveground=xarray.open_dataset(local_filename,engine="cfgrib" ,filter_by_keys={'typeOfLevel': 'heightAboveGround','level':2})
For the 2m height in Python3 looks like this:
data_2maboveground=xarray.open_dataset(local_filename,engine="cfgrib" ,backend_kwargs={'filter_by_keys':{'typeOfLevel': 'heightAboveGround','level':2}})
PD. I added ('typeOfLevel':'heightAboveGround') cause I have more data in the GRIB
Perhaps this is linked to https://github.com/ecmwf/cfgrib/issues/75 but I am using the latest version of
cfgrib
and I cannot read some variables in a GFS file. I am trying to access this: https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.20211014/18/atmos/gfs.t18z.pgrb2.0p25.f027If I try to read it:
And If I try to: d = xr.open_dataset('gfs.t18z.pgrb2.0p25.f027', decode_cf = True, engine = 'cfgrib', backend_kwargs = {'filter_by_keys':{ ...: 'typeOfLevel': 'heightAboveGround'}} )
skipping variable: paramId==167 shortName='t2m' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==174096 shortName='sh2' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==168 shortName='d2m' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==260242 shortName='r2' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==260255 shortName='aptmp' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==3015 shortName='tmax' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==3016 shortName='tmin' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=2.0) skipping variable: paramId==165 shortName='u10' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=10.0) skipping variable: paramId==166 shortName='v10' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=10.0) skipping variable: paramId==131 shortName='u' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=('heightAboveGround',), data=array([20., 30., 40., 50., 80.])) skipping variable: paramId==132 shortName='v' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=('heightAboveGround',), data=array([20., 30., 40., 50., 80.])) skipping variable: paramId==130 shortName='t' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=('heightAboveGround',), data=array([ 80., 100.])) skipping variable: paramId==133 shortName='q' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=80.0) skipping variable: paramId==54 shortName='pres' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=80.0) skipping variable: paramId==228246 shortName='u100' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=100.0) skipping variable: paramId==228247 shortName='v100' Traceback (most recent call last): File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 660, in build_dataset_components dict_merge(variables, coord_vars) File "/home/felicma/miniconda3/envs/pydev/lib/python3.7/site-packages/cfgrib/dataset.py", line 593, in dict_merge "key=%r value=%r new_value=%r" % (key, master[key], value) cfgrib.dataset.DatasetBuildError: key present and new value is different: key='heightAboveGround' value=Variable(dimensions=('heightAboveGround',), data=array([1000., 4000.])) new_value=Variable(dimensions=(), data=100.0)