Contact: zeng  @  zegraph.com      Last update: August 2014

GRIB Library

The grib library include three functions for decoding data in GRIB-1 and GRIB-2 formats. GRIB-1 code is borrowed from wgrib.c and GRIB-2 code from NCEP library.

Function Parameter Type Remark
g2unpack(fname[, fsec0, fsec1, fsec2, fsec3, fsec4, fsec5, fsec6, fsec7]) strings

Unpacks GRIB2 data. The fname must be a GRIB file name and the optional parameters of fsec0 to fsec7 must be callback function names. Those functions will be called when a corresponding section message is found:

  • fsec0 -> To be called with a code in Code Table 0.0 of GRIB2 specification.
  • fsec1 -> To be called with three inputs: an integer pointer to message data, the number of data, and bytes of an integer datum. The pointer contains decoded Section-1 messages, i.e., ptr[0]→identification of originating centre (Code Table C-1), ptr[1]→identification of originating sub-centre, ptr[2]→GRIB master tables version number (Code Table 1.0), ptr[3]→GRIB local tables version number (Code Table 1.1), ptr[4]→significance of reference time (Code Table 1.2), ptr[5]→year, ptr[6]→month, ptr[7]→day, ptr[8]→hour, ptr[9]→minute, ptr[10]→second, ptr[11]→production status of processed data (Code Table 1.3), ptr[12]→type of processed data (Code Table 1.4).
  • fsec2 -> To be called with the same type of inputs as those of fcec1. The pointer is a char array containing local use data.
  • fsec3 -> To be called with three sets of integer pointers and integers similar to those of fsec1. The first pointer contains decoded Section-3 messages, i.e, ptr[0]→, ptr[0]→source of grid definition (Code Table 3.0), ptr[1]→number of grid points in the defined grid, ptr[2]→number of octets needed for each additional grid points definition, ptr[3]→interpretation of list for optional points definition (Code Table 3.11), ptr[4]→grid definition template number (Code Table 3.1). The second pointer is an integer array containing entries (in the order specified) of grid defintion template 3.xx with xx being given by ptr[4]. The third pointer is an integer array containing the number of grid points contained in each row of irregular grid.
  • fsec4 -> To be called with an integer for product definition template number (Code Table 4.0) and two sets of integer pointers and integers similar to those of fsec1. The first pointer is a integer array containing entries (in the order specified) of product defintion template 4.xx with xx being given by the first integer parameter. The second contains decoded floating point values intended to document the vertical discretisation associated to model data on hybrid coordinate vertical levels.
  • fsec5 -> To be called with integer for data Representation template number (Code Table 5.0) and one set of float pointer and integers similar to those of fsec1. The pointer is an integer array containing an entries (in the order specified) of data representation template 5.xx with xx being given by the first integer parameter.
  • fsec6 -> To be called with a integer for bit-map ndicator ( Table 6.0), and a set of integer pointer similar to that of fsec1, The pointer contains bitmap data.
  • fsec7 -> To be called with three inputs: a float pointer to decoded binary data, the number of data, and bytes of a float datum. The pointer contains decoded binary data.
g1unpack(fname[, fsec1, fsec2, fsec3, fsec4]) strings Unpacks GRIB1 data. The fname must be a GRIB file name and the optional parameters of fsec1 to fsec4 must be callback function names (the functions will be called when a corresponding section message is found):
  • fsec1 -> To be called with three inputs: an unsigned char pointer to raw Section-1 message data, the number of data, and bytes of a integer datum.
  • fsec2 -> To be called with the same type of inputs as those of fsec1.
  • fsec3 -> To be called with the same type of inputs as those of fsec1.
  • fsec4 -> To be called with three inputs: a float pointer to decoded binary data, the number of data, and bytes of a float datum.
dgunpack(fname[, func]); strings The fname must be a DGRB file name and the optional func must be a callback function name. The function will be called with six inputs: integer pointer to DGRB message, number of data, and bytes of a datum; and float pointer to decoded binary data, number of data, and bytes of a float datum
qregular(src, region, dst) user, integer, user Converts data on quasi-regular (thinned latitude/longitude) grids to regular grids. The src is a float-type pointer to 3447 data; the region code is from 37 to 44; and the dst is float-type pointer to 73x73 data.

Example-1

load("grib.dll");
g2unpack("grid_file_name");
g2unpack("grid_file_name", "fsec0", "fsec1", "fsec2", "fsec3", "fsec4", "fsec5", "fsec6", "fsec7");

function fsec0(tid)
{
    csv("sec0", tid);
}

function fsec1(ptr, n, e)
{
    csv("sec1", ptr, n, e);
}

function fsec2(ptr, n, e)
{
    csv("sec2", ptr, n, e);
}

function fsec3(ptr1, n1, e1, ptr2, n2, e2, ptr3, n3, e3)
{
    csv("sec3", ptr1, n1, e1, ptr2, n2, e2, ptr3, n3, e3);
}

function fsec4(tid, ptr1, n1, e1, ptr2, n2, e2)
{
    csv("sec4", tid, ptr1, n1, e1, ptr2, n2, e2);
}

function fsec5(tid, ptr, n, e)
{
    csv("sec5", tid, ptr, n, e);
}

function fsec6(tid, ptr, n, e)
{
    csv("sec6", tid, ptr, n, e);
}

function fsec7(ptr, n, e)
{
    csv("sec7", ptr, n, e);
}

Example-2

load("grib.dll");
g1unpack("grid_file_name");
g1unpack("grid_file_name", "fsec1", "fsec2", "fsec3", "fsec4");

function fsec1(ptr, n, e)
{
    csv("sec1", ptr, n, e);
}

function fsec2(ptr, n, e)
{
    csv("sec2", ptr, n, e);
}

function fsec3(ptr, n, e)
{
    csv("sec3", ptr, n, e);
}

function fsec4(ptr, n, e)
{
    csv("sec4", ptr, n, e);
}

Example-3

load("grib.dll");
dgunpack("grid_file_name");
dgunpack("grid_file_name", "callback");

function callback(ptr1, n1, e1, ptr2, n2, e2)
{
    csv(ptr1, n1, e1, ptr2, n2, e2);
}