NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
g2_unpack7.c
Go to the documentation of this file.
1
18#include "grib2_int.h"
19#include <memory.h>
20#include <string.h>
21
66static g2int
67g2c_unpack7_int(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
68 g2int idrsnum, g2int *idrstmpl, g2int ndpts, int v1, float **fld)
69{
70 g2int isecnum;
71 g2int ipos, lensec;
72 float *lfld;
73
74 assert(cgrib && iofst && idrstmpl && fld);
75
76 LOG((2, "g2c_unpack7_int *iofst %ld igdsnum %ld idrsnum %ld ndpts %ld v1 %d",
77 *iofst, igdsnum, idrsnum, ndpts, v1));
78
79 /* Get Length of Section */
80 gbit(cgrib, &lensec, *iofst, 32);
81 *iofst = *iofst + 32;
82 LOG((3, "lensec %ld", lensec));
83
84 /* Get Section Number */
85 gbit(cgrib, &isecnum, *iofst, 8);
86 *iofst = *iofst + 8;
87
88 if (isecnum != 7)
89 return G2_UNPACK_BAD_SEC;
90
91 ipos = *iofst / 8;
92
93 /* If we're using the V1 API, allocate memory for the unpacked
94 * data. If we are using the v2 API, the caller is responsible for
95 * allocating memory to hold the data. */
96 if (v1)
97 {
98 if (!(lfld = calloc(ndpts ? ndpts : 1, sizeof(float))))
99 return G2_UNPACK_NO_MEM;
100 *fld = lfld;
101 }
102 else
103 lfld = *fld;
104
105 if (idrsnum == 0)
106 {
107 simunpack(cgrib + ipos, idrstmpl, ndpts, *fld);
108 }
109 else if (idrsnum == 2 || idrsnum == 3)
110 {
111 if (comunpack(cgrib + ipos, lensec, idrsnum, idrstmpl, ndpts, *fld))
113 }
114 else if (idrsnum == 50)
115 {
116 /* Spectral Simple */
117 simunpack(cgrib + ipos, idrstmpl, ndpts - 1, lfld + 1);
118 rdieee(idrstmpl + 4, lfld, 1);
119 }
120 else if (idrsnum == 51)
121 {
122 /* Spectral complex */
123 if (igdsnum >= 50 && igdsnum <= 53)
124 specunpack(cgrib + ipos, idrstmpl, ndpts, igdstmpl[0], igdstmpl[2],
125 igdstmpl[2], lfld);
126 else
127 {
128 if (v1)
129 fprintf(stderr, "g2_unpack7: Cannot use GDT 3.%d to unpack Data Section 5.51.\n",
130 (int)igdsnum);
131 if (lfld)
132 free(lfld);
133 *fld = NULL;
135 }
136 }
137#if defined USE_JPEG2000 || defined USE_OPENJPEG
138 else if (idrsnum == 40 || idrsnum == 40000)
139 {
140 jpcunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
141 }
142#endif /* USE_JPEG2000 */
143#ifdef USE_PNG
144 else if (idrsnum == 41 || idrsnum == 40010)
145 {
146 pngunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
147 }
148#endif /* USE_PNG */
149#ifdef USE_AEC
150 else if (idrsnum == 42)
151 {
152 aecunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, *fld);
153 }
154#endif /* USE_AEC */
155 else
156 {
157 if (v1)
158 fprintf(stderr, "g2_unpack7: Data Representation Template 5.%d not yet "
159 "implemented.\n",
160 (int)idrsnum);
161 if (lfld)
162 free(lfld);
163 *fld = NULL;
164 return G2_UNPACK7_BAD_DRT;
165 }
166
167 *iofst = *iofst + (8 * lensec);
168
169 return G2_NO_ERROR;
170}
171
213g2int
214g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
215 g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
216{
217 return g2c_unpack7_int(cgrib, iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
218 ndpts, 1, fld);
219}
220
258int
259g2c_unpack7(unsigned char *cgrib, int igdsnum, int gds_tmpl_len, long long int *gdstmpl,
260 int idrsnum, int drs_tmpl_len, long long int *drstmpl, int ndpts, float *fld)
261{
262 g2int iofst = 0;
263 g2int *igdstmpl = NULL, *idrstmpl;
264 int i;
265 int ret;
266
267 LOG((1, "g2c_unpack7 igdsnum %d gds_tmpl_len %d idrsnum %d drs_tmpl_len %d ndpts %d",
268 igdsnum, gds_tmpl_len, idrsnum, drs_tmpl_len, ndpts));
269
270 /* Check inputs. */
271 assert(cgrib && drstmpl && fld);
272 if (gds_tmpl_len && !gdstmpl)
273 return G2C_EINVAL;
274
275 /* Allocate memory to hold the g2int versions of the DRS and GDS
276 * template arrays. */
277 if (gds_tmpl_len)
278 if (!(igdstmpl = malloc(gds_tmpl_len * sizeof(g2int))))
279 return G2C_ENOMEM;
280 if (!(idrstmpl = malloc(drs_tmpl_len * sizeof(g2int))))
281 return G2C_ENOMEM;
282
283 /* Copy the templates. */
284 if (gds_tmpl_len)
285 for (i = 0; i < gds_tmpl_len; i++)
286 igdstmpl[i] = gdstmpl[i];
287 for (i = 0; i < drs_tmpl_len; i++)
288 idrstmpl[i] = drstmpl[i];
289
290 /* Call the internal function that does the work. */
291 ret = g2c_unpack7_int(cgrib, &iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
292 ndpts, 0, &fld);
293
294 /* Free the g2int versions of the templates. */
295 if (igdstmpl)
296 free(igdstmpl);
297 free(idrstmpl);
298
299 return ret;
300}
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
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
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
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
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
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_UNPACK7_BAD_DRT
In g2_unpack7(), unrecognized Data Representation Template.
Definition grib2.h:457
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition grib2.h:449
#define G2_UNPACK7_CORRUPT_SEC
In g2_unpack7(), corrupt section 7.
Definition grib2.h:455
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition grib2.h:450
#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:456
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:423
#define G2C_EINVAL
Invalid input.
Definition grib2.h:481
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
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
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
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
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
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
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