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

wgrib2: wgrib for GRIB-2

read and write grib2 files


wgrib2 is not simple upgrade of wgrib for grib2 files. wgrib can slice and dice grib1 files. wgrib2 is more like four drawers of kitchen utensils as well as the microwave and blender. This kitchen miracle was done by a more developer friendly design and the contributions of many people. Some functionality include,

  • inventory and read grib2 files
  • create subsets
  • create regional subsets by cookie cutter or projections
  • export to ieee, text, binary and CSV
  • export to netcdf and mysql
  • write grib2 fields like averages, accumulations, max/min values
  • convert grid-relative to earth relative winds
  • convert U and V to wind speed, direction
  • writing of new grib2 fields
  • parallel processing by using threads (OpenMP)
  • parallel processing by flow-based programming
  • C callable interface and fortran interface (in progress)

OpenMP Configuration

OpenMP is used to speed up wgrib2 by running big loops over multiple cores. To compile OpenMP into wgrib2, you need set

  1. "USE_OPENMP=1" in the makefile (default)
  2. define the compilers (ex), export CC=gcc ; export FC=gfortran
  3. compile wgrib2: "make"
  4. set two envirnoment variables
  5. export OMP_NUM_THREADS=4 (ex. for 4 threads)

In general, you set the number of threads to be ≤ than the number of CPU cores available. Usually I set the number to 3 on a quad-core machine. That leaves one core for unrelated work. I usually set OMP_WAIT_POLICY=PASSIVE in order to reduce the CPU utiliation at the expense of the run time. The threading is helpful when the grids are large and the processing is highly parallelized such as decoding complex grid and in geolocation.

Compiling: NetCDF Configuration

Wgrib2 can be configured with (1) no NetCDF support, (2) NetCDF3 support or (3) NetCDF4 support. For a person who only wants to convert grib2 to netcdf, then option 3 is the obvious choice. In some situations, wgrib2 may be called a hundred times a minute (heavy-duty processing, nomads) with no need for netcdf support. (NetCDF4 increases the executable size almost 3.5x). In this case, wgrib2 should be compiled with option 1. I use option 2 because I don't mind the 50% increase in executable size, I have a 64-bit machine, and I make netcdf files once in a while. 32-bit machines should be configured for either no NetCDF support or NetCDF4 support because the NetCDF3 code fails during internal testing. (The netCDF3 library is no longer supported.) You can see the NetCDF configuration of a compiled wgrib2 by by typing "wgrib2 -config".

  • Option 1: No NetCDF support
    • fast compile
    • small executable (3.9 MB as of 2014)
    • compile works on 32+ bit machines
    • makefile configuration: USE_NETCDF3=0, USE_NETCDF4=0
  • Option 2: NetCDF3 support (default in makefile)
    • fast compile
    • modest increase in executable size (5.7 MB vs 3.9 MB as of 2014)
    • compile may fail on 32-bit machines
    • makefile configuration: USE_NETCDF3=1, USE_NETCDF4=0
  • Option 3: NetCDF4 support
    • slower compile
    • 3.5x increase in executable size (13.5 MB vs 3.9 MB as of 2014)
    • compile works on 32+ bit machines
    • requires fortran90 compiler
    • makefile configuration: USE_NETCDF3=0, USE_NETCDF4=1
    • prompts for downloading netcdf4/hdf5 libraries

Execution: Segment Faults

Wgrib2 is used in NCEP operations and by many daily processing jobs at CPC. Aside from the experimental/alpha features in wgrib2, there are only three known causes from seg faults and two are library related and the another is compiler related.

The jpeg2000 library (Jasper) could seg-fault with larger grids. This problem was fixed with an Ubuntu/Debian update to Jasper (fall 2015) which was incorporated into wgrib2 v2.0.3. If your copy of wgrib2 is using the system-provided Jasper library, then your copy of wgrib2 may be vunerable to this problem (depending on the system Jasper library).

Older versions of wgrib2 used g2clib to decode grib data and that caused problems with some wgrib2 releases by 3rd parties. The problems were that wgrib2 had patches to g2clib which were not fully incorporated until g2clib v1.4.0. Another problem is that g2clib can be compiled with either a 32 or 64 bit interface. It is not clear which interface that the 3rd parties are using. This g2clib issue has become a non-problem because g2clib is no longer the default decoder. (You can still cause a seg-fault if you enable the g2clib decoder and the system g2clib is incompatible.)

The third problem is compiler specific and only affects

  1. wgrib2 v2.0.4
  2. wgrib2 was compiled with OpenMP support
  3. number of threads is greater than one
  4. creating simple packed grib messages
  5. all the grid points are undefined
  6. compiled with older versions of gcc such as 2.4.7
This problem appears in gcc 2.4.7's handling of unsigned int looping variables when OpenMP is enabled. Gcc 2.4.5 was released 6/1993 followed by the 10/1993 release of gcc 2.5.0.

There a potential for seg-faults by reading using specially crafted png or jpeg2000 compressed grib files. For example, a few days after the release of wgrib2 v2.0.3, the pnglib was upgraded to fix a seg-fault when reading a specially crafted png file.


2/2016: wgrib2 2.0.4 works with grib sections up to 4GB and up to 2G grid points. The next step, up to 4G grid points is a work in progress. Please update 20 2.0.4 because this version has fixed complex packing encoding, Jasper library for large jpeg2000 files, fix for decoding legal but strange complex packed fields. The latter 3 fixes were introduced in earlier versions.

8/2014: Make your wgrib2 executable smaller on linux by, strip wgrib2 strip removes symbols which are used in debugging from 6.3MB -> 2.2 MB (case 1) 4.7 MB -> 1.9 MB (case 2)

5/2012: OpenMP, experimental gctpc geolocation package

6/2010: Interpolate to new grids using the IPOLATES library (optional package)

5/2008: mysql support, thanks Niklas Sondell
custom regional grids, write your own grib files

Kristian Nilssen has sent in a module to write netcdf files. Compiled cleanly and converted grib2 into netcdf. Will be released as an alpha option because the output format may change.

Sergey Varlamov has improved the netcdf module to add multiple output times.

2/2007: Manfred Schwarb went over the code last Christmas-New Years and sent in 31 patches. 31 patches sounds like a lot but he must have used a spell checker on the code. Anyways some of the patches were for the more unpolished code in the package which caused me to go over that code. Manfred also sent in a script that can read a grib2 table from the web documentation and create C code. Much better than typing by hand or any other script that I had in my tool box.

9/16/2005 alpha version is released to the public.

Some Good Stuff


  • each option call corresponds to a subroutine call
  • type wgrib2 to see primary options
  • inventory format is specified on command line by options
  • if no "inv" option is specified, -s is used
  • Experimental wgrib2 library can be call from C and fortran.

Joining the development effort

  • Source code: knowledge of C and some grib-2
  • Donated source code / non-trivial bug fixes must either be GNU or public domain.
  • Prefer PD bug fixes for PD code and GNU bug fixes for GNU code.
  • Bug reports are important
  • Contact

Contributions by

  • Wesley Ebisuzai: many modules
  • Reinoud Bokhorst: tosubmsg, checksum
  • Jaakko Hyvätti: gribtab
  • Dusan Jovic: staggered grids, proj4 code
  • Kristian Nilssen: netcdf module
  • Karl Pfeiffer: georeference
  • Pablo Romero: unix_time
  • Manfred Schwarb: many modules
  • Arlindo da Silva: openGrADS, bbox
  • Niklas Sondell: mysql module
  • Sergey Varlamov: netcdf module improvements, georeferencing updates
  • thanks to the people who report the bugs and more who provide the fixes!


The options

Some solutions

Selecting Fields/Records/Message

Select/Ignore by name/level/time/etc

  • -match process records that match a posix extended regular expression
  • -not process records not matching a regular expression
  • -match_inv the inventory used by -match and -not
  • -i reads inventory from stdin for record selection

Select by number (better to use previous method)

  • -d dump specific record
  • -for select a range of records to process (nth message)
  • -for_n select a range of records to process (nth message/submessage)

Selective Processing: if options

After selecting the fields to process, you can refine the processing by the "if" options. For example, you selected the winds and temperatures. You can use the "if" to send the winds to one file and the temperatures to another file. In general it is faster to use the -match/-not options because there is less processing of the data.

  • -if if this then that
  • -not_if if not this then that
  • -fi closes -if (often an output option closes the if)

Individual Grid Point Data

  • -ij print value at grid point (i, j)
  • -ijlat print lat, lon, value at grid point (i, j)
  • -ilat print lat, lon, value of Nth grid point
  • -lon prints the lat-lon, and value of the grid point nearest the specified lat-lon
  • -max prints the maximum value
  • -min prints the minimum value
  • -stats prints some statistics about the fields
  • -V verbose inventory (shows stats)

Regridding, Interpolating to new grids

Wgrib2 has the ability to convert grib files from one grid to another. The conversion uses a user-selected interpolation scheme: bilinear (default), bicubic, nearest neighbor and budget. The supported grids (7/2010) include lat-lon, gaussian, Lambert conformal and polar stereographic. This capability uses the NCEP IPOLATES library and is an optional package.

Note: the interpolation uses scalar and vector interpolation schemes. For the vector quantities the V field must immediately follow the corresponding U field.

Modifying grid data

Wgrib2 allows options to modify the grid point data. The various -undefine options set the grid values to undefined. This reduces the size of the CSV and grib output. The various -import functions allow you to overwrite the data with the contents of a file. The -rpn option runs a calculator with 10 registers. Unlike an ordinary calculator that works with numbers, this calculator works with grids. These options allow you to
  • set grid points to undefined based on region
  • set grid points to undefined based on value
  • Change units, useful for making CSV files
  • Compute new fields
    • thickness
    • find max/min/ave value in a time series
    • wind speed/direction
  • Convert binary data to grib2
  • Convert GrADS compatible data data to grib2 (
  • Merge subregions; i.e., combine tiles
  • various -import options
  • -rpn Reverse Polish Notation calculator
  • -ijundefine undefine/maskout regions
  • -undefine undefine/maskout regions
  • -undefine_val undefine/maskout grid points by value
  • -grib_out writes the current field in grib format

    Exporting data to other programs

    • -netcdf: write data in netcdf format
    • -mysql: export data to a mysql database
    • -mysql_speed: export data to a mysql database
    • -spread: write data for spreadsheets
    • -csv: write in column separated values, another one for spreadsheets
    • -text: data in text format
    • -bin: data native binary floating point
    • -ieee: data in big endian IEEE format
    • -ijbox: write a rectangular grid of data
    • -AAIG: arcinfo ascii grid, GIS
    For a short list of options, type "wgrib -h"
    For a complete list, type "wgrib -help all"
    To search for an option, type "wgrib -help keyword"

    Writing grib2

    Writing grib2 is problem with no perfect solution. Some people want speed, some people want ease of use. Some people want it to run the code on a hundred processors and other want to run it on Windows XP. Here is a list of techniques that involve wgrib2. Here is the background for techniques that use the import function.
    1. The basic -import_ieee/bin/text option.
    2. using -set_metadata
    3. Example of converting HDF to grib2

    Machines able to run wgrib2

    • linux: 64-bit with IPOLATES
      • Redhat 6 Enterprise: primary development system
      • Redhat 5 Enterprise: remove compile option "-Werror=format-security" from the makefile. This is a mandatory compile option for some distributions and is just a good idea. However, the gcc in RH5 doesn't support this option.
      • Ubuntu: 64-bit with IPOLATES
    • Redhat linux: 32-bit with IPOLATES, not tested recently, use netcdf4 instead of netcdf3
    • Ubuntu: 32-bit with IPOLATES, not tested recently
    • Mandriva linux (old report)
    • AIX: use makefile, some fiddling with libraries is necessary, not tested recently
    • Solaris, needs gnu make and gcc (old report)
    • HPUX, needs changes to makefiles (old report)
    • Windows: using Cygwin system produces 32-bit binaries. using netcdf4
    • Windows: compiled MingW (not recent), Watcom C
    • Intel-based Mac
    • The IPOLATES library has been tested with on g95, gfortran, openf95 and AIX fortran
    The makefile works on Redhat and Ubuntu (with needed installed options). For other systems, you may have to modify the makefile.

    System dependencies: 32 vs 64 bit, Windows vs Linux/UNIX

    The wgrib2 source code is written to be portable; there are no issues with big vs little endian or the size of the integer as long as it meets the minimum C requirement (32-bits). For windows, the cygwin's gcc supports the POSIX standard and allows the standard source code to be compiled. It is possible to compile the code with many of the POSIX extentions turned off was done when compiling with the Watcom compiler.

    The main difference between the 32 and the 64-bit versions is that the 32-bit version is limited to 2 GB files and has limited memory. Note: a 32-bit wgrib2 can handle larger than 2GB files if the files are read through stdin.

    The maximum size depends on the version of wgrib2. For versions up to v2.0.3, the maximum grib message was limited to 2GB and maximum number of grid points was limited to 0.5 GB. For v2.0.4, the limits became 4G for the message size an 2GB for number of grid points, NX and NY. Some routines can handle the maximum supported by the grib standared, 4G. Of course, these limits are for the 64-bit version of wgrib2 with sufficient memory (32 GB).

    The main difference between the windows and linux versions is that windows treats binary and text files differently. Consequently you can send a grib or binary data to STDOUT in unix/linux with no problem. Under windows, the results are implementation dependent. In addition, POSIX1 and POSIX2 functions may not be supported or may function differently. Consequently certain options may not work or work differently.

    • The maximum number of open files is system dependent.
    • The maximum number of arguments to wgrib2 may be limited by the system.
    • The netcdf conversion is done in memory, so the speed is dependent on the free memory available.

    Source Code and Compling Hints

    The wgrib2 source code is written to the POSIX standard. Features requiring POSIX2, such as regular expressions, can usually be turned off in the makefile. The wgrib2 code can be compiled with 32 or 64 bit pointers and integers. However, the code has to be compiled in a like manner for all the libraries. Some packages are optional (netcdf, mysql) and enabling these options can really increase the executable size.

    While compiling wgrib2, you may see warnings about unknown pragmas. Pragmas are "comments" that are used when compiling an OpenMP version of the code. The default is to compile with OpenMP turned on. However, in some environments, turning off OpenMP is preferred.

    Netcdf3, the default, often has problems compiling cleanly on 32-bit machines. (It fails in the testing phase.) The work-around is to compile with Netcdf4 or without Netcdf. The best solution is to move to a 64-bit system. This will allow you to process files that are larger than 2GB. All the linux systems that I have access to are 64 bits, so this problem isn't going to be resolved soon.

    Precompiled code from External Sites

    There are many sites with precompiled versions of wgrib2. This list is neither exhaustive nor an endorsement of the sites. I have not tested the wgrib2 executables from these sites and YMMV.

    Other programs that call wgrib2 (incomplete list)


    The code is being developed on a Redhat linux x86_64 platform in standard (POSIX) C. The wgrib2 source code requires an integer size of 32 or more bits. Incompatibilites tend to be related to problems in compiling of the external libraries (ex. netcdf, jasper).

    Change logfile


    8/2015: wgrib2 v2.0.0-v2.0.2 -ieee does not work with OpenMP. Fixed 2.0.3

    8/2015: complex packing: problem when creating if grid values are all undefined. Fixed 2.0.3

    5/2015: callable wgrib2 and fatal errors. The error handling code can cause a seg fault on subsequent calls to wgrib2. Problem has been fixed v2.0.3

    1/2014: Jasper library bug? Grib files that have large grids and that are compressed with jpeg-2000 can have problems. Have seen this problem in GrADS, wgrib2 and it has been reported in WCT. If you exceed the critical grid size, the program will have problems reading the jpeg-2000 compressed grib message. The critical grid size seems to vary with the program and how it was compiled. I've tried the Fedora and Ubuntu updates to the Jasper library and that didn't fix the problem. Note that critical size was larger for wgrib2 than GrADS. So it was possible to rewrite the file using a different compression which GrADS can read. We found problems when the grid size was 6 million. The problem has been reported at 5 million for different programs. The common stack size was quickly ruled out by setting the stack size to a large number and by running the programs on a system with an unlimited stack size.

    Please report them to When you report bugs, try to make them reproduceable on a linux machine and include sample data.


    The source code modules for wgrib2 are either in the public domain or under the GNU licence depending on the authors of the various modules. Wgrib2 uses libraries that are in the public domain, under various GNU licences, the Image Power JPEG-2000 Public Licence, libpng licence, the zlib licence, the netcdf licence*, HDF5* licence, MySQL licence* and perhaps others. How about one licence to rule them all?

    * optional package

  • 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: Oct 1, 2015
    Disclaimer Privacy Policy