Loading...
Searching...
No Matches
simunpack.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
g2int
simunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, float *fld)
{
g2int *ifld;
g2int j, nbits;
float ref, bscale, dscale;
assert(cpack && idrstmpl && fld);
LOG((3, "simunpack ndpts %ld idrstmpl: %ld %ld %ld %ld %ld", ndpts, idrstmpl[0],
idrstmpl[1], idrstmpl[2], idrstmpl[3], idrstmpl[4]));
rdieee(idrstmpl, &ref, 1);
bscale = int_power(2.0, idrstmpl[1]);
dscale = int_power(10.0, -idrstmpl[2]);
nbits = idrstmpl[3];
if (!(ifld = calloc(ndpts, sizeof(g2int))))
{
fprintf(stderr, "Could not allocate space in simunpack.\n "
"Data field NOT upacked.\n");
return G2_JPCUNPACK_MEM;
}
/* If nbits equals 0, we have a constant field where the reference
* value is the data value at each gridpoint. */
if (nbits != 0)
{
gbits(cpack, ifld, 0, nbits, 0, ndpts);
for (j = 0; j < ndpts; j++)
fld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
else
{
for (j = 0; j < ndpts; j++)
fld[j] = ref;
}
free(ifld);
return G2_NO_ERROR;
}
gbits
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
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:437
G2_JPCUNPACK_MEM
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition grib2.h:482
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
int_power
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition int_power.c:18
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
rdieee
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
simunpack
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