104 unsigned char *cpack;
105 static g2int zero = 0, one = 1, four = 4, five = 5, six = 6, seven = 7;
106 const g2int minsize = 50000;
107 g2int iofst, ibeg, lencurr, len, nsize;
108 g2int ilen, isecnum, i, nbits, temp, left;
109 g2int ibmprev, j, lcpack, ioctet, newlen, ndpts;
110 g2int lensec4, lensec5, lensec6, lensec7;
111 g2int issec3 = 0, isprevbmap = 0, lpos3 = 0, JJ, KK, MM;
115#if defined USE_PNG || defined USE_JPEG2000 || defined USE_OPENJPEG || defined USE_AEC
116 unsigned int allones = 4294967295u;
117 g2int width, height, iscan, itemp;
138 gbit(cgrib, &ilen, iofst, 32);
140 gbit(cgrib, &isecnum, iofst, 8);
152 gbit(cgrib, &ibmprev, iofst, 8);
154 if (ibmprev >= 0 && ibmprev <= 253)
167 printf(
"g2_addfield: Section byte counts don''t add to total.\n");
168 printf(
"g2_addfield: Sum of section byte counts = %ld\n", len);
169 printf(
"g2_addfield: Total byte count in Section 0 = %ld\n", lencurr);
175 if (isecnum != 3 && isecnum != 7)
177 printf(
"g2_addfield: Sections 4-7 can only be added after Section 3 or 7.\n");
178 printf(
"g2_addfield: Section %ld was the last found in given GRIB message.\n",
185 printf(
"g2_addfield: Sections 4-7 can only be added if Section 3 was previously included.\n");
186 printf(
"g2_addfield: Section 3 was not found in given GRIB message.\n");
187 printf(
"g2_addfield: Call to routine addgrid required to specify Grid definition.\n");
194 sbit(cgrib, &four, iofst, 8);
196 sbit(cgrib, &numcoord, iofst, 16);
198 sbit(cgrib, &ipdsnum, iofst, 16);
217 for (i = 0; i < mappds->
maplen; i++)
219 nbits = abs(mappds->
map[i]) * 8;
220 if ((mappds->
map[i] >= 0) || (ipdstmpl[i] >= 0))
221 sbit(cgrib, ipdstmpl + i, iofst, nbits);
224 sbit(cgrib, &one, iofst, 1);
225 temp = abs(ipdstmpl[i]);
226 sbit(cgrib, &temp, iofst + 1, nbits-1);
228 iofst = iofst + nbits;
235 for (i = 0; i < mappds->
extlen; i++)
237 nbits = abs(mappds->
ext[i]) * 8;
238 if (mappds->
ext[i] >= 0 || ipdstmpl[j] >= 0)
239 sbit(cgrib, ipdstmpl + j, iofst, nbits);
242 sbit(cgrib, &one, iofst, 1);
243 temp = abs(ipdstmpl[j]);
244 sbit(cgrib, &temp, iofst + 1, nbits - 1);
246 iofst = iofst + nbits;
258 coordieee = calloc(numcoord,
sizeof(
g2int));
259 mkieee(coordlist, coordieee, numcoord);
260 sbits(cgrib, coordieee, iofst, 32, 0, numcoord);
261 iofst = iofst + (32 * numcoord);
267 lensec4 = (iofst - ibeg) / 8;
268 sbit(cgrib, &lensec4, ibeg, 32);
277 if (ibmap == 0 || ibmap == 254)
279 pfld = malloc(ngrdpts *
sizeof(
float));
281 for (j = 0; j < ngrdpts; j++)
284 pfld[ndpts++] = fld[j];
297 cpack = malloc(nsize);
301 simpack(pfld, ndpts, idrstmpl, cpack, &lcpack);
302 else if (idrsnum == 2 || idrsnum == 3)
303 cmplxpack(pfld, ndpts, idrsnum, idrstmpl, cpack, &lcpack);
304 else if (idrsnum == 50)
306 simpack(pfld + 1, ndpts - 1, idrstmpl, cpack, &lcpack);
307 mkieee(pfld, idrstmpl + 4, 1);
309 else if (idrsnum == 51)
311 getpoly(cgrib + lpos3, &JJ, &KK, &MM);
312 if (JJ != 0 && KK != 0 && MM != 0)
313 specpack(pfld, ndpts, JJ, KK, MM, idrstmpl, cpack, &lcpack);
316 printf(
"g2_addfield: Cannot pack DRT 5.51.\n");
320#if defined USE_JPEG2000 || defined USE_OPENJPEG
321 else if (idrsnum == 40 || idrsnum == 40000)
325 getdim(cgrib + lpos3, &width, &height, &iscan);
326 if (width == 0 || height == 0)
331 else if (width == allones || height == allones)
336 else if ((iscan & 32) == 32)
349 jpcpack(pfld, width, height, idrstmpl, cpack, &lcpack);
353 else if (idrsnum == 41 || idrsnum == 40010)
357 getdim(cgrib + lpos3, &width, &height, &iscan);
358 if (width == 0 || height == 0)
363 else if (width == allones || height == allones)
368 else if ((iscan & 32) == 32)
380 pngpack(pfld, width, height, idrstmpl, cpack, &lcpack);
384 else if (idrsnum == 42)
388 getdim(cgrib + lpos3, &width, &height, &iscan);
389 if (width == 0 || height == 0)
394 else if (width == allones || height == allones)
399 else if ((iscan & 32) == 32)
413 aecpack(pfld, width, height, idrstmpl, cpack, &lcpack);
418 printf(
"g2_addfield: Data Representation Template 5.%ld not yet implemented.\n", idrsnum);
423 if (ibmap == 0 || ibmap == 254)
441 sbit(cgrib, &five, iofst, 8);
443 sbit(cgrib, &ndpts, iofst, 32);
445 sbit(cgrib, &idrsnum, iofst, 16);
451 for (i = 0; i < mapdrs->
maplen; i++)
453 nbits = abs(mapdrs->
map[i]) * 8;
454 if (mapdrs->
map[i] >= 0 || idrstmpl[i] >= 0)
455 sbit(cgrib, idrstmpl + i, iofst, nbits);
458 sbit(cgrib, &one, iofst, 1);
459 temp = abs(idrstmpl[i]);
460 sbit(cgrib, &temp, iofst + 1, nbits - 1);
462 iofst = iofst + nbits;
468 lensec5 = (iofst - ibeg) / 8;
469 sbit(cgrib, &lensec5, ibeg, 32);
474 sbit(cgrib, &six, iofst, 8);
476 sbit(cgrib, &ibmap, iofst, 8);
482 sbits(cgrib, bmap, iofst, 1, 0, ngrdpts);
483 iofst = iofst + ngrdpts;
488 if (ibmap == 254 && !isprevbmap)
490 printf(
"g2_addfield: Requested previously defined bitmap,");
491 printf(
" but one does not exist in the current GRIB message.\n");
497 left = 8 - (iofst % 8);
500 sbit(cgrib, &zero, iofst, left);
501 iofst = iofst + left;
503 lensec6 = (iofst - ibeg) / 8;
504 sbit(cgrib, &lensec6, ibeg, 32);
509 sbit(cgrib, &seven, iofst, 8);
517 for (j = 0; j < lcpack; j++)
518 cgrib[ioctet + j] = cpack[j];
519 iofst = iofst + (8 * lcpack);
524 lensec7 = (iofst - ibeg) / 8;
525 sbit(cgrib, &lensec7, ibeg, 32);
531 newlen = lencurr + lensec4 + lensec5 + lensec6 + lensec7;
532 sbit(cgrib, &newlen, 96, 32);