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