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 float bscale, dscale, rmax, rmin, temp;
58 static float alog2 =
ALOG2;
67 for (j = 1; j < ndpts; j++)
69 if (fld[j] > rmax) rmax = fld[j];
70 if (fld[j] < rmin) rmin = fld[j];
79 ifld = calloc(ndpts,
sizeof(
g2int));
80 gref = calloc(ndpts,
sizeof(
g2int));
81 gwidth = calloc(ndpts,
sizeof(
g2int));
82 glen = calloc(ndpts,
sizeof(
g2int));
87 imin = (
g2int)rint(rmin*dscale);
90 for (j = 0; j < ndpts; j++)
91 ifld[j] = (
g2int)rint(fld[j] * dscale) - imin;
97 for (j = 0; j < ndpts; j++)
98 ifld[j] = (
g2int)rint(((fld[j] * dscale) - rmin) * bscale);
105 if (idrstmpl[16] !=1 && idrstmpl[16] != 2)
107 if (idrstmpl[16] == 1)
110 for (j = ndpts-1; j > 0; j--)
111 ifld[j] = ifld[j] - ifld[j - 1];
114 else if (idrstmpl[16] == 2)
118 for (j = ndpts - 1; j > 1; j--)
119 ifld[j] = ifld[j] - (2 * ifld[j - 1]) + ifld[j - 2];
127 for (j = isd; j < ndpts; j++)
130 for (j = isd; j < ndpts; j++)
131 ifld[j] = ifld[j] - minsd;
134 temp = log((
double)(abs(minsd) + 1)) / alog2;
135 nbitsd = (
g2int)ceil(temp) + 1;
140 if (idrstmpl[16] == 2 && ival2 > ival1)
142 temp = log((
double)(maxorig + 1)) / alog2;
143 nbitorig = (
g2int)ceil(temp) + 1;
144 if (nbitorig > nbitsd)
148 if ((nbitsd % 8) != 0)
149 nbitsd = nbitsd+(8-(nbitsd%8));
158 sbit(cpack, &ival1, iofst, nbitsd);
159 iofst = iofst+nbitsd;
163 sbit(cpack, &one, iofst, 1);
166 sbit(cpack, &itemp, iofst, nbitsd-1);
167 iofst = iofst + nbitsd - 1;
169 if (idrstmpl[16] == 2)
174 sbit(cpack, &ival2, iofst, nbitsd);
175 iofst = iofst + nbitsd;
179 sbit(cpack, &one, iofst, 1);
182 sbit(cpack, &itemp, iofst, nbitsd - 1);
183 iofst = iofst + nbitsd - 1;
190 sbit(cpack, &minsd, iofst, nbitsd);
191 iofst = iofst + nbitsd;
195 sbit(cpack, &one, iofst, 1);
198 sbit(cpack, &itemp, iofst, nbitsd - 1);
199 iofst = iofst + nbitsd - 1;
208 maxgrps = (ndpts / minpk) + 1;
209 jmin = calloc(maxgrps,
sizeof(
g2int));
210 jmax = calloc(maxgrps,
sizeof(
g2int));
211 lbit = calloc(maxgrps,
sizeof(
g2int));
213 pack_gp(&kfildo, ifld, &ndpts, &missopt, &minpk, &inc, &miss1, &miss2,
214 jmin, jmax, lbit, glen, &maxgrps, &ngroups, &ibit, &jbit,
215 &kbit, &novref, &lbitref, &ier);
216 for (ng = 0; ng<ngroups; ng++)
217 glen[ng] = glen[ng] + novref;
225 for (ng = 0; ng < ngroups; ng++)
231 for (lg = 1; lg < glen[ng]; lg++)
233 if (ifld[j] < gref[ng])
241 if (gref[ng] != imax)
243 temp = log((
double)(imax - gref[ng] + 1))/alog2;
244 gwidth[ng] = (
g2int)ceil(temp);
250 for (lg = 0; lg < glen[ng]; lg++)
252 ifld[j] = ifld[j] - gref[ng];
263 for (j = 1; j < ngroups; j++)
268 temp = log((
double)(igmax + 1)) / alog2;
269 nbitsgref = (
g2int)ceil(temp);
270 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
271 itemp = nbitsgref * ngroups;
272 iofst = iofst + itemp;
274 if ((itemp % 8) != 0)
276 left = 8 - (itemp % 8);
277 sbit(cpack, &zero, iofst, left);
278 iofst = iofst + left;
288 ngwidthref = gwidth[0];
289 for (j = 1; j < ngroups; j++)
291 if (gwidth[j] > iwmax)
293 if (gwidth[j] < ngwidthref)
294 ngwidthref = gwidth[j];
296 if (iwmax != ngwidthref)
298 temp = log((
double)(iwmax - ngwidthref +1)) / alog2;
299 nbitsgwidth = (
g2int)ceil(temp);
300 for (i = 0; i < ngroups; i++)
301 gwidth[i] = gwidth[i] - ngwidthref;
302 sbits(cpack, gwidth, iofst, nbitsgwidth, 0, ngroups);
303 itemp = nbitsgwidth * ngroups;
304 iofst = iofst + itemp;
306 if ((itemp % 8) != 0)
308 left = 8 - (itemp % 8);
309 sbit(cpack, &zero, iofst, left);
310 iofst = iofst + left;
316 for (i = 0; i < ngroups; i++)
325 for (j = 1; j < ngroups - 1; j++)
329 if (glen[j] < nglenref)
332 nglenlast = glen[ngroups - 1];
333 if (ilmax != nglenref)
335 temp = log((
double)(ilmax - nglenref + 1)) / alog2;
336 nbitsglen = (
g2int)ceil(temp);
337 for (i = 0; i < ngroups - 1; i++)
338 glen[i] = glen[i] - nglenref;
339 sbits(cpack, glen, iofst, nbitsglen, 0, ngroups);
340 itemp = nbitsglen * ngroups;
341 iofst = iofst + itemp;
343 if ((itemp % 8) != 0)
345 left = 8 - (itemp % 8);
346 sbit(cpack, &zero, iofst, left);
347 iofst = iofst + left;
353 for (i = 0; i < ngroups; i++)
359 for (ng = 0; ng < ngroups; ng++)
361 glength = glen[ng] + nglenref;
362 if (ng == (ngroups - 1))
364 grpwidth = gwidth[ng] + ngwidthref;
367 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
368 iofst = iofst + (grpwidth * glength);
373 if ((iofst % 8) != 0)
375 left = 8 - (iofst % 8);
376 sbit(cpack, &zero, iofst, left);
377 iofst = iofst + left;
400 mkieee(&rmin, idrstmpl, 1);
401 idrstmpl[3] = nbitsgref;
407 idrstmpl[9] = ngroups;
408 idrstmpl[10] = ngwidthref;
409 idrstmpl[11] = nbitsgwidth;
410 idrstmpl[12] = nglenref;
412 idrstmpl[14] = nglenlast;
413 idrstmpl[15] = nbitsglen;
416 idrstmpl[17] = nbitsd / 8;
void compack(float *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 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 ...