Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get additional attributes from grib file #89

Closed
enyfeo opened this issue Jun 7, 2019 · 8 comments
Closed

Get additional attributes from grib file #89

enyfeo opened this issue Jun 7, 2019 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@enyfeo
Copy link

enyfeo commented Jun 7, 2019

Hi,
There are some attributes within the grib_file that are being ignored when we open the grib file in cfgrib. Is there a way to augment the list of keys that you would like to include to be converted into the xarray dataset?
They also do not appears in the output using cfgrib.open_file

For example :
dateOfModelVersion = 20181010;

@alexamici alexamici added the enhancement New feature or request label Jun 7, 2019
@alexamici alexamici self-assigned this Jun 7, 2019
@alexamici
Copy link
Contributor

@enyfeo I'm surprised that this feature had not been requested earlier :)

The reason I didn't implement a way to access the other keys yet is that I couldn't come up with a reasonable API and I fear the implementation would make the open_dataset slower.

I agree this is an important new feature.

@alexamici
Copy link
Contributor

alexamici commented Jul 23, 2019

@enyfeo and @EddyCMWF in branch stable/0.9.7.x you can now add the read_keys argument to backend_kwargs with a list of the keys you want to add. Note that all keys MUST have the same value for all messages in a variable. For example to add GRIB_systemNumber:

>>> ds = xr.open_dataset('seas5.grib', engine='cfgrib', backend_kwargs={'read_keys':['systemNumber']})
>>> ds.mx2t24
<xarray.DataArray 'mx2t24' (time: 2, step: 3, latitude: 61, longitude: 120)>
dask.array<shape=(2, 3, 61, 120), dtype=float32, chunksize=(2, 3, 61, 120)>
Coordinates:
    number      int64 ...
  * time        (time) datetime64[ns] 2018-04-01 2018-05-01
  * step        (step) timedelta64[ns] 30 days 31 days 61 days
    surface     int64 ...
  * latitude    (latitude) float64 90.0 87.0 84.0 81.0 ... -84.0 -87.0 -90.0
  * longitude   (longitude) float64 0.0 3.0 6.0 9.0 ... 348.0 351.0 354.0 357.0
    valid_time  (time, step) datetime64[ns] dask.array<shape=(2, 3), chunksize=(2, 3)>
Attributes:
    GRIB_paramId:                             51
    GRIB_shortName:                           mx2t24
    ...
    GRIB_systemNumber:                        5
    long_name:                                Maximum temperature at 2 metres...
    units:                                    K

Note that the feature is experimental.

@enyfeo
Copy link
Author

enyfeo commented Jul 23, 2019

Hi, Looks great,
Will check it out.

@alexamici
Copy link
Contributor

I released version 0.9.7.2 with the feature.

@edupenabad
Copy link

I have just tried this one with both 0.9.7.2 and 0.9.7.3 and I haven't managed to get the systemNumber keyword value:

In [3]: ds=xr.open_dataset('seas5_t2m_6h.grib',engine='cfgrib',backend_kwargs={'read_keys':['systemNumber']})                                           

In [4]: ds                                                                                                                                              
Out[4]: 
<xarray.Dataset>
Dimensions:     (latitude: 181, longitude: 360, step: 16, time: 2)
Coordinates:
    number      int64 ...
  * time        (time) datetime64[ns] 2019-10-01 2019-11-01
  * step        (step) timedelta64[ns] 0 days 06:00:00 ... 4 days 00:00:00
    surface     int64 ...
  * latitude    (latitude) float64 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
  * longitude   (longitude) float64 0.0 1.0 2.0 3.0 ... 356.0 357.0 358.0 359.0
    valid_time  (time, step) datetime64[ns] ...
Data variables:
    t2m         (time, step, latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            1
    GRIB_centre:             ecmf
    GRIB_centreDescription:  European Centre for Medium-Range Weather Forecasts
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             European Centre for Medium-Range Weather Forecasts
    history:                 2019-11-15T17:55:12 GRIB to CDM+CF via cfgrib-0....

In [5]: xr.__version__                                                                                                                                  
Out[5]: '0.14.0'

In [6]: cfgrib.__version__                                                                                                                              
Out[6]: '0.9.7.2'

Do you have any suggestion on what I might be doing wrong?

@alexamici
Copy link
Contributor

@edupenabad additional keys are added to the variable attrs, not to the global ones, check out the attrs of ds.t2m

@enyfeo
Copy link
Author

enyfeo commented Nov 16, 2019 via email

@edupenabad
Copy link

Thanks!
My fault! I just got confused when having some GRIB keywords in the global attributes and not others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants