102 unsigned char *cpack;
103 static g2int zero = 0, one = 1, four = 4, five = 5, six = 6, seven = 7;
104 const g2int minsize = 50000;
105 g2int iofst, ibeg, lencurr, len, nsize;
106 g2int ilen, isecnum, i, nbits, temp, left;
107 g2int ibmprev, j, lcpack, ioctet, newlen, ndpts;
108 g2int lensec4, lensec5, lensec6, lensec7;
109 g2int issec3 = 0, isprevbmap = 0, lpos3 = 0, JJ, KK, MM;
113 #if defined USE_PNG || defined USE_JPEG2000 || defined USE_OPENJPEG
114 unsigned int allones = 4294967295u;
115 g2int width, height, iscan, itemp;
136 gbit(cgrib, &ilen, iofst, 32);
138 gbit(cgrib, &isecnum, iofst, 8);
150 gbit(cgrib, &ibmprev, iofst, 8);
152 if (ibmprev >= 0 && ibmprev <= 253)
165 printf(
"g2_addfield: Section byte counts don''t add to total.\n");
166 printf(
"g2_addfield: Sum of section byte counts = %ld\n", len);
167 printf(
"g2_addfield: Total byte count in Section 0 = %ld\n", lencurr);
173 if (isecnum != 3 && isecnum != 7)
175 printf(
"g2_addfield: Sections 4-7 can only be added after Section 3 or 7.\n");
176 printf(
"g2_addfield: Section %ld was the last found in given GRIB message.\n",
183 printf(
"g2_addfield: Sections 4-7 can only be added if Section 3 was previously included.\n");
184 printf(
"g2_addfield: Section 3 was not found in given GRIB message.\n");
185 printf(
"g2_addfield: Call to routine addgrid required to specify Grid definition.\n");
192 sbit(cgrib, &four, iofst, 8);
194 sbit(cgrib, &numcoord, iofst, 16);
196 sbit(cgrib, &ipdsnum, iofst, 16);
215 for (i = 0; i < mappds->
maplen; i++)
217 nbits = abs(mappds->
map[i]) * 8;
218 if ((mappds->
map[i] >= 0) || (ipdstmpl[i] >= 0))
219 sbit(cgrib, ipdstmpl + i, iofst, nbits);
222 sbit(cgrib, &one, iofst, 1);
223 temp = abs(ipdstmpl[i]);
224 sbit(cgrib, &temp, iofst + 1, nbits-1);
226 iofst = iofst + nbits;
233 for (i = 0; i < mappds->
extlen; i++)
235 nbits = abs(mappds->
ext[i]) * 8;
236 if (mappds->
ext[i] >= 0 || ipdstmpl[j] >= 0)
237 sbit(cgrib, ipdstmpl + j, iofst, nbits);
240 sbit(cgrib, &one, iofst, 1);
241 temp = abs(ipdstmpl[j]);
242 sbit(cgrib, &temp, iofst + 1, nbits - 1);
244 iofst = iofst + nbits;
256 coordieee = calloc(numcoord,
sizeof(
g2int));
257 mkieee(coordlist, coordieee, numcoord);
258 sbits(cgrib, coordieee, iofst, 32, 0, numcoord);
259 iofst = iofst + (32 * numcoord);
265 lensec4 = (iofst - ibeg) / 8;
266 sbit(cgrib, &lensec4, ibeg, 32);
275 if (ibmap == 0 || ibmap == 254)
277 pfld = malloc(ngrdpts *
sizeof(
float));
279 for (j = 0; j < ngrdpts; j++)
282 pfld[ndpts++] = fld[j];
295 cpack = malloc(nsize);
299 simpack(pfld, ndpts, idrstmpl, cpack, &lcpack);
300 else if (idrsnum == 2 || idrsnum == 3)
301 cmplxpack(pfld, ndpts, idrsnum, idrstmpl, cpack, &lcpack);
302 else if (idrsnum == 50)
304 simpack(pfld + 1, ndpts - 1, idrstmpl, cpack, &lcpack);
305 mkieee(pfld, idrstmpl + 4, 1);
307 else if (idrsnum == 51)
309 getpoly(cgrib + lpos3, &JJ, &KK, &MM);
310 if (JJ != 0 && KK != 0 && MM != 0)
311 specpack(pfld, ndpts, JJ, KK, MM, idrstmpl, cpack, &lcpack);
314 printf(
"g2_addfield: Cannot pack DRT 5.51.\n");
318 #if defined USE_JPEG2000 || defined USE_OPENJPEG
319 else if (idrsnum == 40 || idrsnum == 40000)
323 getdim(cgrib + lpos3, &width, &height, &iscan);
324 if (width == 0 || height == 0)
329 else if (width == allones || height == allones)
334 else if ((iscan & 32) == 32)
347 jpcpack(pfld, width, height, idrstmpl, cpack, &lcpack);
351 else if (idrsnum == 41 || idrsnum == 40010)
355 getdim(cgrib + lpos3, &width, &height, &iscan);
356 if (width == 0 || height == 0)
361 else if (width == allones || height == allones)
366 else if ((iscan & 32) == 32)
378 pngpack(pfld, width, height, idrstmpl, cpack, &lcpack);
383 printf(
"g2_addfield: Data Representation Template 5.%ld not yet implemented.\n", idrsnum);
388 if (ibmap == 0 || ibmap == 254)
406 sbit(cgrib, &five, iofst, 8);
408 sbit(cgrib, &ndpts, iofst, 32);
410 sbit(cgrib, &idrsnum, iofst, 16);
416 for (i = 0; i < mapdrs->
maplen; i++)
418 nbits = abs(mapdrs->
map[i]) * 8;
419 if (mapdrs->
map[i] >= 0 || idrstmpl[i] >= 0)
420 sbit(cgrib, idrstmpl + i, iofst, nbits);
423 sbit(cgrib, &one, iofst, 1);
424 temp = abs(idrstmpl[i]);
425 sbit(cgrib, &temp, iofst + 1, nbits - 1);
427 iofst = iofst + nbits;
433 lensec5 = (iofst - ibeg) / 8;
434 sbit(cgrib, &lensec5, ibeg, 32);
439 sbit(cgrib, &six, iofst, 8);
441 sbit(cgrib, &ibmap, iofst, 8);
447 sbits(cgrib, bmap, iofst, 1, 0, ngrdpts);
448 iofst = iofst + ngrdpts;
453 if (ibmap == 254 && !isprevbmap)
455 printf(
"g2_addfield: Requested previously defined bitmap,");
456 printf(
" but one does not exist in the current GRIB message.\n");
462 left = 8 - (iofst % 8);
465 sbit(cgrib, &zero, iofst, left);
466 iofst = iofst + left;
468 lensec6 = (iofst - ibeg) / 8;
469 sbit(cgrib, &lensec6, ibeg, 32);
474 sbit(cgrib, &seven, iofst, 8);
482 for (j = 0; j < lcpack; j++)
483 cgrib[ioctet + j] = cpack[j];
484 iofst = iofst + (8 * lcpack);
489 lensec7 = (iofst - ibeg) / 8;
490 sbit(cgrib, &lensec7, ibeg, 32);
496 newlen = lencurr + lensec4 + lensec5 + lensec6 + lensec7;
497 sbit(cgrib, &newlen, 96, 32);