Skip Navigation Links 
NOAA logo - Click to go to the NOAA home page National Weather Service   NWS logo - Click to go to the NWS home page
Climate Prediction Center

About Us
   Our Mission
   Who We Are

Contact Us
   CPC Information
   CPC Web Team

HOME > Monitoring_and_Data > Oceanic and Atmospheric Data > Reanalysis: Atmospheric Data > wgrib2 -if,-not_if

wgrib2: -if, -not_if

Introduction if block structure

The -if option returns either true or false. What happens next depends on whether you are using Version 1 or Version 2 of the If blocks. Wgrib2 2.0.9 is needed for Version 2.

Version 1 If blocks

For Version 1 of the IF blocks, the -if option returns true or false. If true, then all the options up to and including the next "output" option are executed. If false, then all the options up to and including the next "output" option are not executed. Options following the output option are executed as normal.With Version 1, nesting of IF blocks is not defined.


    wgrib2 (...) [if] [list of non-ouput options] [output option]

   [if] = -if, -not_if, -if_fs, -not_if_fs, -if_n, -if_rec

   if [if] returns the value of false, the list of non-output options
                                       and next output option are not executed
           returns the value of true, the following options are executed
   [output option] is the terminator of the if block

   If blocks should not be nested.


     wgrib2 gribfile -if ":(UGRD|VGRD):" -grib winds.grb \
                     -if ":TMP:" -grib tmp.grb \
                     -if ":HGT:" -grib hgt.grb \
                     -not_if ":(UGRD|VGRD|TMP|HGT):" -grib rest.grb

     note: -grib is an output option

     wgrib2 gribfile -new_grid_interpolation bilinear \
        -if ":SOTYP:" -new_grid_interpolation neighbor -fi \

     note: -fi is an output option

Version 2 If blocks (introduced wgrib2 v2.0.9)

For Version 2 of the IF blocks, wgrib2 uses a proper -if, -elseif, -else and -endif options to implement IF blocking structure. IF blocks can be nested. Note, the "if" flag is reset prior to processing of a record.


1. The if blocks can now be nested, and are terminated by an -endif.
2. Within the if block, you can have -else and -elseif options.
3. The -fi option is not allowed.
4. Version 1 and Version 2 if blocks cannot be mixed.


     wgrib2 gribfile -if ":(UGRD|VGRD):" -grib winds.grb \
                     -elseif ":TMP:" -grib tmp.grb \
                     -elseif ":HGT:" -grib hgt.grb \
                     -else -grib rest.grb -endif

     wgrib2 gribfile -new_grid_interpolation bilinear \
        -if ":SOTYP:" -new_grid_interpolation neighbor -endif \


The Version 1 limits, the maximum number of -if options on a command line is limited by (1) system limit of open files (2) maximum length of a command line, (3) maximum number of regular expressions allowed MAX_MATCH (wgrib.h), and (4) maximum number of parsed arguments N_ARGLIST (wgrib.h).

The Version 2 limits include the Version 1 limits and include a limit of the number of nested IF blocks.

Version 1 and Version 2 IF blocks cannot be mixed. The -fi option is restricted to Version 1 IF blocks. There are NO plans to eliminate Version 1 IF blocks. However, Version 2 IF blocks are recommended for wgrib2 v2.0.9+.

Introduction -if and -not_if


-if X

X is a regular expression
sets the "run_flag" if X matches the "match inventory",
used to control the execution of following options

Future Changes

The format of the "match inventory" has evolved and will continue to evolve. The rule for future changes is that new items in the "match inventory" will be added as the second last item. Consequently the last item in the inventory will always be ":vt=YYYYMMDDHH:". In order to future proof your -match, and -not selections, you must not include any item before the ":vt=YYYYMMDD:" field.

    -match ":vt=2011111500:"                  good
    -not ":vt=2011111500:$"                   good (dollar sign matches the end of the line)
    -not ":n=10:vt=2011111500:"               bad (item before :vt=)
    -match ":RH:975 mb:anl::vt=2010050806:"   bad (item before :vt=)
Some recent changes (as of Nov 2011) to the match inventory include:
  • adding the "extended name of the variable", ex. TMP.prob_<273
  • adding the inventory number, ex. n=10
  • adding ensemble/chemical/probability information (-misc)

See also: -not, -end, -match, -i, -if. -fi. -set_regex.

NOAA/ National Weather Service
National Centers for Environmental Prediction
Climate Prediction Center
5830 University Research Court
College Park, Maryland 20740
Climate Prediction Center Web Team
Page last modified: May 15, 2005
Disclaimer Privacy Policy