NCEPLIBS-g2c  1.6.4
simunpack.c
Go to the documentation of this file.
1 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "grib2.h"
7 
25 g2int
26 simunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts,
27  g2float *fld)
28 {
29  g2int *ifld;
30  g2int j, nbits;
31  g2float ref, bscale, dscale;
32 
33  rdieee(idrstmpl, &ref, 1);
34  bscale = int_power(2.0, idrstmpl[1]);
35  dscale = int_power(10.0, -idrstmpl[2]);
36  nbits = idrstmpl[3];
37 
38  if (!(ifld = calloc(ndpts, sizeof(g2int))))
39  {
40  fprintf(stderr, "Could not allocate space in simunpack.\n "
41  "Data field NOT upacked.\n");
42  return(1);
43  }
44 
45  /* If nbits equals 0, we have a constant field where the reference
46  * value is the data value at each gridpoint. */
47  if (nbits != 0)
48  {
49  gbits(cpack, ifld, 0, nbits, 0, ndpts);
50  for (j = 0; j < ndpts; j++)
51  fld[j] = (((g2float)ifld[j] * bscale) + ref) * dscale;
52  }
53  else
54  {
55  for (j = 0; j < ndpts; j++)
56  fld[j] = ref;
57  }
58 
59  free(ifld);
60  return(0);
61 }
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte, g2int nskip, g2int n)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:57
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 rdieee(g2int *rieee, g2float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
Definition: rdieee.c:20
g2int simunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, g2float *fld)
This subroutine unpacks a data field that was packed using a simple packing algorithm as defined in t...
Definition: simunpack.c:26