---
name: "g2_addfield.c"
section: "3"
platform: "linux"
package: "libg2c-dev"
version: "2.1.0-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/g2_addfield.c"
---

# g2\_addfield.c(3)

Pack sections 4 through 7 and add them to a GRIB2 message.

## NAME

g2\_addfield.c - Pack sections 4 through 7 and add them to a GRIB2 message.

## SYNOPSIS

`#include 'grib2_int.h'`\
 `#include <stdio.h>`\
 `#include <stdlib.h>`\

### Functions

**g2int** **g2\_addfield** (unsigned char \*cgrib, **g2int** ipdsnum, **g2int** \*ipdstmpl, float \*coordlist, **g2int** numcoord, **g2int** idrsnum, **g2int** \*idrstmpl, float \*fld, **g2int** ngrdpts, **g2int** ibmap, **g2int** \*bmap)\
 Pack sections 4 through 7 and adds them to a GRIB2 message.

## Detailed Description

Pack sections 4 through 7 and add them to a GRIB2 message.

**Author**

Stephen Gilbert

**Date**

2002-11-05

Definition in file **g2\_addfield.c**.

## Function Documentation

### g2int g2\_addfield (unsigned char \* cgrib, g2int ipdsnum, g2int \* ipdstmpl, float \* coordlist, g2int numcoord, g2int idrsnum, g2int \* idrstmpl, float \* fld, g2int ngrdpts, g2int ibmap, g2int \* bmap)

Pack sections 4 through 7 and adds them to a GRIB2 message. They are:

- 4\.

  `Product Definition Section`

- 5\.

  `Data Representation Section`

- 6\.

  `Bit-Map Section`

- 7\.

  `Data Section`

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    2023-09-08   Engle   Added support for new template, 5.42, using CCSDS compression (libaec).

**Parameters**

*cgrib* Char 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. \
 *ipdsnum* Product Definition Template Number (see `Code Table 4.0`). \
 *ipdstmpl* Contains the data values for the Product Definition Template specified by ipdsnum. \
 *coordlist* Array containg floating point values intended to document the vertical discretisation associated to model data on hybrid coordinate vertical levels. \
 *numcoord* number of values in array coordlist. \
 *idrsnum* Data Representation Template Number (see `Code Table 5.0`). \
 *idrstmpl* The 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. \
 *fld* Array of data points to pack. \
 *ngrdpts* Number of data points in grid. i.e. size of fld and bmap. \
 *ibmap* Bitmap 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.

*bmap* Integer array containing bitmap to be added (if ibmap = 0).

**Returns**

- •

  \> 0 Current size of updated GRIB2 message

- •

  **G2\_ADD\_MSG\_INIT** GRIB message was not initialized. Need to call routine **g2\_create()** first.

- •

  **G2\_ADD\_MSG\_COMPLETE** GRIB message already complete. Cannot add new section.

- •

  **G2\_BAD\_SEC\_COUNTS** Sum of Section byte counts doesn't add to total byte count.

- •

  **G2\_BAD\_SEC** Previous Section was not 3 or 7.

- •

  **G2\_ADDFIELD\_BAD\_PDT** Could not find requested Product Definition Template.

- •

  **G2\_ADDFIELD\_BAD\_GDS** Section 3 (GDS) not previously defined in message.

- •

  **G2\_ADDFIELD\_BAD\_DRT** Tried to use unsupported Data Representationi Template.

- •

  **G2\_ADDFIELD\_BAD\_BITMAP** Specified use of a previously defined bitmap, but one does not exist in the GRIB message.

- •

  **G2\_ADDFIELD\_BAD\_GDT** GDT of one of 5.50 through 5.53 required to pack field using DRT 5.51.

- •

  **G2\_ADDFIELD\_ERR** 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 **100** of file **g2\_addfield.c**.

References **aecpack()**, **cmplxpack()**, **gtemplate::ext**, **gtemplate::extlen**, **extpdstemplate()**, **G2\_ADD\_MSG\_COMPLETE**, **G2\_ADD\_MSG\_INIT**, **G2\_ADDFIELD\_BAD\_BITMAP**, **G2\_ADDFIELD\_BAD\_DRT**, **G2\_ADDFIELD\_BAD\_GDS**, **G2\_ADDFIELD\_BAD\_GDT**, **G2\_ADDFIELD\_BAD\_PDT**, **G2\_ADDFIELD\_ERR**, **G2\_BAD\_SEC**, **G2\_BAD\_SEC\_COUNTS**, **g2c\_check\_msg()**, **G2C\_EMSGCOMPLETE**, **G2C\_ENOTGRIB**, **gbit()**, **getdim()**, **getdrstemplate()**, **getpdstemplate()**, **getpoly()**, **jpcpack()**, **gtemplate::map**, **gtemplate::maplen**, **mkieee()**, **gtemplate::needext**, **pngpack()**, **sbit()**, **sbits()**, **simpack()**, and **specpack()**.

## Author

Generated automatically by Doxygen for NCEPLIBS-g2c from the source code.
