NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2_unpack7.c
Go to the documentation of this file.
1
18#include <memory.h>
19#include <string.h>
20#include "grib2_int.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", (int)idrsnum);
160 if (lfld)
161 free(lfld);
162 *fld = NULL;
163 return G2_UNPACK7_BAD_DRT;
164 }
165
166 *iofst = *iofst + (8 * lensec);
167
168 return G2_NO_ERROR;
169}
170
212g2int
213g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
214 g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
215{
216 return g2c_unpack7_int(cgrib, iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
217 ndpts, 1, fld);
218}
219
257int
258g2c_unpack7(unsigned char *cgrib, int igdsnum, int gds_tmpl_len, long long int *gdstmpl,
259 int idrsnum, int drs_tmpl_len, long long int *drstmpl, int ndpts, float *fld)
260{
261 g2int iofst = 0;
262 g2int *igdstmpl = NULL, *idrstmpl;
263 int i;
264 int ret;
265
266 LOG((1, "g2c_unpack7 igdsnum %d gds_tmpl_len %d idrsnum %d drs_tmpl_len %d ndpts %d",
267 igdsnum, gds_tmpl_len, idrsnum, drs_tmpl_len, ndpts));
268
269 /* Check inputs. */
270 assert(cgrib && drstmpl && fld);
271 if (gds_tmpl_len && !gdstmpl)
272 return G2C_EINVAL;
273
274 /* Allocate memory to hold the g2int versions of the DRS and GDS
275 * template arrays. */
276 if (gds_tmpl_len)
277 if (!(igdstmpl = malloc(gds_tmpl_len * sizeof(g2int))))
278 return G2C_ENOMEM;
279 if (!(idrstmpl = malloc(drs_tmpl_len * sizeof(g2int))))
280 return G2C_ENOMEM;
281
282 /* Copy the templates. */
283 if (gds_tmpl_len)
284 for (i = 0; i < gds_tmpl_len; i++)
285 igdstmpl[i] = gdstmpl[i];
286 for (i = 0; i < drs_tmpl_len; i++)
287 idrstmpl[i] = drstmpl[i];
288
289 /* Call the internal function that does the work. */
290 ret = g2c_unpack7_int(cgrib, &iofst, igdsnum, igdstmpl, idrsnum, idrstmpl,
291 ndpts, 0, &fld);
292
293 /* Free the g2int versions of the templates. */
294 if (igdstmpl)
295 free(igdstmpl);
296 free(idrstmpl);
297
298 return ret;
299}
300
301
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:213
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:258
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:472
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition grib2.h:464
#define G2_UNPACK7_CORRUPT_SEC
In g2_unpack7(), corrupt section 7.
Definition grib2.h:470
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:500
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition grib2.h:465
#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:471
#define G2_NO_ERROR
Function succeeded.
Definition grib2.h:438
#define G2C_EINVAL
Invalid input.
Definition grib2.h:496
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:116
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:426
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