File: bufr_index_build.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 (159 lines) | stat: -rw-r--r-- 4,255 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * (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 "grib_tools.h"

const char* tool_description =
    "Build an index file for a set of input BUFR files.\n"
    "\tWarning: THIS TOOL IS STILL EXPERIMENTAL";
const char* tool_name       = "bufr_index_build";
const char* tool_online_doc = NULL;
const char* tool_usage      = "[options] file file ... ";
grib_index* idx        = NULL;
const char* keys;
const char* default_keys = "mars";

grib_option grib_options[] = {
    /*  {id, args, help}, on, command_line, value */
    { "f", 0, 0, 0, 1, 0 },
    { "o:", "output_index_file",
      "\n\t\tOutput is written to output_index_file."
      "\n\t\tIf an output index file is required and -o is not used, the"
      " output index is written to bufridx\n",
      1, 1, "bufridx" },
    { "k:", 0, 0, 0, 1, 0 },
    { "V", 0, 0, 0, 1, 0 },
    { "T:", 0, 0, 1, 0, "B" },
    { "N", 0,
      "Do not compress index."
      "\n\t\tBy default the index is compressed to remove keys with only one value.\n",
      0, 1, 0 },
    { "h", 0, 0, 0, 1, 0 },
};

static int compress_index;

int grib_options_count = sizeof(grib_options) / sizeof(grib_option);

int main(int argc, char* argv[])
{
    return grib_tool(argc, argv);
}

int grib_tool_before_getopt(grib_runtime_options* options)
{
    return 0;
}

int grib_tool_init(grib_runtime_options* options)
{
    int ret         = 0;
    grib_context* c = grib_context_get_default();

    if (grib_options_on("N"))
        compress_index = 0;
    else
        compress_index = 1;

    if (grib_options_on("k:"))
        keys = grib_options_get_option("k:");
    else
        keys = default_keys;

    options->onlyfiles = 1;

    idx = grib_index_new(c, keys, &ret);
    codes_index_set_product_kind(idx, PRODUCT_BUFR);
    codes_index_set_unpack_bufr(idx, 1);

    if (!idx || ret)
        grib_context_log(c, GRIB_LOG_FATAL,
                         "Unable to create index %s", grib_get_error_message(ret));

    return 0;
}

int grib_tool_new_filename_action(grib_runtime_options* options, const char* file)
{
    int ret = 0;
    printf("--- %s: processing %s\n", tool_name, file);
    ret = grib_index_add_file(idx, file);
    if (ret) {
        fprintf(stderr, "Error: %s\n", grib_get_error_message(ret));
        exit(ret);
    }
    return 0;
}

int grib_tool_new_file_action(grib_runtime_options* options, grib_tools_file* file)
{
    return 0;
}

int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
    return 0;
}

int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
    return 0;
}

int grib_tool_finalise_action(grib_runtime_options* options)
{
    const grib_index_key* the_keys = 0;
    const grib_string_list* values = 0;
    bool first = true;

    if (compress_index) {
        grib_index_compress(idx);
    }
    printf("--- %s: keys included in the index file %s:\n",
           tool_name, options->outfile->name);
    printf("--- ");

    the_keys = idx->keys;
    while (the_keys) {
        if (!first)
            printf(", ");
        printf("%s", the_keys->name);
        the_keys = the_keys->next;
        first    = false;
    }
    printf("\n");
    the_keys = idx->keys;
    while (the_keys) {
        printf("--- %s = { ", the_keys->name);
        values = the_keys->values;
        first  = true;
        while (values) {
            if (!first)
                printf(", ");
            printf("%s", values->value);
            first  = false;
            values = values->next;
        }
        printf(" }\n");
        the_keys = the_keys->next;
    }
    printf("--- %d message(s) indexed\n", idx->count);

    if (idx->count)
        grib_index_write(idx, options->outfile->name);
    grib_index_delete(idx);
    return 0;
}

int grib_no_handle_action(grib_runtime_options* options, int err)
{
    fprintf(dump_file, "\t\t\"ERROR: unreadable message\"\n");
    return 0;
}