eccodes/tests/bufr_extract_headers.cc

63 lines
2.0 KiB
C++
Raw Normal View History

/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
*
* 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-09-24 14:43:27 +00:00
#include "eccodes.h"
#undef NDEBUG
#include <assert.h>
2019-09-24 14:43:27 +00:00
2019-11-29 11:13:18 +00:00
#define MAX_KEYS 100
2019-09-24 14:43:27 +00:00
int main(int argc, char* argv[])
{
2019-09-24 14:43:27 +00:00
char *filename, *keys;
int i, err = 0;
int num_messages = 0;
2019-11-07 12:03:45 +00:00
codes_bufr_header* header_array = NULL;
codes_context* c = codes_context_get_default();
const int strict_mode = 1;
int requested_print_keys_count = MAX_KEYS;
codes_values requested_print_keys[MAX_KEYS];
2019-11-20 12:25:38 +00:00
/* Usage: prog keys file */
assert(argc == 3);
2019-09-24 14:43:27 +00:00
keys = argv[1]; /* comma-separated like bufr_ls/bufr_get */
2019-09-24 14:43:27 +00:00
filename = argv[2];
2019-11-18 15:40:14 +00:00
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages, strict_mode);
2019-11-08 12:51:15 +00:00
if (err) {
printf("ERROR: %s\n", grib_get_error_message(err));
2019-11-08 12:51:15 +00:00
return 1;
}
2019-12-11 15:35:13 +00:00
/* Mimic the behaviour of bufr_get -f -p keys for testing */
err = parse_keyval_string(NULL, keys, 0, GRIB_TYPE_UNDEFINED, requested_print_keys, &requested_print_keys_count);
2020-03-26 14:26:17 +00:00
if (err) return 1;
assert(requested_print_keys_count > 0);
for (i = 0; i < num_messages; ++i) {
int j;
for (j = 0; j < requested_print_keys_count; ++j) {
size_t vlen = 0;
char value[512] = {0,};
CODES_CHECK(codes_bufr_header_get_string(&header_array[i], requested_print_keys[j].name, value, &vlen), 0);
assert(vlen > 0);
2019-11-20 15:39:55 +00:00
if (j > 0) printf(" ");
printf("%s", value);
}
2019-09-24 14:43:27 +00:00
printf("\n");
}
2019-11-07 12:03:45 +00:00
free(header_array);
for (i = 0; i < requested_print_keys_count; ++i) {
2019-11-20 12:25:38 +00:00
free((char*)requested_print_keys[i].name);
}
return 0;
}