eccodes/tests/bufr_keys_iter.cc

72 lines
2.1 KiB
C++
Raw Normal View History

2016-10-24 16:34:12 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- 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.
*/
2019-11-07 12:03:45 +00:00
2016-10-24 16:34:12 +00:00
#include "eccodes.h"
#undef NDEBUG
2016-10-24 16:34:12 +00:00
#include <assert.h>
#define ITER_ALL_KEYS 1
#define ITER_DATA_KEYS 2
int main(int argc, char* argv[])
2016-10-24 16:34:12 +00:00
{
int err = 0;
int i = 0;
codes_handle* h = NULL;
codes_bufr_keys_iterator* kiter = NULL;
char* input_filename = NULL;
FILE* f = NULL;
int iterator_mode = ITER_ALL_KEYS;
2018-12-10 16:48:36 +00:00
if (argc == 1 || argc > 3)
2024-03-04 20:45:39 +00:00
return 1; // usage: prog [-a|-d] infile
2018-12-10 16:48:36 +00:00
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-a") == 0) {
2018-12-10 16:48:36 +00:00
iterator_mode = ITER_ALL_KEYS;
}
else if (strcmp(argv[i], "-d") == 0) {
2018-12-10 16:48:36 +00:00
iterator_mode = ITER_DATA_KEYS;
}
else {
2018-12-10 16:48:36 +00:00
input_filename = argv[i];
break; /* Only 1 file allowed */
2017-08-21 17:34:27 +00:00
}
}
2017-08-21 17:34:27 +00:00
2018-12-10 16:48:36 +00:00
assert(input_filename);
f = fopen(input_filename, "rb");
2016-10-24 16:34:12 +00:00
assert(f);
h = codes_handle_new_from_file(NULL, f, PRODUCT_BUFR, &err);
assert(h);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
2018-12-10 16:48:36 +00:00
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)) {
2023-12-09 14:50:06 +00:00
const char* kname = codes_bufr_keys_iterator_get_name(kiter);
2016-10-24 16:34:12 +00:00
printf("%s\n", kname);
}
codes_bufr_keys_iterator_delete(kiter);
2016-11-29 16:57:51 +00:00
codes_handle_delete(h);
2020-12-31 18:10:07 +00:00
codes_context_delete(codes_context_get_default());
2016-10-24 16:34:12 +00:00
return 0;
}