Package: cfgrib
cfgrib: A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodesPython interface to map GRIB files to theUnidata’s Common Data Model v4following theCF Conventions.
The high level API is designed to support a GRIB engine forxarrayand it is inspired bynetCDF4-pythonandh5netcdf.
Low level access and decoding is performed via theECMWF ecCodes libraryand
theeccodes python package.Features with development statusBeta:enables theengine='cfgrib'option to read GRIB files withxarray,reads most GRIB 1 and 2 files including heterogeneous ones withcfgrib.open_datasets,supports all modern versions of Python 3.9, 3.8, 3.7 and PyPy3,the 0.9.6.x series with support for Python 2 will stay active and receive critical bugfixes,works wherevereccodes-pythondoes:Linux,MacOSandWindowsconda-forge package on all supported platforms,reads the data lazily and efficiently in terms of both memory usage and disk access,allows larger-than-memory and distributed processing viaxarrayanddask,supports translating coordinates to different data models and naming conventions,supports writing the index of a GRIB file to disk, to save a full-file scan on open,accepts objects implementing a genericFieldsetinterface as described inADVANCED_USAGE.rst.Work in progress:Betainstall acfgributility that can convert a GRIB fileto_netcdfwith a optional conversion to a specific coordinates data model,
see#40.Alpha/Brokensupport writing carefully-craftedxarray.Dataset’s to a GRIB1 or GRIB2 file,
see theAdvanced write usagesection below,#18and#156.Limitations:relies onecCodesfor the CF attributes of the data variables,relies onecCodesfor anything related to coordinate systems /gridType,
see#28.InstallationThe easiest way to installcfgriband all its binary dependencies is viaConda:$ conda install -c conda-forge cfgribalternatively, if you install the binary dependencies yourself, you can install the
Python package fromPyPIwith:$ pip install cfgribBinary dependenciescfgribdepends on theeccodes python packageto access the ECMWFecCodesbinary library,
when not usingcondaplease follow theSystem dependenciessection there.You may run a simple selfcheck command to ensure that your system is set up correctly:$ python -m cfgrib selfcheck
Found: ecCodes v2.20.0.
Your system is ready.UsageFirst, you need a well-formed GRIB file, if you don’t have one at hand you can download ourERA5 on pressure levels sample:$ wget http://download.ecmwf.int/test-data/cfgrib/era5-levels-members.gribRead-onlyxarrayGRIB engineMost ofcfgribusers want to open a GRIB file as axarray.Datasetand
need to havexarrayinstalled:$ pip install xarrayIn a Python interpreter try:>>> import xarray as xr
>>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib')
>>> ds
<xarray.Dataset>
Dimensions:        (number: 10, time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120)
Coordinates:
  * number         (number) int64 0 1 2 3 4 5 6 7 8 9
  * time           (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00
    step           timedelta64[ns] ...
  * isobaricInhPa  (isobaricInhPa) float64 850.0 500.0
  * 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 ... 351.0 354.0 357.0
    valid_time     (time) datetime64[ns] ...
Data variables:
    z              (number, time, isobaricInhPa, latitude, longitude) float32 ...
    t              (number, time, isobaricInhPa, 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:                 ...Thecfgribenginesupports all read-only features ofxarraylike:merge the content of several GRIB files into a single dataset usingxarray.open_mfdataset,work with larger-than-memory datasets withdask,allow distributed processing withdask.distributed.Read arbitrary GRIB keysBy defaultcfgribreads a limited set of ecCodes recognisedkeysfrom the GRIB files
and exposes them asDatasetorDataArrayattributes with theGRIB_prefix.
It is possible to havecfgribread additional keys to the attributes by adding theread_keysdictionary key to thebackend_kwargswith values the list of desired GRIB keys:>>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib',
...                      backend_kwargs={'read_keys': ['experimentVersionNumber']})
>>> ds.t.attrs['GRIB_experimentVersionNumber']
'0001'Translate to a custom data modelContrary to netCDF the GRIB data format is not self-describing and several details of the mapping
to theUnidata Common Data Modelare arbitrarily set by the software components decoding the format.
Details like names and units of the coordinates are particularly important becausexarraybroadcast and selection rules depend on them.cf2cfmis a small coordinate translation module distributed withcfgribthat make it easy to
translate CF compliant coordinates, like the one provided bycfgrib, to a user-defined
custom data model with setout_name,unitsandstored_direction.For example to translate acfgribstyledxr.Datasetto the classicECMWFcoordinate
naming conventions you can:>>> import cf2cdm
>>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib')
>>> cf2cdm.translate_coords(ds, cf2cdm.ECMWF)
<xarray.Dataset>
Dimensions:     (number: 10, time: 4, level: 2, latitude: 61, longitude: 120)
Coordinates:
  * number      (number) int64 0 1 2 3 4 5 6 7 8 9
  * time        (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00
    step        timedelta64[ns] ...
  * level       (level) float64 850.0 500.0
  * 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) datetime64[ns] ...
Data variables:
    z           (number, time, level, latitude, longitude) float32 ...
    t           (number, time, level, 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:                 ...To translate to the Common Data Model of the Climate Data Store use:>>> import cf2cdm
>>> cf2cdm.translate_coords(ds, cf2cdm.CDS)
<xarray.Dataset>
Dimensions:                  (realization: 10, forecast_reference_time: 4, plev: 2, lat: 61, lon: 120)
Coordinates:
  * realization              (realization) int64 0 1 2 3 4 5 6 7 8 9
  * forecast_reference_time  (forecast_reference_time) datetime64[ns] 2017-01...
    leadtime                 timedelta64[ns] ...
  * plev                     (plev) float64 8.5e+04 5e+04
  * lat                      (lat) float64 -90.0 -87.0 -84.0 ... 84.0 87.0 90.0
  * lon                      (lon) float64 0.0 3.0 6.0 9.0 ... 351.0 354.0 357.0
    time                     (forecast_reference_time) datetime64[ns] ...
Data variables:
    z                        (realization, forecast_reference_time, plev, lat, lon) float32 ...
    t                        (realization, forecast_reference_time, plev, lat, lon) 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:                 ...Filter heterogeneous GRIB filesxr.open_datasetcan open a GRIB file only if all the messages
with the sameshortNamecan be represented as a single hypercube.
For example, a variabletcannot have bothisobaricInhPaandhybridtypeOfLevel’s,
as this would result in multiple hypercubes for the same variable.
Opening a non-conformant GRIB file will fail with aValueError: multiple values for uniquekey...error message, see#2.Furthermore if different variables depend on the same coordinate, for examplestep,
the values of the coordinate must match exactly.
For example, if variablestandzshare the samestepcoordinate,
they must both have exactly the same set of steps.
Opening a non-conformant GRIB file will fail with aValueError: key present and new value isdifferent...error message, see#13.In most cases you can handle complex GRIB files containing heterogeneous messages by passing
thefilter_by_keyskey inbackend_kwargsto select which GRIB messages belong to a
well formed set of hypercubes.For example to openUS National Weather Service complex GRIB2 filesyou can use:>>> xr.open_dataset('nam.t00z.awp21100.tm00.grib2', engine='cfgrib',
...     backend_kwargs={'filter_by_keys': {'typeOfLevel': 'surface'}})
<xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] ...
    step        timedelta64[ns] ...
    surface     float64 ...
    latitude    (y, x) float64 ...
    longitude   (y, x) float64 ...
    valid_time  datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    gust        (y, x) float32 ...
    sp          (y, x) float32 ...
    orog        (y, x) float32 ...
    tp          (y, x) float32 ...
    acpcp       (y, x) float32 ...
    csnow       (y, x) float32 ...
    cicep       (y, x) float32 ...
    cfrzr       (y, x) float32 ...
    crain       (y, x) float32 ...
    cape        (y, x) float32 ...
    cin         (y, x) float32 ...
    hpbl        (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP...
    history:                 ...
>>> xr.open_dataset('nam.t00z.awp21100.tm00.grib2', engine='cfgrib',
...     backend_kwargs={'filter_by_keys': {'typeOfLevel': 'heightAboveGround', 'level': 2}})
<xarray.Dataset>
Dimensions:            (y: 65, x: 93)
Coordinates:
    time               datetime64[ns] ...
    step               timedelta64[ns] ...
    heightAboveGround  float64 ...
    latitude           (y, x) float64 ...
    longitude          (y, x) float64 ...
    valid_time         datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    t2m                (y, x) float32 ...
    r2                 (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP...
    history:                 ...Automatic filteringcfgribalso provides a function that automates the selection of appropriatefilter_by_keysand returns a list of all validxarray.Dataset’s in the GRIB file.>>> import cfgrib
>>> cfgrib.open_datasets('nam.t00z.awp21100.tm00.grib2')
[<xarray.Dataset>
Dimensions:                (y: 65, x: 93)
Coordinates:
    time                   datetime64[ns] 2018-09-17
    step                   timedelta64[ns] 00:00:00
    atmosphereSingleLayer  float64 0.0
    latitude               (y, x) float64 ...
    longitude              (y, x) float64 ...
    valid_time             datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    pwat                   (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    cloudBase   float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    pres        (y, x) float32 ...
    gh          (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    cloudTop    float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    pres        (y, x) float32 ...
    t           (y, x) float32 ...
    gh          (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:            (y: 65, x: 93)
Coordinates:
    time               datetime64[ns] 2018-09-17
    step               timedelta64[ns] 00:00:00
    heightAboveGround  float64 10.0
    latitude           (y, x) float64 ...
    longitude          (y, x) float64 ...
    valid_time         datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    u10                (y, x) float32 ...
    v10                (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:            (y: 65, x: 93)
Coordinates:
    time               datetime64[ns] 2018-09-17
    step               timedelta64[ns] 00:00:00
    heightAboveGround  float64 2.0
    latitude           (y, x) float64 12.19 12.39 12.58 ... 57.68 57.49 57.29
    longitude          (y, x) float64 226.5 227.2 227.9 ... 308.5 309.6 310.6
    valid_time         datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    t2m                (y, x) float32 ...
    r2                 (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:                 (heightAboveGroundLayer: 2, y: 65, x: 93)
Coordinates:
    time                    datetime64[ns] 2018-09-17
    step                    timedelta64[ns] 00:00:00
  * heightAboveGroundLayer  (heightAboveGroundLayer) float64 1e+03 3e+03
    latitude                (y, x) float64 ...
    longitude               (y, x) float64 ...
    valid_time              datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    hlcy                    (heightAboveGroundLayer, y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:        (isobaricInhPa: 19, y: 65, x: 93)
Coordinates:
    time           datetime64[ns] 2018-09-17
    step           timedelta64[ns] 00:00:00
  * isobaricInhPa  (isobaricInhPa) float64 1e+03 950.0 900.0 ... 150.0 100.0
    latitude       (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude      (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time     datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    t              (isobaricInhPa, y, x) float32 ...
    u              (isobaricInhPa, y, x) float32 ...
    v              (isobaricInhPa, y, x) float32 ...
    w              (isobaricInhPa, y, x) float32 ...
    gh             (isobaricInhPa, y, x) float32 ...
    r              (isobaricInhPa, y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:        (isobaricInhPa: 5, y: 65, x: 93)
Coordinates:
    time           datetime64[ns] 2018-09-17
    step           timedelta64[ns] 00:00:00
  * isobaricInhPa  (isobaricInhPa) float64 1e+03 850.0 700.0 500.0 250.0
    latitude       (y, x) float64 ...
    longitude      (y, x) float64 ...
    valid_time     datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    absv           (isobaricInhPa, y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:       (y: 65, x: 93)
Coordinates:
    time          datetime64[ns] 2018-09-17
    step          timedelta64[ns] 00:00:00
    isothermZero  float64 0.0
    latitude      (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude     (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time    datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    gh            (y, x) float32 ...
    r             (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    maxWind     float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    pres        (y, x) float32 ...
    u           (y, x) float32 ...
    v           (y, x) float32 ...
    gh          (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    meanSea     float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    prmsl       (y, x) float32 ...
    mslet       (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:                  (pressureFromGroundLayer: 2, y: 65, x: 93)
Coordinates:
    time                     datetime64[ns] 2018-09-17
    step                     timedelta64[ns] 00:00:00
  * pressureFromGroundLayer  (pressureFromGroundLayer) float64 9e+03 1.8e+04
    latitude                 (y, x) float64 12.19 12.39 12.58 ... 57.49 57.29
    longitude                (y, x) float64 226.5 227.2 227.9 ... 309.6 310.6
    valid_time               datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    cape                     (pressureFromGroundLayer, y, x) float32 ...
    cin                      (pressureFromGroundLayer, y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:                  (pressureFromGroundLayer: 5, y: 65, x: 93)
Coordinates:
    time                     datetime64[ns] 2018-09-17
    step                     timedelta64[ns] 00:00:00
  * pressureFromGroundLayer  (pressureFromGroundLayer) float64 3e+03 ... 1.5e+04
    latitude                 (y, x) float64 12.19 12.39 12.58 ... 57.49 57.29
    longitude                (y, x) float64 226.5 227.2 227.9 ... 309.6 310.6
    valid_time               datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    t                        (pressureFromGroundLayer, y, x) float32 ...
    u                        (pressureFromGroundLayer, y, x) float32 ...
    v                        (pressureFromGroundLayer, y, x) float32 ...
    r                        (pressureFromGroundLayer, y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:                  (y: 65, x: 93)
Coordinates:
    time                     datetime64[ns] 2018-09-17
    step                     timedelta64[ns] 00:00:00
    pressureFromGroundLayer  float64 3e+03
    latitude                 (y, x) float64 ...
    longitude                (y, x) float64 ...
    valid_time               datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    pli                      (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:                  (y: 65, x: 93)
Coordinates:
    time                     datetime64[ns] 2018-09-17
    step                     timedelta64[ns] 00:00:00
    pressureFromGroundLayer  float64 1.8e+04
    latitude                 (y, x) float64 ...
    longitude                (y, x) float64 ...
    valid_time               datetime64[ns] ...
Dimensions without coordinates: y, x
Data variables:
    4lftx                    (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    surface     float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    cape        (y, x) float32 ...
    sp          (y, x) float32 ...
    acpcp       (y, x) float32 ...
    cin         (y, x) float32 ...
    orog        (y, x) float32 ...
    tp          (y, x) float32 ...
    crain       (y, x) float32 ...
    cfrzr       (y, x) float32 ...
    cicep       (y, x) float32 ...
    csnow       (y, x) float32 ...
    gust        (y, x) float32 ...
    hpbl        (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP , <xarray.Dataset>
Dimensions:     (y: 65, x: 93)
Coordinates:
    time        datetime64[ns] 2018-09-17
    step        timedelta64[ns] 00:00:00
    tropopause  float64 0.0
    latitude    (y, x) float64 12.19 12.39 12.58 12.77 ... 57.68 57.49 57.29
    longitude   (y, x) float64 226.5 227.2 227.9 228.7 ... 308.5 309.6 310.6
    valid_time  datetime64[ns] 2018-09-17
Dimensions without coordinates: y, x
Data variables:
    t           (y, x) float32 ...
    u           (y, x) float32 ...
    v           (y, x) float32 ...
    trpp        (y, x) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP...
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP ]Advanced usageWrite supportPlease note that write support is Alpha.Onlyxarray.Dataset’s incanonicalform,
that is, with the coordinates names matching exactly thecfgribcoordinates,
can be saved at the moment:>>> from cfgrib.xarray_to_grib import to_grib
>>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib').sel(number=0)
>>> ds
<xarray.Dataset>
Dimensions:        (time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120)
Coordinates:
    number         int64 0
  * time           (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00
    step           timedelta64[ns] ...
  * isobaricInhPa  (isobaricInhPa) float64 850.0 500.0
  * 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 ... 351.0 354.0 357.0
    valid_time     (time) datetime64[ns] ...
Data variables:
    z              (time, isobaricInhPa, latitude, longitude) float32 ...
    t              (time, isobaricInhPa, 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:                 ...
>>> to_grib(ds, 'out1.grib', grib_keys={'edition': 2})
>>> xr.open_dataset('out1.grib', engine='cfgrib')
<xarray.Dataset>
Dimensions:        (time: 4, isobaricInhPa: 2, latitude: 61, longitude: 120)
Coordinates:
    number         ...
  * time           (time) datetime64[ns] 2017-01-01 ... 2017-01-02T12:00:00
    step           timedelta64[ns] ...
  * isobaricInhPa  (isobaricInhPa) float64 850.0 500.0
  * 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 ... 351.0 354.0 357.0
    valid_time     (time) datetime64[ns] ...
Data variables:
    z              (time, isobaricInhPa, latitude, longitude) float32 ...
    t              (time, isobaricInhPa, latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    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:                 ...Per-variable GRIB keys can be set by setting theattrsvariable with key prefixed byGRIB_,
for example:>>> import numpy as np
>>> import xarray as xr
>>> ds2 = xr.DataArray(
...     np.zeros((5, 6)) + 300.,
...     coords=[
...         np.linspace(90., -90., 5),
...         np.linspace(0., 360., 6, endpoint=False),
...     ],
...     dims=['latitude', 'longitude'],
... ).to_dataset(name='skin_temperature')
>>> ds2.skin_temperature.attrs['GRIB_shortName'] = 'skt'
>>> to_grib(ds2, 'out2.grib')
>>> xr.open_dataset('out2.grib', engine='cfgrib')
<xarray.Dataset>
Dimensions:     (latitude: 5, longitude: 6)
Coordinates:
    time        datetime64[ns] ...
    step        timedelta64[ns] ...
    surface     float64 ...
  * latitude    (latitude) float64 90.0 45.0 0.0 -45.0 -90.0
  * longitude   (longitude) float64 0.0 60.0 120.0 180.0 240.0 300.0
    valid_time  datetime64[ns] ...
Data variables:
    skt         (latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             consensus
    GRIB_centreDescription:  Consensus
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             Consensus
    history:                 ...Dataset / Variable APIThe use ofxarrayis not mandatory and you can access the content of a GRIB file as
an hypercube with the high level API in a Python interpreter:>>> ds = cfgrib.open_file('era5-levels-members.grib')
>>> ds.attributes['GRIB_edition']
1
>>> sorted(ds.dimensions.items())
[('isobaricInhPa', 2), ('latitude', 61), ('longitude', 120), ('number', 10), ('time', 4)]
>>> sorted(ds.variables)
['isobaricInhPa', 'latitude', 'longitude', 'number', 'step', 't', 'time', 'valid_time', 'z']
>>> var = ds.variables['t']
>>> var.dimensions
('number', 'time', 'isobaricInhPa', 'latitude', 'longitude')
>>> var.data[:, :, :, :, :].mean()
262.92133
>>> ds = cfgrib.open_file('era5-levels-members.grib')
>>> ds.attributes['GRIB_edition']
1
>>> sorted(ds.dimensions.items())
[('isobaricInhPa', 2), ('latitude', 61), ('longitude', 120), ('number', 10), ('time', 4)]
>>> sorted(ds.variables)
['isobaricInhPa', 'latitude', 'longitude', 'number', 'step', 't', 'time', 'valid_time', 'z']
>>> var = ds.variables['t']
>>> var.dimensions
('number', 'time', 'isobaricInhPa', 'latitude', 'longitude')
>>> var.data[:, :, :, :, :].mean()
262.92133GRIB index fileBy defaultcfgribsaves the index of the GRIB file to disk appending.idxto the GRIB file name.
Index files are anexperimentaland completely optional feature, feel free to
remove them and try again in case of problems. Index files saving can be disable passing
addingindexpath=''to thebackend_kwargskeyword argument.Project resourcesDevelopmenthttps://github.com/ecmwf/cfgribDownloadhttps://pypi.org/project/cfgribUser supporthttps://stackoverflow.com/search?q=cfgribCode qualityContributingThe main repository is hosted on GitHub,
testing, bug reports and contributions are highly welcomed and appreciated:https://github.com/ecmwf/cfgribPlease see the CONTRIBUTING.rst document for the best way to help.Lead developers:Iain Russell-ECMWFBaudouin Raoult- ECMWFMain contributors:Alessandro Amici-B-OpenAureliana Barghini- B-OpenLeonardo Barcaroli- B-OpenSee also the list ofcontributorswho participated in this project.LicenseCopyright 2017-2021 European Centre for Medium-Range Weather Forecasts (ECMWF).Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.Changelog for cfgrib0.9.10.4 (2023-05-19)added –var-encoding-json (or -v) option to the to_netcdf tool, e.g.cfgrib to_netcdf-v'{"dtype":"float", "scale_factor": 0.1}'-o$OUTFILE $INFILESee#334.fix issue where xarrays derived from Gaussian grids did not have the correct
geometry when written back out as GRIB
See#330.fix issue where open_datasets() could merge different GRIB fields
that have the same data values
See#336.0.9.10.3 (2022-11-24)large reduction in memory leak
See#320.Replaceddistutils.versionbypackaging.versionand
added description and url to the xarray plugin.
See#318.0.9.10.2 (2022-10-04)added –netcdf_kwargs_json option to ‘cfgrib to_netcdf’
See#294.fixed support for GRIB files with alternativeRowScanning=1
See#296.fixed support for missing values
See#313.0.9.10.1 (2022-03-16)Fix failure to read index files.
See#292.Allow backend kwargs to be provided in the to_netcdf executable,
either via a json format string, or a path to a json file via -b.
See#288.Fixed issue where the use of relpath() could cause a problem on Windows.
See#284.Fix passing of pathlib.Path.
See#282.Fixed issue where writing an ensemble number into a GRIB file caused an error.
See#278.0.9.10.0 (2022-01-31)Big internal refactor to add support for a genericFieldsetsimilar to Metview.
See#243.0.9.9.1 (2021-09-29)Fix the plugin interface that was missingextra_coords.
See#231.Fix the crash whenextra_coordsreturn a scalar.
See#238.Improve type-hints.
Needed by#243.0.9.9.0 (2021-04-09)Depend on the ECMWFeccodes python packageto access
the low level ecCodes C-library, dropping all other GRIB decoding options.
See:#95,#14.#204,#147and#141.Many performance improvements during the generation of the index and during data access.
See:#142and#197.filter_by_keysnow can select on all keys known toecCodeswithout the need to
add non default ones toread_keysexplicitly.
See:#187.Include support forengine=”cfgrib”usingxarray0.18+ new backend API.
See:#216.Fixed issue where could not load a GRIB message that has only one grid point.
See:#199.Decodelevelcoordinates as float in all cases, fixed issue with non-int levels.
See:#195.0.9.8.5 (2020-11-11)Simpler and clearer messages in the event of errors.UseECCODES_DIRenvironment variable if present. Ported fromeccodes-pythonby xavierabellan. See:#162.Fix using current ecCodes bindings when settingCFGRIB_USE_EXTERNAL_ECCODES_BINDINGS=1.0.9.8.4 (2020-08-03)Useecmwflibsif present to find theecCodesinstallation.0.9.8.3 (2020-06-25)Added support forindexingDate,indexingTimetime coordinates.lambert_azimuthal_equal_areagrids are now returned as 2D arrays.
See:#119.0.9.8.2 (2020-05-22)Add support for MULTI-FIELD messages used in some GRIB products to storeuandvcomponents of wind (e.g. GFS, NAM, etc). This has been the single
most reported bug incfgribwith two failed attempts at fixing it already.
Let’s see if the third time’s a charm. Please test!
See:#45,#76and#111.0.9.8.1 (2020-03-13)Always open GRIB files in binary mode, by @b8raoult0.9.8.0 (2020-03-12)Add support of experimental pyeccodes low-level driver by @b8raoult0.9.7.7 (2020-01-24)Add support forforecastMonthincf2cdm.translate_coords.0.9.7.6 (2019-12-05)Fix the README.0.9.7.5 (2019-12-05)Deprecateensure_valid_timeand the config optionpreferred_time_dimensionthat
are now better handled viatime_dims.0.9.7.4 (2019-11-22)Add more options totime_dimsforecasts products may be represented as('time', 'verifying_time')or('time', 'forecastMonth').
See:#97.0.9.7.3 (2019-11-04)Add support for selecting the time coordinates to use as dimensions viatime_dims.
Forecasts products may be represented as('time', 'step')(the default),('time', 'valid_time')or('valid_time', 'step').
See:#97.Reduce the in-memory footprint of theFieldIndexand the size of.idxfiles.0.9.7.2 (2019-09-24)Add support to read additional keys from the GRIB files viaread_keys, they
appear in the variableattrsand you canfilter_by_keyson them.
This is a general solution for all issues where users know the name of the additional keys
they are interested in.
See:#89and#101.0.9.7.1 (2019-07-08)Fix a bytes-in-the-place-of-str bug when attempting to write a GRIB on Windows.
See:#91.Honor settingindexpathinopen_datasets,
See:#93.0.9.7 (2019-05-27)Much improvedcfgrib.open_datasetsheuristics now reads many more
heterogeneous GRIB files. The function is now a supported API.
See:#63,#66,#73and#75.Fix conda dependencies on Python 2 only package,
See:#78.0.9.7rc1 (2019-05-14)Drop support for Python 2, in line withxarray0.12.0.
The 0.9.6.x series will be supported long term for Python 2 users.
See:#69.Sync internal ecCodes bindings API to the one in eccodes-python.
See:#81.Source code has been formatted withblack-S-l99.Added initial support for spectral coordinates.0.9.6.2 (2019-04-15)Improve merging of variables into a dataset.
See:#63.0.9.6.1.post1 (2019-03-17)Fix an issue in the README format.0.9.6.1 (2019-03-17)Fixed (for real) MULTI-FIELD messages,
See:#45.Added a protocol version to the index file. Old*.idxfiles must be removed.0.9.6.post1 (2019-03-07)Fix an important typo in the README. See:#64.0.9.6 (2019-02-26)Add support forWindowsby installingecCodesviaconda.
See:#7.Addedconda-forgepackage.
See:#5.0.9.5.7 (2019-02-24)Fixed a serious bug in the computation of the suggestedfilter_by_keysfor non-cubic
GRIB files. As a resultcfgrib.xarray_store.open_datasetswas not finding all the
variables in the files.
See:#54.Fixed a serious bug in variable naming that could drop or at worse mix the values of variables.
Again see:#54.Re-opened#45as the fix was returning wrong data.
Now we are back to dropping all variable in a MULTI-FIELD except the first.0.9.5.6 (2019-02-04)Do not set explicit timezone inunitsto avoid crashing some versions ofxarray.
See:#44.0.9.5.5 (2019-02-02)Enable ecCodes implicit MULTI-FIELD support by default, needed for NAM Products by NCEP.
See:#45.Added support fordepthBelowLandcoordinate.0.9.5.4 (2019-01-25)Add support for buildingvalid_timefrom a badtime-stephypercube.0.9.5.3 (2019-01-25)Also convert isvalid_timecan index all times and steps intranslate_coords.0.9.5.2 (2019-01-24)Setvalid_timeas preferred time dimension for the CDS data model.Fall back to using the genericGRIB2ecCodestemplate when no better option is found.
See:#39.0.9.5.1 (2018-12-27)Fix the crash when usingcf2cdm.translate_coordson datasets with non-dimension coordinates.
See:#41.Added acfgribscript that can translate GRIB to netCDF.
See:#40.0.9.5 (2018-12-20)Drop support forxarrayversions prior tov0.11to reduce complexity.
(This is really only v0.10.9).
See:#32.Declare the data asCF-1.7compliant via theConventionsglobal attribute.
See:#36.Tested larger-than-memory and distributed processing viadaskanddask.distributed.
See:#33.Promote write support viacfgrib.to_gribtoAlpha.
See:#18.Provide thecf2cdm.translate_coordsutility function to translate the coordinates
between CF-compliant data models, defined byout_name,unitsandstore_direction.
See:#24.Providecfgrib.__version__.
See:#31.Raise with a better error message when users attempt to open a file that is not a GRIB.
See:#34.Make 2D grids forrotated_llandrotated_gggridType’s.
See:#35.0.9.4.1 (2018-11-08)Fix formatting for PyPI page.0.9.4 (2018-11-08)Saves one index file per set ofindex_keysin a much more robust way.Refactor CF-encoding and add the newencode_cfoption tobackend_kwargs.
See:#23.Refactor error handling and the option to ignore errors (not well documented yet).
See:#13.Do not crash ongridTypenot fully supported by the installedecCodesSee:#27.Several smaller bug fixes and performance improvements.0.9.3.1 (2018-10-28)Assorted README fixes, in particular advertise index file support as alpha.0.9.3 (2018-10-28)Big performance improvement: add alpha support to save to and read from disk
the GRIB index produced by the full-file scan at the first open.
See:#20.0.9.2 (2018-10-22)Rename coordinateair_pressuretoisobaricInhPafor consistency
with all other verticallevelcoordinates.
See:#25.0.9.1.post1 (2018-10-19)Fix PyPI description.0.9.1 (2018-10-19)Change the usage ofcfgrib.open_datasetto allign it withxarray.open_dataset,
in particularfilter_by_keymust be added into thebackend_kwargsdictionary.
See:#21.0.9.0 (2018-10-14)Beta release with read support.