NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2_unpack5.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdlib.h>
8#include "grib2_int.h"
9
40g2_unpack5(unsigned char *cgrib, g2int *iofst, g2int *ndpts, g2int *idrsnum,
41 g2int **idrstmpl, g2int *mapdrslen)
42{
43 g2int needext, i, j, nbits, isecnum;
44 g2int lensec, isign, newlen;
45 g2int *lidrstmpl = 0;
46 gtemplate *mapdrs;
47
48 *idrstmpl = 0; /* NULL*/
49
50 gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
51 *iofst = *iofst + 32;
52 gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
53 *iofst = *iofst + 8;
54
55 if (isecnum != 5)
56 {
57 *ndpts = 0;
58 *mapdrslen = 0;
59 return G2_UNPACK_BAD_SEC;
60 }
61
62 gbit(cgrib, ndpts, *iofst, 32); /* Get num of data points */
63 *iofst = *iofst + 32;
64 gbit(cgrib, idrsnum, *iofst, 16); /* Get Data Rep Template Num. */
65 *iofst = *iofst + 16;
66
67 /* Gen Data Representation Template */
68 if (!(mapdrs = getdrstemplate(*idrsnum)))
69 {
70 *mapdrslen = 0;
71 return G2_UNPACK5_BAD_DRT;
72 }
73 *mapdrslen = mapdrs->maplen;
74 needext = mapdrs->needext;
75
76 /* Unpack each value into array ipdstmpl from the appropriate
77 * number of octets, which are specified in corresponding
78 * entries in array mapdrs. */
79 if (*mapdrslen > 0)
80 lidrstmpl = calloc(*mapdrslen, sizeof(g2int));
81 if (!lidrstmpl)
82 {
83 *mapdrslen = 0;
84 *idrstmpl = NULL;
85 if (mapdrs)
86 free(mapdrs);
87 return G2_UNPACK_NO_MEM;
88 }
89 else
90 {
91 *idrstmpl = lidrstmpl;
92 }
93 for (i = 0; i < mapdrs->maplen; i++)
94 {
95 nbits = abs(mapdrs->map[i]) * 8;
96 if (mapdrs->map[i] >= 0)
97 {
98 gbit(cgrib, lidrstmpl + i, *iofst, nbits);
99 }
100 else
101 {
102 gbit(cgrib, &isign, *iofst, 1);
103 gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
104 if (isign == 1)
105 lidrstmpl[i] = -1 * lidrstmpl[i];
106 }
107 *iofst = *iofst + nbits;
108 }
109
110 /* Check to see if the Data Representation Template needs to be
111 * extended. The number of values in a specific gtemplate may
112 * vary depending on data specified in the "static" part of the
113 * gtemplate. */
114 if (needext == 1)
115 {
116 free(mapdrs);
117 mapdrs = extdrstemplate(*idrsnum, lidrstmpl);
118 newlen = mapdrs->maplen + mapdrs->extlen;
119 lidrstmpl = realloc(lidrstmpl, newlen * sizeof(g2int));
120 *idrstmpl = lidrstmpl;
121
122 /* Unpack the rest of the Data Representation Template */
123 j = 0;
124 for (i = *mapdrslen; i < newlen; i++)
125 {
126 nbits = abs(mapdrs->ext[j]) * 8;
127 if (mapdrs->ext[j] >= 0)
128 {
129 gbit(cgrib, lidrstmpl + i, *iofst, nbits);
130 }
131 else
132 {
133 gbit(cgrib, &isign, *iofst, 1);
134 gbit(cgrib, lidrstmpl + i, *iofst + 1, nbits - 1);
135 if (isign == 1)
136 lidrstmpl[i] = -1 * lidrstmpl[i];
137 }
138 *iofst = *iofst + nbits;
139 j++;
140 }
141 *mapdrslen = newlen;
142 }
143 if (mapdrs->ext)
144 free(mapdrs->ext);
145 if (mapdrs)
146 free(mapdrs);
147
148 return G2_NO_ERROR;
149}
gtemplate * getdrstemplate(g2int number)
This subroutine returns DRS template information for a specified Data Representation Template.
gtemplate * extdrstemplate(g2int number, g2int *list)
This subroutine generates the remaining octet map for a given Data Representation Template,...
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
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
#define G2_UNPACK5_BAD_DRT
In g2_unpack5(), undefined Data Representation Template.
Definition grib2.h:468
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition grib2.h:464
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition grib2.h:465
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:438
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
g2int * ext
Number of octets of each entry in the extension part of the template.
Definition grib2_int.h:300
g2int extlen
Number of entries in the template extension.
Definition grib2_int.h:296
g2int * map
Number of octets of each entry in the static part of the template.
Definition grib2_int.h:290
g2int needext
Indicates whether or not the template needs to be extended.
Definition grib2_int.h:293
g2int maplen
Number of entries in the static part of the template.
Definition grib2_int.h:286
Struct for GRIB template, returned by getgridtemplate().
Definition grib2_int.h:276