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));
215 sbit(cpack, &ival1, iofst, nbitsd);
216 iofst = iofst + nbitsd;
220 sbit(cpack, &one, iofst, 1);
223 sbit(cpack, &itemp, iofst, nbitsd-1);
224 iofst = iofst + nbitsd - 1;
226 if (idrstmpl[16] == 2)
231 sbit(cpack, &ival2, iofst, nbitsd);
232 iofst = iofst + nbitsd;
236 sbit(cpack, &one, iofst, 1);
239 sbit(cpack, &itemp, iofst, nbitsd-1);
240 iofst = iofst + nbitsd - 1;
246 sbit(cpack, &minsd, iofst, nbitsd);
247 iofst = iofst + nbitsd;
251 sbit(cpack, &one, iofst, 1);
254 sbit(cpack, &itemp, iofst, nbitsd-1);
255 iofst = iofst + nbitsd - 1;
262 for (j = 0; j < nonmiss; j++)
268 for (j = 0; j < ndpts; j++)
270 if (ifldmiss[j] == 0)
275 else if (ifldmiss[j] == 1)
279 else if (ifldmiss[j] == 2)
290 maxgrps = (ndpts / minpk) + 1;
291 jmin = calloc(maxgrps,
sizeof(
g2int));
292 jmax = calloc(maxgrps,
sizeof(
g2int));
293 lbit = calloc(maxgrps,
sizeof(
g2int));
294 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
295 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
296 &kbit, &novref, &lbitref, &ier);
297 for (ng = 0; ng < ngroups; ng++)
298 glen[ng] = glen[ng] + novref;
306 for (ng = 0; ng < ngroups; ng++)
309 num0 = num1 = num2 = 0;
310 for (j = n; j < n + glen[ng]; j++)
312 if (ifldmiss[j] == 0)
314 if (ifldmiss[j] == 1)
316 if (ifldmiss[j] == 2)
340 gref[ng] = 2147483647;
343 for (lg = 0; lg < glen[ng]; lg++)
345 if (ifldmiss[j] == 0)
347 if (ifld[j] < gref[ng])
359 if (gref[ng] != imax)
361 temp = log((
double)(imax - gref[ng] + 1)) / alog2;
362 gwidth[ng] = (
g2int)ceil(temp);
372 for (lg = 0; lg < glen[ng]; lg++)
374 if (ifldmiss[j] == 0)
375 ifld[j] = ifld[j] - gref[ng];
376 else if (ifldmiss[j] == 1)
378 else if (ifldmiss[j] == 2)
390 for (j = 1; j < ngroups; j++)
399 temp = log((
double)(igmax + 1)) / alog2;
400 nbitsgref = (
g2int)ceil(temp);
403 for (j = 0; j < ngroups; j++)
410 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
411 itemp = nbitsgref * ngroups;
412 iofst = iofst + itemp;
414 if ((itemp % 8) != 0)
416 left = 8 - (itemp % 8);
417 sbit(cpack, &zero, iofst, left);
418 iofst = iofst + left;
430 ngwidthref = gwidth[0];
431 for (j = 1; j < ngroups; j++)
433 if (gwidth[j] > iwmax)
435 if (gwidth[j] < ngwidthref)
436 ngwidthref = gwidth[j];
438 if (iwmax != ngwidthref)
440 temp = log((
double)(iwmax - ngwidthref + 1)) / alog2;
441 nbitsgwidth = (
g2int)ceil(temp);
442 for (i = 0; i<ngroups; i++)
443 gwidth[i] = gwidth[i]-ngwidthref;
444 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
445 itemp = nbitsgwidth * ngroups;
446 iofst = iofst + itemp;
450 left = 8 - (itemp % 8);
451 sbit(cpack, &zero, iofst, left);
452 iofst = iofst + left;
458 for (i = 0; i < ngroups; i++)
467 for (j = 1; j < ngroups - 1; j++)
471 if (glen[j] < nglenref)
474 nglenlast = glen[ngroups - 1];
475 if (ilmax != nglenref)
477 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
478 nbitsglen = (
g2int)ceil(temp);
479 for (i = 0; i < ngroups - 1; i++)
480 glen[i] = glen[i] - nglenref;
481 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
482 itemp = nbitsglen * ngroups;
483 iofst = iofst + itemp;
485 if ((itemp % 8) != 0)
487 left = 8 - (itemp % 8);
488 sbit(cpack, &zero, iofst, left);
489 iofst = iofst + left;
495 for (i = 0; i < ngroups; i++)
501 for (ng = 0; ng < ngroups; ng++)
503 glength = glen[ng] + nglenref;
504 if (ng == (ngroups - 1))
506 grpwidth = gwidth[ng] + ngwidthref;
509 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
510 iofst = iofst + (grpwidth * glength);
516 if ((iofst % 8) != 0)
518 left = 8 - (iofst % 8);
519 sbit(cpack, &zero, iofst, left);
520 iofst = iofst + left;
538 mkieee(&rmin, idrstmpl, 1);
539 idrstmpl[3] = nbitsgref;
542 idrstmpl[9] = ngroups;
543 idrstmpl[10] = ngwidthref;
544 idrstmpl[11] = nbitsgwidth;
545 idrstmpl[12] = nglenref;
547 idrstmpl[14] = nglenlast;
548 idrstmpl[15] = nbitsglen;
550 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 ...