NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
simunpack.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdlib.h>
8#include "grib2_int.h"
9
28simunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, float *fld)
29{
30 g2int *ifld;
31 g2int j, nbits;
32 float ref, bscale, dscale;
33
34 assert(cpack && idrstmpl && fld);
35
36 LOG((3, "simunpack ndpts %ld idrstmpl: %ld %ld %ld %ld %ld", ndpts, idrstmpl[0],
37 idrstmpl[1], idrstmpl[2], idrstmpl[3], idrstmpl[4]));
38
39 rdieee(idrstmpl, &ref, 1);
40 bscale = int_power(2.0, idrstmpl[1]);
41 dscale = int_power(10.0, -idrstmpl[2]);
42 nbits = idrstmpl[3];
43
44 if (!(ifld = calloc(ndpts, sizeof(g2int))))
45 {
46 fprintf(stderr, "Could not allocate space in simunpack.\n "
47 "Data field NOT upacked.\n");
48 return G2_JPCUNPACK_MEM;
49 }
50
51 /* If nbits equals 0, we have a constant field where the reference
52 * value is the data value at each gridpoint. */
53 if (nbits != 0)
54 {
55 gbits(cpack, ifld, 0, nbits, 0, ndpts);
56 for (j = 0; j < ndpts; j++)
57 fld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
58 }
59 else
60 {
61 for (j = 0; j < ndpts; j++)
62 fld[j] = ref;
63 }
64
65 free(ifld);
66 return G2_NO_ERROR;
67}
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Unpack arbitrary size values from a packed bit string, right justifying each value in the unpacked io...
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:32
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)
Read 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, float *fld)
Unpack a data field that was packed using a simple packing algorithm, using info from the GRIB2 Data ...
Definition simunpack.c:28