NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
misspack.c
Go to the documentation of this file.
1
7#include <stdlib.h>
8#include <math.h>
9#include "grib2_int.h"
10
42void
43misspack(float *fld, g2int ndpts, g2int idrsnum, g2int *idrstmpl,
44 unsigned char *cpack, g2int *lcpack)
45{
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; /* ln(2.0) */
60 static g2int one = 1;
61
62 bscale = int_power(2.0, -idrstmpl[1]);
63 dscale = int_power(10.0, idrstmpl[2]);
64 missopt = idrstmpl[6];
65 if (missopt != 1 && missopt != 2)
66 {
67 printf("misspack: Unrecognized option.\n");
68 *lcpack = -1;
69 return;
70 }
71 else
72 { /* Get missing values */
73 rdieee(idrstmpl + 7, &rmissp, 1);
74 if (missopt == 2)
75 rdieee(idrstmpl + 8, &rmisss, 1);
76 }
77
78 /* Find min value of non-missing values in the data, AND set up
79 * missing value mapping of the field. */
80 ifldmiss = calloc(ndpts, sizeof(g2int));
81 rmin = 1E+37;
82 if (missopt == 1)
83 { /* Primary missing value only */
84 for (j = 0; j < ndpts; j++)
85 {
86 if (fld[j] == rmissp)
87 {
88 ifldmiss[j] = 1;
89 }
90 else
91 {
92 ifldmiss[j] = 0;
93 if (fld[j] < rmin)
94 rmin = fld[j];
95 }
96 }
97 }
98 if (missopt == 2)
99 { /* Primary and secondary missing values */
100 for (j = 0; j < ndpts; j++)
101 {
102 if (fld[j] == rmissp)
103 {
104 ifldmiss[j] = 1;
105 }
106 else if (fld[j] == rmisss)
107 {
108 ifldmiss[j] = 2;
109 }
110 else
111 {
112 ifldmiss[j] = 0;
113 if (fld[j] < rmin)
114 rmin = fld[j];
115 }
116 }
117 }
118
119 /* Allocate work arrays: Note: -ifldmiss[j],j = 0,ndpts-1 is a map
120 * of original field indicating which of the original data values
121 * are primary missing (1), sencondary missing (2) or non-missing
122 * (0). -jfld[j],j = 0,nonmiss-1 is a subarray of just the
123 * non-missing values from the original field. */
124 iofst = 0;
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));
130
131 /* Scale original data. */
132 nonmiss = 0;
133 if (idrstmpl[1] == 0)
134 { /* No binary scaling */
135 imin = (g2int)rint(rmin * dscale);
136 rmin = (float)imin;
137 for (j = 0; j < ndpts; j++)
138 {
139 if (ifldmiss[j] == 0)
140 {
141 jfld[nonmiss] = (g2int)rint(fld[j] * dscale) - imin;
142 nonmiss++;
143 }
144 }
145 }
146 else
147 { /* Use binary scaling factor */
148 rmin = rmin * dscale;
149 for (j = 0; j < ndpts; j++)
150 {
151 if (ifldmiss[j] == 0)
152 {
153 jfld[nonmiss] = (g2int)rint(((fld[j] * dscale) - rmin) * bscale);
154 nonmiss++;
155 }
156 }
157 }
158
159 /* Calculate Spatial differences, if using DRS Template 5.3. */
160 if (idrsnum == 3)
161 { /* spatial differences */
162 if (idrstmpl[16] != 1 && idrstmpl[16] != 2)
163 idrstmpl[16] = 2;
164 if (idrstmpl[16] == 1)
165 { /* first order */
166 ival1 = jfld[0];
167 for (j = nonmiss - 1; j > 0; j--)
168 jfld[j] = jfld[j] - jfld[j - 1];
169 jfld[0] = 0;
170 }
171 else if (idrstmpl[16] == 2)
172 { /* second order */
173 ival1 = jfld[0];
174 ival2 = jfld[1];
175 for (j = nonmiss - 1; j > 1; j--)
176 jfld[j] = jfld[j] - (2 * jfld[j - 1]) + jfld[j - 2];
177 jfld[0] = 0;
178 jfld[1] = 0;
179 }
180
181 /* Subtract min value from spatial diff field. */
182 isd = idrstmpl[16];
183 minsd = jfld[isd];
184 for (j = isd; j < nonmiss; j++)
185 if (jfld[j] < minsd)
186 minsd = jfld[j];
187 for (j = isd; j < nonmiss; j++)
188 jfld[j] = jfld[j] - minsd;
189
190 /* Find num of bits need to store minsd and add 1 extra bit to
191 * indicate sign. */
192 temp = log((double)(abs(minsd) + 1)) / alog2;
193 nbitsd = (g2int)ceil(temp) + 1;
194
195 /* Find num of bits need to store ifld[0] (and ifld[1] if
196 * using 2nd order differencing). */
197 maxorig = ival1;
198 if (idrstmpl[16] == 2 && ival2 > ival1)
199 maxorig = ival2;
200 temp = log((double)(maxorig + 1)) / alog2;
201 nbitorig = (g2int)ceil(temp) + 1;
202 if (nbitorig > nbitsd)
203 nbitsd = nbitorig;
204
205 /* increase number of bits to even multiple of 8 (octet) */
206 if (nbitsd % 8)
207 nbitsd = nbitsd + (8 - (nbitsd % 8));
208
209 /* Store extra spatial differencing info into the packed data
210 * section. */
211 if (nbitsd != 0)
212 {
213 /* pack first original value */
214 if (ival1 >= 0) {
215 sbit(cpack, &ival1, iofst, nbitsd);
216 iofst = iofst + nbitsd;
217 }
218 else
219 {
220 sbit(cpack, &one, iofst, 1);
221 iofst = iofst + 1;
222 itemp = abs(ival1);
223 sbit(cpack, &itemp, iofst, nbitsd-1);
224 iofst = iofst + nbitsd - 1;
225 }
226 if (idrstmpl[16] == 2)
227 {
228 /* pack second original value */
229 if (ival2 >= 0)
230 {
231 sbit(cpack, &ival2, iofst, nbitsd);
232 iofst = iofst + nbitsd;
233 }
234 else
235 {
236 sbit(cpack, &one, iofst, 1);
237 iofst = iofst + 1;
238 itemp = abs(ival2);
239 sbit(cpack, &itemp, iofst, nbitsd-1);
240 iofst = iofst + nbitsd - 1;
241 }
242 }
243 /* pack overall min of spatial differences */
244 if (minsd >= 0)
245 {
246 sbit(cpack, &minsd, iofst, nbitsd);
247 iofst = iofst + nbitsd;
248 }
249 else
250 {
251 sbit(cpack, &one, iofst, 1);
252 iofst = iofst + 1;
253 itemp = abs(minsd);
254 sbit(cpack, &itemp, iofst, nbitsd-1);
255 iofst = iofst + nbitsd - 1;
256 }
257 }
258 } /* end of spatial diff section */
259
260 /* Expand non-missing data values to original grid. */
261 miss1 = jfld[0];
262 for (j = 0; j < nonmiss; j++)
263 if (jfld[j] < miss1)
264 miss1 = jfld[j];
265 miss1--;
266 miss2 = miss1-1;
267 n = 0;
268 for (j = 0; j < ndpts; j++)
269 {
270 if (ifldmiss[j] == 0)
271 {
272 ifld[j] = jfld[n];
273 n++;
274 }
275 else if (ifldmiss[j] == 1)
276 {
277 ifld[j] = miss1;
278 }
279 else if (ifldmiss[j] == 2)
280 {
281 ifld[j] = miss2;
282 }
283 }
284
285 /* Determine Groups to be used. Use Dr. Glahn's algorithm for
286 * determining grouping. */
287 kfildo = 6;
288 minpk = 10;
289 inc = 1;
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;
299 free(jmin);
300 free(jmax);
301 free(lbit);
302
303 /* For each group, find the group's reference value (min) and the
304 * number of bits needed to hold the remaining values. */
305 n = 0;
306 for (ng = 0; ng < ngroups; ng++)
307 {
308 /* how many of each type? */
309 num0 = num1 = num2 = 0;
310 for (j = n; j < n + glen[ng]; j++)
311 {
312 if (ifldmiss[j] == 0)
313 num0++;
314 if (ifldmiss[j] == 1)
315 num1++;
316 if (ifldmiss[j] == 2)
317 num2++;
318 }
319 if (num0 == 0)
320 { /* all missing values */
321 if (num1 == 0)
322 { /* all secondary missing */
323 gref[ng] = -2;
324 gwidth[ng] = 0;
325 }
326 else if (num2 == 0)
327 { /* all primary missing */
328 gref[ng] = -1;
329 gwidth[ng] = 0;
330 }
331 else
332 { /* both primary and secondary */
333 gref[ng] = 0;
334 gwidth[ng] = 1;
335 }
336 }
337 else
338 { /* contains some non-missing data */
339 /* find max and min values of group */
340 gref[ng] = 2147483647;
341 imax = -2147483647;
342 j = n;
343 for (lg = 0; lg < glen[ng]; lg++)
344 {
345 if (ifldmiss[j] == 0)
346 {
347 if (ifld[j] < gref[ng])
348 gref[ng] = ifld[j];
349 if (ifld[j] > imax)
350 imax = ifld[j];
351 }
352 j++;
353 }
354 if (missopt == 1)
355 imax = imax+1;
356 if (missopt == 2)
357 imax = imax+2;
358 /* calc num of bits needed to hold data */
359 if (gref[ng] != imax)
360 {
361 temp = log((double)(imax - gref[ng] + 1)) / alog2;
362 gwidth[ng] = (g2int)ceil(temp);
363 }
364 else
365 {
366 gwidth[ng] = 0;
367 }
368 }
369 /* Subtract min from data */
370 j = n;
371 mtemp = (g2int)int_power(2., gwidth[ng]);
372 for (lg = 0; lg < glen[ng]; lg++)
373 {
374 if (ifldmiss[j] == 0) /* non-missing */
375 ifld[j] = ifld[j] - gref[ng];
376 else if (ifldmiss[j] == 1) /* primary missing */
377 ifld[j] = mtemp - 1;
378 else if (ifldmiss[j] == 2) /* secondary missing */
379 ifld[j] = mtemp - 2;
380 j++;
381 }
382 /* increment fld array counter */
383 n = n + glen[ng];
384 }
385
386 /* Find max of the group references and calc num of bits needed to
387 * pack each groups reference value, then pack up group reference
388 * values. */
389 igmax = gref[0];
390 for (j = 1; j < ngroups; j++)
391 if (gref[j] > igmax)
392 igmax = gref[j];
393 if (missopt == 1)
394 igmax = igmax + 1;
395 if (missopt == 2)
396 igmax = igmax + 2;
397 if (igmax != 0)
398 {
399 temp = log((double)(igmax + 1)) / alog2;
400 nbitsgref = (g2int)ceil(temp);
401 /* reset the ref values of any "missing only" groups. */
402 mtemp = (g2int)int_power(2., nbitsgref);
403 for (j = 0; j < ngroups; j++)
404 {
405 if (gref[j] == -1)
406 gref[j] = mtemp-1;
407 if (gref[j] == -2)
408 gref[j] = mtemp-2;
409 }
410 sbits(cpack, gref, iofst, nbitsgref, 0, ngroups);
411 itemp = nbitsgref * ngroups;
412 iofst = iofst + itemp;
413 /* Pad last octet with Zeros, if necessary. */
414 if ((itemp % 8) != 0)
415 {
416 left = 8 - (itemp % 8);
417 sbit(cpack, &zero, iofst, left);
418 iofst = iofst + left;
419 }
420 }
421 else
422 {
423 nbitsgref = 0;
424 }
425
426 /* Find max/min of the group widths and calc num of bits needed to
427 * pack each groups width value, then pack up group width
428 * values. */
429 iwmax = gwidth[0];
430 ngwidthref = gwidth[0];
431 for (j = 1; j < ngroups; j++)
432 {
433 if (gwidth[j] > iwmax)
434 iwmax = gwidth[j];
435 if (gwidth[j] < ngwidthref)
436 ngwidthref = gwidth[j];
437 }
438 if (iwmax != ngwidthref)
439 {
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;
447 /* Pad last octet with Zeros, if necessary. */
448 if ((itemp %8) != 0)
449 {
450 left = 8 - (itemp % 8);
451 sbit(cpack, &zero, iofst, left);
452 iofst = iofst + left;
453 }
454 }
455 else
456 {
457 nbitsgwidth = 0;
458 for (i = 0; i < ngroups; i++)
459 gwidth[i] = 0;
460 }
461
462 /* Find max/min of the group lengths and calc num of bits needed
463 * to pack each groups length value, then pack up group length
464 * values. */
465 ilmax = glen[0];
466 nglenref = glen[0];
467 for (j = 1; j < ngroups - 1; j++)
468 {
469 if (glen[j] > ilmax)
470 ilmax = glen[j];
471 if (glen[j] < nglenref)
472 nglenref = glen[j];
473 }
474 nglenlast = glen[ngroups - 1];
475 if (ilmax != nglenref)
476 {
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;
484 /* Pad last octet with Zeros, if necessary. */
485 if ((itemp % 8) != 0)
486 {
487 left = 8 - (itemp % 8);
488 sbit(cpack, &zero, iofst, left);
489 iofst = iofst + left;
490 }
491 }
492 else
493 {
494 nbitsglen = 0;
495 for (i = 0; i < ngroups; i++)
496 glen[i] = 0;
497 }
498
499 /* For each group, pack data values. */
500 n = 0;
501 for (ng = 0; ng < ngroups; ng++)
502 {
503 glength = glen[ng] + nglenref;
504 if (ng == (ngroups - 1))
505 glength = nglenlast;
506 grpwidth = gwidth[ng] + ngwidthref;
507 if (grpwidth != 0)
508 {
509 sbits(cpack, ifld + n, iofst, grpwidth, 0, glength);
510 iofst = iofst + (grpwidth * glength);
511 }
512 n = n + glength;
513 }
514
515 /* Pad last octet with Zeros, if necessary, */
516 if ((iofst % 8) != 0)
517 {
518 left = 8 - (iofst % 8);
519 sbit(cpack, &zero, iofst, left);
520 iofst = iofst + left;
521 }
522 *lcpack = iofst / 8;
523
524 if (ifld)
525 free(ifld);
526 if (jfld)
527 free(jfld);
528 if (ifldmiss)
529 free(ifldmiss);
530 if (gref)
531 free(gref);
532 if (gwidth)
533 free(gwidth);
534 if (glen)
535 free(glen);
536
537 /* Fill in ref value and number of bits in Template 5.2. */
538 mkieee(&rmin, idrstmpl, 1); /* ensure reference value is IEEE format */
539 idrstmpl[3] = nbitsgref;
540 idrstmpl[4] = 0; /* original data were reals */
541 idrstmpl[5] = 1; /* general group splitting */
542 idrstmpl[9] = ngroups; /* Number of groups */
543 idrstmpl[10] = ngwidthref; /* reference for group widths */
544 idrstmpl[11] = nbitsgwidth; /* num bits used for group widths */
545 idrstmpl[12] = nglenref; /* Reference for group lengths */
546 idrstmpl[13] = 1; /* length increment for group lengths */
547 idrstmpl[14] = nglenlast; /* True length of last group */
548 idrstmpl[15] = nbitsglen; /* num bits used for group lengths */
549 if (idrsnum == 3)
550 idrstmpl[17] = nbitsd / 8; /* num bits used for extra spatial differencing values */
551}
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
Definition gbits.c:178
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
Definition gbits.c:38
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
void mkieee(float *a, g2int *rieee, g2int num)
Store a list of real values in 32-bit IEEE floating point format.
Definition mkieee.c:22
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition int_power.c:18
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 ...
Definition pack_gp.c:255
#define ALOG2
ln(2.0)
Definition grib2_int.h:30
void rdieee(g2int *rieee, float *a, g2int num)
Read a list of real values in 32-bit IEEE floating point format.
Definition rdieee.c:20
void misspack(float *fld, g2int ndpts, g2int idrsnum, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
Pack a data field using a complex packing algorithm.
Definition misspack.c:43