NCEPLIBS-g2c  1.7.0
grib2_int.h File Reference

Header file with internal function prototypes NCEPLIBS-g2c library. More...

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include "grib2.h"

Go to the source code of this file.

Data Structures

struct  gtemplate
 Struct for GRIB template. More...
 

Macros

#define ALOG2   (0.69314718)
 ln(2.0) More...
 
#define G2C_JASPER_JPEG_FORMAT_NAME   "jpc"
 Name of JPEG codec in Jasper. More...
 
#define LOG(e)
 Ignore logging to stdout. More...
 

Typedefs

typedef struct gtemplate gtemplate
 Struct for GRIB template. More...
 

Functions

void cmplxpack (float *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. More...
 
void compack (float *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. More...
 
int comunpack (unsigned char *cpack, g2int lensec, g2int idrsnum, g2int *idrstmpl, g2int ndpts, float *fld)
 This subroutine unpacks a data field that was packed using a complex packing algorithm as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.2 or Template 5.3. More...
 
int dec_jpeg2000 (char *injpc, g2int bufsize, g2int *outfld)
 This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer Software. More...
 
int dec_png (unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
 Decode PNG. More...
 
int enc_jpeg2000 (unsigned char *cin, g2int width, g2int height, g2int nbits, g2int ltype, g2int ratio, g2int retry, char *outjpc, g2int jpclen)
 This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer Software. More...
 
int enc_png (unsigned char *data, g2int width, g2int height, g2int nbits, unsigned char *pngbuf)
 Encode PNG. More...
 
gtemplateextdrstemplate (g2int number, g2int *list)
 This subroutine generates the remaining octet map for a given Data Representation Template, if required. More...
 
gtemplateextgridtemplate (g2int number, g2int *list)
 This subroutine generates the remaining octet map for a given Grid Definition Template, if required. More...
 
gtemplateextpdstemplate (g2int number, g2int *list)
 This subroutine generates the remaining octet map for a given Product Definition Template, if required. More...
 
int g2c_check_msg (unsigned char *cgrib, g2int *lencurr, int verbose)
 Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already terminated with '7777'. More...
 
void gbit (unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
 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 *in, g2int *iout, g2int iskip, g2int nbits, 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. More...
 
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. More...
 
gtemplategetdrstemplate (g2int number)
 This subroutine returns DRS template information for a specified Data Representation Template. More...
 
gtemplategetgridtemplate (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). More...
 
gtemplategetpdstemplate (g2int number)
 This subroutine returns PDS template information for a specified Product Definition Template. More...
 
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. More...
 
double int_power (double x, g2int y)
 Function similar to C pow() power function. More...
 
void jpcpack (float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
 This subroutine packs up a data field into a JPEG2000 code stream. More...
 
g2int jpcunpack (unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
 Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representation Template 5.40 or 5.40000. More...
 
void misspack (float *fld, g2int ndpts, g2int idrsnum, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
 This function packs up a data field using a complex packing algorithm as defined in the GRIB2 documention. More...
 
void mkieee (float *a, g2int *rieee, g2int num)
 This subroutine stores a list of real values in 32-bit IEEE floating point format. More...
 
int pack_gp (g2int *kfildo, g2int *ic, g2int *nxy, g2int *is523, g2int *minpk, g2int *inc, g2int *missp, g2int *misss, g2int *jmin, g2int *jmax, g2int *lbit, g2int *nov, g2int *ndg, g2int *lx, g2int *ibit, g2int *jbit, g2int *kbit, g2int *novref, g2int *lbitref, g2int *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). More...
 
void pngpack (float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
 This subroutine packs up a float data field into PNG image format. More...
 
g2int pngunpack (unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
 This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB2 Data Representation Template 5.41 or 5.40010. More...
 
void rdieee (g2int *rieee, float *a, g2int num)
 This subroutine reads a list of real values in 32-bit IEEE floating point format. More...
 
void sbit (unsigned char *out, g2int *in, g2int iskip, g2int nbits)
 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 *out, g2int *in, g2int iskip, g2int nbits, 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. More...
 
void simpack (float *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. More...
 
g2int simunpack (unsigned char *cpack, g2int *idrstmpl, g2int ndpts, float *fld)
 This subroutine unpacks a data field that was packed using a simple packing algorithm as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.0. More...
 
void specpack (float *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. More...
 
g2int specunpack (unsigned char *cpack, g2int *idrstmpl, g2int ndpts, g2int JJ, g2int KK, g2int MM, float *fld)
 This subroutine unpacks a spectral data field that was packed using the complex packing algorithm for spherical harmonic data as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.51. More...
 

Detailed Description

Header file with internal function prototypes NCEPLIBS-g2c library.

Program History Log

Date Programmer Comments
2021-11-08 Ed Hartnett Initial
Author
Ed Hartnett
Date
2021-11-08

Definition in file grib2_int.h.


Data Type Documentation

◆ gtemplate

struct gtemplate

Struct for GRIB template.

Definition at line 28 of file grib2_int.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

◆ ALOG2

#define ALOG2   (0.69314718)

ln(2.0)

Definition at line 21 of file grib2_int.h.

◆ G2C_JASPER_JPEG_FORMAT_NAME

#define G2C_JASPER_JPEG_FORMAT_NAME   "jpc"

Name of JPEG codec in Jasper.

Definition at line 23 of file grib2_int.h.

◆ LOG

#define LOG (   e)

Ignore logging to stdout.

Library was not built with LOGGING=ON.

Definition at line 138 of file grib2_int.h.

Typedef Documentation

◆ gtemplate

typedef struct gtemplate gtemplate

Struct for GRIB template.

Definition at line 56 of file grib2_int.h.

Function Documentation

◆ cmplxpack()

void cmplxpack ( float *  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. Will be set to -1 if missing value management field is not 1 or 2.
Author
Stephen Gilbert
Date
2004-08-27

Definition at line 36 of file cmplxpack.c.

References compack(), and misspack().

Referenced by g2_addfield().

◆ compack()

void compack ( float *  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 ALOG2, int_power(), mkieee(), pack_gp(), sbit(), and sbits().

Referenced by cmplxpack().

◆ comunpack()

int comunpack ( unsigned char *  cpack,
g2int  lensec,
g2int  idrsnum,
g2int idrstmpl,
g2int  ndpts,
float *  fld 
)

This subroutine unpacks a data field that was packed using a complex packing algorithm as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.2 or Template 5.3.

Supports GRIB2 complex packing templates with or without spatial differences (i.e. DRTs 5.2 and 5.3).

Program History Log

Date Programmer Comments
2002-10-29 Gilbert Initial
2004-12-16 Gilbert Added test (from Arthur Taylor/MDL) verifying group widths/lengths
Parameters
cpackpointer to the packed data field.
lenseclength of section 7 (used for error checking).
idrsnumData Representation Template number. Must equal 2 or 3.
idrstmplpointer to the array of values for Data Representation Template 5.2 or 5.3
ndptsThe number of data values to unpack
fldContains the unpacked data values. Must be allocated with at least ndpts * sizeof(float) bytes before calling this routine.
Returns
0 for success, error code otherwise.
Author
Stephen Gilbert
Date
2002-10-29

Definition at line 42 of file comunpack.c.

References gbit(), gbits(), int_power(), and rdieee().

Referenced by g2_unpack7().

◆ dec_jpeg2000()

int dec_jpeg2000 ( char *  injpc,
g2int  bufsize,
g2int outfld 
)

This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer Software.

Program History Log

Date Programmer Comments
2002-12-02 Gilbert Initial
2022-04-15 Hartnett Converted to use jas_ instead of jpc_ functions.
Parameters
injpcInput JPEG2000 code stream.
bufsizeLength (in bytes) of the input JPEG2000 code stream.
outfldOutput matrix of grayscale image values.
Returns
Author
Stephen Gilbert
Date
2002-12-02
Author
Ed Hartnett

Definition at line 38 of file dec_jpeg2000.c.

References G2_JASPER_DECODE, G2_JASPER_DECODE_COLOR, G2_JASPER_INIT, and G2C_JASPER_JPEG_FORMAT_NAME.

Referenced by jpcunpack_int().

◆ dec_png()

int dec_png ( unsigned char *  pngbuf,
g2int width,
g2int height,
unsigned char *  cout 
)

Decode PNG.

Parameters
pngbufPointer to PNG buffer.
widthPointer to width.
heightPointer to height.
coutOutput buffer.
Returns
0 for success, error code otherwise.
Author
Stephen Gilbert

Definition at line 61 of file dec_png.c.

References user_read_data().

Referenced by pngunpack_int().

◆ enc_jpeg2000()

int enc_jpeg2000 ( unsigned char *  cin,
g2int  width,
g2int  height,
g2int  nbits,
g2int  ltype,
g2int  ratio,
g2int  retry,
char *  outjpc,
g2int  jpclen 
)

This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using JasPer Software.

Program History Log

Date Programmer Comments
2002-12-02 Gilbert Initial
2004-12-16 Gilbert Added retry argument allowing increased guard bits.
2022-04-15 Hartnett Converted to use jas_ instead of jpc_ functions.
Parameters
cinPacked matrix of Grayscale image values to encode.
widthwidth of image.
heightheight of image.
nbitsdepth (in bits) of image. i.e number of bits used to hold each data value.
ltypeindicator of lossless or lossy compression.
  • 1, for lossy compression
  • != 1, for lossless compression
ratiotarget compression ratio. (ratio:1) Used only when ltype == 1.
retryIf 1 try increasing number of guard bits.
outjpcOutput encoded JPEG2000 code stream.
jpclenNumber of bytes allocated for the output JPEG2000 code stream in outjpc.
Returns
  • > 0 = Length in bytes of encoded JPEG2000 code stream
  • G2_JASPER_INIT Error initializing jasper library.
  • G2_JASPER_ENCODE Error encode jpeg2000 code stream.
Note
Requires JasPer Software version 1.500.4 or 1.700.2 or later.
Author
Stephen Gilbert
Date
2002-12-02
Author
Ed Hartnett

Definition at line 51 of file enc_jpeg2000.c.

References G2_JASPER_ENCODE, G2_JASPER_INIT, G2C_JASPER_JPEG_FORMAT_NAME, and MAXOPTSSIZE.

Referenced by jpcpack_int().

◆ enc_png()

int enc_png ( unsigned char *  data,
g2int  width,
g2int  height,
g2int  nbits,
unsigned char *  pngbuf 
)

Encode PNG.

Parameters
datadata.
widthwidth.
heightheight.
nbitsnumber of bits.
pngbufPNG buffer.
Returns
PNG length, or negative number for error.
Author
Stephen Gilbert

Definition at line 75 of file enc_png.c.

References user_flush_data(), and user_write_data().

Referenced by pngpack_int().

◆ 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 212 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 229 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 329 of file pdstemplates.c.

References getpdsindex(), and getpdstemplate().

Referenced by g2_addfield(), and g2_unpack4().

◆ g2c_check_msg()

int g2c_check_msg ( unsigned char *  cgrib,
g2int lencurr,
int  verbose 
)

Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already terminated with '7777'.

Parameters
cgribBuffer that contains the GRIB message.
lencurrPointer that gets the length of the GRIB message.
verboseIf non-zero, print any error messages to stdout.
Returns
Author
Ed Hartnett
Date
Nov 11, 2021

Definition at line 26 of file util.c.

References G2C_MSG_COMPLETE, G2C_NO_ERROR, G2C_NOT_GRIB, and gbit().

Referenced by g2_addfield(), g2_addgrid(), g2_addlocal(), and g2_gribend().

◆ gbit()

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

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.
nbitsnumber 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(), g2c_check_msg(), and seekgb().

◆ gbits()

void gbits ( unsigned char *  in,
g2int iout,
g2int  iskip,
g2int  nbits,
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.
nbitsNumber 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_int(), simunpack(), and specunpack().

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

◆ 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 166 of file drstemplates.c.

References getdrsindex(), 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 180 of file gridtemplates.c.

References getgridindex(), 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 281 of file pdstemplates.c.

References getpdsindex(), and templatespds.

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

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

◆ 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 18 of file int_power.c.

Referenced by compack(), comunpack(), jpcpack_int(), jpcunpack_int(), misspack(), mkieee(), pngpack_int(), pngunpack_int(), rdieee(), simpack(), simunpack(), and specunpack().

◆ jpcpack()

void jpcpack ( float *  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.

Parameters
fldPointer to the float or double data values to pack.
widthThe number of points in the x direction.
heightThe number 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.
cpackA pointer that will get the packed data field. Must be allocated before this function is called. Pass the allocated size in the lcpack parameter.
lcpackPointer that gets the length of packed field in cpack. This must be set by the calling function to the size available in cpack.
Author
Stephen Gilbert, Ed Hartnett

Definition at line 253 of file jpcpack.c.

Referenced by g2_addfield().

◆ jpcunpack()

g2int jpcunpack ( unsigned char *  cpack,
g2int  len,
g2int idrstmpl,
g2int  ndpts,
float *  fld 
)

Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representation Template 5.40 or 5.40000.

Parameters
cpackThe packed data.
lenThe length of the packed data.
idrstmplPointer to array of values for Data Representation Template 5.40 or 5.40000.
ndptsThe number of data values to unpack.
fldA pointer that gets the unpacked data values as an array of float.
Returns
0 for success, 1 for memory allocation error.
Author
Stephem Gilbert
Date
2003-08-27
Author
Ed Hartnett

Definition at line 108 of file jpcunpack.c.

Referenced by g2_unpack7().

◆ misspack()

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

This function 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 (See Template 5.2 and Template 5.3).

This function fills in GRIB2 Data Representation template arrays 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 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 42 of file misspack.c.

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

Referenced by cmplxpack().

◆ mkieee()

void mkieee ( float *  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 22 of file mkieee.c.

References int_power().

Referenced by compack(), g2_addfield(), jpcpack_int(), misspack(), pngpack_int(), simpack(), and specpack().

◆ pack_gp()

int pack_gp ( g2int kfildo,
g2int ic,
g2int nxy,
g2int is523,
g2int minpk,
g2int inc,
g2int missp,
g2int misss,
g2int jmin,
g2int jmax,
g2int lbit,
g2int nov,
g2int ndg,
g2int lx,
g2int ibit,
g2int jbit,
g2int kbit,
g2int novref,
g2int lbitref,
g2int 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 g2int. 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 g2int 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 g2int 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 g2int 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.
                      (g2int)
             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 255 of file pack_gp.c.

References FALSE_, reduce(), and TRUE_.

Referenced by compack(), and misspack().

◆ pngpack()

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

This subroutine packs up a float 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
fldPointer to array of float that contains 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
Author
Ed Hartnett

Definition at line 224 of file pngpack.c.

Referenced by g2_addfield().

◆ pngunpack()

g2int pngunpack ( unsigned char *  cpack,
g2int  len,
g2int idrstmpl,
g2int  ndpts,
float *  fld 
)

This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB2 Data Representation Template 5.41 or 5.40010.

Parameters
cpackThe packed data field (character*1 array).
lenlength of packed field cpack().
idrstmplPointer to array of values for Data Representation Template 5.41 or 5.40010.
ndptsThe number of data values to unpack.
fldContains the unpacked data values.
Returns
0 for success, 1 for memory allocation error.
Author
Stephen Gilbert
Date
2003-08-27
Author
Ed Hartnett

Definition at line 105 of file pngunpack.c.

Referenced by g2_unpack7().

◆ rdieee()

void rdieee ( g2int rieee,
float *  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 21 of file rdieee.c.

References int_power().

Referenced by comunpack(), g2_miss(), g2_unpack4(), g2_unpack7(), jpcunpack_int(), misspack(), pngunpack_int(), simunpack(), and specunpack().

◆ sbit()

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

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.
nbitsNumber 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  nbits,
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.
nbitsNumber 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_int(), misspack(), pngpack_int(), sbit(), and simpack().

◆ simpack()

void simpack ( float *  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 ALOG2, int_power(), mkieee(), sbit(), and sbits().

Referenced by g2_addfield(), and specpack().

◆ simunpack()

g2int simunpack ( unsigned char *  cpack,
g2int idrstmpl,
g2int  ndpts,
float *  fld 
)

This subroutine unpacks a data field that was packed using a simple packing algorithm as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.0.

Parameters
cpackpointer to the packed data field.
idrstmplpointer to the array of values for Data Representation Template 5.0.
ndptsThe number of data values to unpack.
fldContains the unpacked data values. fld must be `* allocated with at least ndpts * sizeof(float) bytes before calling this routine.
Returns
0 for success, error code otherwise.
Author
Stephen Gilbert
Date
2002-10-29

Definition at line 26 of file simunpack.c.

References G2_JPCUNPACK_MEM, G2_NO_ERROR, gbits(), int_power(), and rdieee().

Referenced by g2_unpack7().

◆ specpack()

void specpack ( float *  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().

◆ specunpack()

g2int specunpack ( unsigned char *  cpack,
g2int idrstmpl,
g2int  ndpts,
g2int  JJ,
g2int  KK,
g2int  MM,
float *  fld 
)

This subroutine unpacks a spectral data field that was packed using the complex packing algorithm for spherical harmonic data as defined in the GRIB2 documention, using info from the GRIB2 Data Representation Template 5.51.

Parameters
cpackpointer to the packed data field.
idrstmplpointer to the array of values for Data Representation Template 5.51.
ndptsThe number of data values to unpack (real and imaginary parts).
JJpentagonal resolution parameter.
KKpentagonal resolution parameter.
MMpentagonal resolution parameter.
fldContains the unpacked data values. fld must be allocated with at least ndpts * sizeof(float) bytes before calling this routine.
Returns
0 for success, -3 for wrong type.
Author
Stephen Gilbert
Date
2000-06-21

Definition at line 35 of file specunpack.c.

References G2_NO_ERROR, G2_SPECUNPACK_TYPE, gbits(), int_power(), and rdieee().

Referenced by g2_unpack7().