NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
g2_info.c
Go to the documentation of this file.
1
10#include <stdio.h>
11#include <stdlib.h>
12#include "grib2_int.h"
13
70g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1,
71 g2int *numfields, g2int *numlocal)
72{
73 g2int mapsec1len = 13;
74 g2int mapsec1[13] = {2, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};
75 g2int i, j, istart, iofst, lengrib, lensec0, lensec1;
76 g2int ipos, isecnum, nbits, lensec;
77
78 *numlocal = 0;
79 *numfields = 0;
80
81 LOG((2, "g2_info"));
82
83 /* Check for beginning of GRIB message in the first 100 bytes. */
84 istart = -1;
85 for (j = 0; j < 100; j++)
86 {
87 if (cgrib[j] == 'G' && cgrib[j + 1] == 'R' && cgrib[j + 2] == 'I' &&
88 cgrib[j + 3] == 'B')
89 {
90 istart = j;
91 break;
92 }
93 }
94 if (istart == -1)
95 {
96 printf("g2_info: Beginning characters GRIB not found.");
97 return G2_INFO_NO_GRIB;
98 }
99
100 LOG((3, "msg found at byte %ld", istart));
101
102 /* Unpack Section 0 - Indicator Section. */
103 iofst = 8 * (istart + 6);
104 gbit(cgrib, listsec0, iofst, 8); /* Discipline */
105 iofst = iofst + 8;
106 gbit(cgrib, &listsec0[1], iofst, 8); /* GRIB edition number */
107 iofst = iofst + 8;
108 /* iofst = iofst + 32; */
109 /* gbit(cgrib, &lengrib, iofst, 32); /\* Length of GRIB message *\/ */
110 gbit(cgrib, &lengrib, iofst, 64); /* Length of GRIB message */
111 /* iofst = iofst + 32; */
112 iofst = iofst + 64;
113 listsec0[2] = lengrib;
114 lensec0 = 16;
115 ipos = istart + lensec0;
116 LOG((3, "unpacked section 0, lengrib %ld now at byte %ld", lengrib, ipos));
117
118 /* Currently handles only GRIB Edition 2. */
119 if (listsec0[1] != 2)
120 {
121 printf("g2_info: can only decode GRIB edition 2.");
123 }
124
125 /* Unpack Section 1 - Identification Section */
126 gbit(cgrib, &lensec1, iofst, 32); /* Length of Section 1 */
127 iofst = iofst + 32;
128 gbit(cgrib, &isecnum, iofst, 8); /* Section number (1) */
129 iofst = iofst + 8;
130 if (isecnum != 1)
131 {
132 printf("g2_info: Could not find section 1.");
133 return G2_INFO_NO_SEC1;
134 }
135
136 /* Unpack each input value in array listsec1 into the
137 appropriate number of octets, which are specified in
138 corresponding entries in array mapsec1. */
139 for (i = 0; i < mapsec1len; i++)
140 {
141 nbits = mapsec1[i] * 8;
142 gbit(cgrib, &listsec1[i], iofst, nbits);
143 iofst = iofst + nbits;
144 }
145 ipos = ipos + lensec1;
146 LOG((3, "unpacked section 1, now at byte %ld", ipos));
147
148 /* Loop through the remaining sections to see if they are
149 * valid. Also count the number of times Section 2 and Section
150 * 4 appear. */
151 for (;;)
152 {
153 if (cgrib[ipos] == '7' && cgrib[ipos + 1] == '7' && cgrib[ipos + 2] == '7' &&
154 cgrib[ipos + 3] == '7')
155 {
156 LOG((3, "found 7777 at byte %ld", ipos));
157 ipos = ipos + 4;
158 if (ipos != (istart + lengrib))
159 {
160 printf("g2_info: '7777' found, but not where expected.\n");
161 return G2_INFO_WRONG_END;
162 }
163 break;
164 }
165
166 iofst = ipos * 8;
167 gbit(cgrib, &lensec, iofst, 32); /* Get Length of Section */
168 iofst = iofst + 32;
169 gbit(cgrib, &isecnum, iofst, 8); /* Get Section number */
170 LOG((3, "found section number %ld of length %ld", isecnum, lensec));
171 iofst = iofst + 8;
172 ipos = ipos + lensec; /* Update beginning of section pointer */
173 if (ipos > (istart + lengrib))
174 {
175 printf("g2_info: '7777' not found at end of GRIB message.\n");
176 return G2_INFO_BAD_END;
177 }
178 if (isecnum >= 2 && isecnum <= 7)
179 {
180 /* Increment counter for total number of local sections
181 * or fields found. */
182 if (isecnum == 2)
183 (*numlocal)++;
184 else if (isecnum == 4)
185 (*numfields)++;
186 }
187 else
188 {
189 printf("g2_info: Invalid section number found in GRIB message: %ld\n", isecnum);
190 return G2_INFO_INVAL_SEC;
191 }
192 }
193
194 return 0;
195}
g2int g2_info(unsigned char *cgrib, g2int *listsec0, g2int *listsec1, g2int *numfields, g2int *numlocal)
Search through a GRIB2 message and return the number of gridded fields found in the message and the n...
Definition g2_info.c:70
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_INFO_NO_SEC1
g2_info() can't find section 1.
Definition grib2.h:442
#define G2_INFO_INVAL_SEC
g2_info() found invalid section number.
Definition grib2.h:445
#define G2_INFO_GRIB_VERSION
Wrong GRIB version for g2_info(), must be 2.
Definition grib2.h:441
#define G2_INFO_BAD_END
g2_info() didn't find "7777" at end of message.
Definition grib2.h:444
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2_INFO_NO_GRIB
g2_info() can't find beginning characters "GRIB".
Definition grib2.h:440
#define G2_INFO_WRONG_END
g2_info() found "7777" not where expected.
Definition grib2.h:443
Header file with internal function prototypes NCEPLIBS-g2c library.
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:426