Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

 

Section
Column

What are GRIB files

Excerpt

The instructions in this This article show ways different approaches to convert GRIB to netCDF for use in other analysis and visualisation software.

GRIB files are the standard file format used at ECMWF. Files retrieved from the MARS archive, or from the public data server for reanalysis products such as ERA-Interim are all GRIB format.   GRIB is a WMO standard and consists of GRIB Edition 1 and Edition 2.

The OpenIFS/IFS models output fields in GRIB format. These files are a mix of GRIB-1 & GRIB-2 format messages, the multi-level fields are encoded as GRIB-2, whereas surface fields are GRIB-1. GRIB files can also contain multiple vertical coordinates: pressure levels, model levels, sub-surface levels etc. This can cause a problem with some 3rd party tools, as the same GRIB variable code is used for each axis. The instructions below show how to work around this by splitting the file to separate fields on different vertical axes.

OpenIFS model output

The OpenIFS model outputs two types of files: those beginning with ICMSH contain fields represented as spherical harmonics, those that begin ICMGG contain Considering the output from OpenIFS, the ICMGG files contain all the gridpoint fields.  The ICMSH files are the spherical harmonics of the wind fields, pressure and temperature . See and require a spectral transform to convert to gridded data. Also see controlling the OpenIFS output for more details of output fields and options.

Grib-api tools

Retrieving data from MARS archive

Please note that grib-api is being phased out in preference for ecCodes. All the grib_* commands shown below are still available with ecCodes.

grib_to_netcdf

This command will convert one or more GRIB files to netCDF and is available with the ECMWF ecCodes/grib-api software (for grib-api, versions above 1.11.0 are recommended).

For more details please see: grib_to_netcdf description.

if using the MARS archive (apps.ecmwf.int) (e.g. for reanalysis products such as ERA-Interim/ERA-5), it is possible to download the files in netCDF format as well as GRIB.

ecCodes/grib-api tools

Info

Please note that grib-api is being phased out in preference for ecCodes. All the grib_* commands shown below are still available with ecCodes.

grib_to_netcdf

This command will convert one or more GRIB files to netCDF and is available with the ECMWF ecCodes/grib-api software (for grib-api, versions above 1.11.0 are recommended). For more details please see: grib_to_netcdf description.

grib_to_netcdf grib_to_netcdf only works correctly when a single level coordinate is present in the GRIB file. Often the model output files have fields on multiple level types (ie. hybrid model levels and pressure levels).

Code Block
titleSplit How to split GRIB file into separate level types
grib_copy ICMGGftkm+001440 ICMGG_[typeOfLevel].grb

In this example, the GRIB model output file ICMGGftkm+001440 contains a number of different model level types. The special square bracket "[ ]" syntax is recognised by grib_copy (and other grib commands such as grib_filter) and can contain any valid GRIB key.

This example will copy the original file, separating the level types into their own file: ICMGG_hybrid.grb, ICMGG_isobaricInhPA.grb, ICMGG_surface ...and so on.

grib_to_netcdf can then be used on the individual files:

Code Block
titleConvert GRIB file to netCDF
grib_to_netcdf ICMGG_hybrid.grb -o ICMGG_hybrid.nc

To convert from vorticity and divergence to wind u and v, please see CDO instructions below.

Note that By default, grib_to_netcdf does not perform any regridding. If the fields use a reduced Gaussian latitude grid, they will not be converted to a regular grid. Use CDO instead to do this.

Column
width320px
Panel
bgColorwhite
titleBGColorlightlightgrey
titleOn this page

Table of Contents
indent10px

Info
The MetView application is available for analysis and visualisation of OpenIFS output.
Please see 'Using Metview with OpenIFS' for more details.
Panel
titleSee also...

ecCodes - grib tools

grib-api - deprecated grib tools

MARS user documentation

NCAR command language (NCL)

NCL provides a tool to convert both GRIB-1 & GRIB-2 to netCDF called ncl_convert2nc.

will pack the data into scaled integers with an offset to optimize space.

If you prefer data stored as floats then use:

Code Block
languagebash
titleConvert GRIB to netCDF storing values as floats rather than scaled integers...
grib_to_netcdf -D NC_FLOAT ICMGG_hybrid.grb -o ICMGG_hybrid.nc

To convert from vorticity and divergence to wind u and v, please see CDO instructions below.

Note that grib_to_netcdf does not do any regridding. If the fields use a reduced Gaussian latitude grid, they will not be converted to a regular grid. Use CDO instead to do this as described below.

Column
width320px
Panel
bgColorwhite
titleBGColorlightlightgrey
titleOn this page

Table of Contents
indent10px

Info
The MetView application is available for analysis and visualisation of OpenIFS output and will both convert spectral to gridded parameters and plot reduced Gaussian grid directly.
Please see 'Using Metview with OpenIFS' for more details.
Panel
titleSee also...

ecCodes - grib tools

MARS user documentation

NCAR command language (NCL)

NCL provides a tool to convert both GRIB-1 & GRIB-2 to netCDF called ncl_convert2nc.

NCL NCL example scripts to convert to netCDF are also available.

...

CDO supports GRIB-2 but needs either the GRIBgrib_API api or ecCodes library from ECMWF to be included. This is necessary in order to work correctly with OpenIFS model output.

...

CDO can be used to interpolate from the reduced Gaussian grid to a regular Gaussian grid (IFS/OpenIFS resolutions T21 & T42 use a regular Gaussian grid):

Code Block
titleConvert to regular Gaussian grid
 cdo -R copy <input grib> <output grib>
Info

If the -R option does not work, use 'cdo setgridtype,regular' instead.

If this step does not work, see workarounds below.

Steps to convert GRIB to netCDF

...

This will ensure the following steps work correctly.:

Code Block
titleExample: Separate multiple z axes into individual files
cdo splitzaxis ICMSHg4a4+000000.grb ICMSHg4a4+000000_split
ls ICMSH*split*
ICMSHg4a4+000000_split01.grb
ICMSHg4a4+000000_split02.grb
ICMSHg4a4+000000_split03.grb
ICMSHg4a4+000000_split04.grb

In this example, the different files might contain: '01' - fields on pressure levels, '02' - fields on model levels, '03' - hybrid levels, '04' - surface and so on. The number of files created depends on the number of different types of levels. Use the command 'grib_ls' to inspect the contents of each split file.

...

Another way to split the file would be using the grib_copy command from the ecCodes (or grib-_api (or ecCodes) library software as shown above:

Code Block
grib_copy ICMSHg4a4+000000.grb ICMSHg4a4+000000_[typeOfLevel].grb

In this case 'typeOfLevel' is a GRIB key. The square brackets is a special syntax to the  the grib_copy command. This approach works with any GRIB key.

...

CDO supports two options for converting spectral to gridpoint data:

  • sp2gp    - converts spectral to a quadratic Gaussian grid.
  • sp2gpl   - converts spectral to a linear Gaussian grid, appropriate for IFS data.
  • sp2gp    - converts spectral to a quadratic Gaussian grid (only use this for T21/42 resolutions)

Both Both produce a regular Gaussian grid with equal number of longitudes on each latitude row. Remember that a Gaussian grid has irregularly spaced latitudes (but nearly regular). For more details please see the description of these operators on the CDO homepage.

Convert the resulting output files to netcdf

...

Code Block
titleConvert GRIB file to netCDF file
cdo -f nc copy <input grib> <output netcdf>

...

Code Block
titleSample script to convert OpenIFS GRIB output to netCDF
for i in 000 024 048 072
do

cdo -R copy ICMGGfpp4+000${i}.grb ICMGGfpp4+000${i}_R.grb
cdo splitzaxis ICMGGfpp4+000${i}_R.grb ICMGGfpp4+000${i}
cdo -f nc copy ICMGGfpp4+000${i}01.grb ICMGGfpp4+000${i}_PL_GP.nc		# pressure level gridpoint fields
cdo -f nc copy ICMGGfpp4+000${i}02.grb ICMGGfpp4+000${i}_ML_GP.nc       # model level gridpoint fields

cdo splitzaxis ICMSHfpp4+000${i}.grb ICMSHfpp4+000${i}
cdo -f nc copy -sp2gpl ICMSHfpp4+000${i}01.grb ICMSHfpp4+000${i}01_PL_GP.grb	# spectral fields on press levels transformed to gridpoint
cdo -f nc copy -sp2gpl ICMSHfpp4+000${i}03.grb ICMSHfpp4+000${i}03_ML_GP.grb	# spectral fields on model levels transformed to gridpoint
cdo -f nc copy -sp2gpl ICMSHfpp4+000${i}04.grb ICMSHfpp4+000${i}04_surf_GP.grb	# spectral fields on surface levels transformed to gridpoint

done

Possible problems and solutions

 

CDO does not understand GRIB-2 messages

If you are using an old version of CDO, it may not understand GRIB-2 messages. CDO must be compiled with either grib-api or ecCodes library in order to handle GRIB-2.

A workaround is to use the grib_set command (from the grib_api/ecCodes installation) to change the GRIB edition number for all messages.

Code Block
titleWorkaround if CDO does not understand GRIB2
grib_set -s editionNumber=1 <input grib2 file> <output grib1 file>
_surf_GP.grb	# spectral fields on surface levels transformed to gridpoint

done

Possible problems and solutions

 

cdo -R option does not work with GRIB-2 fields to convert to regular grid

CDO's -R option, to convert from reduced Gaussian grid to regular Gaussian grid, only works with GRIB 1 format (cgribex only) (see cdo man page). This is a problem as multi-level output from OpenIFS is encoded as GRIB-2 data.

A workaround is to temporarily change the edition number to 1. Use the grib_set command (from ecCodes or grib_api) to change the GRIB edition number for all messages:

Code Block
titleWorkaround to convert GRIB 2 to regular grid
grib_set -s editionNumber=1  in.grb  in_1.grb
cdo -R copy  in_1.grb  in_1_R.grb
grib_set -s editionNumber=2  in_1_R.grb  in_2_R.grb

This does not actually convert the message to GRIB-1, it only changes the editionNumber. Any GRIB-2 specific elements are unaltered.

It's recommended that the editionNumber is changed back to 2 to ensure that the parameter names remain correct in any subsequent cdo / grib commands.

Workaround if using more than 128 levels

The workaround above will fail if the grib file contains field with more than 128 levels. This is because there are not enough bits available in the GRIB-1 parameter to encode the total number of model half levels (which would exceed 255).

In this case, the 'pv' GRIB parameter causes the problem (nothing to do with potential vorticity!). This is an array holding the half level values of the model's A & B coefficients that define the location of the levels.

The workaround above can be extended to delete this array while setting the edition number to 1, like this:

Code Block
grib_set -s deletePV=1,editionNumber=1  in.grb  in_1.grb
cdo -R copy  in_1.grb  in_1_R.grb
grib_set -s editionNumber=2  in_1_R.grb  in_2_R.grb

The deletePV option is known as a 'concept' rather than a parameter contained in the GRIB file itself. It ensures the PV array is deleted correctly by grib_set. Now the cdo regridding will work correctly, but remember to reset the editionNumber back to 2.

The A & B half level coefficients can be recovered from the original file by:

Code Block
cdo vct in.grb > pv.txt
Note
Note this will only work if you are using less than 128 model levels as it does not actually convert the message to GRIB-1 if there are any GRIB-2 specific elements in the messages.

 

Parameter names are lost for GRIB-1 fields

...

Warning

Do not use this option for GRIB-2 fields. It sets the GRIB table default to be specific to the GRIB-1 ECMWF tables. If problems persist, we recommend using grib_to_netcdf to convert to netCDF.

 

 

Acknowledgements

Thanks to Paul Dando of User Support for help with the contents of this page.

...