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