Loading...
Searching...
No Matches
g2_unpack7.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <memory.h>
#include <string.h>
static g2int
g2c_unpack7_int(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
g2int idrsnum, g2int *idrstmpl, g2int ndpts, int v1, float **fld)
{
g2int isecnum;
g2int ipos, lensec;
float *lfld;
assert(cgrib && iofst && idrstmpl && fld);
LOG((2, "g2c_unpack7_int *iofst %ld igdsnum %ld idrsnum %ld ndpts %ld v1 %d",
*iofst, igdsnum, idrsnum, ndpts, v1));
/* Get Length of Section */
gbit(cgrib, &lensec, *iofst, 32);
*iofst = *iofst + 32;
LOG((3, "lensec %ld", lensec));
/* Get Section Number */
gbit(cgrib, &isecnum, *iofst, 8);
*iofst = *iofst + 8;
if (isecnum != 7)
return G2_UNPACK_BAD_SEC;
ipos = *iofst / 8;
/* If we're using the V1 API, allocate memory for the unpacked
* data. If we are using the v2 API, the caller is responsible for
* allocating memory to hold the data. */
if (v1)
{
if (!(lfld = calloc(ndpts ? ndpts : 1, sizeof(float))))
return G2_UNPACK_NO_MEM;
*fld = lfld;
}
else
lfld = *fld;
if (idrsnum == 0)
{
simunpack(cgrib + ipos, idrstmpl, ndpts, *fld);
}
else if (idrsnum == 2 || idrsnum == 3)
{
if (comunpack(cgrib + ipos, lensec, idrsnum, idrstmpl, ndpts, *fld))
return G2_UNPACK7_CORRUPT_SEC;
}
else if (idrsnum == 50)
{
/* Spectral Simple */
simunpack(cgrib + ipos, idrstmpl, ndpts - 1, lfld + 1);
rdieee(idrstmpl + 4, lfld, 1);
}
else if (idrsnum == 51)
{
/* Spectral complex */
if (igdsnum >= 50 && igdsnum <= 53)
specunpack(cgrib + ipos, idrstmpl, ndpts, igdstmpl[0], igdstmpl[2],
igdstmpl[2], lfld);
else
{
if (v1)
fprintf(stderr, "g2_unpack7: Cannot use GDT 3.%d to unpack Data Section 5.51.\n",
(int)igdsnum);
if (lfld)
free(lfld);
*fld = NULL;
return G2_UNPACK7_WRONG_GDT;
}
}
#if defined USE_JPEG2000 || defined USE_OPENJPEG
else if (idrsnum == 40 || idrsnum == 40000)
{
jpcunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
}
#endif /* USE_JPEG2000 */
#ifdef USE_PNG
else if (idrsnum == 41 || idrsnum == 40010)
{
pngunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
}
#endif /* USE_PNG */
#ifdef USE_AEC
else if (idrsnum == 42)
{
aecunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
}
#endif /* USE_AEC */
else
{
if (v1)
fprintf(stderr, "g2_unpack7: Data Representation Template 5.%d not yet "
"implemented.\n",
(int)idrsnum);
if (lfld)
free(lfld);
*fld = NULL;
return G2_UNPACK7_BAD_DRT;
}
*iofst = *iofst + (8 * lensec);
return G2_NO_ERROR;
}
g2int
g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
{
return g2c_unpack7_int(cgrib, iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
ndpts, 1, fld);
}
int
g2c_unpack7(unsigned char *cgrib, int igdsnum, int gds_tmpl_len, long long int *gdstmpl,
int idrsnum, int drs_tmpl_len, long long int *drstmpl, int ndpts, float *fld)
{
g2int iofst = 0;
g2int *igdstmpl = NULL, *idrstmpl;
int i;
int ret;
LOG((1, "g2c_unpack7 igdsnum %d gds_tmpl_len %d idrsnum %d drs_tmpl_len %d ndpts %d",
igdsnum, gds_tmpl_len, idrsnum, drs_tmpl_len, ndpts));
/* Check inputs. */
assert(cgrib && drstmpl && fld);
if (gds_tmpl_len && !gdstmpl)
return G2C_EINVAL;
/* Allocate memory to hold the g2int versions of the DRS and GDS
* template arrays. */
if (gds_tmpl_len)
if (!(igdstmpl = malloc(gds_tmpl_len * sizeof(g2int))))
return G2C_ENOMEM;
if (!(idrstmpl = malloc(drs_tmpl_len * sizeof(g2int))))
return G2C_ENOMEM;
/* Copy the templates. */
if (gds_tmpl_len)
for (i = 0; i < gds_tmpl_len; i++)
igdstmpl[i] = gdstmpl[i];
for (i = 0; i < drs_tmpl_len; i++)
idrstmpl[i] = drstmpl[i];
/* Call the internal function that does the work. */
ret = g2c_unpack7_int(cgrib, &iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
ndpts, 0, &fld);
/* Free the g2int versions of the templates. */
if (igdstmpl)
free(igdstmpl);
free(idrstmpl);
return ret;
}
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
comunpack
int comunpack(unsigned char *cpack, g2int lensec, g2int idrsnum, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack a data field that was packed using a complex packing algorithm, using info from the GRIB2 Data...
Definition comunpack.c:41
g2c_unpack7_int
static g2int g2c_unpack7_int(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl, g2int idrsnum, g2int *idrstmpl, g2int ndpts, int v1, float **fld)
Unpacks Section 7 (Data Section) of a GRIB2 message.
Definition g2_unpack7.c:67
g2_unpack7
g2int g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl, g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
This subroutine unpacks Section 7 (Data Section) of a GRIB2 message.
Definition g2_unpack7.c:214
g2c_unpack7
int g2c_unpack7(unsigned char *cgrib, int igdsnum, int gds_tmpl_len, long long int *gdstmpl, int idrsnum, int drs_tmpl_len, long long int *drstmpl, int ndpts, float *fld)
This subroutine unpacks Section 7 (Data Section) of a GRIB2 message.
Definition g2_unpack7.c:259
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_UNPACK7_BAD_DRT
#define G2_UNPACK7_BAD_DRT
In g2_unpack7(), unrecognized Data Representation Template.
Definition grib2.h:471
G2_UNPACK_BAD_SEC
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition grib2.h:463
G2_UNPACK7_CORRUPT_SEC
#define G2_UNPACK7_CORRUPT_SEC
In g2_unpack7(), corrupt section 7.
Definition grib2.h:469
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:499
G2_UNPACK_NO_MEM
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition grib2.h:464
G2_UNPACK7_WRONG_GDT
#define G2_UNPACK7_WRONG_GDT
In g2_unpack7(), need one of GDT 3.50 through 3.53 to decode DRT 5.51.
Definition grib2.h:470
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:437
G2C_EINVAL
#define G2C_EINVAL
Invalid input.
Definition grib2.h:495
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
jpcunpack
g2int jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition jpcunpack.c:123
pngunpack
g2int pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition pngunpack.c:126
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
specunpack
g2int specunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, g2int JJ, g2int KK, g2int MM, float *fld)
Unpack a spectral data field that was packed using the complex packing algorithm for spherical harmon...
Definition specunpack.c:35
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
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