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 = 0;
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 float rmissp, rmisss, bscale, dscale, rmin, temp;
59 static float alog2 =
ALOG2;
64 missopt = idrstmpl[6];
65 if (missopt != 1 && missopt != 2)
67 printf(
"misspack: Unrecognized option.\n");
73 rdieee(idrstmpl + 7, &rmissp, 1);
75 rdieee(idrstmpl + 8, &rmisss, 1);
80 ifldmiss = calloc(ndpts,
sizeof(
g2int));
84 for (j = 0; j < ndpts; j++)
100 for (j = 0; j < ndpts; j++)
102 if (fld[j] == rmissp)
106 else if (fld[j] == rmisss)
125 ifld = calloc(ndpts,
sizeof(
g2int));
126 jfld = calloc(ndpts,
sizeof(
g2int));
127 gref = calloc(ndpts,
sizeof(
g2int));
128 gwidth = calloc(ndpts,
sizeof(
g2int));
129 glen = calloc(ndpts,
sizeof(
g2int));
133 if (idrstmpl[1] == 0)
135 imin = (
g2int)rint(rmin * dscale);
137 for (j = 0; j < ndpts; j++)
139 if (ifldmiss[j] == 0)
141 jfld[nonmiss] = (
g2int)rint(fld[j] * dscale) - imin;
148 rmin = rmin * dscale;
149 for (j = 0; j < ndpts; j++)
151 if (ifldmiss[j] == 0)
153 jfld[nonmiss] = (
g2int)rint(((fld[j] * dscale) - rmin) * bscale);
162 if (idrstmpl[16] != 1 && idrstmpl[16] != 2)
164 if (idrstmpl[16] == 1)
167 for (j = nonmiss - 1; j > 0; j--)
168 jfld[j] = jfld[j] - jfld[j - 1];
171 else if (idrstmpl[16] == 2)
175 for (j = nonmiss - 1; j > 1; j--)
176 jfld[j] = jfld[j] - (2 * jfld[j - 1]) + jfld[j - 2];
184 for (j = isd; j < nonmiss; j++)
187 for (j = isd; j < nonmiss; j++)
188 jfld[j] = jfld[j] - minsd;
192 temp = log((
double)(abs(minsd) + 1)) / alog2;
193 nbitsd = (
g2int)ceil(temp) + 1;
198 if (idrstmpl[16] == 2 && ival2 > ival1)
200 temp = log((
double)(maxorig + 1)) / alog2;
201 nbitorig = (
g2int)ceil(temp) + 1;
202 if (nbitorig > nbitsd)
207 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)
291 maxgrps = (ndpts / minpk) + 1;
292 jmin = calloc(maxgrps,
sizeof(
g2int));
293 jmax = calloc(maxgrps,
sizeof(
g2int));
294 lbit = calloc(maxgrps,
sizeof(
g2int));
295 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
296 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
297 &kbit, &novref, &lbitref, &ier);
298 for (ng = 0; ng < ngroups; ng++)
299 glen[ng] = glen[ng] + novref;
307 for (ng = 0; ng < ngroups; ng++)
310 num0 = num1 = num2 = 0;
311 for (j = n; j < n + glen[ng]; j++)
313 if (ifldmiss[j] == 0)
315 if (ifldmiss[j] == 1)
317 if (ifldmiss[j] == 2)
341 gref[ng] = 2147483647;
344 for (lg = 0; lg < glen[ng]; lg++)
346 if (ifldmiss[j] == 0)
348 if (ifld[j] < gref[ng])
360 if (gref[ng] != imax)
362 temp = log((
double)(imax - gref[ng] + 1)) / alog2;
363 gwidth[ng] = (
g2int)ceil(temp);
373 for (lg = 0; lg < glen[ng]; lg++)
375 if (ifldmiss[j] == 0)
376 ifld[j] = ifld[j] - gref[ng];
377 else if (ifldmiss[j] == 1)
379 else if (ifldmiss[j] == 2)
391 for (j = 1; j < ngroups; j++)
400 temp = log((
double)(igmax + 1)) / alog2;
401 nbitsgref = (
g2int)ceil(temp);
404 for (j = 0; j < ngroups; j++)
411 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
412 itemp = nbitsgref * ngroups;
413 iofst = iofst + itemp;
415 if ((itemp % 8) != 0)
417 left = 8 - (itemp % 8);
418 sbit(cpack, &zero, iofst, left);
419 iofst = iofst + left;
431 ngwidthref = gwidth[0];
432 for (j = 1; j < ngroups; j++)
434 if (gwidth[j] > iwmax)
436 if (gwidth[j] < ngwidthref)
437 ngwidthref = gwidth[j];
439 if (iwmax != ngwidthref)
441 temp = log((
double)(iwmax - ngwidthref + 1)) / alog2;
442 nbitsgwidth = (
g2int)ceil(temp);
443 for (i = 0; i < ngroups; i++)
444 gwidth[i] = gwidth[i] - ngwidthref;
445 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
446 itemp = nbitsgwidth * ngroups;
447 iofst = iofst + itemp;
449 if ((itemp % 8) != 0)
451 left = 8 - (itemp % 8);
452 sbit(cpack, &zero, iofst, left);
453 iofst = iofst + left;
459 for (i = 0; i < ngroups; i++)
468 for (j = 1; j < ngroups - 1; j++)
472 if (glen[j] < nglenref)
475 nglenlast = glen[ngroups - 1];
476 if (ilmax != nglenref)
478 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
479 nbitsglen = (
g2int)ceil(temp);
480 for (i = 0; i < ngroups - 1; i++)
481 glen[i] = glen[i] - nglenref;
482 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
483 itemp = nbitsglen * ngroups;
484 iofst = iofst + itemp;
486 if ((itemp % 8) != 0)
488 left = 8 - (itemp % 8);
489 sbit(cpack, &zero, iofst, left);
490 iofst = iofst + left;
496 for (i = 0; i < ngroups; i++)
502 for (ng = 0; ng < ngroups; ng++)
504 glength = glen[ng] + nglenref;
505 if (ng == (ngroups - 1))
507 grpwidth = gwidth[ng] + ngwidthref;
510 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
511 iofst = iofst + (grpwidth * glength);
517 if ((iofst % 8) != 0)
519 left = 8 - (iofst % 8);
520 sbit(cpack, &zero, iofst, left);
521 iofst = iofst + left;
539 mkieee(&rmin, idrstmpl, 1);
540 idrstmpl[3] = nbitsgref;
543 idrstmpl[9] = ngroups;
544 idrstmpl[10] = ngwidthref;
545 idrstmpl[11] = nbitsgwidth;
546 idrstmpl[12] = nglenref;
548 idrstmpl[14] = nglenlast;
549 idrstmpl[15] = nbitsglen;
551 idrstmpl[17] = nbitsd / 8;
int pack_gp(g2int *kfildo, g2int *ic, g2int *nxy, g2int *is523, g2int *minpk, g2int *inc, g2int *missp, g2int *misss, g2int *jmin, g2int *jmax, g2int *lbit, g2int *nov, g2int *ndg, g2int *lx, g2int *ibit, g2int *jbit, g2int *kbit, g2int *novref, g2int *lbitref, g2int *ier)
Determines groups of variable size, but at least of size minpk, the associated max (jmax( )) and min ...