wgrib2: callable wgrib2
Introduction
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
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.
FTN API
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:
|