Loading...
Searching...
No Matches
g2_unpack5.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
g2int
g2_unpack5(unsigned char *cgrib, g2int *iofst, g2int *ndpts, g2int *idrsnum,
g2int **idrstmpl, g2int *mapdrslen)
{
g2int needext, i, j, nbits, isecnum;
g2int lensec, isign, newlen;
g2int *lidrstmpl = 0;
gtemplate *mapdrs;
*idrstmpl = 0; /* NULL*/
gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
*iofst = *iofst + 32;
gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
*iofst = *iofst + 8;
if (isecnum != 5)
{
*ndpts = 0;
*mapdrslen = 0;
return G2_UNPACK_BAD_SEC;
}
gbit(cgrib, ndpts, *iofst, 32); /* Get num of data points */
*iofst = *iofst + 32;
gbit(cgrib, idrsnum, *iofst, 16); /* Get Data Rep Template Num. */
*iofst = *iofst + 16;
/* Gen Data Representation Template */
if (!(mapdrs = getdrstemplate(*idrsnum)))
{
*mapdrslen = 0;
return G2_UNPACK5_BAD_DRT;
}
*mapdrslen = mapdrs->maplen;
needext = mapdrs->needext;
/* Unpack each value into array ipdstmpl from the appropriate
* number of octets, which are specified in corresponding
* entries in array mapdrs. */
if (*mapdrslen > 0)
lidrstmpl = calloc(*mapdrslen, sizeof(g2int));
if (!lidrstmpl)
{
*mapdrslen = 0;
*idrstmpl = NULL;
if (mapdrs)
free(mapdrs);
return G2_UNPACK_NO_MEM;
}
else
{
*idrstmpl = lidrstmpl;
}
for (i = 0; i < mapdrs->maplen; i++)
{
nbits = abs(mapdrs->map[i]) * 8;
if (mapdrs->map[i] >= 0)
{
gbit(cgrib, lidrstmpl + i, *iofst, nbits);
}
else
{
gbit(cgrib, &isign, *iofst, 1);
gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
if (isign == 1)
lidrstmpl[i] = -1 * lidrstmpl[i];
}
*iofst = *iofst + nbits;
}
/* Check to see if the Data Representation Template needs to be
* extended. The number of values in a specific gtemplate may
* vary depending on data specified in the "static" part of the
* gtemplate. */
if (needext == 1)
{
free(mapdrs);
mapdrs = extdrstemplate(*idrsnum, lidrstmpl);
newlen = mapdrs->maplen + mapdrs->extlen;
lidrstmpl = realloc(lidrstmpl, newlen * sizeof(g2int));
*idrstmpl = lidrstmpl;
/* Unpack the rest of the Data Representation Template */
j = 0;
for (i = *mapdrslen; i < newlen; i++)
{
nbits = abs(mapdrs->ext[j]) * 8;
if (mapdrs->ext[j] >= 0)
{
gbit(cgrib, lidrstmpl + i, *iofst, nbits);
}
else
{
gbit(cgrib, &isign, *iofst, 1);
gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
if (isign == 1)
lidrstmpl[i] = -1 * lidrstmpl[i];
}
*iofst = *iofst + nbits;
j++;
}
*mapdrslen = newlen;
}
if (mapdrs->ext)
free(mapdrs->ext);
if (mapdrs)
free(mapdrs);
return G2_NO_ERROR;
}
getdrstemplate
gtemplate * getdrstemplate(g2int number)
This subroutine returns DRS template information for a specified Data Representation Template.
Definition drstemplates.c:172
extdrstemplate
gtemplate * extdrstemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Data Representation Template,...
Definition drstemplates.c:218
g2_unpack5
g2int g2_unpack5(unsigned char *cgrib, g2int *iofst, g2int *ndpts, g2int *idrsnum, g2int **idrstmpl, g2int *mapdrslen)
Unpack Section 5 (Data Representation Section) of a GRIB2 message.
Definition g2_unpack5.c:40
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get arbitrary size values from a packed bit string, right justifying each value in the unpacked iout ...
Definition gbits.c:20
G2_UNPACK5_BAD_DRT
#define G2_UNPACK5_BAD_DRT
In g2_unpack5(), undefined Data Representation Template.
Definition grib2.h:467
G2_UNPACK_BAD_SEC
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition grib2.h:463
G2_UNPACK_NO_MEM
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition grib2.h:464
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:437
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
gtemplate::ext
g2int * ext
Number of octets of each entry in the extension part of the template.
Definition grib2_int.h:302
gtemplate::extlen
g2int extlen
Number of entries in the template extension.
Definition grib2_int.h:298
gtemplate::map
g2int * map
Number of octets of each entry in the static part of the template.
Definition grib2_int.h:292
gtemplate::needext
g2int needext
Indicates whether or not the template needs to be extended.
Definition grib2_int.h:295
gtemplate::maplen
g2int maplen
Number of entries in the static part of the template.
Definition grib2_int.h:288
gtemplate
Struct for GRIB template, returned by getgridtemplate().
Definition grib2_int.h:278