Versions Compared

Key

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

grib_compare examples

...

The default behaviour for grib_compare without any option is to perform a bit by bit comparison of the two messages. If the messages are found to be bitwise different then grib_compare switches to a "key based" mode to find out which coded keys are different. To see how grib_compare works we first set the shortName=2d (2 metre dew point temperature) in the file regular_latlon_surface.grib1

Code Block
 
> grib_set -s shortName=2d regular_latlon_surface.grib1 2d.grib1

grib_copy examples

...

To copy only the pressure levels from a file

Code Block

>  grib_copy -w levtype=pl ../data/tigge_pf_ecmwf.grib2 out.grib

...

To copy only the fields that are not on pressure levels from a file

Code Block

>  grib_copy -w levtype!=pl ../data/tigge_pf_ecmwf.grib2 out.grib

...

To copy only the first three fields from a file

Code Block

>  grib_copy -w count=1/2/3 ../data/tigge_pf_ecmwf.grib2 out.grib

...

A grib_file with multi field messages can be converted in single field messages with a simple grib_copy.

Code Block

>  grib_copy multi.grib simple.grib

...

Use the square brackets to insert the value of a key in the name of the output file (This is a good way to split a large GRIB file)

Code Block

>  grib_copy in.grib 'out_[shortName].grib'
Note: we need to quote the name of the output so the shell does not interpret the square brackets

...

To copy fields whose typeOfLevel is either 'surface' or 'meanSea'

Code Block

>  grib_copy -w typeOfLevel=surface/meanSea orig.grib out.grib

...

To copy selected fields and apply sorting (sorted by level in ascending order)

Code Block

>  grib_copy -w typeOfLevel=heightAboveGround -B'level:i asc' tigge_af_ecmwf.grib2 out.grib
Note: we need to specify the ':i' to get a numerical sort. By default values are sorted as strings so a level of 100 would come before 20!

grib_dump examples

...

To dump in a WMO documentation style with hexadecimal octet values (-H).

Code Block

> grib_dump -OH ../data/reduced_gaussian_model_level.grib1

...

To add key aliases and type information.

Code Block

> grib_dump -OtaH ../data/reduced_gaussian_model_level.grib1

...

To obtain all the key names (computed keys included) available in a grib file.

Code Block
 
>  grib_dump -D ../data/regular_latlon_surface.grib1

grib_filter examples

...

The grib_filter processes sequentially all grib messages contained in the input files and applies the rules to each one of them. Input messages can be written to the output by using the "write" statement. The write statement can be parameterised so that output is sent to multiple files depending on key values used in the output file name. If we write a rules_file containing the only statement:

Code Block

write "../data/split/[centre]_[date]_[dataType]_[levelType].grib[editionNumber]";
Applying this rules_file to the "../data/tigge_pf_ecmwf.grib2" grib file we obtain several files in the ../data/split directory containing fields split according to their key values
Code Block

> grib_filter rules_file ../data/tigge_pf_ecmwf.grib2
> ls ../data/split
</p></li><br /></ol><h2>grib_get examples</h2><ol><li><p> grib_get fails if a key is not found. 
<ac:structured-macro ac:name="code"><ac:plain-text-body><![CDATA[ 
> grib_get -p gribname ../data/tigge_pf_ecmwf.grib2

...

To get the step of the first GRIB message in a file:

Code Block
 
> grib_get -w count=1 -p step ../data/tigge_pf_ecmwf.grib2

grib_get_data examples

...

To get a latitude, longitude, value list, skipping the missing values(=9999)

Code Block

> grib_get_data ../data/reduced_gaussian_model_level.grib2

...

If you want to define your missing value=1111 and to print the string 'missing' in place of it

Code Block

> grib_get_data -m 1111:missing ../data/reduced_gaussian_model_level.grib2

...

If you want to print the value of other keys with the data value list

Code Block

> grib_get_data -p centre,level,step ../data/reduced_gaussian_model_level.grib2

grib_index_build examples

...

By default grib_index_build will index on the MARS keys.

Code Block
 
>  grib_index_build ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2

...

To specify a custom list of keys to index on, use the -k option.

Code Block
 
>  grib_index_build -k paramId,dataDate ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2

grib_ls examples

...

Without options a default list of keys is printed. The default list is different depending on the type of grib message.

Code Block
 
>  grib_ls ../data/reduced*.grib1 ../data/regular*.grib1 ../data/reduced*.grib2 

...

To print offset and count number in file use the keys offset and count Also the total count in a set of files is available as countTotal

Code Block
 
>  grib_ls -p offset,count,countTotal ../data/reduced*.grib1

...

To list only a subset of messages use the -w (where option). Only the pressure levels are listed with the following line.

Code Block
 
>  grib_ls -w levelType=pl ../tigge_pf_ecmwf.grib2 

...

All the grib messages not on pressure levels are listed as follows:

Code Block
 
>  grib_ls -w levelType!=pl ../tigge_pf_ecmwf.grib2 

...

To get the closest grid point to a latitude/longitude.

Code Block
 
> grib_ls -l 51.46,-1.33,1 -p paramId,name ../data/reduced_gaussian_surface.grib2
../data/reduced_gaussian_surface.grib2
</p></li><br /></ol><h2>grib_set examples</h2><ol><li><p> To set productDefinitionTemplateNumber=2 only for the fields with productDefinitionTemplateNumber=11 
<ac:structured-macro ac:name="code"><ac:plain-text-body><![CDATA[
> grib_set -s productDefinitionTemplateNumber=2 -w productDefinitionTemplateNumber=11 ../data/tigge_pf_ecmwf.grib2 out.grib2

...

To set productDefinitionTemplateNumber=2 only for the fields for which productDefinitionTemplateNumber is not equal to 11

Code Block

> grib_set -s productDefinitionTemplateNumber=2 -w productDefinitionTemplateNumber!=11 tigge_pf_ecmwf.grib2 out.grib2

...

When a key is not used all the bits of its value should be set to 1 to indicate that it is missing. Since the length (number of octet) is different from a key to another, the value that we have to code for missing keys is not unique. To give an easy way to set a key to missing a string "missing" or "MISSING" is accepted by grib_set as follows:

Code Block

> grib_set -s scaleFactorOfFirstFixedSurface=missing,scaledValueOfFirstFixedSurface=MISSING ../data/regular_latlon_surface.grib2 out.grib2
Since some values can not be set to missing you can get an error for those keys.

...

To set scaleFactorOfSecondFixedSurface to missing only for the fields for which scaleFactorOfSecondFixedSurface is not missing:

Code Block
 
> grib_set -s scaleFactorOfSecondFixedSurface=missing -w scaleFactorOfSecondFixedSurface!=missing tigge_pf_ecmwf.grib2 out.grib2

...

It's possible to produce a grib edition 2 file from a grib edition 1 just changing the edition number with grib_set. At this stage of development all the geography parameters, level and time information is correctly translated, for the product definition extra set calls must be done. To do this properly, \ref grib_filter is suggested.

Code Block
 
grib_set -s edition=2 ../data/reduced_gaussian_pressure_level.grib1

...

With grib edition 2 is possible to compress data using the jpeg algorithm. To change packing algorithm from grid_simple (simple packing) to grid_jpeg (jpeg2000 packing):

Code Block
 
> grib_set -s packingType=grid_jpeg ../data/regular_gaussian_model_level.grib2 out.grib2

...

It's possible to ask ecCodes to calculate the number of bits per value needed to pack a given field with a fixed number of decimal digits of precision. For example if we want to pack a temperature expressed in Kelvin with 1 digits of precision after the decimal point we can set changeDecimalPrecision=1

Code Block
 
> grib_set -s changeDecimalPrecision=1 ../data/regular_latlon_surface.grib2 ../data/out.grib2

grib_to_netcdf examples

...

Produce a NetCDF file from grib edition 1

Code Block

> grib_to_netcdf -o output.nc input.grib1

...

If your grib file has analysis and 6-hour forecast, then ignore keys 'type' and 'step'. Thus type=an/fc and step=00/06 will not be considered as netcdf dimensions.

Code Block

> grib_to_netcdf -I type,step -o output.nc input.grib

...

Do not use time of validity. If time of validity is used, it means the 1D time coordinate is considered as date+time+step, otherwise 3 different dimensions are created. The default behaviour is to use the time of validity.

Code Block

> grib_to_netcdf -T -o output.nc input.grib

...

Produce NetCDF with data type of FLOAT (32bit floating point, for higher precision). Note these types were chosen to provide a reasonably wide range of trade-offs between data precision and number of bits required for each value

Code Block

> grib_to_netcdf -D NC_FLOAT -o output.nc input.grib