A curated list of open technology projects to sustain a stable climate, energy supply, biodiversity and natural resources.

cfgrib

A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes.
https://github.com/ecmwf/cfgrib

Category: Climate Change
Sub Category: Climate Data Processing and Analysis

Keywords

grib meteorology

Keywords from Contributors

weather bufr closember decoding encoding convolutional-neural-network wmo earth-science climate conda

Last synced: about 18 hours ago
JSON representation

Repository metadata

A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes

README.rst

          cfgrib: A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes
======================================================================================================================

.. image:: https://img.shields.io/pypi/v/cfgrib.svg
   :target: https://pypi.python.org/pypi/cfgrib/

Python interface to map GRIB files to the
`Unidata's Common Data Model v4 `_
following the `CF Conventions `_.
The high level API is designed to support a GRIB engine for `xarray `_
and it is inspired by `netCDF4-python `_
and `h5netcdf `_.
Low level access and decoding is performed via the
`ECMWF ecCodes library `_ and
the `eccodes python package `_.

Features with development status **Beta**:

- enables the ``engine='cfgrib'`` option to read GRIB files with *xarray*,
- reads most GRIB 1 and 2 files including heterogeneous ones with ``cfgrib.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 wherever *eccodes-python* does: *Linux*, *MacOS* and *Windows*
- conda-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 via *xarray* and *dask*,
- 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 generic *Fieldset* interface as described in `ADVANCED_USAGE.rst`.

Work in progress:

- **Beta** install a ``cfgrib`` utility that can convert a GRIB file ``to_netcdf``
  with a optional conversion to a specific coordinates data model,
  see `#40 `_.
- **Alpha/Broken** support writing carefully-crafted ``xarray.Dataset``'s to a GRIB1 or GRIB2 file,
  see the *Advanced write usage* section below, `#18 `_
  and `#156 `_.

Limitations:

- relies on *ecCodes* for the CF attributes of the data variables,
- relies on *ecCodes* for anything related to coordinate systems / ``gridType``,
  see `#28 `_.


Installation
============

The easiest way to install *cfgrib* and all its binary dependencies is via `Conda `_::

    $ conda install -c conda-forge cfgrib

alternatively, if you install the binary dependencies yourself, you can install the
Python package from *PyPI* with::

    $ pip install cfgrib


Binary dependencies
-------------------

*cfgrib* depends on the `eccodes python package `_
to access the ECMWF *ecCodes* binary library,
when not using *conda* please follow the *System dependencies* section 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.


Usage
=====

First, you need a well-formed GRIB file, if you don't have one at hand you can download our
`ERA5 on pressure levels sample `_::

    $ wget https://get.ecmwf.int/repository/test-data/cfgrib/era5-levels-members.grib


Read-only *xarray* GRIB engine
------------------------------

Most of *cfgrib* users want to open a GRIB file as a ``xarray.Dataset`` and
need to have *xarray* installed::

    $ pip install xarray

In a Python interpreter try:

.. code-block:: python

    >>> import xarray as xr
    >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib')
    >>> ds
    
    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:                 ...

The *cfgrib* ``engine`` supports all read-only features of *xarray* like:

* merge the content of several GRIB files into a single dataset using ``xarray.open_mfdataset``,
* work with larger-than-memory datasets with `dask `_,
* allow distributed processing with `dask.distributed `_.


Read arbitrary GRIB keys
------------------------

By default *cfgrib* reads a limited set of ecCodes recognised *keys* from the GRIB files
and exposes them as ``Dataset`` or ``DataArray`` attributes with the ``GRIB_`` prefix.
It is possible to have *cfgrib* read additional keys to the attributes by adding the
``read_keys`` dictionary key to the ``backend_kwargs`` with values the list of desired GRIB keys:

.. code-block:: python

    >>> 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 model
--------------------------------

Contrary to netCDF the GRIB data format is not self-describing and several details of the mapping
to the *Unidata Common Data Model* are arbitrarily set by the software components decoding the format.
Details like names and units of the coordinates are particularly important because
*xarray* broadcast and selection rules depend on them.
``cf2cfm`` is a small coordinate translation module distributed with *cfgrib* that make it easy to
translate CF compliant coordinates, like the one provided by *cfgrib*, to a user-defined
custom data model with set ``out_name``, ``units`` and ``stored_direction``.

For example to translate a *cfgrib* styled ``xr.Dataset`` to the classic *ECMWF* coordinate
naming conventions you can:

.. code-block:: python

    >>> import cf2cdm
    >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib')
    >>> cf2cdm.translate_coords(ds, cf2cdm.ECMWF)
    
    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:

.. code-block:: python

    >>> import cf2cdm
    >>> cf2cdm.translate_coords(ds, cf2cdm.CDS)
    
    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 files
-------------------------------

``xr.open_dataset`` can open a GRIB file only if all the messages
with the same ``shortName`` can be represented as a single hypercube.
For example, a variable ``t`` cannot have both ``isobaricInhPa`` and ``hybrid`` ``typeOfLevel``'s,
as this would result in multiple hypercubes for the same variable.
Opening a non-conformant GRIB file will fail with a ``ValueError: multiple values for unique key...``
error message, see `#2 `_.

Furthermore if different variables depend on the same coordinate, for example ``step``,
the values of the coordinate must match exactly.
For example, if variables ``t`` and ``z`` share the same ``step`` coordinate,
they must both have exactly the same set of steps.
Opening a non-conformant GRIB file will fail with a ``ValueError: key present and new value is different...``
error message, see `#13 `_.

In most cases you can handle complex GRIB files containing heterogeneous messages by passing
the ``filter_by_keys`` key in ``backend_kwargs`` to select which GRIB messages belong to a
well formed set of hypercubes.

For example to open
`US National Weather Service complex GRIB2 files `_
you can use:

.. code-block:: python

    >>> xr.open_dataset('nam.t00z.awp21100.tm00.grib2', engine='cfgrib',
    ...     backend_kwargs={'filter_by_keys': {'typeOfLevel': 'surface'}})
    
    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 ...
        unknown     (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}})
    
    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 filtering
-------------------

*cfgrib* also provides a function that automates the selection of appropriate ``filter_by_keys``
and returns a list of all valid ``xarray.Dataset``'s in the GRIB file.

.. code-block:: python

    >>> import cfgrib
    >>> cfgrib.open_datasets('nam.t00z.awp21100.tm00.grib2')
    [
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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, 
    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:
        unknown     (y, x) float32 ...
        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 ...
    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, 
    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 usage
==============

Write support
=============

**Please note that write support is Alpha.**
Only ``xarray.Dataset``'s in *canonical* form,
that is, with the coordinates names matching exactly the *cfgrib* coordinates,
can be saved at the moment:

.. code-block:: python

    >>> from cfgrib.xarray_to_grib import to_grib
    >>> ds = xr.open_dataset('era5-levels-members.grib', engine='cfgrib').sel(number=0)
    >>> ds
    
    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')
    
    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 the ``attrs`` variable with key prefixed by ``GRIB_``,
for example:

.. code-block:: python

    >>> 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')
    
    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 API
----------------------

The use of *xarray* is not mandatory and you can access the content of a GRIB file as
an hypercube with the high level API in a Python interpreter:

.. code-block:: python

    >>> 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.92133


GRIB index file
---------------

By default *cfgrib* saves the index of the GRIB file to disk appending ``.idx``
to the GRIB file name.
Index files are an **experimental** and completely optional feature, feel free to
remove them and try again in case of problems. Index files saving can be disable passing
adding ``indexpath=''`` to the ``backend_kwargs`` keyword argument.


Geographic Coordinate Caching
-----------------------------

By default, *cfgrib* caches computed geography coordinates for each record in the GRIB
file when opening a dataset, which significantly speeds up dataset creation.
This cache can theoretically grow unboundedly in memory in long-lived
applications which read many different grid types. Should it be necessary,
caching can be disabled by passing `backend_kwargs=dict(cache_geo_coords=False)`
to `xarray.open_dataset()`, `cfgrib.open_dataset()`, or
`cfgrib.open_datasets()`.

Project resources
=================

============= =========================================================
Development   https://github.com/ecmwf/cfgrib
Download      https://pypi.org/project/cfgrib
User support  https://stackoverflow.com/search?q=cfgrib
Code quality  .. image:: https://codecov.io/gh/ecmwf/cfgrib/branch/master/graph/badge.svg
                :target: https://codecov.io/gh/ecmwf/cfgrib
                :alt: Coverage status on Codecov
============= =========================================================


Contributing
============

The main repository is hosted on GitHub,
testing, bug reports and contributions are highly welcomed and appreciated:

https://github.com/ecmwf/cfgrib

Please see the CONTRIBUTING.rst document for the best way to help.

Lead developers:

- `Iain Russell `_ - `ECMWF `_
- `Baudouin Raoult `_ - ECMWF

Main contributors:

- `Alessandro Amici `_ - `B-Open `_
- `Aureliana Barghini `_ - B-Open
- `Leonardo Barcaroli `_ - B-Open

See also the list of `contributors `_ who participated in this project.


License
=======

Copyright 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.

        

Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 4 days ago

Total Commits: 1,503
Total Committers: 26
Avg Commits per committer: 57.808
Development Distribution Score (DDS): 0.162

Commits in past year: 50
Committers in past year: 9
Avg Commits per committer in past year: 5.556
Development Distribution Score (DDS) in past year: 0.66

Name Email Commits
Alessandro Amici a****i@b****u 1260
Iain Russell I****l@e****t 67
Aureliana Barghini a****i@b****u 59
EddyCMWF E****t@e****t 28
Baudouin Raoult b****t@e****t 26
shahramn s****m@e****t 17
Levi Cowan m****t@g****m 12
Michael Niklas m****s@g****m 6
Dusan Figala d****a@p****m 4
Sandor Kertesz S****z@e****t 3
Metamess m****s@g****m 3
Mike Taves m****s@g****m 2
Ryan May r****y@u****u 2
Xavier Abellan Ecija x****n@e****t 2
Ben Mares s****1@t****m 1
Brian Blaylock b****k 1
Jakub Recman j****b@o****m 1
JarnoRFB r****e@w****e 1
Joachim Meyer j****r@u****u 1
Juniper Tyree 5****r 1
Maxime Warnier m****x@g****m 1
Ray Bell r****0@g****m 1
Stephan Hoyer s****r@g****m 1
StephanSiemen s****n@g****m 1
ilopezgp i****p@g****m 1
xufanglu 3****v 1

Committer domains:


Issue and Pull Request metadata

Last synced: 2 days ago

Total issues: 267
Total pull requests: 156
Average time to close issues: 3 months
Average time to close pull requests: 13 days
Total issue authors: 160
Total pull request authors: 34
Average comments per issue: 3.38
Average comments per pull request: 1.76
Merged pull request: 128
Bot issues: 0
Bot pull requests: 1

Past year issues: 20
Past year pull requests: 25
Past year average time to close issues: 28 days
Past year average time to close pull requests: 9 days
Past year issue authors: 19
Past year pull request authors: 9
Past year average comments per issue: 0.95
Past year average comments per pull request: 2.08
Past year merged pull request: 22
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/ecmwf/cfgrib

Top Issue Authors

  • alexamici (38)
  • meteoDaniel (9)
  • blaylockbk (9)
  • matteodefelice (7)
  • Metamess (5)
  • abakleriche (5)
  • guidocioni (5)
  • EddyCMWF (4)
  • iainrussell (4)
  • jsillin (3)
  • lassiterdc (3)
  • enyfeo (3)
  • jdassink (3)
  • jodemaey (3)
  • jsSvensson (3)

Top Pull Request Authors

  • alexamici (72)
  • shahramn (10)
  • EddyCMWF (10)
  • aurghs (9)
  • iainrussell (9)
  • b8raoult (8)
  • meridionaljet (3)
  • Metamess (3)
  • steph-ben (3)
  • mwtoews (2)
  • sandorkertesz (2)
  • StephanSiemen (2)
  • juntyr (2)
  • recmanj (1)
  • emfdavid (1)

Top Issue Labels

  • bug (55)
  • enhancement (37)
  • works for me (5)
  • prioriy - low (5)
  • won't fix (5)
  • duplicate (5)
  • distribution (4)
  • documentation (4)
  • question (2)
  • invalid (1)

Top Pull Request Labels

  • contributor (12)
  • approved-for-ci (7)
  • dependencies (1)

Package metadata

pypi.org: cfgrib

Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes.

  • Homepage: https://github.com/ecmwf/cfgrib
  • Documentation: https://cfgrib.readthedocs.io/
  • Licenses: Apache License Version 2.0
  • Latest release: 0.9.7 (published almost 6 years ago)
  • Last Synced: 2025-04-25T12:09:51.199Z (2 days ago)
  • Versions: 60
  • Dependent Packages: 49
  • Dependent Repositories: 124
  • Downloads: 252,475 Last month
  • Docker Downloads: 1,289
  • Rankings:
    • Dependent packages count: 0.445%
    • Downloads: 1.005%
    • Dependent repos count: 1.353%
    • Docker downloads count: 1.427%
    • Average: 2.145%
    • Stargazers count: 3.37%
    • Forks count: 5.268%
  • Maintainers (1)
spack.io: py-cfgrib

Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes.

  • Homepage: https://github.com/ecmwf/cfgrib
  • Licenses: []
  • Latest release: 0.9.14.1 (published 6 months ago)
  • Last Synced: 2025-04-25T12:09:52.014Z (2 days ago)
  • Versions: 4
  • Dependent Packages: 1
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Stargazers count: 12.005%
    • Average: 13.422%
    • Forks count: 13.617%
    • Dependent packages count: 28.067%
  • Maintainers (1)
conda-forge.org: cfgrib

  • Homepage: https://github.com/ecmwf/cfgrib
  • Licenses: Apache-2.0
  • Latest release: 0.9.7 (published almost 6 years ago)
  • Last Synced: 2025-04-25T12:10:41.149Z (2 days ago)
  • Versions: 22
  • Dependent Packages: 10
  • Dependent Repositories: 100
  • Rankings:
    • Dependent repos count: 3.404%
    • Dependent packages count: 5.9%
    • Average: 13.694%
    • Stargazers count: 21.847%
    • Forks count: 23.624%

Dependencies

ci/requirements-dev.txt pypi
  • IPython *
  • check-manifest *
  • detox *
  • matplotlib *
  • notebook *
  • pip-tools *
  • pyroma *
  • pytest-mypy *
  • setuptools *
  • tox *
  • tox-pyenv *
  • wheel *
  • zest.releaser *
ci/requirements-docs.in pypi
  • Sphinx *
  • pytest-runner *
  • xarray *
ci/requirements-tests.in pypi
  • dask *
  • pytest *
  • pytest-cov *
  • pytest-flakes *
  • pytest-mccabe *
  • pytest-pep8 *
  • pytest-runner *
  • scipy *
  • xarray *
ci/requirements-tests.txt pypi
  • apipkg ==1.5
  • attrs ==19.3.0
  • cffi ==1.14.0
  • click ==7.1.2
  • coverage ==5.1
  • dask ==2.16.0
  • execnet ==1.7.1
  • importlib-metadata ==1.6.0
  • mccabe ==0.6.1
  • more-itertools ==8.2.0
  • numpy ==1.18.4
  • packaging ==20.3
  • pandas ==1.0.3
  • pep8 ==1.7.1
  • pluggy ==0.13.1
  • py ==1.8.1
  • pycparser ==2.20
  • pyflakes ==2.2.0
  • pyparsing ==2.4.7
  • pytest ==5.4.2
  • pytest-cache ==1.0
  • pytest-cov ==2.8.1
  • pytest-flakes ==4.0.0
  • pytest-mccabe ==1.0
  • pytest-pep8 ==1.0.6
  • pytest-runner ==5.2
  • python-dateutil ==2.8.1
  • pytz ==2020.1
  • scipy ==1.4.1
  • six ==1.14.0
  • toolz ==0.10.0
  • wcwidth ==0.1.9
  • xarray ==0.15.1
  • zipp ==3.1.0
setup.py pypi
  • attrs >=19.2
.github/workflows/weekly.yml actions
  • actions/checkout v2 composite
  • actions/upload-artifact v2 composite
  • conda-incubator/setup-miniconda v2 composite
ci/requirements-docs.txt pypi
  • alabaster ==0.7.12
  • attrs ==19.3.0
  • babel ==2.9.1
  • certifi ==2020.4.5.1
  • cffi ==1.14.0
  • chardet ==3.0.4
  • click ==7.1.2
  • docutils ==0.16
  • idna ==2.9
  • imagesize ==1.2.0
  • jinja2 ==2.11.2
  • markupsafe ==1.1.1
  • numpy ==1.18.4
  • packaging ==20.3
  • pandas ==1.0.3
  • pycparser ==2.20
  • pygments ==2.6.1
  • pyparsing ==2.4.7
  • pytest-runner ==5.2
  • python-dateutil ==2.8.1
  • pytz ==2020.1
  • requests ==2.23.0
  • six ==1.14.0
  • snowballstemmer ==2.0.0
  • sphinx ==3.0.3
  • sphinxcontrib-applehelp ==1.0.2
  • sphinxcontrib-devhelp ==1.0.2
  • sphinxcontrib-htmlhelp ==1.0.3
  • sphinxcontrib-jsmath ==1.0.1
  • sphinxcontrib-qthelp ==1.0.3
  • sphinxcontrib-serializinghtml ==1.1.4
  • urllib3 ==1.26.5
  • xarray ==0.15.1
.github/workflows/ci.yml actions
  • ecmwf-actions/dispatch-private-downstream-ci v1 composite
pyproject.toml pypi

Score: 21.97631109130966