NCEPLIBS-g2c 2.1.0
Loading...
Searching...
No Matches
g2cinq.c
Go to the documentation of this file.
1
7#include "grib2_int.h"
8#include <stdarg.h>
9
12
16
30int
31g2c_inq(int g2cid, int *num_msg)
32{
33 int ret = G2C_NOERROR;
34
35 /* Check input parameters. */
36 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
37 return G2C_EBADID;
38
39 /* If using threading, lock the mutex. */
40 MUTEX_LOCK(m);
41
42 /* Find the open file. */
43 if (g2c_file[g2cid].g2cid != g2cid)
44 ret = G2C_EBADID;
45
46 /* If the caller wants to know the number of messages, tell
47 * them. */
48 if (!ret)
49 if (num_msg)
50 *num_msg = g2c_file[g2cid].num_messages;
51
52 /* If using threading, unlock the mutex. */
53 MUTEX_UNLOCK(m);
54
55 return ret;
56}
57
86int
87g2c_inq_msg(int g2cid, int msg_num, unsigned char *discipline, int *num_fields,
88 int *num_local, short *center, short *subcenter, unsigned char *master_version,
89 unsigned char *local_version)
90{
92 int ret = G2C_NOERROR;
93
94 /* Check input parameters. */
95 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
96 return G2C_EBADID;
97 if (msg_num < 0)
98 return G2C_EINVAL;
99
100 /* If using threading, lock the mutex. */
101 MUTEX_LOCK(m);
102
103 /* Find the open file. */
104 if (g2c_file[g2cid].g2cid != g2cid)
105 ret = G2C_EBADID;
106
107 /* Find the file and message. */
108 if (!ret)
109 {
110 ret = G2C_ENOMSG;
111 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
112 {
113 if (msg->msg_num == msg_num)
114 {
115 if (discipline)
116 *discipline = msg->discipline;
117 if (num_fields)
118 *num_fields = msg->num_fields;
119 if (num_local)
120 *num_local = msg->num_local;
121 if (center)
122 *center = msg->center;
123 if (subcenter)
124 *subcenter = msg->subcenter;
125 if (master_version)
126 *master_version = msg->master_version;
127 if (local_version)
128 *local_version = msg->local_version;
129 ret = G2C_NOERROR;
130 break;
131 }
132 }
133 }
134
135 /* If using threading, unlock the mutex. */
136 MUTEX_UNLOCK(m);
137
138 return ret;
139}
140
169int
170g2c_inq_msg_time(int g2cid, int msg_num, unsigned char *sig_ref_time, short *year,
171 unsigned char *month, unsigned char *day, unsigned char *hour,
172 unsigned char *minute, unsigned char *second)
173{
175 int ret = G2C_NOERROR;
176
177 /* Check input parameters. */
178 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
179 return G2C_EBADID;
180 if (msg_num < 0)
181 return G2C_EINVAL;
182
183 /* If using threading, lock the mutex. */
184 MUTEX_LOCK(m);
185
186 /* Find the open file. */
187 if (g2c_file[g2cid].g2cid != g2cid)
188 ret = G2C_EBADID;
189
190 /* Find the file and message. */
191 if (!ret)
192 {
193 ret = G2C_ENOMSG;
194 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
195 {
196 if (msg->msg_num == msg_num)
197 {
198 if (sig_ref_time)
199 *sig_ref_time = msg->sig_ref_time;
200 if (year)
201 *year = msg->year;
202 if (month)
203 *month = msg->month;
204 if (day)
205 *day = msg->day;
206 if (hour)
207 *hour = msg->hour;
208 if (minute)
209 *minute = msg->minute;
210 if (second)
211 *second = msg->second;
212 ret = G2C_NOERROR;
213 break;
214 }
215 }
216 }
217
218 /* If using threading, unlock the mutex. */
219 MUTEX_UNLOCK(m);
220
221 return ret;
222}
223
246int
247g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len,
248 long long int *pds_template, int *gds_template_len, long long int *gds_template,
249 int *drs_template_len, long long int *drs_template)
250{
252 G2C_SECTION_INFO_T *sec4, *sec3, *sec5;
253 int t;
254 int ret = G2C_NOERROR;
255
256 /* Is this an open GRIB2 file? */
257 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
258 return G2C_EBADID;
259 if (msg_num < 0 || prod_num < 0)
260 return G2C_EINVAL;
261
262 /* If using threading, lock the mutex. */
263 MUTEX_LOCK(m);
264
265 if (g2c_file[g2cid].g2cid != g2cid)
266 ret = G2C_EBADID;
267
268 /* Find the message. */
269 if (!ret)
270 {
271 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
272 if (msg->msg_num == msg_num)
273 break;
274 if (!msg)
275 ret = G2C_ENOMSG;
276 }
277
278 /* Find the product. After this, sec4 will point to the
279 * appropropriate section 4 G2C_SECTION_INFO_T. */
280 if (!ret)
281 {
282 for (sec4 = msg->sec; sec4; sec4 = sec4->next)
283 if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
284 break;
285 if (!sec4)
286 ret = G2C_ENOPRODUCT;
287 /* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
288 }
289
290 /* Return the info to the caller. */
291 if (!ret)
292 {
293 if (pds_template_len)
294 *pds_template_len = sec4->template_len;
295 if (pds_template)
296 for (t = 0; t < sec4->template_len; t++)
297 pds_template[t] = sec4->template[t];
298 }
299
300 /* Find the GDS. */
301 if (!ret)
302 {
303 for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
304 if (sec3->sec_num == 3)
305 break;
306 if (!sec3)
307 ret = G2C_ENOSECTION;
308 }
309
310 /* Return the info to the caller. */
311 if (!ret)
312 {
313 if (gds_template_len)
314 *gds_template_len = sec3->template_len;
315 if (gds_template)
316 for (t = 0; t < sec3->template_len; t++)
317 gds_template[t] = sec3->template[t];
318 }
319
320 /* Find the DRS. */
321 if (!ret)
322 {
323 for (sec5 = sec4->next; sec5; sec5 = sec5->next)
324 if (sec5->sec_num == 5)
325 break;
326 if (!sec5)
327 ret = G2C_ENOSECTION;
328 }
329
330 /* Return the info to the caller. */
331 if (!ret)
332 {
333 if (drs_template_len)
334 *drs_template_len = sec5->template_len;
335 if (drs_template)
336 for (t = 0; t < sec5->template_len; t++)
337 drs_template[t] = sec5->template[t];
338 }
339
340 /* If using threading, unlock the mutex. */
341 MUTEX_UNLOCK(m);
342
343 return ret;
344}
345
371int
372g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
373 char *name, float *val)
374{
376 G2C_SECTION_INFO_T *sec4, *sec3;
377 G2C_DIM_INFO_T *dim;
378 int d;
379 int ret = G2C_NOERROR;
380
381 /* Are these valid IDs? */
382 if (g2cid < 0 || g2cid > G2C_MAX_FILES)
383 return G2C_EBADID;
384 if (msg_num < 0 || prod_num < 0 || dim_num < 0)
385 return G2C_EINVAL;
386
387 /* If using threading, lock the mutex. */
388 MUTEX_LOCK(m);
389
390 /* Find the file. */
391 if (g2c_file[g2cid].g2cid != g2cid)
392 ret = G2C_EBADID;
393
394 /* Find the message. */
395 if (!ret)
396 {
397 for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
398 if (msg->msg_num == msg_num)
399 break;
400 if (!msg)
401 ret = G2C_ENOMSG;
402 }
403
404 /* Find the product. After this, sec4 will point to the
405 * appropropriate section 4 G2C_SECTION_INFO_T. */
406 if (!ret)
407 {
408 for (sec4 = msg->sec; sec4; sec4 = sec4->next)
409 if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
410 break;
411 if (!sec4)
412 ret = G2C_ENOPRODUCT;
413 /* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
414 }
415
416 /* Find the GDS. */
417 if (!ret)
418 {
419 for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
420 if (sec3->sec_num == 3)
421 break;
422 if (!sec3)
423 ret = G2C_ENOSECTION;
424 dim = &((G2C_SECTION3_INFO_T *)sec3->sec_info)->dim[dim_num];
425 }
426
427 /* Give the caller the info they want. */
428 if (!ret)
429 {
430 if (len)
431 *len = dim->len;
432 if (name)
433 strncpy(name, dim->name, G2C_MAX_NAME);
434 if (val)
435 for (d = 0; d < dim->len; d++)
436 val[d] = dim->value[d];
437 }
438
439 /* If using threading, unlock the mutex. */
440 MUTEX_UNLOCK(m);
441
442 return ret;
443}
444
467int
468g2c_inq_dim_info(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
469 char *name)
470{
471 return g2c_inq_dim(g2cid, msg_num, prod_num, dim_num, len, name, NULL);
472}
int g2c_inq(int g2cid, int *num_msg)
Learn about a GRIB2 file.
Definition g2cinq.c:31
G2C_FILE_INFO_T g2c_file[G2C_MAX_FILES+1]
Global file information.
Definition g2cfile.c:10
int g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len, char *name, float *val)
Learn about the one of the dimensions of a GRIB2 product.
Definition g2cinq.c:372
int g2c_inq_msg_time(int g2cid, int msg_num, unsigned char *sig_ref_time, short *year, unsigned char *month, unsigned char *day, unsigned char *hour, unsigned char *minute, unsigned char *second)
Learn about the date/time information in a GRIB2 message.
Definition g2cinq.c:170
int g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len, long long int *pds_template, int *gds_template_len, long long int *gds_template, int *drs_template_len, long long int *drs_template)
Inquire about a product.
Definition g2cinq.c:247
int g2c_inq_msg(int g2cid, int msg_num, unsigned char *discipline, int *num_fields, int *num_local, short *center, short *subcenter, unsigned char *master_version, unsigned char *local_version)
Learn about a GRIB2 message.
Definition g2cinq.c:87
int g2c_inq_dim_info(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len, char *name)
Learn about the one of the dimensions of a GRIB2 product.
Definition g2cinq.c:468
#define G2C_MAX_FILES
Maximum number of open files.
Definition grib2.h:273
#define G2C_ENOMSG
No GRIB message found.
Definition grib2.h:495
#define G2C_ENOSECTION
Cannot find section.
Definition grib2.h:499
#define G2C_MAX_NAME
Maximum length of a name.
Definition grib2.h:274
#define G2C_ENOPRODUCT
Product not found.
Definition grib2.h:508
#define G2C_EINVAL
Invalid input.
Definition grib2.h:489
#define G2C_EBADID
Bad ID.
Definition grib2.h:491
#define G2C_NOERROR
No error.
Definition grib2.h:484
Header file with internal function prototypes NCEPLIBS-g2c library.
struct g2c_section_info * next
Pointer to next in list.
Definition grib2_int.h:178
struct g2c_section_info * sec
List of section metadata.
Definition grib2_int.h:164
size_t len
Length of dimension.
Definition grib2_int.h:188
short subcenter
Originating subcenter.
Definition grib2_int.h:152
unsigned char master_version
GRIB master tables version number.
Definition grib2_int.h:153
#define MUTEX_UNLOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:119
unsigned char hour
Hour.
Definition grib2_int.h:159
unsigned char minute
Minute.
Definition grib2_int.h:160
size_t num_messages
Number of messages in the file.
Definition grib2_int.h:242
int num_local
Number of local sections in the message.
Definition grib2_int.h:143
unsigned char sig_ref_time
Significance of reference time.
Definition grib2_int.h:155
unsigned char discipline
Discipline from section 0.
Definition grib2_int.h:140
char name[G2C_MAX_NAME+1]
Name of dimension.
Definition grib2_int.h:189
struct g2c_section_info * prev
Pointer to previous in list.
Definition grib2_int.h:179
int num_fields
Number of fields in the message.
Definition grib2_int.h:142
unsigned char sec_num
Section number.
Definition grib2_int.h:175
void * sec_info
Pointer to struct specific for section 3, 4, 5, 6, or 7.
Definition grib2_int.h:177
float * value
Array of dimension values.
Definition grib2_int.h:190
short year
Year.
Definition grib2_int.h:156
unsigned char second
Second.
Definition grib2_int.h:161
unsigned char day
Day.
Definition grib2_int.h:158
int template_len
Number of entries in template.
Definition grib2_int.h:181
unsigned char local_version
Version number of GRIB local tables used to augment Master Tables.
Definition grib2_int.h:154
unsigned char month
Month.
Definition grib2_int.h:157
size_t msg_num
Number of message in file (0-based).
Definition grib2_int.h:137
#define MUTEX_LOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:116
#define EXTERN_MUTEX(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:113
long long int * template
Grid, product, or data template.
Definition grib2_int.h:180
struct g2c_message_info * next
Pointer to next in list.
Definition grib2_int.h:166
short center
Originating center.
Definition grib2_int.h:151
Keep information about dimensions defined in section 3.
Definition grib2_int.h:186
This is the information about each open file.
Definition grib2_int.h:238
This is the information about each message.
Definition grib2_int.h:136
Information about Section 3 GRID DEFINITION SECTION.
Definition grib2_int.h:196
Information about Section 4 PRODUCT DEFINITION SECTION.
Definition grib2_int.h:209
Information about a section 3 through 7 in a GRIB2 message.
Definition grib2_int.h:171