NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
simpack.c
Go to the documentation of this file.
1
6#include <stdlib.h>
7#include <math.h>
8#include "grib2_int.h"
9
35void
36simpack(float *fld, g2int ndpts, g2int *idrstmpl,
37 unsigned char *cpack, g2int *lcpack)
38{
39 static g2int zero = 0;
40 g2int *ifld;
41 g2int j, nbits, imin, imax, maxdif, nbittot, left;
42 float bscale, dscale, rmax, rmin, temp;
43 double maxnum;
44 static float alog2 = ALOG2; /* ln(2.0) */
45
46 LOG((3, "simpack ndpts %ld", ndpts));
47
48 bscale = int_power(2.0, -idrstmpl[1]);
49 dscale = int_power(10.0, idrstmpl[2]);
50 if (idrstmpl[3] <= 0 || idrstmpl[3] > 31)
51 nbits = 0;
52 else
53 nbits = idrstmpl[3];
54
55 /* Find max and min values in the data. */
56 rmax = fld[0];
57 rmin = fld[0];
58 for (j = 1; j < ndpts; j++) {
59 if (fld[j] > rmax)
60 rmax = fld[j];
61 if (fld[j] < rmin)
62 rmin = fld[j];
63 }
64
65 ifld = calloc(ndpts, sizeof(g2int));
66
67 /* If max and min values are not equal, pack up field. If they are
68 * equal, we have a constant field, and the reference value (rmin)
69 * is the value for each point in the field and set nbits to 0. */
70 if (rmin != rmax) {
71
72 /* Determine which algorithm to use based on user-supplied
73 * binary scale factor and number of bits. */
74 if (nbits == 0 && idrstmpl[1] == 0) {
75
76 /* No binary scaling and calculate minumum number of bits
77 * in which the data will fit. */
78 imin = (g2int)rint(rmin * dscale);
79 imax = (g2int)rint(rmax * dscale);
80 maxdif = imax - imin;
81 temp = log((double)(maxdif + 1)) / alog2;
82 nbits = (g2int)ceil(temp);
83 rmin = (float)imin;
84 /* scale data */
85 for(j = 0; j < ndpts; j++)
86 ifld[j] = (g2int)rint(fld[j] * dscale) - imin;
87 }
88 else if (nbits != 0 && idrstmpl[1] == 0) {
89
90 /* Use minimum number of bits specified by user and adjust
91 * binary scaling factor to accomodate data. */
92 rmin = rmin * dscale;
93 rmax = rmax * dscale;
94 maxnum = int_power(2.0, nbits) - 1;
95 temp = log(maxnum / (rmax - rmin)) / alog2;
96 idrstmpl[1] = (g2int)ceil(-1.0 * temp);
97 bscale = int_power(2.0, -idrstmpl[1]);
98 /* scale data */
99 for (j = 0; j < ndpts; j++)
100 ifld[j] = (g2int)rint(((fld[j] * dscale) - rmin) * bscale);
101 }
102 else if (nbits == 0 && idrstmpl[1] != 0) {
103
104 /* Use binary scaling factor and calculate minumum number
105 * of bits in which the data will fit. */
106 rmin = rmin * dscale;
107 rmax = rmax * dscale;
108 maxdif = (g2int)rint((rmax - rmin) * bscale);
109 temp = log((double)(maxdif + 1)) / alog2;
110 nbits = (g2int)ceil(temp);
111 /* scale data */
112 for (j = 0; j < ndpts; j++)
113 ifld[j] = (g2int)rint(((fld[j] * dscale) - rmin) * bscale);
114 }
115 else if (nbits != 0 && idrstmpl[1] != 0) {
116
117 /* Use binary scaling factor and use minumum number of
118 * bits specified by user. Dangerous - may loose
119 * information if binary scale factor and nbits not set
120 * properly by user. */
121 rmin = rmin * dscale;
122 /* scale data */
123 for (j = 0; j < ndpts; j++)
124 ifld[j] = (g2int)rint(((fld[j] * dscale) - rmin) * bscale);
125 }
126
127 /* Pack data, Pad last octet with Zeros, if necessary, and
128 * calculate the length of the packed data in bytes. */
129 sbits(cpack, ifld, 0, nbits, 0, ndpts);
130 nbittot = nbits * ndpts;
131 left = 8 - (nbittot % 8);
132 if (left != 8) {
133 sbit(cpack, &zero, nbittot, left); /* Pad with zeros to fill Octet. */
134 nbittot = nbittot + left;
135 }
136 *lcpack = nbittot / 8;
137 }
138 else {
139 nbits = 0;
140 *lcpack = 0;
141 }
142
143 /* Fill in ref value and number of bits in Template 5.0. */
144 mkieee(&rmin, idrstmpl, 1); /* ensure reference value is IEEE format. */
145 idrstmpl[3] = nbits;
146 idrstmpl[4] = 0; /* original data were reals. */
147
148 free(ifld);
149}
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
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:426
#define ALOG2
ln(2.0)
Definition grib2_int.h:30
void simpack(float *fld, g2int ndpts, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
Packs a data field using the simple packing algorithm.
Definition simpack.c:36