103 unsigned char *cpack;
104 static g2int zero = 0, one = 1, four = 4, five = 5, six = 6, seven = 7;
105 const g2int minsize = 50000;
106 g2int iofst, ibeg, lencurr, len, nsize;
107 g2int ilen, isecnum, i, nbits, temp, left;
108 g2int ibmprev, j, lcpack, ioctet, newlen, ndpts;
109 g2int lensec4, lensec5, lensec6, lensec7;
110 g2int issec3 = 0, isprevbmap = 0, lpos3 = 0, JJ, KK, MM;
114 #if defined USE_PNG || defined USE_JPEG2000 || defined USE_OPENJPEG || defined USE_AEC
115 unsigned int allones = 4294967295u;
116 g2int width, height, iscan, itemp;
137 gbit(cgrib, &ilen, iofst, 32);
139 gbit(cgrib, &isecnum, iofst, 8);
151 gbit(cgrib, &ibmprev, iofst, 8);
153 if (ibmprev >= 0 && ibmprev <= 253)
166 printf(
"g2_addfield: Section byte counts don''t add to total.\n");
167 printf(
"g2_addfield: Sum of section byte counts = %ld\n", len);
168 printf(
"g2_addfield: Total byte count in Section 0 = %ld\n", lencurr);
174 if (isecnum != 3 && isecnum != 7)
176 printf(
"g2_addfield: Sections 4-7 can only be added after Section 3 or 7.\n");
177 printf(
"g2_addfield: Section %ld was the last found in given GRIB message.\n",
184 printf(
"g2_addfield: Sections 4-7 can only be added if Section 3 was previously included.\n");
185 printf(
"g2_addfield: Section 3 was not found in given GRIB message.\n");
186 printf(
"g2_addfield: Call to routine addgrid required to specify Grid definition.\n");
193 sbit(cgrib, &four, iofst, 8);
195 sbit(cgrib, &numcoord, iofst, 16);
197 sbit(cgrib, &ipdsnum, iofst, 16);
216 for (i = 0; i < mappds->
maplen; i++)
218 nbits = abs(mappds->
map[i]) * 8;
219 if ((mappds->
map[i] >= 0) || (ipdstmpl[i] >= 0))
220 sbit(cgrib, ipdstmpl + i, iofst, nbits);
223 sbit(cgrib, &one, iofst, 1);
224 temp = abs(ipdstmpl[i]);
225 sbit(cgrib, &temp, iofst + 1, nbits-1);
227 iofst = iofst + nbits;
234 for (i = 0; i < mappds->
extlen; i++)
236 nbits = abs(mappds->
ext[i]) * 8;
237 if (mappds->
ext[i] >= 0 || ipdstmpl[j] >= 0)
238 sbit(cgrib, ipdstmpl + j, iofst, nbits);
241 sbit(cgrib, &one, iofst, 1);
242 temp = abs(ipdstmpl[j]);
243 sbit(cgrib, &temp, iofst + 1, nbits - 1);
245 iofst = iofst + nbits;
257 coordieee = calloc(numcoord,
sizeof(
g2int));
258 mkieee(coordlist, coordieee, numcoord);
259 sbits(cgrib, coordieee, iofst, 32, 0, numcoord);
260 iofst = iofst + (32 * numcoord);
266 lensec4 = (iofst - ibeg) / 8;
267 sbit(cgrib, &lensec4, ibeg, 32);
276 if (ibmap == 0 || ibmap == 254)
278 pfld = malloc(ngrdpts *
sizeof(
float));
280 for (j = 0; j < ngrdpts; j++)
283 pfld[ndpts++] = fld[j];
296 cpack = malloc(nsize);
300 simpack(pfld, ndpts, idrstmpl, cpack, &lcpack);
301 else if (idrsnum == 2 || idrsnum == 3)
302 cmplxpack(pfld, ndpts, idrsnum, idrstmpl, cpack, &lcpack);
303 else if (idrsnum == 50)
305 simpack(pfld + 1, ndpts - 1, idrstmpl, cpack, &lcpack);
306 mkieee(pfld, idrstmpl + 4, 1);
308 else if (idrsnum == 51)
310 getpoly(cgrib + lpos3, &JJ, &KK, &MM);
311 if (JJ != 0 && KK != 0 && MM != 0)
312 specpack(pfld, ndpts, JJ, KK, MM, idrstmpl, cpack, &lcpack);
315 printf(
"g2_addfield: Cannot pack DRT 5.51.\n");
319 #if defined USE_JPEG2000 || defined USE_OPENJPEG
320 else if (idrsnum == 40 || idrsnum == 40000)
324 getdim(cgrib + lpos3, &width, &height, &iscan);
325 if (width == 0 || height == 0)
330 else if (width == allones || height == allones)
335 else if ((iscan & 32) == 32)
348 jpcpack(pfld, width, height, idrstmpl, cpack, &lcpack);
352 else if (idrsnum == 41 || idrsnum == 40010)
356 getdim(cgrib + lpos3, &width, &height, &iscan);
357 if (width == 0 || height == 0)
362 else if (width == allones || height == allones)
367 else if ((iscan & 32) == 32)
379 pngpack(pfld, width, height, idrstmpl, cpack, &lcpack);
383 else if (idrsnum == 42)
387 getdim(cgrib + lpos3, &width, &height, &iscan);
388 if (width == 0 || height == 0)
393 else if (width == allones || height == allones)
398 else if ((iscan & 32) == 32)
412 aecpack(pfld, width, height, idrstmpl, cpack, &lcpack);
417 printf(
"g2_addfield: Data Representation Template 5.%ld not yet implemented.\n", idrsnum);
422 if (ibmap == 0 || ibmap == 254)
440 sbit(cgrib, &five, iofst, 8);
442 sbit(cgrib, &ndpts, iofst, 32);
444 sbit(cgrib, &idrsnum, iofst, 16);
450 for (i = 0; i < mapdrs->
maplen; i++)
452 nbits = abs(mapdrs->
map[i]) * 8;
453 if (mapdrs->
map[i] >= 0 || idrstmpl[i] >= 0)
454 sbit(cgrib, idrstmpl + i, iofst, nbits);
457 sbit(cgrib, &one, iofst, 1);
458 temp = abs(idrstmpl[i]);
459 sbit(cgrib, &temp, iofst + 1, nbits - 1);
461 iofst = iofst + nbits;
467 lensec5 = (iofst - ibeg) / 8;
468 sbit(cgrib, &lensec5, ibeg, 32);
473 sbit(cgrib, &six, iofst, 8);
475 sbit(cgrib, &ibmap, iofst, 8);
481 sbits(cgrib, bmap, iofst, 1, 0, ngrdpts);
482 iofst = iofst + ngrdpts;
487 if (ibmap == 254 && !isprevbmap)
489 printf(
"g2_addfield: Requested previously defined bitmap,");
490 printf(
" but one does not exist in the current GRIB message.\n");
496 left = 8 - (iofst % 8);
499 sbit(cgrib, &zero, iofst, left);
500 iofst = iofst + left;
502 lensec6 = (iofst - ibeg) / 8;
503 sbit(cgrib, &lensec6, ibeg, 32);
508 sbit(cgrib, &seven, iofst, 8);
516 for (j = 0; j < lcpack; j++)
517 cgrib[ioctet + j] = cpack[j];
518 iofst = iofst + (8 * lcpack);
523 lensec7 = (iofst - ibeg) / 8;
524 sbit(cgrib, &lensec7, ibeg, 32);
530 newlen = lencurr + lensec4 + lensec5 + lensec6 + lensec7;
531 sbit(cgrib, &newlen, 96, 32);
void aecpack(float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This function packs up a float array into a AEC code stream.
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 docum...
gtemplate * getdrstemplate(g2int number)
This subroutine returns DRS template information for a specified Data Representation Template.
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)
This routine packs up Sections 4 through 7 for a given field and adds them to a GRIB2 message.
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...
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 ...
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 ...
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 De...
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 Def...
#define G2_BAD_SEC_COUNTS
Sum of Section byte counts doesn't add to total byte count.
#define G2_ADDFIELD_BAD_GDS
In g2_addfield() section 3 (GDS) not previously defined in message.
#define G2_BAD_SEC
Previous Section was unexpected.
#define G2_ADDFIELD_BAD_BITMAP
In g2_addfield() no bitmap in the GRIB message.
#define G2_ADDFIELD_BAD_DRT
In g2_addfield() unsupported Data Representationi Template.
#define G2_ADD_MSG_COMPLETE
GRIB message already complete.
#define G2_ADDFIELD_BAD_PDT
In g2_addfield() could not find requested Product Definition Template.
#define G2_ADD_MSG_INIT
GRIB message was not initialized - call g2_create() first.
#define G2C_ENOTGRIB
GRIB header not found.
#define G2_ADDFIELD_BAD_GDT
In g2_addfield() GDT of one of 5.50 through 5.53 required when using DRT 5.51.
#define G2C_EMSGCOMPLETE
GRIB message already complete.
#define G2_ADDFIELD_ERR
In g2_addfield() error packing data field.
int64_t g2int
Long integer type.
Header file with internal function prototypes NCEPLIBS-g2c library.
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.
void mkieee(float *a, g2int *rieee, g2int num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
g2int * ext
Number of octets of each entry in the extension part of the template.
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 termi...
gtemplate * extpdstemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Product Definition 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 needext
Indicates whether or not the template needs to be extended.
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 harmoni...
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 docu...
void jpcpack(float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This function packs up a float array into a JPEG2000 code stream.
gtemplate * getpdstemplate(g2int number)
This subroutine returns PDS template information for a specified Product Definition Template.
g2int maplen
Number of entries in the static part of the template.
Struct for GRIB template, returned by getgridtemplate().