eccodes/tests/bufr_keys_iter.c

78 lines
2.1 KiB
C
Raw Normal View History

2016-10-24 16:34:12 +00:00
/*
2018-01-02 11:31:02 +00:00
* Copyright 2005-2018 ECMWF.
2016-10-24 16:34:12 +00:00
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_api_internal.h"
#include "eccodes.h"
#include <assert.h>
extern char *optarg;
extern int optind;
2017-09-01 16:36:16 +00:00
static void usage(const char* prog)
2016-10-24 16:34:12 +00:00
{
printf("usage: %s [-a|-d] infile\n",prog);
2016-10-24 16:34:12 +00:00
exit(1);
}
#define ITER_ALL_KEYS 1
#define ITER_DATA_KEYS 2
2016-10-24 16:34:12 +00:00
int main(int argc,char* argv[])
{
2017-08-21 17:34:27 +00:00
int err = 0, opt = 0;
2016-10-24 16:34:12 +00:00
codes_handle* h = NULL;
codes_bufr_keys_iterator* kiter = NULL;
2016-10-24 16:34:12 +00:00
char* input_filename = NULL;
2017-08-21 17:34:27 +00:00
const char* prog = argv[0];
2016-10-24 16:34:12 +00:00
FILE* f = NULL;
int iterator_mode = ITER_ALL_KEYS;
2016-10-24 16:34:12 +00:00
2017-08-21 17:34:27 +00:00
while ((opt = getopt(argc, argv, "ad")) != -1) {
switch (opt) {
case 'a':
iterator_mode = ITER_ALL_KEYS;
break;
case 'd':
iterator_mode = ITER_DATA_KEYS;
break;
default:
usage(prog);
break;
}
}
2017-08-21 17:34:27 +00:00
/* After option processing expect just 1 file */
if (argc-optind != 1) usage(prog);
input_filename = argv[argc-1];
2016-10-24 16:34:12 +00:00
f = fopen(input_filename, "r");
assert(f);
h = codes_handle_new_from_file(NULL, f, PRODUCT_BUFR, &err);
assert(h);
CODES_CHECK(codes_set_long(h,"unpack",1), 0);
if (iterator_mode == ITER_ALL_KEYS) {
/*printf("Dumping ALL keys\n");*/
kiter = codes_bufr_keys_iterator_new(h, 0);
} else {
/*printf("Dumping only DATA SECTION keys\n");*/
2017-08-21 17:34:27 +00:00
assert(iterator_mode == ITER_DATA_KEYS);
kiter=codes_bufr_data_section_keys_iterator_new(h);
}
2016-10-24 16:34:12 +00:00
while(codes_bufr_keys_iterator_next(kiter))
{
char* kname = codes_bufr_keys_iterator_get_name(kiter);
printf("%s\n", kname);
}
codes_bufr_keys_iterator_delete(kiter);
2016-11-29 16:57:51 +00:00
codes_handle_delete(h);
2016-10-24 16:34:12 +00:00
return 0;
}