NCEPLIBS-g2c  1.7.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,
27  float *fld)
28 {
29  g2int *ifld;
30  g2int j, nbits;
31  float 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 G2_JPCUNPACK_MEM;
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] = (((float)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 G2_NO_ERROR;
61 }
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition: grib2.h:275
rdieee
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
int_power
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition: int_power.c:18
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
simunpack
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
gbits
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
G2_JPCUNPACK_MEM
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition: grib2.h:320