19 #if defined USE_JPEG2000 || defined USE_OPENJPEG
111 static unsigned char G = 0x47;
112 static unsigned char R = 0x52;
113 static unsigned char I = 0x49;
114 static unsigned char B = 0x42;
115 static unsigned char s7 = 0x37;
117 unsigned char *cpack;
118 static g2int zero = 0, one = 1, four = 4, five = 5, six = 6, seven = 7;
119 const g2int minsize = 50000;
120 g2int iofst, ibeg, lencurr, len, nsize;
121 g2int ilen, isecnum, i, nbits, temp, left;
122 g2int ibmprev, j, lcpack, ioctet, newlen, ndpts;
123 g2int lensec4, lensec5, lensec6, lensec7;
124 g2int issec3 = 0, isprevbmap = 0, lpos3 = 0, JJ, KK, MM;
126 g2int width, height, iscan, itemp;
129 unsigned int allones = 4294967295u;
132 if (cgrib[0] != G || cgrib[1] != R || cgrib[2] != I || cgrib[3] != B)
134 printf(
"g2_addfield: GRIB not found in given message.\n");
135 printf(
"g2_addfield: Call to routine g2_create required to initialize GRIB messge.\n");
141 gbit(cgrib, &lencurr, 96, 32);
144 if (cgrib[lencurr-4] == s7 && cgrib[lencurr-3] == s7 &&
145 cgrib[lencurr-2] == s7 && cgrib[lencurr-1] == s7)
147 printf(
"g2_addfield: GRIB message already complete. Cannot add new section.\n");
159 gbit(cgrib, &ilen, iofst, 32);
161 gbit(cgrib, &isecnum, iofst, 8);
173 gbit(cgrib, &ibmprev, iofst, 8);
175 if (ibmprev >= 0 && ibmprev <= 253)
188 printf(
"g2_addfield: Section byte counts don''t add to total.\n");
189 printf(
"g2_addfield: Sum of section byte counts = %ld\n", len);
190 printf(
"g2_addfield: Total byte count in Section 0 = %ld\n", lencurr);
197 if ((isecnum != 3) && (isecnum != 7))
199 printf(
"g2_addfield: Sections 4-7 can only be added after Section 3 or 7.\n");
200 printf(
"g2_addfield: Section ',isecnum,' was the last found in given GRIB message.\n");
207 printf(
"g2_addfield: Sections 4-7 can only be added if Section 3 was previously included.\n");
208 printf(
"g2_addfield: Section 3 was not found in given GRIB message.\n");
209 printf(
"g2_addfield: Call to routine addgrid required to specify Grid definition.\n");
217 sbit(cgrib, &four, iofst, 8);
219 sbit(cgrib, &numcoord, iofst, 16);
221 sbit(cgrib, &ipdsnum, iofst, 16);
243 for (i = 0; i < mappds->
maplen; i++)
245 nbits = abs(mappds->
map[i]) * 8;
246 if ((mappds->
map[i] >= 0) || (ipdstmpl[i] >= 0))
247 sbit(cgrib, ipdstmpl + i, iofst, nbits);
250 sbit(cgrib, &one, iofst, 1);
251 temp = abs(ipdstmpl[i]);
252 sbit(cgrib, &temp, iofst + 1, nbits-1);
254 iofst = iofst + nbits;
261 for (i = 0; i < mappds->
extlen; i++)
263 nbits = abs(mappds->
ext[i]) * 8;
264 if ((mappds->
ext[i] >= 0) || (ipdstmpl[j] >= 0))
265 sbit(cgrib, ipdstmpl + j, iofst, nbits);
268 sbit(cgrib, &one, iofst, 1);
269 temp = abs(ipdstmpl[j]);
270 sbit(cgrib, &temp, iofst + 1, nbits-1);
272 iofst = iofst + nbits;
282 coordieee = calloc(numcoord,
sizeof(
g2int));
283 mkieee(coordlist, coordieee, numcoord);
284 sbits(cgrib, coordieee, iofst, 32, 0, numcoord);
285 iofst = iofst + (32*numcoord);
291 lensec4 = (iofst - ibeg) / 8;
292 sbit(cgrib, &lensec4, ibeg, 32);
304 if (ibmap == 0 || ibmap == 254)
306 pfld = malloc(ngrdpts *
sizeof(
g2float));
308 for (j = 0; j < ngrdpts; j++)
311 pfld[ndpts++] = fld[j];
324 cpack = malloc(nsize);
328 simpack(pfld, ndpts, idrstmpl, cpack, &lcpack);
329 else if (idrsnum == 2 || idrsnum == 3)
330 cmplxpack(pfld, ndpts, idrsnum, idrstmpl, cpack, &lcpack);
331 else if (idrsnum == 50)
333 simpack(pfld + 1, ndpts - 1, idrstmpl, cpack, &lcpack);
334 mkieee(pfld, idrstmpl + 4, 1);
336 else if (idrsnum == 51)
338 getpoly(cgrib + lpos3, &JJ, &KK, &MM);
339 if (JJ != 0 && KK != 0 && MM != 0)
340 specpack(pfld, ndpts, JJ, KK, MM, idrstmpl, cpack, &lcpack);
343 printf(
"g2_addfield: Cannot pack DRT 5.51.\n");
347 #if defined USE_JPEG2000 || defined USE_OPENJPEG
348 else if (idrsnum == 40 || idrsnum == 40000)
352 getdim(cgrib + lpos3, &width, &height, &iscan);
353 if (width == 0 || height == 0)
358 else if (width == allones || height == allones)
363 else if ((iscan&32) == 32)
376 jpcpack(pfld, width, height, idrstmpl, cpack, &lcpack);
380 else if (idrsnum == 41 || idrsnum == 40010)
384 getdim(cgrib + lpos3, &width, &height, &iscan);
385 if (width==0 || height==0)
390 else if (width == allones || height == allones)
395 else if ((iscan & 32) == 32)
407 pngpack(pfld, width, height, idrstmpl, cpack, &lcpack);
412 printf(
"g2_addfield: Data Representation Template 5.%ld not yet implemented.\n", idrsnum);
416 if (ibmap == 0 || ibmap == 254)
432 sbit(cgrib, &five, iofst, 8);
434 sbit(cgrib, &ndpts, iofst, 32);
436 sbit(cgrib, &idrsnum, iofst, 16);
442 for (i = 0; i < mapdrs->
maplen; i++)
444 nbits = abs(mapdrs->
map[i]) * 8;
445 if ((mapdrs->
map[i] >= 0) || (idrstmpl[i] >= 0))
446 sbit(cgrib, idrstmpl + i, iofst, nbits);
449 sbit(cgrib, &one, iofst, 1);
450 temp = abs(idrstmpl[i]);
451 sbit(cgrib, &temp, iofst + 1, nbits - 1);
453 iofst = iofst + nbits;
459 lensec5 = (iofst - ibeg) / 8;
460 sbit(cgrib, &lensec5, ibeg, 32);
465 sbit(cgrib, &six, iofst, 8);
467 sbit(cgrib, &ibmap, iofst, 8);
473 sbits(cgrib, bmap, iofst, 1, 0, ngrdpts);
474 iofst = iofst + ngrdpts;
479 if (ibmap == 254 && !isprevbmap)
481 printf(
"g2_addfield: Requested previously defined bitmap,");
482 printf(
" but one does not exist in the current GRIB message.\n");
489 left = 8 - (iofst % 8);
492 sbit(cgrib, &zero, iofst, left);
493 iofst = iofst + left;
495 lensec6 = (iofst-ibeg) / 8;
496 sbit(cgrib, &lensec6, ibeg, 32);
501 sbit(cgrib, &seven, iofst, 8);
509 for (j = 0; j < lcpack; j++)
510 cgrib[ioctet + j] = cpack[j];
511 iofst = iofst + (8*lcpack);
516 lensec7 = (iofst - ibeg) / 8;
517 sbit(cgrib, &lensec7, ibeg, 32);
523 newlen = lencurr + lensec4 + lensec5 + lensec6 + lensec7;
524 sbit(cgrib, &newlen, 96, 32);
gtemplate * getdrstemplate(g2int number)
This subroutine returns DRS template information for a specified Data Representation Template.
g2int getpoly(unsigned char *, g2int *, g2int *, g2int *)
This subroutine returns the J, K, and M pentagonal resolution parameters specified in a GRIB Grid Def...
g2int getdim(unsigned char *, g2int *, g2int *, g2int *)
This subroutine returns the dimensions and scanning mode of a grid definition packed in GRIB2 Grid De...
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 docum...
void pngpack(g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
This subroutine packs up a data field into PNG image format.
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 harmoni...
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 docu...
void jpcpack(g2float *, g2int, g2int, g2int *, unsigned char *, g2int *)
This subroutine packs up a data field into a JPEG2000 code stream.
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.
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbyte)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbyte, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Header file for NCEPLIBS-g2c library.
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 needext
Indicates whether or not the template needs to be extended.
g2int maplen
Number of entries in the static part of the template.
int64_t g2int
Long integer type.
Struct for GRIB template.
void mkieee(g2float *a, g2int *rieee, g2int num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
gtemplate * extpdstemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Product Definition Template,...
gtemplate * getpdstemplate(g2int number)
This subroutine returns PDS template information for a specified Product Definition Template.