File: bufr_coordinate_descriptors.cc

package info (click to toggle)
eccodes 2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,404 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 283; xml: 183; awk: 66
file content (94 lines) | stat: -rw-r--r-- 3,478 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * (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>

// Get the X part of a BUFR descriptor (FXY) and determine if it
// is a coordinate.
// See https://confluence.ecmwf.int/display/UDOC/What+are+coordinate+descriptors+-+ecCodes+BUFR+FAQ
static int is_coord_using_Xcode(const char* X)
{
    if (strcmp(X, "01") == 0) return 1;
    if (strcmp(X, "02") == 0) return 1;
    if (strcmp(X, "04") == 0) return 1;
    if (strcmp(X, "05") == 0) return 1;
    if (strcmp(X, "06") == 0) return 1;
    if (strcmp(X, "07") == 0) return 1;
    if (strcmp(X, "08") == 0) return 1;
    return 0;
}

int main(int argc, char* argv[])
{
    int err         = 0;
    codes_handle* h = NULL;
    char* filename  = NULL;
    FILE* fin       = NULL;
    int is_coord    = 0;
    int is_header   = 0;

    assert (argc == 2);
    filename = argv[1];

    fin = fopen(filename, "rb");
    assert(fin);
    while ((h = codes_handle_new_from_file(NULL, fin, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
        codes_bufr_keys_iterator* kiter = NULL;
        CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
        kiter = codes_bufr_keys_iterator_new(h, 0);
        assert(kiter);
        while (codes_bufr_keys_iterator_next(kiter)) {
            char* name = codes_bufr_keys_iterator_get_name(kiter);
            if (strcmp(name, "subsetNumber") == 0) continue;
            if (!codes_bufr_key_is_header(h, name, &err) && !err) {
                char name1[256] = {0,};
                char scode[256] = {0,};
                char X[3] = {0,}; // the 'X' part of FXY
                size_t slen     = 256;
                snprintf(name1, 256, "%s->code", name);
                int error = codes_get_string(h, name1, scode, &slen);
                if (!error) {
                    assert(strlen(scode) == 6);
                    X[0] = scode[1];
                    X[1] = scode[2];
                    X[2] = 0;
                    int is_X_coord = is_coord_using_Xcode(X);
                    is_coord = codes_bufr_key_is_coordinate(h, name, &error);
                    assert(!error);
                    if (is_coord != is_X_coord) {
                        fprintf(stderr, "ERROR: %s X=%s   is_coord=%d is_X_coord=%d\n", name, X, is_coord, is_X_coord);
                        return 1;
                    }
                }
                //printf(" name1 = %s \n",name1);
                //assert( !codes_bufr_key_is_header(h, name1, &err) && !err );
            }
            else {
                // header keys cannot be coordinate descriptors
                is_coord = codes_bufr_key_is_coordinate(h, name, &err);
                assert(!is_coord);
            }
        }

        is_coord = codes_bufr_key_is_coordinate(h, "nosuchkey", &err);
        assert(err == CODES_NOT_FOUND);
        assert(is_coord == 0);
        is_header = codes_bufr_key_is_header(h, "nosuchkey", &err);
        assert(err == CODES_NOT_FOUND);
        assert(is_header == 0);

        codes_bufr_keys_iterator_delete(kiter);
        codes_handle_delete(h);
    }
    fclose(fin);
    return 0;
}