NCEPLIBS-g2c  1.6.4
g2_addfield.c File Reference

Pack up Sections 4 through 7 for a given field and adds them to a GRIB2 message. More...

#include <stdio.h>
#include <stdlib.h>
#include "grib2.h"

Go to the source code of this file.

Functions

void cmplxpack (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...
 
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. More...
 
g2int getdim (unsigned char *, g2int *, g2int *, g2int *)
 This subroutine returns the dimensions and scanning mode of a grid definition packed in GRIB2 Grid Definition Section 3 format. More...
 
g2int getpoly (unsigned char *, g2int *, g2int *, g2int *)
 This subroutine returns the J, K, and M pentagonal resolution parameters specified in a GRIB Grid Definition Section (GDS) used spherical harmonic coefficients using GDT 5.50 through 5.53. More...
 
void jpcpack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs up a data field into a JPEG2000 code stream. More...
 
void pngpack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs up a data field into PNG image format. 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...
 
void specpack (g2float *, g2int, g2int, g2int, g2int, g2int *, unsigned char *, g2int *)
 This subroutine packs a spectral data field using the complex packing algorithm for spherical harmonic data as defined in the GRIB2 Data Representation Template 5.51. More...
 

Detailed Description

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

Author
Stephen Gilbert
Date
2002-11-05

Definition in file g2_addfield.c.

Function Documentation

◆ cmplxpack()

void cmplxpack ( 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 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 by compack routine.
  • 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
2004-08-27

Definition at line 35 of file cmplxpack.c.

References compack(), and misspack().

Referenced by g2_addfield().

◆ 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().

◆ getdim()

g2int getdim ( unsigned char *  csec3,
g2int width,
g2int height,
g2int iscan 
)

This subroutine returns the dimensions and scanning mode of a grid definition packed in GRIB2 Grid Definition Section 3 format.

Parameters
csec3Character array that contains the packed GRIB2 GDS.
widthx (or i) dimension of the grid. 0 if grid is not recognized.
heighty (or j) dimension of the grid. 0 if grid is not recognized.
iscanScanning mode (see Table 3.4). 0 of grid not recognized.
Returns
Always returns 0.
Author
Stephen Gilbert
Date
2002-12-11

Definition at line 29 of file getdim.c.

References g2_unpack3().

Referenced by g2_addfield().

◆ getpoly()

g2int getpoly ( unsigned char *  csec3,
g2int jj,
g2int kk,
g2int mm 
)

This subroutine returns the J, K, and M pentagonal resolution parameters specified in a GRIB Grid Definition Section (GDS) used spherical harmonic coefficients using GDT 5.50 through 5.53.

If 51 - complex data spectral packing is used as the data representation template number (see Table 5.0), then the Grid Definition Template Number in section 3 should be one of:

  • 50: Spherical Harmonic Coefficients (See Template 3.50)
  • 51: Rotated Spherical Harmonic Coefficients (See Template 3.51)
  • 52: Stretched Spherical Harmonic Coefficients (See Template 3.52)
  • 53: Rotated and Stretched Spherical Harmonic Coefficients (See Template 3.53)
Parameters
csec3Character array that contains the packed GRIB2 GDS.
jjJ pentagonal resolution parameter.
kkK pentagonal resolution parameter.
mmM pentagonal resolution parameter.
Returns
always returns 0.
Note
Returns jj, kk, and mm set to zero, if grid template not recognized.
Author
Stephen Gilbert
Date
2002-12-11

Definition at line 40 of file getpoly.c.

References g2_unpack3().

Referenced by g2_addfield().

◆ jpcpack()

void jpcpack ( g2float fld,
g2int  width,
g2int  height,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs up a data field into a JPEG2000 code stream.

After the data field is scaled, and the reference value is subtracted out, it is treated as a grayscale image and passed to a JPEG2000 encoder. It also fills in GRIB2 Data Representation Template 5.40 or 5.40000 with the appropriate values.

Program History Log

Date Programmer Comments
2003-08-17 Gilbert Initial.
2004-11-92 Gilbert Fixed bug packing a near constant field.
2004-07-19 Gilbert If jpeg2000 encoding fails, try again with different encoder options.
2005-05-10 Gilbert Imposed minimum size on cpack.
Parameters
fldContains the data values to pack.
widthnumber of points in the x direction.
heightnumber of points in the y direction.
idrstmplContains the array of values for Data Representation Template Table 5.40 or 5.40000.
  • 0 Reference value - ignored on input, set by jpcpack routine.
  • 1 Binary Scale Factor - used on input, unchanged by jpcpack routine.
  • 2 Decimal Scale Factor - used on input, unchanged by jpcpack routine.
  • 3 number of bits for each data value - ignored on input
  • 4 Original field type - currently ignored on input Data values assumed to be reals. Set to 0 on output.
  • 5 if 0 use lossless compression, if 1 use lossy compression.
  • 6 Desired compression ratio, if idrstmpl[5]=1. Set to 255, if idrstmpl[5]=0.
cpackThe packed data field.
lcpackPointer that points to a g2int which contains the length of packed field in cpack. This must be set by the calling function.
Author
Stephen Gilbert
Date
2003-08-17

Definition at line 53 of file jpcpack.c.

References enc_jpeg2000(), int_power(), mkieee(), and sbits().

Referenced by g2_addfield().

◆ pngpack()

void pngpack ( g2float fld,
g2int  width,
g2int  height,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs up a data field into PNG image format.

After the data field is scaled, and the reference value is subtracted out, it is treated as a grayscale image and passed to a PNG encoder. It also fills in GRIB2 Data Representation Template 5.41 or 5.40010 with the appropriate values.

Parameters
fldContains the data values to pack.
widthnumber of points in the x direction.
heightnumber of points in the y direction.
idrstmplContains the array of values for Data Representation Template 5.41 or 5.40010.
  • 0 Reference value - ignored on input, set by pngpack routine.
  • 1 Binary Scale Factor - used on input.
  • 2 Decimal Scale Factor - used on input.
  • 3 number of bits for each grayscale pixel value - ignored on input.
  • 4 Original field type - currently ignored on input, set = 0 on output. Data values assumed to be reals.
cpackThe packed data field.
lcpacklength of packed field cpack.
Author
Stephen Gilbert
Date
2003-08-27

Definition at line 38 of file pngpack.c.

References enc_png(), int_power(), mkieee(), and sbits().

Referenced by g2_addfield().

◆ 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.

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

Referenced by g2_addfield(), and specpack().

◆ specpack()

void specpack ( g2float fld,
g2int  ndpts,
g2int  JJ,
g2int  KK,
g2int  MM,
g2int idrstmpl,
unsigned char *  cpack,
g2int lcpack 
)

This subroutine packs a spectral data field using the complex packing algorithm for spherical harmonic data as defined in the GRIB2 Data Representation Template 5.51.

Parameters
fldContains the data values to be packed.
ndptsThe number of data values to pack.
JJJ - pentagonal resolution parameter.
KKK - pentagonal resolution parameter.
MMM - pentagonal resolution parameter.
idrstmplContains the array of 10 values for [Data Representation Template 5.51](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-51.shtml).
cpackPointer that gets the packed data field. Must be allocated by caller.
lcpacklength of packed field cpack.
Author
Stephen Gilbert
Date
2002-12-19

Definition at line 33 of file specpack.c.

References mkieee(), and simpack().

Referenced by g2_addfield().