File: bufr_extract_headers.cc

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (62 lines) | stat: -rw-r--r-- 2,038 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * (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.
 */

#include "eccodes.h"
#undef NDEBUG
#include <assert.h>

#define MAX_KEYS 100
int main(int argc, char* argv[])
{
    char *filename, *keys;
    int i, err = 0;
    int num_messages                = 0;
    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];

    /* Usage: prog keys file */
    assert(argc == 3);

    keys     = argv[1]; /* comma-separated like bufr_ls/bufr_get */
    filename = argv[2];

    err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages, strict_mode);
    if (err) {
        printf("ERROR: %s\n", grib_get_error_message(err));
        return 1;
    }

    /* 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);
    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);
            if (j > 0) printf(" ");
            printf("%s", value);
        }
        printf("\n");
    }

    free(header_array);
    for (i = 0; i < requested_print_keys_count; ++i) {
        free((char*)requested_print_keys[i].name);
    }

    return 0;
}