grib_api: grib_filter

- Main Page
- Modules
- Files
- Related Pages
- Examples

# grib_filter

## DESCRIPTION

Apply the rules defined in rules_file to each grib message in the grib files provided as arguments.

## USAGE

grib_filter [options] rules_file grib_file grib_file ...

## OPTIONS

-f
Force. Force the execution not to fail on error.
-o output_grib_file
Output grib is written to ouput_grib_file. If an ouput grib file is required and -o is not used, theouput grib is written to filtered.out
-M
Multi-grib support off. Turn off support for multiple fields in single grib message
-V
Version.
-g
Copy GTS header.
-G
GRIBEX compatibility mode.
-7
Does not fail when the message has wrong length
-v
Verbose.

## grib_filter examples

1. The rules accepted by grib_filter are different from the grib_convert rules due to the kind of work grib_filter it is supposed to do.
   The main difference between grib_filter and grib_convert is that the convert is a 1 field in input 1 field in output tool, while the filter is a 1 field in input as many field you need in output. At this aim the filter syntax allows a write in the form: write "filename". So that it is possible repeating as many write you need or using a parametrised write to send the output to many files.
   The grib_filter processes sequentially all the grib messages contained in the input file and it applies the rules to each one.
   Since the filename used in the write statement can contain some key values, taken from the grib processed when applying the "write rule", several files are produced in output containing fields with the same value of the keys used in the file name.
   Indeed if we write a rules_file containing the only statement:

       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 containting fields splitted according their keys values

       >grib_filter rules_file ../data/tigge_pf_ecmwf.grib2
       >ls ../data/split
       ecmf_20060619_pf_sfc.grib2
       ecmf_20060630_pf_sfc.grib2
       ecmf_20070122_pf_pl.grib2
       ecmf_20070122_pf_pt.grib2
       ecmf_20070122_pf_pv.grib2
       ecmf_20070122_pf_sfc.grib2

2. The key values in the file name can also be obtained in a different format by indicating explicitly the type required after a colon.
   - :l for long
   - :d for double
   - :s for string
   The following statement works in a slightly different way from the previous example, including in the output file name the long values for centre and dataType.

       write "../data/split/[centre:l]_[date]_[dataType:l]_[levelType].grib[editionNumber]";

   Running the same command again we obtain a different list of files.

       >grib_filter rules_file ../data/tigge_pf_ecmwf.grib2
       >ls ../data/split
       98_20060619_4_sfc.grib2
       98_20060630_4_sfc.grib2
       98_20070122_4_pl.grib2
       98_20070122_4_pt.grib2
       98_20070122_4_pv.grib2
       98_20070122_4_sfc.grib2

3. Other statements are allowed in the grib_filter syntax:
   - if ( condition ) { statement;}
     The condition can be made using ==,!= and joining single block conditions with || and &&
     The statement can be any valid statement also another nested condition
   - set keyname = keyvalue;
   - print "string to print also with key values like in the file name"
   - transient keyname1 = keyname2;
   - comments beginning with #
   A complex example of grib_filter rules is the following to change temperature in a grib edition 1 file.

       # Temperature
       if ( level == 850 && indicatorOfParameter == 11 ) {
           print "found indicatorOfParameter=[indicatorOfParameter] level=[level] date=[date]";
           transient oldtype = type ;
           set identificationOfOriginatingGeneratingSubCentre=98;
           set gribTablesVersionNo = 128;
           set indicatorOfParameter = 130;
           set localDefinitionNumber=1;
           set marsClass="od";
           set marsStream="kwbc";
           # Negatively/Positively Perturbed Forecast
           if ( oldtype == 2 || oldtype == 3 ) {
             set marsType="pf";
             set experimentVersionNumber="4001";
           }
           # Control Forecast
           if ( oldtype == 1 ) {
             set marsType="cf";
             set experimentVersionNumber="0001";
           }
           set numberOfForecastsInEnsemble=11;
           write;
           print "indicatorOfParameter=[indicatorOfParameter] level=[level] date=[date]";
           print;
       }