Scroll to navigation

g2_addfield.c(3) NCEPLIBS-g2c g2_addfield.c(3)

NAME

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

SYNOPSIS

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

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. 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. 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. 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. void jpcpack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
This subroutine packs up a data field into a JPEG2000 code stream. void pngpack (g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
This subroutine packs up a data field into PNG image format. 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. 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.

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

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

fld Contains the data values to pack.
ndpts The number of data values in array fld
idrsnum Data Representation Template number 5.N. Must equal 2 or 3.
idrstmpl Contains 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 )

cpack The packed data field (character*1 array)
lcpack length of packed field cpack.

Author

Stephen Gilbert

Date

2004-08-27

Definition at line 33 of file cmplxpack.c.

References compack(), and misspack().

Referenced by 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. g2_create() must be called first to initialize a new GRIB2 message. Also, routine g2_addgrid() must be called after g2_create() and before this routine to add the appropriate grid description to the GRIB2 message. Also, a call to g2_gribend() is required to complete GRIB2 message after all fields have been added.

PROGRAM HISTORY LOG:

  • 2002-11-05 Gilbert
  • 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 allowed to use WMO Template no. 5.40 PNG now allowed to use WMO Template no. 5.41
Added check to determine if packing algorithm failed.
  • 2005-05-10 Gilbert - Imposed minimum size on cpack, used to hold encoded bit string.
  • 2009-01-14 Vuong Changed structure name template to gtemplate

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 specified Product Definition Template (N=ipdsnum). Each element of this integer array contains an entry (in the order specified) of Product Defintion Template 4.N.
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 Contains 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. 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
  • -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 103 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().

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

csec3 Character array that contains the packed GRIB2 GDS.
width x (or i) dimension of the grid. 0 of grid not recognized.
height y (or j) dimension of the grid. 0 of grid not recognized.
iscan Scanning mode (see Code Table 3.4). 0 of grid not recognized.

Returns

Always returns 0.

Author

Stephen Gilbert

Date

2002-12-11

Definition at line 30 of file getdim.c.

References g2_unpack3().

Referenced by g2_addfield().

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.

Parameters

csec3 Character array that contains the packed GRIB2 GDS.
jj J - pentagonal resolution parameter.
kk K - pentagonal resolution parameter.
mm M - pentagonal resolution parameter.

Returns

0 for success, error code otherwise.

Note

Returns jj, kk, and mm set to zero, if grid template not recognized.

Author

Stephen Gilbert

Date

2002-12-11

Definition at line 32 of file getpoly.c.

References g2_unpack3().

Referenced by g2_addfield().

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:

  • 2003-08-17 Gilbert
  • 2004-11-92 Gilbert - Fixed bug encountered when packing a near constant field.
  • 2004-07-19 Gilbert - Added check on whether the jpeg2000 encoding was successful. If not, try again with different encoder options.
  • 2005-05-10 Gilbert - Imposed minimum size on cpack, used to hold encoded bit string.

Parameters

fld Contains the data values to pack.
width number of points in the x direction.
height number of points in the y direction.
idrstmpl Contains the array of values for Data Representation Template 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.

cpack The packed data field.
lcpack length of packed field in cpack.

Author

Stephen Gilbert

Date

2003-08-17

Definition at line 49 of file jpcpack.c.

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

Referenced by g2_addfield().

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

fld Contains the data values to pack.
width number of points in the x direction.
height number of points in the y direction.
idrstmpl Contains 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.

cpack The packed data field.
lcpack length of packed field cpack.

Author

Stephen Gilbert

Date

2003-08-27

Definition at line 34 of file pngpack.c.

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

Referenced by g2_addfield().

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

fld Contains the data values to pack.
ndpts The number of data values in array fld.
idrstmpl Contains the array of values for Data Representation Template 5.0.
  • 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.

cpack The packed data field
lcpack length of packed field starting at cpack.

Author

Stephen Gilbert

Date

2002-11-06

Definition at line 32 of file simpack.c.

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

Referenced by g2_addfield(), and 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

fld Contains the packed data values.
ndpts The number of data values to pack.
JJ J - pentagonal resolution parameter.
KK K - pentagonal resolution parameter.
MM M - pentagonal resolution parameter.
idrstmpl Contains the array of values for Data Representation Template 5.51.
cpack The packed data field (character*1 array).
lcpack length of packed field cpack.

Author

Stephen Gilbert

Date

2002-12-19

Definition at line 26 of file specpack.c.

References int_power(), mkieee(), and simpack().

Referenced by g2_addfield().

Author

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

Tue May 3 2022 Version 1.6.4