Loading...
Searching...
No Matches
aecunpack.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
static int
aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
void *fld, int fld_is_double, int verbose)
{
g2int *ifld;
g2int j, ctemplen = 0, nbits;
g2int ccsds_flags, ccsds_block_size, ccsds_rsi;
//g2int ifld1 = 0;
int ret = 0;
float ref, bscale, dscale;
float *ffld = fld;
double *dfld = fld;
unsigned char *ctemp;
size_t nbytes = 0;
LOG((2, "aecunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
/* Get compression parameters from data representation template array. */
rdieee(idrstmpl, &ref, 1);
bscale = int_power(2.0, idrstmpl[1]);
dscale = int_power(10.0, -idrstmpl[2]);
nbits = idrstmpl[3];
ccsds_flags = idrstmpl[5];
ccsds_block_size = idrstmpl[6];
ccsds_rsi = idrstmpl[7];
/* If nbits equals 0, we have a constant field where the reference
* value is the data value at each gridpoint. */
if (nbits != 0)
{
if (!(ifld = calloc(ndpts, sizeof(g2int))))
{
if (verbose)
fprintf(stderr, "Could not allocate space in aecunpack.\n Data field NOT upacked.\n");
return G2C_ENOMEM;
}
/* Determine the number of bytes needed for each value, then allocate the
* buffer for the decoded AEC stream. */
nbytes = (nbits + 7) / 8;
if (nbytes == 3)
nbytes = 4;
ctemplen = nbytes * ndpts;
if ((ctemp = (unsigned char *)malloc(ctemplen)) == NULL)
{
if (verbose)
fprintf(stderr, "Allocation error.\n");
return G2C_ENOMEM;
}
/* Decode the AEC stream. */
ret = dec_aec(cpack, len, nbits, ccsds_flags, ccsds_block_size, ccsds_rsi, ctemp, ctemplen);
if (ret < 0)
return ret;
/* IMPORTANT: The decoded AEC stream is byte-aligned (not bit), so when extracting the data
* values from the buffer via gbits, we need to pass the byte size in bits, not nbits. */
gbits(ctemp, ifld, 0, nbytes * 8, 0, ndpts);
/* Zero out all higher-order bits, preserving nbits. NOTE: This might be unecessary. */
//for (j = 0; j < ndpts; j++)
// ifld1 = ifld[j];
// ifld1 = ifld1 << (sizeof(ifld1)*8-nbits);
// ifld1 = ifld1 >> (sizeof(ifld1)*8-nbits);
// ifld[j] = ifld1;
/* Unscale data. */
if (fld_is_double)
{
for (j = 0; j < ndpts; j++)
dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
else
{
for (j = 0; j < ndpts; j++)
ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
/* Clean up. */
free(ctemp);
free(ifld);
}
else
{
if (fld_is_double)
{
for (j = 0; j < ndpts; j++)
dfld[j] = ref;
}
else
{
for (j = 0; j < ndpts; j++)
ffld[j] = ref;
}
}
return G2C_NOERROR;
}
g2int
aecunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
float *fld)
{
int ret;
LOG((2, "g2c_aecunpack len %lld ndpts %lld", len, ndpts));
ret = aecunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1);
return ret;
}
int
g2c_aecunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
float *fld)
{
g2int idrstmpl8[G2C_AEC_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
LOG((2, "g2c_aecunpackf len %d ndpts %lld", len, ndpts));
for (i = 0; i < G2C_AEC_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return aecunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
}
int
g2c_aecunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
double *fld)
{
g2int idrstmpl8[G2C_AEC_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
LOG((2, "g2c_aecunpackd len %lld ndpts %lld", len, ndpts));
for (i = 0; i < G2C_AEC_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return aecunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
}
g2c_aecunpackd
int g2c_aecunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, double *fld)
Unpack AEC compressed data into an array of doubles, using info from the GRIB2 Data Representation Te...
Definition aecunpack.c:232
aecunpack
g2int aecunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack AEC compressed data into an array of floats, using info from the GRIB2 Data Representation Tem...
Definition aecunpack.c:161
aecunpack_int
static int aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
Unpack AEC compressed data into an array of floats or doubles, using info from the GRIB2 Data Represe...
Definition aecunpack.c:43
g2c_aecunpackf
int g2c_aecunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, float *fld)
Unpack AEC compressed data into an array of floats, using info from the GRIB2 Data Representation Tem...
Definition aecunpack.c:194
dec_aec
int dec_aec(unsigned char *cpack, g2int len, g2int nbits, g2int flags, g2int block_size, g2int rsi, unsigned char *cfld, g2int cfldlen)
Decode an AEC code stream specified in the CCSDS 121.0-B-3 Blue Book.
Definition decenc_aec.c:38
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
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:493
G2C_AEC_DRS_TEMPLATE_LEN
#define G2C_AEC_DRS_TEMPLATE_LEN
Length of the idrstmpl array for AEC packing.
Definition grib2.h:413
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
G2C_NOERROR
#define G2C_NOERROR
No error.
Definition grib2.h:484
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