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 > callable wgrib2

wgrib2: callable wgrib2


Usually wgrib2 is used as a command-line utility; however, wgrib2 can called as a subroutine. The subroutine has the same arguments as an ordinary C main program.

   int wgrib2(int argc, char **argv);
Making the wgrib2 library is similar to compiling wgrib2 but the target of the makefile is "lib".
   tar -xzvf wgrib2.tgz
   cd grib2
   export CC=gcc
   export FC=gfortran
   make lib
   library is in grib2lib/

The above wgrib2 subroutine is the lowest level interface. The next stage is to write a fortran API for reading and writing grib.


The primary way of using callable_wgrib2 is through Fortran calls. See the web pages wgrib2api.

The equivalent calls are given by,

F95:   i = grb2_mk_inv(file, inv) 
C:     i = wgrib2a(file,"-rewind_init", file, "-inv", inv, "-Match_inv", NULL);
F95: i = grb2_inq(file, inv, search1, search2, grid=data, npts=ndata)
C:   i = wgrib2a(file,"-i_file",inv,"-rewind_init",inv,
            "-inv","/dev/null",     /* unix/linux */
            "-fgrep", search1,
            "-fgrep", search2,
            "-rpn_sto", "19",
            "-ftn_api_fn0","-last0","@mem:19", NULL);
      /* check i */
      ndata = wgrib2_get_reg_size(19);
      /* check ndata */
      data = (float *) malloc(ndata * sizeof(float));
      /* check data */
      i = wgrib2_get_reg_data(data, size, 19);
      /* check i */
F75:  i = grb2_wrt:(file, template, tmpno, data2=grid, meta=metadata)
C:    i = wgrib2_set_reg(grid, ndata, 19)
      /* check i */
      sprintf(number,"%d", ndata);
      i = wgrib2a(template,"-rewind_init",template,
          "-d",number,"-rpn_rcl","19","-set_metadata_str", metadata,
          "-set_grib_type", "c2", 
          "-grib_out", file)

The Fortran interface is much nicer than the C interface.

9/2015: The write API was tested by converting a simple GFS post-processer to grib2.

8/2016: After a hiatus of a year, I found need to do some analyses of grib files. To make life easier, I converted converted all the files into grib2 and started using the ftn api to read 10s of thousands of grib files. It's convenient to use.

See also:

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: Sep 20, 2016
Disclaimer Privacy Policy