NCEPLIBS-g2c  1.6.4
grib2.h File Reference

Header file for NCEPLIBS-g2c library. More...

#include <stdio.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  gribfield
 Struct for GRIB field. More...
 
struct  gtemplate
 Struct for GRIB template. More...
 

Macros

#define G2_VERSION   "g2clib-1.6.4"
 Current version of NCEPLIBS-g2c library. More...
 

Typedefs

typedef float g2float
 Float type. More...
 
typedef int64_t g2int
 Long integer type. More...
 
typedef uint64_t g2intu
 Unsigned long integer type. More...
 
typedef struct gribfield gribfield
 Struct for GRIB field. More...
 
typedef struct gtemplate gtemplate
 Struct for GRIB template. More...
 

Functions

void compack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs up a data field using a complex packing algorithm as defined in the GRIB2 documention. More...
 
gtemplateextdrstemplate (g2int, g2int *)
 This subroutine generates the remaining octet map for a given Data Representation Template, if required. More...
 
gtemplateextgridtemplate (g2int, g2int *)
 This subroutine generates the remaining octet map for a given Grid Definition Template, if required. More...
 
gtemplateextpdstemplate (g2int, g2int *)
 This subroutine generates the remaining octet map for a given Product Definition Template, if required. More...
 
g2int g2_addfield (unsigned char *, g2int, g2int *, g2float *, g2int, g2int, g2int *, g2float *, g2int, g2int, g2int *)
 This routine packs up Sections 4 through 7 for a given field and adds them to a GRIB2 message. More...
 
g2int g2_addgrid (unsigned char *, g2int *, g2int *, g2int *, g2int)
 This routine packs up a Grid Definition Section (Section 3) and adds it to a GRIB2 message. More...
 
g2int g2_addlocal (unsigned char *, unsigned char *, g2int)
 This routine adds a Local Use Section (Section 2) to a GRIB2 message. More...
 
g2int g2_create (unsigned char *, g2int *, g2int *)
 This routine initializes a new GRIB2 message and packs GRIB2 sections 0 (Indicator Section) and 1 (Identification Section). More...
 
void g2_free (gribfield *)
 This routine frees up memory that was allocated for struct gribfield. More...
 
g2int g2_getfld (unsigned char *, g2int, g2int, g2int, gribfield **)
 This subroutine returns all the metadata, template values, bit-map (if applicable), and the unpacked data for a given data field. More...
 
g2int g2_gribend (unsigned char *)
 This routine finalizes a GRIB2 message after all grids and fields have been added. More...
 
g2int g2_info (unsigned char *, g2int *, g2int *, g2int *, g2int *)
 This subroutine searches through a GRIB2 message and returns the number of gridded fields found in the message and the number (and maximum size) of Local Use Sections. More...
 
g2int g2_unpack1 (unsigned char *, g2int *, g2int **, g2int *)
 This subroutine unpacks Section 1 - Identification Section as defined in GRIB Edition 2. More...
 
g2int g2_unpack3 (unsigned char *, g2int *, g2int **, g2int **, g2int *, g2int **, g2int *)
 This routine unpacks Section 3 (Grid Definition Section) as defined in GRIB Edition 2. More...
 
g2int g2_unpack4 (unsigned char *, g2int *, g2int *, g2int **, g2int *, g2float **, g2int *)
 This subroutine unpacks Section 4 (Product Definition Section) as defined in GRIB Edition 2. More...
 
g2int g2_unpack5 (unsigned char *, g2int *, g2int *, g2int *, g2int **, g2int *)
 This subroutine unpacks Section 5 (Data Representation Section) as defined in GRIB Edition 2. More...
 
g2int g2_unpack6 (unsigned char *, g2int *, g2int, g2int *, g2int **)
 This subroutine unpacks Section 6 (Bit-Map Section) as defined in GRIB Edition 2. More...
 
g2int g2_unpack7 (unsigned char *, g2int *, g2int, g2int *, g2int, g2int *, g2int, g2float **)
 This subroutine unpacks Section 7 (Data Section) as defined in GRIB Edition 2. More...
 
void gbit (unsigned char *, g2int *, g2int, g2int)
 Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked iout array. More...
 
void gbits (unsigned char *, g2int *, g2int, g2int, g2int, g2int)
 Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked iout array. More...
 
gtemplategetdrstemplate (g2int)
 This subroutine returns DRS template information for a specified Data Representation Template. More...
 
gtemplategetgridtemplate (g2int)
 This subroutine returns grid template information for a specified Grid Definition Template for [Section 3 - the Grid Definition Section (GDS)](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_sect3.shtml). More...
 
gtemplategetpdstemplate (g2int)
 This subroutine returns PDS template information for a specified Product Definition Template. More...
 
double int_power (double, g2int)
 Function similar to C pow() power function. More...
 
void misspack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs up a data field using a complex packing algorithm as defined in the GRIB2 documention. More...
 
void mkieee (g2float *, g2int *, g2int)
 This subroutine stores a list of real values in 32-bit IEEE floating point format. More...
 
int pack_gp (g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *, g2int *)
 Determines groups of variable size, but at least of size minpk, the associated max (jmax( )) and min (jmin( )), the number of bits necessary to hold the values in each group (lbit( )), the number of values in each group (nov( )), the number of bits necessary to pack the jmin( ) values (ibit), the number of bits necessary to pack the lbit( ) values (jbit), and the number of bits necessary to pack the nov( ) values (kbit). More...
 
void rdieee (g2int *, g2float *, g2int)
 This subroutine reads a list of real values in 32-bit IEEE floating point format. More...
 
void sbit (unsigned char *, g2int *, g2int, g2int)
 Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each value in the unpacked array. More...
 
void sbits (unsigned char *, g2int *, g2int, g2int, g2int, g2int)
 Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each value in the unpacked array. More...
 
void seekgb (FILE *, g2int, g2int, g2int *, g2int *)
 This subprogram searches a file for the next GRIB Message. More...
 
void simpack (g2float *, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs up a data field using the simple packing algorithm as defined in the GRIB2 documention. More...
 

Detailed Description

Header file for NCEPLIBS-g2c library.

Program History Log

Date Programmer Comments
2002-10-25 Gilbert Initial
2009-01-14 Vuong Changed struct template to gtemplate
Author
Stephen Gilbert
Date
2002-10-25

Definition in file grib2.h.


Data Type Documentation

◆ gribfield

struct gribfield

Struct for GRIB field.

Definition at line 60 of file grib2.h.

Data Fields
g2int * bmap Integer array containing decoded bitmap, if ibmap=0 or ibap=254.

Otherwise NULL.

g2float * coord_list Array containing floating point values intended to document the vertical discretisation associated to model data on hybrid coordinate vertical levels (part of Section 4).
g2int discipline Message Discipline (see Table 0.0).
g2int expanded Logical value indicating whether the data field was expanded to the grid in the case where a bit-map is present.

If true, the data points in fld match the grid points and zeros were inserted at grid points where data was bit-mapped out. If false, the data values in ld were not expanded to the grid and are just a consecutive array of data points corresponding to each value of "1" in bmap.

g2float * fld Array of ndpts unpacked data points.
g2int griddef Source of grid definition (see Table 3.0).
  • 0 Specified in Table 3.1.
    • 1 Predetermined grid Defined by originating centre.
g2int ibmap Bitmap indicator (see Table 6.0).
  • 0 bitmap applies and is included in Section 6.
  • 1-253 = Predefined bitmap applies
  • 254 = Previously defined bitmap applies to this field
  • 255 = Bit map does not apply to this product.
g2int idrtlen Number of elements in idrtmpl.
g2int * idrtmpl Contains the data values for the Data Representation Template specified by idrtnum.
g2int idrtnum Data Representation Template Number (see Table 5.0).
g2int * idsect Contains the entries in the Identification Section (Section 1).
  • idsect[0] Identification of originating Centre (see Table 0). 7 is the identification for the US National Weather Service.
  • idsect[1] Identification of originating Sub-centre. (See Table C).
  • idsect[2] GRIB Master Tables Version Number (see Table 1.0).
    • 0 Experimental
    • 1 Initial operational version number
  • idsect[3] GRIB Local Tables Version Number (see Table 1.1).
    • 0 Local tables not used
    • 1-254 Number of local tables version used
  • idsect[4] Significance of Reference Time (See Table 1.2).
    • 0 Analysis
    • 1 Start of forecast
    • 2 Verifying time of forecast
    • 3 Observation time
  • idsect[5] Year (4 digits)
  • idsect[6] Month
  • idsect[7) Day
  • idsect[8] Hour
  • idsect[9] Minute
  • idsect[10] Second
  • idsect[11] Production status of processed data (see Table 1.3).
    • 0 Operational products
    • 1 Operational test products
    • 2 Research products
    • 3 Re-analysis products
  • idsect[12] Type of processed data (see Table 1.4).
    • 0 Analysis products
    • 1 Forecast products
    • 2 Analysis and forecast products
    • 3 Control forecast products
    • 4 Perturbed forecast products
    • 5 Control and perturbed forecast products
    • 6 Processed satellite observations
    • 7 Processed radar observations
g2int idsectlen Number of elements in idsect.
g2int ifldnum Field number within GRIB message.
g2int igdtlen Number of elements in igdtmpl - i.e.

number of entries in Grid Defintion Template.

g2int * igdtmpl Contains the data values for the Grid Definition Template specified by igdtnum.
g2int igdtnum Grid Definition Template Number (See Table 3.1).
g2int interp_opt Interpretation of list for optional points definition.

(See Table 3.11).

g2int ipdtlen Number of elements in ipdtmpl - i.e.

number of entries in Product Defintion Template.

g2int * ipdtmpl Contains the data values for the Product Definition Template specified by ipdtnum.
g2int ipdtnum Product Definition Template Number (see Table 4.0).
g2int * list_opt (Used if numoct_opt .ne.

0) This array contains the number of grid points contained in each row (or column) (part of Section 3). NULL if numoct_opt = 0.

unsigned char * local Pointer to character array containing contents of Local Section 2, if included.
g2int locallen Length of array local.
g2int ndpts Number of data points unpacked and returned.
g2int ngrdpts Number of grid points in the defined grid.
g2int num_coord Number of values in array coord_list.
g2int num_opt (Used if numoct_opt .ne.

0) The number of entries in array ideflist - i.e. number of rows (or columns) for which optional grid points are defined. This value is set to zero, if numoct_opt=0.

g2int numoct_opt Number of octets needed for each additional grid points definition.

Used to define number of points in each row (or column) for non-regular grids. = 0, if using regular grid.

g2int unpacked Logical value indicating whether the bitmap and data values were unpacked.

If false, bmap and fld pointers are NULL.

g2int version GRIB edition number (2).

◆ gtemplate

struct gtemplate

Struct for GRIB template.

Definition at line 27 of file grib2.h.

Data Fields
g2int * ext Number of octets of each entry in the extension part of the template.
g2int extlen Number of entries in the template extension.
g2int * map Number of octets of each entry in the static part of the template.
g2int maplen Number of entries in the static part of the template.
g2int needext Indicates whether or not the template needs to be extended.
g2int num The template number.
g2int type The template type: 3 Grid Defintion Template.

4 Product Defintion Template. 5 Data Representation Template.

Macro Definition Documentation

◆ G2_VERSION

#define G2_VERSION   "g2clib-1.6.4"

Current version of NCEPLIBS-g2c library.

Definition at line 18 of file grib2.h.

Typedef Documentation

◆ g2float

typedef float g2float

Float type.

Definition at line 22 of file grib2.h.

◆ g2int

typedef int64_t g2int

Long integer type.

Definition at line 20 of file grib2.h.

◆ g2intu

typedef uint64_t g2intu

Unsigned long integer type.

Definition at line 21 of file grib2.h.

◆ gribfield

typedef struct gribfield gribfield

Struct for GRIB field.

Definition at line 22 of file grib2.h.

◆ gtemplate

typedef struct gtemplate gtemplate

Struct for GRIB template.

Definition at line 22 of file grib2.h.

Function Documentation

◆ compack()

void compack ( g2float fld,
g2int  ndpts,
g2int  idrsnum,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs up a data field using a complex packing algorithm as defined in the GRIB2 documention.

It supports GRIB2 complex packing templates with or without spatial differences (i.e. DRTs 5.2 and 5.3). It also fills in GRIB2 Data Representation Template 5.2 or Template 5.3 with the appropriate values.

Parameters
fldContains the data values to pack.
ndptsThe number of data values in array fld.
idrsnumData Representation Template number. Must equal 2 or 3.
idrstmplContains the array of values for Data Representation Template 5.2 or 5.3.
  • 0 Reference value - ignored on input, set my compack().
  • 1 Binary Scale Factor
  • 2 Decimal Scale Factor
  • 6 Missing value management
  • 7 Primary missing value
  • 8 Secondary missing value
  • 16 Order of Spatial Differencing (1 or 2)
cpackThe packed data field.
lcpacklength of packed field cpack.
Author
Stephen Gilbert
Date
2002-11-07

Definition at line 42 of file compack.c.

References int_power(), mkieee(), pack_gp(), sbit(), and sbits().

Referenced by cmplxpack().

◆ extdrstemplate()

gtemplate* extdrstemplate ( g2int  number,
g2int list 
)

This subroutine generates the remaining octet map for a given Data Representation Template, if required.

Some Templates can vary depending on data values given in an earlier part of the Template, and it is necessary to know some of the earlier entry values to generate the full octet map of the Template.

Parameters
numberThe number of the Data Representation Template that is being requested.
listThe list of values for each entry in the the Data Representation Template.
Returns
Pointer to the returned template struct. Returns NULL pointer, if template not found.
Author
Stephen Gilbert
Date
2000-05-11

Definition at line 110 of file drstemplates.c.

References getdrsindex(), and getdrstemplate().

Referenced by g2_unpack5().

◆ extgridtemplate()

gtemplate* extgridtemplate ( g2int  number,
g2int list 
)

This subroutine generates the remaining octet map for a given Grid Definition Template, if required.

Some Templates can vary depending on data values given in an earlier part of the Template, and it is necessary to know some of the earlier entry values to generate the full octet map of the Template.

This function allocates memory for the extension. The pointer ext in the gtemplate struct must be freed to prevent memory leaks.

Parameters
numberThe number of the Grid Definition Template that is being requested.
listThe list of values for each entry in the Grid Definition Template.
Returns
Pointer to the returned template struct. Returns NULL pointer, if template not found.
Author
Stephen Gilbert
Date
2000-05-09

Definition at line 124 of file gridtemplates.c.

References getgridindex(), and getgridtemplate().

Referenced by g2_addgrid(), and g2_unpack3().

◆ extpdstemplate()

gtemplate* extpdstemplate ( g2int  number,
g2int list 
)

This subroutine generates the remaining octet map for a given Product Definition Template, if required.

Some Templates can vary depending on data values given in an earlier part of the Template, and it is necessary to know some of the earlier entry values to generate the full octet map of the Template.

This function allocates memory in the ext field of the gtemplate struct. This memory must be freed by the caller.

Parameters
numbernumber of the Product Definition Template 4.NN that is being requested.
listThe list of values for each entry in the the Product Definition Template.
Returns
Pointer to the returned template struct. Returns NULL pointer if template not found.
Author
Stephen Gilbert
Date
2000-05-11

Definition at line 121 of file pdstemplates.c.

References getpdsindex(), and getpdstemplate().

Referenced by g2_addfield(), and g2_unpack4().

◆ g2_addfield()

g2int g2_addfield ( unsigned char *  cgrib,
g2int  ipdsnum,
g2int ipdstmpl,
g2float coordlist,
g2int  numcoord,
g2int  idrsnum,
g2int idrstmpl,
g2float fld,
g2int  ngrdpts,
g2int  ibmap,
g2int bmap 
)

This routine packs up Sections 4 through 7 for a given field and adds them to a GRIB2 message.

They are Product Definition Section, Data Representation Section, Bit-Map Section and Data Section, respectively.

This routine is used with routines g2_create(), g2_addlocal(), g2_addgrid(), and g2_gribend() to create a complete GRIB2 message. Function g2_create() must be called first to initialize a new GRIB2 message. Function g2_addgrid() must be called after g2_create() and before this routine to add the appropriate grid description to the GRIB2 message. A call to g2_gribend() is required to complete GRIB2 message after all fields have been added.

Program History Log

Date Programmer Comments
2002-11-05 Gilbert Initial
2002-12-23 Gilbert Added complex spherical harmonic packing
2003-08-27 Gilbert Added support for new templates using PNG and JPEG2000 algorithms/templates.
2004-11-29 Gilbert JPEG2000 now can use WMO Template 5.40 PNG can use WMO Template 5.41. Added packing algorithm check.
2005-05-10 Gilbert Imposed minimum size on cpack.
2009-01-14 Vuong Changed structure name template to gtemplate
Parameters
cgribChar array that contains the GRIB2 message to which sections 4 through 7 should be added. Must be allocated large enough to store the entire GRIB2 message.
ipdsnumProduct Definition Template Number (see Code Table 4.0).
ipdstmplContains the data values for the Product Definition Template specified by ipdsnum.
coordlistArray containg floating point values intended to document the vertical discretisation associated to model data on hybrid coordinate vertical levels.
numcoordnumber of values in array coordlist.
idrsnumData Representation Template Number (see Code Table 5.0).
idrstmplThe data values for the Data Representation Template specified by idrsnum. Note that some values in this template (eg. reference values, number of bits, etc...) may be changed by the data packing algorithms. Use this to specify scaling factors and order of spatial differencing, if desired.
fldArray of data points to pack.
ngrdptsNumber of data points in grid. i.e. size of fld and bmap.
ibmapBitmap indicator (see Code Table 6.0)
  • 0 = bitmap applies and is included in Section 6.
  • 1-253 = Predefined bitmap applies.
  • 254 = Previously defined bitmap applies to this field.
  • 255 = Bit map does not apply to this product.
bmapInteger array containing bitmap to be added (if ibmap = 0).
Returns
  • > 0 Current size of updated GRIB2 message
  • -1 GRIB message was not initialized. Need to call routine g2_create() first.
  • -2 GRIB message already complete. Cannot add new section.
  • -3 Sum of Section byte counts doesn't add to total byte count.
  • -4 Previous Section was not 3 or 7.
  • -5 Could not find requested Product Definition Template.
  • -6 Section 3 (GDS) not previously defined in message.
  • -7 Tried to use unsupported Data Representationi Template.
  • -8 Specified use of a previously defined bitmap, but one does not exist in the GRIB message.
  • -9 GDT of one of 5.50 through 5.53 required to pack field using DRT 5.51.
  • -10 Error packing data field.
Note
Note that the Sections 4 through 7 can only follow Section 3 or Section 7 in a GRIB2 message.
Author
Stephen Gilbert
Date
2002-11-05

Definition at line 106 of file g2_addfield.c.

References cmplxpack(), gtemplate::ext, gtemplate::extlen, extpdstemplate(), gbit(), getdim(), getdrstemplate(), getpdstemplate(), getpoly(), jpcpack(), gtemplate::map, gtemplate::maplen, mkieee(), gtemplate::needext, pngpack(), sbit(), sbits(), simpack(), and specpack().

◆ g2_addgrid()

g2int g2_addgrid ( unsigned char *  cgrib,
g2int igds,
g2int igdstmpl,
g2int ideflist,
g2int  idefnum 
)

This routine packs up a Grid Definition Section (Section 3) and adds it to a GRIB2 message.

It is used with routines g2_create(), g2_addlocal(), g2_addfield(), and g2_gribend() to create a complete GRIB2 message. g2_create() must be called first to initialize a new GRIB2 message.

Program History Log

Date Programmer Comments
2002-11-01 Gilbert Initial.
2009-01-14 Vuong Changed structure name template to gtemplate
Parameters
cgribChar array that contains the GRIB2 message to which section should be added. Must be allocated large enough to store the entire GRIB2 message.
igdsContains information needed for GRIB Grid Definition Section 3. Must be dimensioned >= 5.
  • igds[0] Source of grid definition (see Code Table 3.0).
  • igds[1] Number of grid points in the defined grid.
  • igds[2] Number of octets needed for each additional grid points definition. Used to define number of points in each row (or column) for non-regular grids. = 0, if using regular grid.
  • igds[3] Interpretation of list for optional points definition. (See Code Table 3.11).
  • igds[4] Grid Definition Template Number (See Code Table 3.1).
igdstmplContains the data values for the specified Grid Definition Template (igds[4]). Each element of this integer array contains an entry (in the order specified) of Grid Defintion Template.
ideflist(Used if igds[2] != 0) This array contains the number of grid points contained in each row (or column).
idefnum(Used if igds[2] != 0) The number of entries in array ideflist. i.e. number of rows (or columns) for which optional grid points are defined.
Returns
  • > 0 Current size of updated GRIB2 message
  • -1 GRIB message was not initialized. Need to call routine gribcreate first.
  • -2 GRIB message already complete. Cannot add new section.
  • -3 Sum of Section byte counts doesn't add to total byte count
  • -4 Previous Section was not 1, 2 or 7.
  • -5 Could not find requested Grid Definition Template.
Note
The Grid Def Section (Section 3) can only follow Section 1, 2 or Section 7 in a GRIB2 message.
Author
Stephen Gilbeert
Date
2002-11-01

Definition at line 66 of file g2_addgrid.c.

References gtemplate::ext, extgridtemplate(), gtemplate::extlen, gbit(), getgridtemplate(), gtemplate::map, gtemplate::maplen, gtemplate::needext, sbit(), and sbits().

◆ g2_addlocal()

g2int g2_addlocal ( unsigned char *  cgrib,
unsigned char *  csec2,
g2int  lcsec2 
)

This routine adds a Local Use Section (Section 2) to a GRIB2 message.

It is used with routines g2_create(), g2_addgrid(), g2_addfield(), and g2_gribend() to create a complete GRIB2 message. g2_create() must be called first to initialize a new GRIB2 message.

Parameters
cgribChar array that contains the GRIB2 message to which section 2 should be added. Must be allocated large enough to store the entire GRIB2 message.
csec2Character array containing information to be added in Section 2.
lcsec2Number of bytes of character array csec2 to be added to Section 2.
Returns
> 0 = Current size of updated GRIB2 message.
  • -1 GRIB message was not initialized. Need to call routine gribcreate first.
  • -2 GRIB message already complete. Cannot add new section.
  • -3 Sum of Section byte counts doesn't add to total byte count
  • -4 Previous Section was not 1 or 7.
Note
The Local Use Section (Section 2) can only follow Section 1 or Section 7 in a GRIB2 message.
Author
Stephen Gilbeert
Date
2002-11-01

Definition at line 37 of file g2_addlocal.c.

References gbit(), and sbit().

◆ g2_create()

g2int g2_create ( unsigned char *  cgrib,
g2int listsec0,
g2int listsec1 
)

This routine initializes a new GRIB2 message and packs GRIB2 sections 0 (Indicator Section) and 1 (Identification Section).

This routine is used with routines g2_addlocal(), g2_addgrid(), g2_addfield(), and g2_gribend() to create a complete GRIB2 message. g2_create() must be called first to initialize a new GRIB2 message. A call to g2_gribend() is required to complete GRIB2 message after all fields have been added.

Parameters
[in]cgribCharacter array to contain the GRIB2 message. Must be allocated large enough to store the entire GRIB2 message.
[in]listsec0Contains information needed for GRIB Indicator Section 0. Must be dimensioned >= 2.
  • listsec0[0] Discipline-GRIB Master Table Number (Code Table 0.0).
  • listsec0[1] GRIB Edition Number (currently 2).
[in]listsec1Contains information needed for GRIB Identification Section 1. Must be dimensioned >= 13.
  • listsec1[0] Id of orginating centre (Table 0).
  • listsec1[1] Id of orginating sub-centre (Table C).
  • listsec1[2] GRIB Master Tables Version Number (Table 1.0).
  • listsec1[3] GRIB Local Tables Version Number (Table 1.1).
  • listsec1[4] Significance of Reference Time (Table 1.2)
  • listsec1[5] Reference Time - Year (4 digits)
  • listsec1[6] Reference Time - Month
  • listsec1[7] Reference Time - Day
  • listsec1[8] Reference Time - Hour
  • listsec1[9] Reference Time - Minute
  • listsec1[10] Reference Time - Second
  • listsec1[11] Production status of data (Table 1.3).
  • listsec1[12] Type of processed data (Table 1.4).
Returns
- > 0 Current size of new GRIB2 message
  • -1 Tried to use for version other than GRIB Edition 2

This routine is intended for use with routines g2_addlocal(), g2_addgrid(), g2_addfield(), and g2_gribend() to create a complete GRIB2 message.

Author
Stephen Gilbeert
Date
2002-10-31

Definition at line 63 of file g2_create.c.

References LENSEC0, MAPSEC1LEN, and sbit().

◆ g2_free()

void g2_free ( gribfield gfld)

This routine frees up memory that was allocated for struct gribfield.

Parameters
gfldpointer to gribfield structure (defined in include file grib2.h) returned from routine g2_getfld().
Note
This routine must be called to free up memory used by the decode routine, g2_getfld(), when user no longer needs to reference this data.
Author
Stephen Gilbeert
Date
2002-10-28

Definition at line 24 of file g2_free.c.

References gribfield::bmap, gribfield::coord_list, gribfield::fld, gribfield::idrtmpl, gribfield::idsect, gribfield::igdtmpl, gribfield::ipdtmpl, gribfield::list_opt, and gribfield::local.

◆ g2_getfld()

g2int g2_getfld ( unsigned char *  cgrib,
g2int  ifldnum,
g2int  unpack,
g2int  expand,
gribfield **  gfld 
)

This subroutine returns all the metadata, template values, bit-map (if applicable), and the unpacked data for a given data field.

All of the information returned is stored in a gribfield structure, which is defined in file grib2.h. Users of this routine will need to include grib2.h in their source code that calls this routine.

Since there can be multiple data fields packed into a GRIB2 message, the calling routine indicates which field is being requested with the ifldnum argument.

Program History Log

Date Programmer Comments
2002-10-28 Gilbert Initial
2013-08-08 Vuong Free up memory in array igds - free(igds)
Parameters
cgribCharacter pointer to the GRIB2 message.
ifldnumSpecifies which field in the GRIB2 message to return. The first field is number 1, Fortran style.
unpackBoolean value indicating whether to unpack bitmap/data field.
  • 1 unpack bitmap (if present) and data values.
  • 0 do not unpack bitmap and data values.
expandBoolean value indicating whether the data points should be expanded to the correspond grid, if a bit-map is present. This argument is ignored if unpack == 0 OR if the returned field does not contain a bit-map.
  • 1 if possible, expand data field to grid, inserting zero values at gridpoints that are bitmapped out. (SEE REMARKS2)
  • 0 do not expand data field, leaving it an array of consecutive data points for each "1" in the bitmap.
gfldpointer to structure gribfield containing all decoded data for the data field.
Returns
  • 0 no error
  • 1 Beginning characters "GRIB" not found.
  • 2 GRIB message is not Edition 2.
  • 3 The data field request number was not positive.
  • 4 End string "7777" found, but not where expected.
  • 6 GRIB message did not contain the requested number of data fields.
  • 7 End string "7777" not found at end of message.
  • 8 Unrecognized Section encountered.
  • 9 Data Representation Template 5.NN not yet implemented.
  • 15 Error unpacking Section 1.
  • 16 Error unpacking Section 2.
  • 10 Error unpacking Section 3.
  • 11 Error unpacking Section 4.
  • 12 Error unpacking Section 5.
  • 13 Error unpacking Section 6.
  • 14 Error unpacking Section 7.
  • 17 Previous bitmap specified, yet none exists.
Note
Struct gribfield is allocated by this routine and it also contains pointers to many arrays of data that were allocated during decoding. Users are encouraged to free up this memory, when it is no longer needed, by an explicit call to routine g2_free().

Example:

#include "grib2.h"
gribfield *gfld;
ret=g2_getfld(cgrib,1,1,1,&gfld);
...
g2_free(gfld);
g2int g2_getfld(unsigned char *cgrib, g2int ifldnum, g2int unpack, g2int expand, gribfield **gfld)
This subroutine returns all the metadata, template values, bit-map (if applicable),...
Definition: g2_getfld.c:102
Header file for NCEPLIBS-g2c library.
Struct for GRIB field.
Definition: grib2.h:61

Routine g2_info() can be used to first determine how many data fields exist in a given GRIB message.

Note
It may not always be possible to expand a bit-mapped data field. If a pre-defined bit-map is used and not included in the GRIB2 message itself, this routine would not have the necessary information to expand the data. In this case, gfld->expanded would would be set to 0 (false), regardless of the value of input argument expand.
Author
Stephen Gilbert
Date
2002-10-28

Definition at line 102 of file g2_getfld.c.

References gribfield::bmap, gribfield::coord_list, gribfield::discipline, gribfield::expanded, gribfield::fld, g2_unpack1(), g2_unpack2(), g2_unpack3(), g2_unpack4(), g2_unpack5(), g2_unpack6(), g2_unpack7(), gbit(), gribfield::griddef, gribfield::ibmap, gribfield::idrtlen, gribfield::idrtmpl, gribfield::idrtnum, gribfield::idsect, gribfield::idsectlen, gribfield::ifldnum, gribfield::igdtlen, gribfield::igdtmpl, gribfield::igdtnum, gribfield::interp_opt, gribfield::ipdtlen, gribfield::ipdtmpl, gribfield::ipdtnum, gribfield::list_opt, gribfield::local, gribfield::locallen, gribfield::ndpts, gribfield::ngrdpts, gribfield::num_coord, gribfield::num_opt, gribfield::numoct_opt, gribfield::unpacked, and gribfield::version.

◆ g2_gribend()

g2int g2_gribend ( unsigned char *  cgrib)

This routine finalizes a GRIB2 message after all grids and fields have been added.

It adds the End Section ("7777") to the end of the GRIB message and calculates the length and stores it in the appropriate place in Section 0. This routine is used with routines g2_create(), g2_addlocal(), g2_addgrid(), and g2_addfield() to create a complete GRIB2 message.

Parameters
cgribChar array containing all the data sections added be previous calls to g2_create(), g2_addlocal(), g2_addgrid(), and g2_addfield(). After function is called, contains the finalized GRIB2 message.
Returns
  • > 0 Length of the final GRIB2 message in bytes.
  • -1 GRIB message was not initialized. Need to call routine g2_create first.
  • -2 GRIB message already complete.
  • -3 Sum of Section byte counts doesn't add to total byte count
  • -4 Previous Section was not 7.
Note
This routine is intended for use with routines g2_create(), g2_addlocal(), g2_addgrid(), and g2_addfield() to create a complete GRIB2 message.
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 35 of file g2_gribend.c.

References gbit(), and sbit().

◆ g2_info()

g2int g2_info ( unsigned char *  cgrib,
g2int listsec0,
g2int listsec1,
g2int numfields,
g2int numlocal 
)

This subroutine searches through a GRIB2 message and returns the number of gridded fields found in the message and the number (and maximum size) of Local Use Sections.

Also various checks are performed to see if the message is a valid GRIB2 message.

Parameters
cgribCharacter pointer to the GRIB2 message.
listsec0pointer to an array containing information decoded from GRIB Indicator Section 0. Must be allocated with >= 3 elements.
  • listsec0(0) Discipline-GRIB Master Table Number (Code Table 0.0).
  • listsec0[1] GRIB Edition Number (currently 2).
  • listsec0[2] Length of GRIB message.
listsec1Pointer to an array containing information read from GRIB Identification Section 1. Must be allocated with >= 13 elements.
  • listsec1[0] Id of orginating centre (Table 0).
  • listsec1[1] Id of orginating sub-centre (Table C).
  • listsec1[2] GRIB Master Tables Version Number (Table 1.0).
  • listsec1[3] GRIB Local Tables Version Number (Table 1.1).
  • listsec1[4] Significance of Reference Time (Table 1.2)
  • listsec1[5] Reference Time - Year (4 digits)
  • listsec1[6] Reference Time - Month
  • listsec1[7] Reference Time - Day
  • listsec1[8] Reference Time - Hour
  • listsec1[9] Reference Time - Minute
  • listsec1[10] Reference Time - Second
  • listsec1[11] Production status of data (Table 1.3).
  • listsec1[12] Type of processed data (Table 1.4).
numfieldsThe number of gridded fields found in the GRIB message. That is, the number of occurences of Sections 4 - 7.
numlocalThe number of Local Use Sections ( Section 2 ) found in the GRIB message.
Returns
0 foe success, otherwise:
  • 1 Beginning characters "GRIB" not found.
  • 2 GRIB message is not Edition 2.
  • 3 Could not find Section 1, where expected.
  • 4 End string "7777" found, but not where expected.
  • 5 End string "7777" not found at end of message.
  • 6 Invalid section number found.
Author
Stephen Gilbeert
Date
2002-10-28

Definition at line 67 of file g2_info.c.

References gbit().

◆ g2_unpack1()

g2int g2_unpack1 ( unsigned char *  cgrib,
g2int iofst,
g2int **  ids,
g2int idslen 
)

This subroutine unpacks Section 1 - Identification Section as defined in GRIB Edition 2.

Parameters
cgribchar array containing Section 1 of the GRIB2 message.
iofstBit offset for the beginning of Section 1 in cgrib.
idsPointer that gets an array which contians the information read from Section 1, the Identification section. This array is allocated by this function, and must be freed by caller (using g2_free()).
  • ids[0] Identification of originating Centre (see Table 0).
  • ids[1] Identification of originating Sub-centre (see Table C).
  • ids[2] GRIB Master Tables Version Number (see Table 1.0).
  • ids[3] GRIB Local Tables Version Number (see Table 1.1).
  • ids[4] Significance of Reference Time (see Table 1.2).
  • ids[5] Year (4 digits)
  • ids[6] Month
  • ids[7] Day
  • ids[8] Hour
  • ids[9] Minute
  • ids[10] Second
  • ids[11] Production status of processed data (see Table 1.3).
  • ids[12] Type of processed data (see Table 1.4).
idslenNumber of elements in ids.
Returns
  • 0 no error
  • 2 Array passed is not section 1
  • 6 memory allocation error
Author
Stephen Gilbert
Date
2002-10-29

Definition at line 55 of file g2_unpack1.c.

◆ g2_unpack3()

g2int g2_unpack3 ( unsigned char *  cgrib,
g2int iofst,
g2int **  igds,
g2int **  igdstmpl,
g2int mapgridlen,
g2int **  ideflist,
g2int idefnum 
)

This routine unpacks Section 3 (Grid Definition Section) as defined in GRIB Edition 2.

Program History Log

Date Programmer Comments
2002-10-31 Gilbert Initial
2009-01-14 Vuong Changed structure name template to gtemplate
Parameters
cgribChar array ontaining Section 3 of the GRIB2 message.
iofstBit offset for the beginning of Section 3 in cgrib.
igdsContains information read from the appropriate GRIB Grid Definition Section 3 for the field being returned.
  • igds[0] Source of grid definition (see Table 3.0).
  • igds[1] Number of grid points in the defined grid.
  • igds[2] Number of octets needed for each additional grid points definition. Used to define number of points in each row (or column) for non-regular grids. = 0, if using regular grid.
  • igds[3] Interpretation of list for optional points definition. (See Table 3.11)
  • igds[4] Grid Definition Template Number (see Table 3.1).
igdstmplPointer to integer array containing the data values for the Grid Definition Template specified by igds[4].
mapgridlenNumber of elements in igdstmpl. i.e. number of entries in Grid Defintion Template specified by igds[4].
ideflist(Used if igds[2] .ne. 0) Pointer to integer array containing the number of grid points contained in each row (or column).
idefnum(Used if igds[2] .ne. 0) The number of entries in array ideflist - i.e. number of rows (or columns) for which optional grid points are defined.
Returns
  • 0 no error
  • 2 Not Section 3
  • 5 message contains an undefined Grid Definition Template.
  • 6 memory allocation error
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 57 of file g2_unpack3.c.

◆ g2_unpack4()

g2int g2_unpack4 ( unsigned char *  cgrib,
g2int iofst,
g2int ipdsnum,
g2int **  ipdstmpl,
g2int mappdslen,
g2float **  coordlist,
g2int numcoord 
)

This subroutine unpacks Section 4 (Product Definition Section) as defined in GRIB Edition 2.

Program History Log

Date Programmer Comments
2002-10-31 Gilbert Initial
2009-01-14 Vuong Changed structure name template to gtemplate
Parameters
cgribArray containing Section 4 of the GRIB2 message.
iofstBit offset of the beginning of Section 4 in cgrib. Returned with updated bit offset.
ipdsnumProduct Definition Template Number (see Table 4.0).
ipdstmplPointer that gets an integer array containing the data values for the Product Definition Template specified by ipdsnum.
mappdslenNumber of elements in ipdstmpl - i.e. number of entries in Product Defintion Template specified by ipdsnum.
coordlistPointer that gets an array containing floating point values intended to document the vertical discretisation associated to model data on hybrid coordinate vertical levels.
numcoordnumber of values in array coordlist.
Returns
  • 0 no error
  • 2 Not section 4
  • 5 "GRIB" message contains an undefined Product Definition Template.
  • 6 memory allocation error
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 45 of file g2_unpack4.c.

◆ g2_unpack5()

g2int g2_unpack5 ( unsigned char *  cgrib,
g2int iofst,
g2int ndpts,
g2int idrsnum,
g2int **  idrstmpl,
g2int mapdrslen 
)

This subroutine unpacks Section 5 (Data Representation Section) as defined in GRIB Edition 2.

Program History Log

Date Programmer Comments
2002-10-31 Gilbert Initial
2009-01-14 Vuong Changed structure name template to gtemplate
Parameters
cgribchar array containing Section 5 of the GRIB2 message.
iofstBit offset for the beginning of Section 5 in cgrib. Returned with bit offset at the end of Section 5.
ndptsNumber of data points unpacked and returned.
idrsnumData Representation Template Number (see Code Table 5.0).
idrstmplPointer to an integer array containing the data values for the specified Data Representation Template (N=idrsnum). Each element of this integer array contains an entry (in the order specified) of Data Representation Template 5.N.
mapdrslen-Number of elements in idrstmpl. i.e. number of entries in Data Representation Template 5.N (N=idrsnum).
Returns
  • 0 no error
  • 2 Not Section 5
  • 6 memory allocation error
  • 7 "GRIB" message contains an undefined Data Representation Template.
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 42 of file g2_unpack5.c.

◆ g2_unpack6()

g2int g2_unpack6 ( unsigned char *  cgrib,
g2int iofst,
g2int  ngpts,
g2int ibmap,
g2int **  bmap 
)

This subroutine unpacks Section 6 (Bit-Map Section) as defined in GRIB Edition 2.

Parameters
cgribchar array containing Section 6 of the GRIB2 message.
iofstBit offset of the beginning of Section 6 in cgrib.
ngptsNumber of grid points specified in the bit-map
ibmapBitmap indicator (see Code Table 6.0)
  • 0 bitmap applies and is included in Section 6.
  • 1-253 Predefined bitmap applies
  • 254 Previously defined bitmap applies to this field
  • 255 Bit map does not apply to this product.
bmapPointer to an integer array containing decoded bitmap. (if ibmap=0)
Returns
  • 0 no error
  • 2 Not Section 6
  • 4 Unrecognized pre-defined bit-map.
  • 6 memory allocation error
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 33 of file g2_unpack6.c.

◆ g2_unpack7()

g2int g2_unpack7 ( unsigned char *  cgrib,
g2int iofst,
g2int  igdsnum,
g2int igdstmpl,
g2int  idrsnum,
g2int idrstmpl,
g2int  ndpts,
g2float **  fld 
)

This subroutine unpacks Section 7 (Data Section) as defined in GRIB Edition 2.

Program History Log

Date Programmer Comments
2002-10-31 Gilbert Initial
2002-12-20 Gilbert Added GDT info to arguments and added 5.51 processing.
2003-08-29 Gilbert New templates using PNG and JPEG2000 algorithms/templates.
2004-11-29 Gilbert JPEG2000 now allowed to use WMO Template 5.40 PNG allowed to use 5.41
2004-12-16 Taylor Added check on comunpack return code.
2008-12-23 Wesley Initialize Number of data points unpacked
Parameters
cgribchar array containing Section 7 of the GRIB2 message
iofstBit offset of the beginning of Section 7 in cgrib.
igdsnumGrid Definition Template Number (see Code Table 3.0) (Only used for DRS Template 5.51)
igdstmplPointer to an integer array containing the data values for the specified Grid Definition Template (N=igdsnum). Each element of this integer array contains an entry (in the order specified) of Grid Definition Template 3.N. (Only used for DRS Template 5.51).
idrsnumData Representation Template Number (see Code Table 5.0)
idrstmplPointer to an integer array containing the data values for the specified Data Representation Template (N=idrsnum). Each element of this integer array contains an entry (in the order specified) of Data Representation Template 5.N
ndptsNumber of data points unpacked and returned.
fldPointer to a float array containing the unpacked data field.
Returns
  • 0 no error
  • 2 Not section 7
  • 4 Unrecognized Data Representation Template
  • 5 need one of GDT 3.50 through 3.53 to decode DRT 5.51
  • 6 memory allocation error
  • 7 corrupt section 7.
Author
Stephen Gilbert
Date
2002-10-31

Definition at line 65 of file g2_unpack7.c.

◆ gbit()

void gbit ( unsigned char *  in,
g2int iout,
g2int  iskip,
g2int  nbyte 
)

Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked iout array.

Parameters
inpointer to character array input.
ioutpointer to unpacked array output.
iskipinitial number of bits to skip.
nbytenumber of bits to take.
Author
NOAA Programmer

Definition at line 20 of file gbits.c.

References gbits().

Referenced by comunpack(), g2_addfield(), g2_addgrid(), g2_addlocal(), g2_getfld(), g2_gribend(), g2_info(), g2_unpack1(), g2_unpack2(), g2_unpack3(), g2_unpack4(), g2_unpack5(), g2_unpack6(), g2_unpack7(), and seekgb().

◆ gbits()

void gbits ( unsigned char *  in,
g2int iout,
g2int  iskip,
g2int  nbyte,
g2int  nskip,
g2int  n 
)

Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked iout array.

Parameters
inPointer to character array input.
ioutPointer to unpacked array output.
iskipInitial number of bits to skip.
nbyteNumber of bits to take.
nskipAdditional number of bits to skip on each iteration.
nNumber of iterations.
Author
NOAA Programmer

Definition at line 57 of file gbits.c.

Referenced by comunpack(), g2_unpack3(), g2_unpack4(), g2_unpack6(), gbit(), pngunpack(), simunpack(), and specunpack().

◆ getdrstemplate()

gtemplate* getdrstemplate ( g2int  number)

This subroutine returns DRS template information for a specified Data Representation Template.

The number of entries in the template is returned along with a map of the number of octets occupied by each entry. Also, a flag is returned to indicate whether the template would need to be extended.

Parameters
numberThe number of the Data Representation Template that is being requested.
Returns
Pointer to the returned template struct. Returns NULL if template not found.
Author
Stephen Gilbert
Date
2000-05-11

Definition at line 64 of file drstemplates.c.

References getdrsindex(), drstemplate::mapdrslen, drstemplate::needext, drstemplate::template_num, and templatesdrs.

Referenced by extdrstemplate(), g2_addfield(), and g2_unpack5().

◆ getgridtemplate()

gtemplate* getgridtemplate ( g2int  number)

This subroutine returns grid template information for a specified Grid Definition Template for [Section 3 - the Grid Definition Section (GDS)](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_sect3.shtml).

The number of entries in the template is returned along with a map of the number of octets occupied by each entry. Also, a flag is returned to indicate whether the template would need to be extended.

This function allocates storage for the template. The returned pointer must be freed by the caller.

Parameters
numberThe number of the Grid Definition Template that is being requested.
Returns
Pointer to the returned template struct (must be freed by caller). Returns NULL pointer, if template not found.
Author
Stephen Gilbert
Date
2000-05-09

Definition at line 75 of file gridtemplates.c.

References getgridindex(), gridtemplate::mapgridlen, gridtemplate::needext, gridtemplate::template_num, and templatesgrid.

Referenced by extgridtemplate(), g2_addgrid(), and g2_unpack3().

◆ getpdstemplate()

gtemplate* getpdstemplate ( g2int  number)

This subroutine returns PDS template information for a specified Product Definition Template.

The number of entries in the template is returned along with a map of the number of octets occupied by each entry. Also, a flag is returned to indicate whether the template would need to be extended.

This function allocates memory for the gtemplate struct, which must be freed by the caller.

Parameters
numberthe number of the Product Definition Template that is being requested.
Returns
Pointer to the returned template struct. Returns NULL pointer if template not found.
Author
Stephen Gilbert
Date
2000-05-11

Definition at line 73 of file pdstemplates.c.

References getpdsindex(), pdstemplate::mappdslen, pdstemplate::needext, pdstemplate::template_num, and templatespds.

Referenced by extpdstemplate(), g2_addfield(), and g2_unpack4().

◆ int_power()

double int_power ( double  x,
g2int  y 
)

Function similar to C pow() power function.

Parameters
xThe base value whose power is to be calculated.
yThe power value.
Returns
x**y
Author
Wesley Ebisuzaki

Definition at line 17 of file int_power.c.

Referenced by compack(), comunpack(), jpcpack(), jpcunpack(), misspack(), mkieee(), pngpack(), pngunpack(), rdieee(), simpack(), simunpack(), and specunpack().

◆ misspack()

void misspack ( g2float fld,
g2int  ndpts,
g2int  idrsnum,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs up a data field using a complex packing algorithm as defined in the GRIB2 documention.

It supports GRIB2 complex packing templates with or without spatial differences (i.e. DRTs 5.2 and 5.3). It also fills in GRIB2 Data Representation Template 5.2 or Template 5.3 with the appropriate values. This version assumes that Missing Value Management is being used and that 1 or 2 missing values appear in the data.

Parameters
fldContains the data values to pack
ndptsThe number of data values in array fld
idrsnumData Representation Template number 5.N Must equal 2 or 3.
idrstmplContains the array of values for Data Representation Template 5.2 or 5.3.
  • 0 Reference value - ignored on input, set by misspack routine.
  • 1 Binary Scale Factor - used on input.
  • 2 Decimal Scale Factor- used on input.
  • 6 Missing value management.
  • 7 Primary missing value.
  • 8 Secondary missing value.
  • 16 Order of Spatial Differencing (1 or 2).
cpackThe packed data field (character*1 array).
lcpacklength of packed field cpack.
Author
Stephen Gilbert
Date
2000-06-21

Definition at line 43 of file misspack.c.

References int_power(), mkieee(), pack_gp(), rdieee(), sbit(), and sbits().

Referenced by cmplxpack().

◆ mkieee()

void mkieee ( g2float a,
g2int rieee,
g2int  num 
)

This subroutine stores a list of real values in 32-bit IEEE floating point format.

Parameters
aInput array of floating point values.
numNumber of floating point values to convert.
rieeeOutput array of data values in 32-bit IEEE format stored in g2int integer array. rieee must be allocated with at least 4*num bytes of memory before calling this function.
Author
Stephen Gilbert
Date
2002-10-29

Definition at line 21 of file mkieee.c.

References int_power().

Referenced by compack(), g2_addfield(), jpcpack(), misspack(), pngpack(), simpack(), and specpack().

◆ pack_gp()

int pack_gp ( integer kfildo,
integer ic,
integer nxy,
integer is523,
integer minpk,
integer inc,
integer missp,
integer misss,
integer jmin,
integer jmax,
integer lbit,
integer nov,
integer ndg,
integer lx,
integer ibit,
integer jbit,
integer kbit,
integer novref,
integer lbitref,
integer ier 
)

Determines groups of variable size, but at least of size minpk, the associated max (jmax( )) and min (jmin( )), the number of bits necessary to hold the values in each group (lbit( )), the number of values in each group (nov( )), the number of bits necessary to pack the jmin( ) values (ibit), the number of bits necessary to pack the lbit( ) values (jbit), and the number of bits necessary to pack the nov( ) values (kbit).

The routine is designed to determine the groups such that a small number of bits is necessary to pack the data without excessive computations. If all values in the group are zero, the number of bits to use in packing is defined as zero when there can be no missing values; when there can be missing values, the number of bits must be at least 1 to have the capability to recognize the missing value. However, if all values in a group are missing, the number of bits needed is 0, and the unpacker recognizes this. All variables are integer. Even though the groups are initially of size minpk or larger, an adjustment between two groups (the lookback procedure) may make a group smaller than minpk. The control on group size is that the sum of the sizes of the two consecutive groups, each of size minpk or larger, is not decreased. When determining the number of bits necessary for packing, the largest value that can be accommodated in, say, mbits, is 2**mbits-1; this largest value (and the next smallest value) is reserved for the missing value indicator (only) when is523 ne 0. If the dimension ndg is not large enough to hold all the groups, the local value of minpk is increased by 50 percent. This is repeated until ndg will suffice. A diagnostic is printed whenever this happens, which should be very rarely. If it happens often, ndg in subroutine pack should be increased and a corresponding increase in subroutine unpack made. Considerable code is provided so that no more checking for missing values within loops is done than necessary; the added efficiency of this is relatively minor, but does no harm. For grib2, the reference value for the length of groups in nov( ) and for the number of bits necessary to pack group values are determined, and subtracted before jbit and kbit are determined.

When 1 or more groups are large compared to the others, the width of all groups must be as large as the largest. A subroutine reduce breaks up large groups into 2 or more to reduce total bits required. If reduce should abort, pack_gp will be executed again without the call to reduce.

PROGRAM HISTORY LOG:

  • February 1994 Glahn tdl mos-2000
  • June 1995 Glahn modified for lmiss error.
  • July 1996 Glahn added misss
  • February 1997 Glahn removed 4 redundant tests for missp.eq.0; inserted a test to better handle a string of 9999's
  • February 1997 Glahn added loops to eliminate test for misss when misss = 0
  • March 1997 Glahn corrected for secondary missing value
  • March 1997 Glahn corrected for use of local value of minpk
  • March 1997 Glahn corrected for secondary missing value
  • March 1997 Glahn changed calculating number of bits through exponents to an array (improved overall packing performance by about 35 percent!). allowed 0 bits for packing jmin( ), lbit( ), and nov( ).
  • May 1997 Glahn a number of changes for efficiency. mod functions eliminated and one ifthen added. jount removed. recomputation of bits not made unless necessary after moving points from one group to another. nendb adjusted to eliminate possibility of very small group at the end. about 8 percent improvement in overall packing. iskipa removed; there is always a group b that can become group a. control on size of group b (statement below 150) added. added adda, and use of ge and le instead of gt and lt in loops between 150 and 160. ibitbs added to shorten trips through loop.
  • March 2000 Glahn modified for grib2; changed name from packgp
  • january 2001 Glahn comments; ier = 706 substituted for stops; added return1; removed statement number 110; added ier and * return
  • November 2001 Glahn changed some diagnostic formats to allow printing larger numbers
  • November 2001 Glahn added misslx( ) to put maximum value into jmin( ) when all values missing to agree with grib standard.
  • November 2001 Glahn changed two tests on missp and misss eq 0 to tests on is523. however, missp and misss cannot in general be = 0.
  • November 2001 Glahn added call to reduce; defined itest before loops to reduce computation; started large group when all same value
  • December 2001 Glahn modified and added a few comments
  • January 2002 Glahn removed loop before 150 to determine a group of all same value
  • January 2002 Glahn changed mallow from 9999999 to 2**30+1, and made it a parameter
  • March 2002 Glahn added non fatal ier = 716, 717; removed nendb=nxy above 150; added iersav=0; comments

DATA SET USE

  • kfildo - unit number for output (print) file. (output)
Parameters
kfildounit number for output (print) file. (input)
icarray to hold data for packing. the values do not have to be positive at this point, but must be in the range -2**30 to +2**30 (the the value of mallow). these integer values will be retained exactly through packing and unpacking. (input)
nxynumber of values in ic( ). also treated as its dimension. (input)
is523missing value management 0=data contains no missing values 1=data contains primary missing values 2=data contains primary and secondary missing values (input)
minpkthe minimum size of each group, except possibly the last one. (input)
incthe number of values to add to an already existing group in determining whether or not to start a new group. ideally, this would be 1, but each time inc values are attempted, the max and min of the next minpk values must be found. this is "a loop within a loop," and a slightly larger value may give about as good results with slightly less computational time. if inc is le 0, 1 is used, and a diagnostic is output. note: it is expected that inc will equal 1. the code uses inc primarily in the loops starting at statement 180. if inc were 1, there would not need to be loops as such. however, kinc (the local value of inc) is set ge 1 when near the end of the data to forestall a very small group at the end. (input)
misspwhen missing points can be present in the data, they will have the value missp or misss. missp is the primary missing value and misss is the secondary missing value . these must not be values that would occur with subtracting the minimum (reference) value or scaling. for example, missp = 0 would not be advisable. (input)
missssecondary missing value indicator (see missp). (input)
jminthe minimum of each group (j=1,lx). (output)
jmaxthe maximum of each group (j=1,lx). this is not really needed, but since the max of each group must be found, saving it here is cheap in case the user wants it. (output)
lbitthe number of bits necessary to pack each group (j=1,lx). it is assumed the minimum of each group will be removed before packing, and the values to pack will, therefore, all be positive. however, ic( ) does not necessarily contain all positive values. if the overall minimum has been removed (the usual case), then ic( ) will contain only positive values. (output)
novthe number of values in each group (j=1,lx). (output)
ndgthe dimension of jmin, jmax, lbit, and nov. (input)
lxthe number of groups determined. (output)
ibitthe number of bits necessary to pack the jmin(j) values, j=1,lx. (output)
jbitthe number of bits necessary to pack the lbit(j) values, j=1,lx. (output)
kbitthe number of bits necessary to pack the nov(j) values, j=1,lx. (output)
novrefreference value for nov( ). (output)
lbitrefreference value for lbit( ). (output)
ierError code
  • 0 No error.
  • 706 value will not pack in 30 bits–fatal
  • 714 error in reduce–non-fatal
  • 715 ngp not large enough in reduce–non-fatal
  • 716 minpk inceased–non-fatal
  • 717 inc set
  • 1–non-fatal
  • alternate return when ier ne 0 and fatal error.
Returns
0 - check ier for error code.
   INTERNAL VARIABLES
              cfeed = contains the character representation
                      of a printer form feed.
              ifeed = contains the integer value of a printer
                      form feed.
               kinc = working copy of inc. may be modified.
               mina = minimum value in group a.
               maxa = maximum value in group a.
              nenda = the place in ic( ) where group a ends.
             kstart = the place in ic( ) where group a starts.
              ibita = number of bits needed to hold values in group a.
               minb = minimum value in group b.
               maxb = maximum value in group b.
              nendb = the place in ic( ) where group b ends.
              ibitb = number of bits needed to hold values in group b.
               minc = minimum value in group c.
               maxc = maximum value in group c.
             ktotal = count of number of values in ic( ) processed.
              nount = number of values added to group a.
              lmiss = 0 when is523 = 0. when packing into a
                      specific number of bits, say mbits,
                      the maximum value that can be handled is
                      2**mbits-1. when is523 = 1, indicating
                      primary missing values, this maximum value
                      is reserved to hold the primary missing value
                      indicator and lmiss = 1. when is523 = 2,
                      the value just below the maximum i.e.,
                      2**mbits-2 is reserved to hold the secondary
                      missing value indicator and lmiss = 2.
             lminpk = local value of minpk. this will be adjusted
                      upward whenever ndg is not large enough to hold
                      all the groups.
             mallow = the largest allowable value for packing.
             mislla = set to 1 when all values in group a are missing.
                      this is used to distinguish between a real
                      minimum when all values are not missing
                      and a minimum that has been set to zero when
                      all values are missing. 0 otherwise.
                      note that this does not distinguish between
                      primary and secondary missings when secondary
                      missings are present. this means that
                      lbit( ) will not be zero with the resulting
                      compression efficiency when secondary missings
                      are present. also note that a check has been
                      made earlier to determine that secondary
                      missings are really there.
             misllb = set to 1 when all values in group b are missing.
                      this is used to distinguish between a real
                      minimum when all values are not missing
                      and a minimum that has been set to zero when
                      all values are missing. 0 otherwise.
             misllc = performs the same function for group c that
                      mislla and misllb do for groups b and c,
                      respectively.
           ibxx2(j) = an array that when this routine is first entered
                      is set to 2**j, j=0,30. ibxx2(30) = 2**30, which
                      is the largest value packable, because 2**31
                      is larger than the integer word size.
             ifirst = set by data statement to 0. changed to 1 on
                      first
                      entry when ibxx2( ) is filled.
              minak = keeps track of the location in ic( ) where the
                      minimum value in group a is located.
              maxak = does the same as minak, except for the maximum.
              minbk = the same as minak for group b.
              maxbk = the same as maxak for group b.
              minck = the same as minak for group c.
              maxck = the same as maxak for group c.
               adda = keeps track whether or not an attempt to add
                      points to group a was made. if so, then adda
                      keeps from trying to put one back into b.
                      (logical)
             ibitbs = keeps current value if ibitb so that loop
                      ending at 166 doesn't have to start at
                      ibitb = 0 every time.
          misslx(j) = mallow except when a group is all one value (and
                      lbit(j) = 0) and that value is missing. in
                      that case, misslx(j) is missp or misss. this
                      gets inserted into jmin(j) later as the
                      missing indicator; it can't be put in until
                      the end, because jmin( ) is used to calculate
                      the maximum number of bits (ibits) needed to
                      pack jmin( ).

Definition at line 256 of file pack_gp.c.

References FALSE_, reduce(), and TRUE_.

Referenced by compack(), and misspack().

◆ rdieee()

void rdieee ( g2int rieee,
g2float a,
g2int  num 
)

This subroutine reads a list of real values in 32-bit IEEE floating point format.

Parameters
rieeeg2int array of floating point values in 32-bit IEEE format.
numNumber of floating point values to convert.
afloat array of real values. a must be allocated with at least 4*num bytes of memory before calling this function.
Author
Stephen Gilbert
Date
2002-10-25

Definition at line 20 of file rdieee.c.

References int_power().

Referenced by comunpack(), g2_miss(), g2_unpack4(), g2_unpack7(), jpcunpack(), misspack(), pngunpack(), simunpack(), and specunpack().

◆ sbit()

void sbit ( unsigned char *  out,
g2int in,
g2int  iskip,
g2int  nbyte 
)

Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each value in the unpacked array.

Parameters
outPointer to packed array output. Must be allocated large enough to hold output.
inPointer to unpacked array input.
iskipInitial number of bits to skip.
nbyteNumber of bits to pack.
Author
NOAA Programmer

Definition at line 38 of file gbits.c.

References sbits().

Referenced by compack(), g2_addfield(), g2_addgrid(), g2_addlocal(), g2_create(), g2_gribend(), misspack(), and simpack().

◆ sbits()

void sbits ( unsigned char *  out,
g2int in,
g2int  iskip,
g2int  nbyte,
g2int  nskip,
g2int  n 
)

Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each value in the unpacked array.

Parameters
outPointer to packed array output. Must be allocated large enough to hold output.
inPointer to unpacked array input.
iskipInitial number of bits to skip.
nbyteNumber of bits to pack.
nskipAdditional number of bits to skip on each iteration.
nNumber of iterations.
Author
NOAA Programmer

Definition at line 114 of file gbits.c.

Referenced by compack(), g2_addfield(), g2_addgrid(), jpcpack(), misspack(), pngpack(), sbit(), and simpack().

◆ seekgb()

void seekgb ( FILE *  lugb,
g2int  iseek,
g2int  mseek,
g2int lskip,
g2int lgrib 
)

This subprogram searches a file for the next GRIB Message.

The search is done starting at byte offset iseek of the file referenced by lugb for mseek bytes at a time. If found, the starting position and length of the message are returned in lskip and lgrib, respectively. The search is terminated when an EOF or I/O error is encountered.

PROGRAM HISTORY LOG:

  • 2002-10-28 GILBERT Modified from Iredell's skgb subroutine
  • 2009-01-16 VUONG Changed lskip to 4 instead of sizof(g2int)
Parameters
lugbFILE pointer for the file to search. File must be opened before this routine is called.
iseeknumber of bytes in the file to skip before search.
mseeknumber of bytes to search at a time.
lskipnumber of bytes to skip from the beggining of the file to where the GRIB message starts.
lgribnumber of bytes in message (set to 0, if no message found).
Author
Stephen Gilbert
Date
2002-10-28

Definition at line 32 of file seekgb.c.

References gbit().

◆ simpack()

void simpack ( g2float fld,
g2int  ndpts,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs up a data field using the simple packing algorithm as defined in the GRIB2 documention.

It also fills in GRIB2 Data Representation Template 5.0 with the appropriate values.

Parameters
fldContains the data values to pack.
ndptsThe number of data values in array fld.
idrstmplContains the array of values for [Data Representation Template 5.0](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-0.shtml).
  • 0 Reference value - ignored on input - set by simpack routine.
  • 1 Binary Scale Factor - unchanged from input.
  • 2 Decimal Scale Factor - unchanged from input.
  • 3 Number of bits used to pack data, if value is > 0 and <= 31. If this input value is 0 or outside above range then the num of bits is calculated based on given data and scale factors.
  • 4 Original field type - currently ignored on input. Data values assumed to be reals. Set to 0 by simpack routine.
cpackThe packed data field
lcpacklength of packed field starting at cpack.
Author
Stephen Gilbert
Date
2002-11-06

Definition at line 34 of file simpack.c.