44 unsigned char *cpack,
g2int *lcpack)
46 g2int *ifld, *ifldmiss, *jfld;
47 g2int *jmin, *jmax, *lbit;
48 static g2int zero = 0;
49 g2int *gref, *gwidth, *glen;
50 g2int glength, grpwidth;
51 g2int i, n, iofst, imin, ival1, ival2, isd, minsd, nbitsd;
52 g2int nbitsgref, left, iwmax, ngwidthref, nbitsgwidth, ilmax;
53 g2int nglenref, nglenlast, nbitsglen;
54 g2int j, missopt, nonmiss, itemp, maxorig, nbitorig, miss1, miss2;
55 g2int ngroups, ng, num0, num1, num2;
56 g2int imax, lg, mtemp, ier, igmax;
57 g2int kfildo, minpk, inc, maxgrps, ibit, jbit, kbit, novref, lbitref;
58 g2float rmissp, rmisss, bscale, dscale, rmin, temp;
59 static g2int simple_alg = 0;
60 static g2float alog2 = 0.69314718;
65 missopt = idrstmpl[6];
66 if (missopt != 1 && missopt != 2)
68 printf(
"misspack: Unrecognized option.\n");
74 rdieee(idrstmpl + 7, &rmissp, 1);
76 rdieee(idrstmpl + 8, &rmisss, 1);
81 ifldmiss = calloc(ndpts,
sizeof(
g2int));
85 for (j = 0; j < ndpts; j++)
101 for (j = 0; j < ndpts; j++)
103 if (fld[j] == rmissp)
107 else if (fld[j] == rmisss)
126 ifld = calloc(ndpts,
sizeof(
g2int));
127 jfld = calloc(ndpts,
sizeof(
g2int));
128 gref = calloc(ndpts,
sizeof(
g2int));
129 gwidth = calloc(ndpts,
sizeof(
g2int));
130 glen = calloc(ndpts,
sizeof(
g2int));
134 if (idrstmpl[1] == 0)
136 imin = (
g2int)rint(rmin * dscale);
138 for (j = 0; j < ndpts; j++)
140 if (ifldmiss[j] == 0)
142 jfld[nonmiss] = (
g2int)rint(fld[j] * dscale) - imin;
149 rmin = rmin * dscale;
150 for (j = 0; j < ndpts; j++)
152 if (ifldmiss[j] == 0)
154 jfld[nonmiss] = (
g2int)rint(((fld[j] * dscale) - rmin) * bscale);
163 if (idrstmpl[16] != 1 && idrstmpl[16] != 2)
165 if (idrstmpl[16] == 1)
168 for (j = nonmiss - 1; j > 0; j--)
169 jfld[j] = jfld[j] - jfld[j - 1];
172 else if (idrstmpl[16] == 2)
176 for (j = nonmiss - 1; j > 1; j--)
177 jfld[j] = jfld[j] - (2 * jfld[j - 1]) + jfld[j - 2];
185 for (j = isd; j < nonmiss; j++)
188 for (j = isd; j < nonmiss; j++)
189 jfld[j] = jfld[j] - minsd;
193 temp = log((
double)(abs(minsd) + 1)) / alog2;
194 nbitsd = (
g2int)ceil(temp) + 1;
199 if (idrstmpl[16] == 2 && ival2 > ival1)
201 temp = log((
double)(maxorig + 1)) / alog2;
202 nbitorig = (
g2int)ceil(temp) + 1;
203 if (nbitorig > nbitsd)
208 nbitsd = nbitsd+(8-(nbitsd%8));
216 sbit(cpack, &ival1, iofst, nbitsd);
217 iofst = iofst + nbitsd;
221 sbit(cpack, &one, iofst, 1);
224 sbit(cpack, &itemp, iofst, nbitsd-1);
225 iofst = iofst + nbitsd - 1;
227 if (idrstmpl[16] == 2)
232 sbit(cpack, &ival2, iofst, nbitsd);
233 iofst = iofst + nbitsd;
237 sbit(cpack, &one, iofst, 1);
240 sbit(cpack, &itemp, iofst, nbitsd-1);
241 iofst = iofst + nbitsd - 1;
247 sbit(cpack, &minsd, iofst, nbitsd);
248 iofst = iofst + nbitsd;
252 sbit(cpack, &one, iofst, 1);
255 sbit(cpack, &itemp, iofst, nbitsd-1);
256 iofst = iofst + nbitsd - 1;
263 for (j = 0; j < nonmiss; j++)
269 for (j = 0; j < ndpts; j++)
271 if (ifldmiss[j] == 0)
276 else if (ifldmiss[j] == 1)
280 else if (ifldmiss[j] == 2)
290 ngroups = ndpts / 10;
291 for (j = 0; j < ngroups; j++)
297 glen[ngroups - 1] = itemp;
306 maxgrps = (ndpts / minpk) + 1;
307 jmin = calloc(maxgrps,
sizeof(
g2int));
308 jmax = calloc(maxgrps,
sizeof(
g2int));
309 lbit = calloc(maxgrps,
sizeof(
g2int));
310 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
311 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
312 &kbit, &novref, &lbitref, &ier);
313 for (ng = 0; ng < ngroups; ng++)
314 glen[ng] = glen[ng]+novref;
323 for (ng = 0; ng<ngroups; ng++)
326 num0 = num1 = num2 = 0;
327 for (j = n; j < n + glen[ng]; j++)
329 if (ifldmiss[j] == 0)
331 if (ifldmiss[j] == 1)
333 if (ifldmiss[j] == 2)
342 else if (num2 == 0) {
354 gref[ng] = 2147483647;
357 for (lg = 0; lg < glen[ng]; lg++)
359 if (ifldmiss[j] == 0) {
360 if (ifld[j] < gref[ng])
367 if (missopt == 1) imax = imax+1;
368 if (missopt == 2) imax = imax+2;
370 if (gref[ng] != imax) {
371 temp = log((
double)(imax-gref[ng]+1))/alog2;
372 gwidth[ng] = (
g2int)ceil(temp);
381 for (lg = 0; lg<glen[ng]; lg++)
383 if (ifldmiss[j] == 0)
384 ifld[j] = ifld[j] - gref[ng];
385 else if (ifldmiss[j] == 1)
387 else if (ifldmiss[j] == 2)
399 for (j = 1; j < ngroups; j++)
408 temp = log((
double)(igmax + 1)) / alog2;
409 nbitsgref = (
g2int)ceil(temp);
412 for (j = 0; j < ngroups; j++)
419 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
420 itemp = nbitsgref * ngroups;
421 iofst = iofst + itemp;
423 if ((itemp % 8) != 0)
425 left = 8 - (itemp % 8);
426 sbit(cpack, &zero, iofst, left);
427 iofst = iofst + left;
439 ngwidthref = gwidth[0];
440 for (j = 1; j < ngroups; j++)
442 if (gwidth[j] > iwmax)
444 if (gwidth[j] < ngwidthref)
445 ngwidthref = gwidth[j];
447 if (iwmax != ngwidthref)
449 temp = log((
double)(iwmax - ngwidthref + 1)) / alog2;
450 nbitsgwidth = (
g2int)ceil(temp);
451 for (i = 0; i<ngroups; i++)
452 gwidth[i] = gwidth[i]-ngwidthref;
453 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
454 itemp = nbitsgwidth * ngroups;
455 iofst = iofst + itemp;
459 left = 8 - (itemp % 8);
460 sbit(cpack, &zero, iofst, left);
461 iofst = iofst + left;
467 for (i = 0; i < ngroups; i++)
476 for (j = 1; j < ngroups - 1; j++)
480 if (glen[j] < nglenref)
483 nglenlast = glen[ngroups - 1];
484 if (ilmax != nglenref)
486 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
487 nbitsglen = (
g2int)ceil(temp);
488 for (i = 0; i < ngroups - 1; i++)
489 glen[i] = glen[i] - nglenref;
490 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
491 itemp = nbitsglen * ngroups;
492 iofst = iofst + itemp;
494 if ((itemp % 8) != 0)
496 left = 8 - (itemp % 8);
497 sbit(cpack, &zero, iofst, left);
498 iofst = iofst + left;
503 for (i = 0; i < ngroups; i++)
509 for (ng = 0; ng < ngroups; ng++)
511 glength = glen[ng] + nglenref;
512 if (ng == (ngroups - 1))
514 grpwidth = gwidth[ng] + ngwidthref;
517 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
518 iofst = iofst + (grpwidth * glength);
524 if ((iofst % 8) != 0)
526 left = 8 - (iofst % 8);
527 sbit(cpack, &zero, iofst, left);
528 iofst = iofst + left;
546 mkieee(&rmin, idrstmpl, 1);
547 idrstmpl[3] = nbitsgref;
550 idrstmpl[9] = ngroups;
551 idrstmpl[10] = ngwidthref;
552 idrstmpl[11] = nbitsgwidth;
553 idrstmpl[12] = nglenref;
555 idrstmpl[14] = nglenlast;
556 idrstmpl[15] = nbitsglen;
559 idrstmpl[17] = nbitsd / 8;
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.
int64_t g2int
Long integer type.
double int_power(double x, g2int y)
Function similar to C pow() power function.
void misspack(g2float *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...
void mkieee(g2float *a, g2int *rieee, g2int num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
int pack_gp(integer *kfildo, integer *ic, integer *nxy, integer *is523, integer *minpk, integer *inc, integer *missp, integer *misss, integer *jmin, integer *jmax, integer *lbit, integer *nov, integer *ndg, integer *lx, integer *ibit, integer *jbit, integer *kbit, integer *novref, integer *lbitref, integer *ier)
Determines groups of variable size, but at least of size minpk, the associated max (jmax( )) and min ...
void rdieee(g2int *rieee, g2float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.