Using the
...
GRIB tools
The GRIB tools are part of the ECMWF GRIB API Library. They are a set of command line programs for interactive and batch decoding and processing of GRIB data
They provide ready and tested solutions to the most common processing of GRIB data and work with both GRIB edition 1 and GRIB edition 2.
All of the tools use a common syntax:
Code Block |
---|
grib_<tool> [options] grib_file grib_file … [output_grib] |
There are tools for getting information about the GRIB API installation and the keys available
- grib_info, grib_keys
There are tools to inspect the content of and compare GRIB messages
- grib_ls, grib_dump, grib_get, grib_get_data, grib_compare
There are tools for counting and copying some messages
- grib_count, grib_copy
There is a tool for making changes to the content of a GRIB message
- grib_set
There is an advanced tool with a macro language provided via a rules file which allows for more sophisticated manipulation of GRIB files
- grib_filter
There is a tool for converting from GRIB to netCDF
- grib_to_netcdf
You will find some example data in $SCRATCH/ectrain/trx/openifs or you can use output from one of your own experiments.
Let's play !
Getting help with the tools
...
To copy all of the pressure level data at 1000 hPa from a number of separate files and store the output in a single file, try:
Code Block grib_copy -w level=1000 ICMSHepc8* lev1000.grib
Note Remember that you can check the output file with grib_ls to see the result !
To do the same with only temperature field and to store in descending forecast step order, try:
Code Block grib_copy -w level=1000,shortName=t -B "stepRange:i desc" ICMSHepc8* lev1000_ordered.grib
Now let's gather all of the temperature fields and store in order of decreasing level and decreasing step:
Code Block grib_copy -w shortName=t -B "level:i desc, stepRange:i desc" ICMSHepc8* all_t_ordered.grib
Note It is important when using the "order by" (-B) option to specify the key type with, e.g., "level:i" to avoid unexpected (unwanted ?) results. Remember that the keys key values are strings by default. Usually, . we want a numeric order.
Key names can be used to specify the output file names. Try:
Code Block grib_copy ICMGGepc8* "[shortName]_[level].grib[edition]"
This provides a very convenient way to filter GRIB messages into separate files.
...
Suppose we want to use the soil temperature on level 1 to provide a sea-surface temperature. We can change the header information for this parameter and set it to sea-surface temperature
Code Block grib_set -S -s shortName=sst -w shortName=stl1 ICMGGepc8+* sst.grib
Note By default, all parameters are copied to the output file. The 'strict' option (-S) writes only those messages that are changed to the output file. To see the difference, try running grib_set again but without the -S option !
This is still not encoded like a true sea-surface temperature. To do this, we need also to change the typeOfLevel:
Code Block grib_set -S -s shortName=sst,typeOfLevel=surface -w shortName=stl1 ICMGGepc8+* sst.grib
An offset can be added to all data values:
Code Block grib_set -S -s offsetValuesBy=-273.15 -w shortName=stl1 ICMGGepc8+* stl1_in_degC.grib
Note Remember: use "grib_ls -n statistics" to make a quick check of the values in output file.
The values can be multiplied by a constant factor:
Code Block grib_set -S -s scaleValuesBy=0.102 -w shortName=z ICMGGepc8+* gh.grib
Some values are read only and cannot be changed. Try:
Code Block grib_set -S -s scaleValuesBy=0.000001,units="kg cm**-2" -w shortName=tcw ICMGGepc8+000000 tcm_kgcm-2.grib
The units key cannot be set as it is read only. the only way to change the units key is by changing the parameter (either the shortName or paramId), assuming there is a suitable parameter defined to express Total column water in units of kg/cm2.
All data values can be set to a constant
Code Block grib_set -S -d 1.0 -w shortName=tcw ICMGGepc8+000000 out.grib
Note The resulting file is small as it contains only the reference value. This is the method used to create the ifs_sample files used to provide 'template' GRIBs for model output.
Key names can be used to specify the output file names. This works in the same way as for grib_copy. Try:
Code Block grib_set -S -s scaleValuesBy=0.102,shortName=gh -w shortName=z ICMGGepc8+* "[shortName]_[dataDate]_[dataTime]_[stepRange].grib"
Warning |
---|
grib_set is very powerful but it cannot be used to:
|
Resources and further reading
- The WMO FM 92 GRIB Manuals can be obtained from www.wmo.int/pages/prog/www/WMOCodes.html
- The ECMWF GRIB API manual is available atat https://software.ecmwf.int/wiki/display/GRIB/Home
- ECMWF parameter database: http://old.ecmwf.int/publications/manuals/d/gribapi/param
- GRIB 1 keys http://old.ecmwf.int/publications/manuals/d/gribapi/fm92/grib1/
- GRIB 2 keys http://old.ecmwf.int/publications/manuals/d/gribapi/fm92/grib2/
- Edition independent keys http://old.ecmwf.int/publications/manuals/d/gribapi/keys/
- The GRIB API Tools are documented at GRIB tools
- Details of the GRIB API Fortran 90 interface:Fortran package grib_api
- Example Python scripts and Fortran 90 and C programs can be found at GRIB API examples
- ECMWF GRIB API training course material: GRIB API: Library and Tools
...