43 unsigned char *cpack,
g2int *lcpack)
46 static g2int zero = 0;
47 g2int *ifld, *gref, *glen, *gwidth;
48 g2int *jmin, *jmax, *lbit;
49 g2int i, j, n, imin, imax, left;
50 g2int isd, itemp, ilmax, ngwidthref = 0, nbitsgwidth = 0;
51 g2int nglenref = 0, nglenlast = 0, iofst, ival1, ival2;
52 g2int minsd, nbitsd = 0, maxorig, nbitorig, ngroups;
53 g2int lg, ng, igmax, iwmax, nbitsgref;
54 g2int glength, grpwidth, nbitsglen = 0;
55 g2int kfildo, minpk, inc, maxgrps, ibit, jbit, kbit, novref, lbitref;
56 g2int missopt, miss1, miss2, ier;
57 g2float bscale, dscale, rmax, rmin, temp;
58 static g2int simple_alg = 0;
59 static g2float alog2 = 0.69314718;
68 for (j = 1; j < ndpts; j++)
70 if (fld[j] > rmax) rmax = fld[j];
71 if (fld[j] < rmin) rmin = fld[j];
80 ifld = calloc(ndpts,
sizeof(
g2int));
81 gref = calloc(ndpts,
sizeof(
g2int));
82 gwidth = calloc(ndpts,
sizeof(
g2int));
83 glen = calloc(ndpts,
sizeof(
g2int));
88 imin = (
g2int)rint(rmin*dscale);
91 for (j = 0; j < ndpts; j++)
92 ifld[j] = (
g2int)rint(fld[j] * dscale) - imin;
98 for (j = 0; j < ndpts; j++)
99 ifld[j] = (
g2int)rint(((fld[j] * dscale) - rmin) * bscale);
106 if (idrstmpl[16] !=1 && idrstmpl[16] != 2)
108 if (idrstmpl[16] == 1)
111 for (j = ndpts-1; j > 0; j--)
112 ifld[j] = ifld[j] - ifld[j - 1];
115 else if (idrstmpl[16] == 2)
119 for (j = ndpts - 1; j > 1; j--)
120 ifld[j] = ifld[j] - (2 * ifld[j - 1]) + ifld[j - 2];
128 for (j = isd; j < ndpts; j++)
131 for (j = isd; j < ndpts; j++)
132 ifld[j] = ifld[j] - minsd;
135 temp = log((
double)(abs(minsd) + 1)) / alog2;
136 nbitsd = (
g2int)ceil(temp) + 1;
141 if (idrstmpl[16] == 2 && ival2 > ival1)
143 temp = log((
double)(maxorig + 1)) / alog2;
144 nbitorig = (
g2int)ceil(temp) + 1;
145 if (nbitorig > nbitsd)
149 if ((nbitsd % 8) != 0)
150 nbitsd = nbitsd+(8-(nbitsd%8));
159 sbit(cpack, &ival1, iofst, nbitsd);
160 iofst = iofst+nbitsd;
164 sbit(cpack, &one, iofst, 1);
167 sbit(cpack, &itemp, iofst, nbitsd-1);
168 iofst = iofst + nbitsd - 1;
170 if (idrstmpl[16] == 2)
175 sbit(cpack, &ival2, iofst, nbitsd);
176 iofst = iofst + nbitsd;
180 sbit(cpack, &one, iofst, 1);
183 sbit(cpack, &itemp, iofst, nbitsd - 1);
184 iofst = iofst + nbitsd - 1;
191 sbit(cpack, &minsd, iofst, nbitsd);
192 iofst = iofst + nbitsd;
196 sbit(cpack, &one, iofst, 1);
199 sbit(cpack, &itemp, iofst, nbitsd - 1);
200 iofst = iofst + nbitsd - 1;
210 ngroups = ndpts / 10;
211 for (j = 0; j < ngroups; j++)
216 ngroups = ngroups + 1;
217 glen[ngroups - 1] = itemp;
226 maxgrps = (ndpts / minpk) + 1;
227 jmin = calloc(maxgrps,
sizeof(
g2int));
228 jmax = calloc(maxgrps,
sizeof(
g2int));
229 lbit = calloc(maxgrps,
sizeof(
g2int));
231 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
232 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
233 &kbit, &novref, &lbitref, &ier);
234 for (ng = 0; ng<ngroups; ng++)
235 glen[ng] = glen[ng] + novref;
244 for (ng = 0; ng < ngroups; ng++)
250 for (lg = 1; lg < glen[ng]; lg++)
252 if (ifld[j] < gref[ng])
260 if (gref[ng] != imax)
262 temp = log((
double)(imax - gref[ng] + 1))/alog2;
263 gwidth[ng] = (
g2int)ceil(temp);
269 for (lg = 0; lg < glen[ng]; lg++)
271 ifld[j] = ifld[j] - gref[ng];
282 for (j = 1; j < ngroups; j++)
287 temp = log((
double)(igmax + 1)) / alog2;
288 nbitsgref = (
g2int)ceil(temp);
289 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
290 itemp = nbitsgref * ngroups;
291 iofst = iofst + itemp;
293 if ((itemp % 8) != 0)
295 left = 8 - (itemp % 8);
296 sbit(cpack, &zero, iofst, left);
297 iofst = iofst + left;
307 ngwidthref = gwidth[0];
308 for (j = 1; j < ngroups; j++)
310 if (gwidth[j] > iwmax)
312 if (gwidth[j] < ngwidthref)
313 ngwidthref = gwidth[j];
315 if (iwmax != ngwidthref)
317 temp = log((
double)(iwmax - ngwidthref +1)) / alog2;
318 nbitsgwidth = (
g2int)ceil(temp);
319 for (i = 0; i < ngroups; i++)
320 gwidth[i] = gwidth[i] - ngwidthref;
321 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
322 itemp = nbitsgwidth * ngroups;
323 iofst = iofst + itemp;
325 if ((itemp % 8) != 0)
327 left = 8 - (itemp % 8);
328 sbit(cpack, &zero, iofst, left);
329 iofst = iofst + left;
335 for (i = 0; i < ngroups; i++)
344 for (j = 1; j < ngroups - 1; j++)
348 if (glen[j] < nglenref)
351 nglenlast = glen[ngroups - 1];
352 if (ilmax != nglenref)
354 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
355 nbitsglen = (
g2int)ceil(temp);
356 for (i = 0; i < ngroups - 1; i++)
357 glen[i] = glen[i] - nglenref;
358 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
359 itemp = nbitsglen * ngroups;
360 iofst = iofst + itemp;
362 if ((itemp % 8) != 0)
364 left = 8 - (itemp % 8);
365 sbit(cpack, &zero, iofst, left);
366 iofst = iofst + left;
372 for (i = 0; i < ngroups; i++)
378 for (ng = 0; ng < ngroups; ng++)
380 glength = glen[ng] + nglenref;
381 if (ng == (ngroups - 1))
383 grpwidth = gwidth[ng] + ngwidthref;
386 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
387 iofst = iofst + (grpwidth * glength);
392 if ((iofst % 8) != 0)
394 left = 8 - (iofst % 8);
395 sbit(cpack, &zero, iofst, left);
396 iofst = iofst + left;
419 mkieee(&rmin, idrstmpl, 1);
420 idrstmpl[3] = nbitsgref;
426 idrstmpl[9] = ngroups;
427 idrstmpl[10] = ngwidthref;
428 idrstmpl[11] = nbitsgwidth;
429 idrstmpl[12] = nglenref;
431 idrstmpl[14] = nglenlast;
432 idrstmpl[15] = nbitsglen;
435 idrstmpl[17] = nbitsd / 8;
void compack(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 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 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 ...