43 unsigned char *cpack,
g2int *lcpack)
45 g2int *ifld, *ifldmiss, *jfld;
46 g2int *jmin, *jmax, *lbit;
47 static g2int zero = 0;
48 g2int *gref, *gwidth, *glen;
49 g2int glength, grpwidth;
50 g2int i, n, iofst, imin, ival1, ival2, isd, minsd, nbitsd = 0;
51 g2int nbitsgref, left, iwmax, ngwidthref, nbitsgwidth, ilmax;
52 g2int nglenref, nglenlast, nbitsglen;
53 g2int j, missopt, nonmiss, itemp, maxorig, nbitorig, miss1, miss2;
54 g2int ngroups, ng, num0, num1, num2;
55 g2int imax, lg, mtemp, ier, igmax;
56 g2int kfildo, minpk, inc, maxgrps, ibit, jbit, kbit, novref, lbitref;
57 float rmissp, rmisss, bscale, dscale, rmin, temp;
58 static float alog2 =
ALOG2;
63 missopt = idrstmpl[6];
64 if (missopt != 1 && missopt != 2)
66 printf(
"misspack: Unrecognized option.\n");
72 rdieee(idrstmpl + 7, &rmissp, 1);
74 rdieee(idrstmpl + 8, &rmisss, 1);
79 ifldmiss = calloc(ndpts,
sizeof(
g2int));
83 for (j = 0; j < ndpts; j++)
99 for (j = 0; j < ndpts; j++)
101 if (fld[j] == rmissp)
105 else if (fld[j] == rmisss)
124 ifld = calloc(ndpts,
sizeof(
g2int));
125 jfld = calloc(ndpts,
sizeof(
g2int));
126 gref = calloc(ndpts,
sizeof(
g2int));
127 gwidth = calloc(ndpts,
sizeof(
g2int));
128 glen = calloc(ndpts,
sizeof(
g2int));
132 if (idrstmpl[1] == 0)
134 imin = (
g2int)rint(rmin * dscale);
136 for (j = 0; j < ndpts; j++)
138 if (ifldmiss[j] == 0)
140 jfld[nonmiss] = (
g2int)rint(fld[j] * dscale) - imin;
147 rmin = rmin * dscale;
148 for (j = 0; j < ndpts; j++)
150 if (ifldmiss[j] == 0)
152 jfld[nonmiss] = (
g2int)rint(((fld[j] * dscale) - rmin) * bscale);
161 if (idrstmpl[16] != 1 && idrstmpl[16] != 2)
163 if (idrstmpl[16] == 1)
166 for (j = nonmiss - 1; j > 0; j--)
167 jfld[j] = jfld[j] - jfld[j - 1];
170 else if (idrstmpl[16] == 2)
174 for (j = nonmiss - 1; j > 1; j--)
175 jfld[j] = jfld[j] - (2 * jfld[j - 1]) + jfld[j - 2];
183 for (j = isd; j < nonmiss; j++)
186 for (j = isd; j < nonmiss; j++)
187 jfld[j] = jfld[j] - minsd;
191 temp = log((
double)(abs(minsd) + 1)) / alog2;
192 nbitsd = (
g2int)ceil(temp) + 1;
197 if (idrstmpl[16] == 2 && ival2 > ival1)
199 temp = log((
double)(maxorig + 1)) / alog2;
200 nbitorig = (
g2int)ceil(temp) + 1;
201 if (nbitorig > nbitsd)
206 nbitsd = nbitsd + (8 - (nbitsd % 8));
214 sbit(cpack, &ival1, iofst, nbitsd);
215 iofst = iofst + nbitsd;
219 sbit(cpack, &one, iofst, 1);
222 sbit(cpack, &itemp, iofst, nbitsd-1);
223 iofst = iofst + nbitsd - 1;
225 if (idrstmpl[16] == 2)
230 sbit(cpack, &ival2, iofst, nbitsd);
231 iofst = iofst + nbitsd;
235 sbit(cpack, &one, iofst, 1);
238 sbit(cpack, &itemp, iofst, nbitsd-1);
239 iofst = iofst + nbitsd - 1;
245 sbit(cpack, &minsd, iofst, nbitsd);
246 iofst = iofst + nbitsd;
250 sbit(cpack, &one, iofst, 1);
253 sbit(cpack, &itemp, iofst, nbitsd-1);
254 iofst = iofst + nbitsd - 1;
261 for (j = 0; j < nonmiss; j++)
267 for (j = 0; j < ndpts; j++)
269 if (ifldmiss[j] == 0)
274 else if (ifldmiss[j] == 1)
278 else if (ifldmiss[j] == 2)
289 maxgrps = (ndpts / minpk) + 1;
290 jmin = calloc(maxgrps,
sizeof(
g2int));
291 jmax = calloc(maxgrps,
sizeof(
g2int));
292 lbit = calloc(maxgrps,
sizeof(
g2int));
293 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
294 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
295 &kbit, &novref, &lbitref, &ier);
296 for (ng = 0; ng < ngroups; ng++)
297 glen[ng] = glen[ng] + novref;
305 for (ng = 0; ng < ngroups; ng++)
308 num0 = num1 = num2 = 0;
309 for (j = n; j < n + glen[ng]; j++)
311 if (ifldmiss[j] == 0)
313 if (ifldmiss[j] == 1)
315 if (ifldmiss[j] == 2)
339 gref[ng] = 2147483647;
342 for (lg = 0; lg < glen[ng]; lg++)
344 if (ifldmiss[j] == 0)
346 if (ifld[j] < gref[ng])
358 if (gref[ng] != imax)
360 temp = log((
double)(imax - gref[ng] + 1)) / alog2;
361 gwidth[ng] = (
g2int)ceil(temp);
371 for (lg = 0; lg < glen[ng]; lg++)
373 if (ifldmiss[j] == 0)
374 ifld[j] = ifld[j] - gref[ng];
375 else if (ifldmiss[j] == 1)
377 else if (ifldmiss[j] == 2)
389 for (j = 1; j < ngroups; j++)
398 temp = log((
double)(igmax + 1)) / alog2;
399 nbitsgref = (
g2int)ceil(temp);
402 for (j = 0; j < ngroups; j++)
409 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
410 itemp = nbitsgref * ngroups;
411 iofst = iofst + itemp;
413 if ((itemp % 8) != 0)
415 left = 8 - (itemp % 8);
416 sbit(cpack, &zero, iofst, left);
417 iofst = iofst + left;
429 ngwidthref = gwidth[0];
430 for (j = 1; j < ngroups; j++)
432 if (gwidth[j] > iwmax)
434 if (gwidth[j] < ngwidthref)
435 ngwidthref = gwidth[j];
437 if (iwmax != ngwidthref)
439 temp = log((
double)(iwmax - ngwidthref + 1)) / alog2;
440 nbitsgwidth = (
g2int)ceil(temp);
441 for (i = 0; i<ngroups; i++)
442 gwidth[i] = gwidth[i]-ngwidthref;
443 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
444 itemp = nbitsgwidth * ngroups;
445 iofst = iofst + itemp;
449 left = 8 - (itemp % 8);
450 sbit(cpack, &zero, iofst, left);
451 iofst = iofst + left;
457 for (i = 0; i < ngroups; i++)
466 for (j = 1; j < ngroups - 1; j++)
470 if (glen[j] < nglenref)
473 nglenlast = glen[ngroups - 1];
474 if (ilmax != nglenref)
476 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
477 nbitsglen = (
g2int)ceil(temp);
478 for (i = 0; i < ngroups - 1; i++)
479 glen[i] = glen[i] - nglenref;
480 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
481 itemp = nbitsglen * ngroups;
482 iofst = iofst + itemp;
484 if ((itemp % 8) != 0)
486 left = 8 - (itemp % 8);
487 sbit(cpack, &zero, iofst, left);
488 iofst = iofst + left;
494 for (i = 0; i < ngroups; i++)
500 for (ng = 0; ng < ngroups; ng++)
502 glength = glen[ng] + nglenref;
503 if (ng == (ngroups - 1))
505 grpwidth = gwidth[ng] + ngwidthref;
508 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
509 iofst = iofst + (grpwidth * glength);
515 if ((iofst % 8) != 0)
517 left = 8 - (iofst % 8);
518 sbit(cpack, &zero, iofst, left);
519 iofst = iofst + left;
537 mkieee(&rmin, idrstmpl, 1);
538 idrstmpl[3] = nbitsgref;
541 idrstmpl[9] = ngroups;
542 idrstmpl[10] = ngwidthref;
543 idrstmpl[11] = nbitsgwidth;
544 idrstmpl[12] = nglenref;
546 idrstmpl[14] = nglenlast;
547 idrstmpl[15] = nbitsglen;
549 idrstmpl[17] = nbitsd / 8;
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
int64_t g2int
Long integer type.
Header file with internal function prototypes NCEPLIBS-g2c library.
void mkieee(float *a, g2int *rieee, g2int num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
double int_power(double x, g2int y)
Function similar to C pow() power function.
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 ...
void rdieee(g2int *rieee, float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
void misspack(float *fld, g2int ndpts, g2int idrsnum, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This function packs up a data field using a complex packing algorithm as defined in the GRIB2 documen...