eccodes/tools/bufr_index_build.cc

160 lines
4.1 KiB
C++
Raw Normal View History

2014-07-06 13:18:40 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2014-07-06 13:18:40 +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.
*/
#include "grib_tools.h"
2020-07-17 14:37:57 +00:00
const char* tool_description =
2020-01-22 13:10:59 +00:00
"Build an index file for a set of input BUFR files.\n"
2020-08-19 15:20:27 +00:00
"\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 ... ";
2021-02-07 22:06:42 +00:00
grib_index* idx = NULL;
2023-01-06 20:25:57 +00:00
const char* keys;
const char* default_keys = "mars";
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
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",
2023-11-02 19:52:42 +00:00
0, 1, 0 },
{ "h", 0, 0, 0, 1, 0 },
2014-07-06 13:18:40 +00:00
};
2020-07-13 18:09:48 +00:00
static int compress_index;
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
int grib_options_count = sizeof(grib_options) / sizeof(grib_option);
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
int main(int argc, char* argv[])
2015-02-09 14:59:10 +00:00
{
2020-01-23 12:24:46 +00:00
return grib_tool(argc, argv);
2014-07-06 13:18:40 +00:00
}
2015-02-09 14:59:10 +00:00
int grib_tool_before_getopt(grib_runtime_options* options)
{
return 0;
2014-07-06 13:18:40 +00:00
}
2015-02-09 14:59:10 +00:00
int grib_tool_init(grib_runtime_options* options)
{
2020-01-22 13:10:59 +00:00
int ret = 0;
grib_context* c = grib_context_get_default();
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
if (grib_options_on("N"))
compress_index = 0;
else
compress_index = 1;
2014-07-06 13:18:40 +00:00
if (grib_options_on("k:"))
2020-01-22 13:10:59 +00:00
keys = grib_options_get_option("k:");
else
2020-01-22 13:10:59 +00:00
keys = default_keys;
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
options->onlyfiles = 1;
2014-07-06 13:18:40 +00:00
2020-01-22 13:10:59 +00:00
idx = grib_index_new(c, keys, &ret);
2020-07-13 18:09:48 +00:00
codes_index_set_product_kind(idx, PRODUCT_BUFR);
codes_index_set_unpack_bufr(idx, 1);
2014-07-06 13:18:40 +00:00
if (!idx || ret)
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_FATAL,
"Unable to create index %s", grib_get_error_message(ret));
2014-07-06 13:18:40 +00:00
return 0;
2014-07-06 13:18:40 +00:00
}
2020-01-22 13:10:59 +00:00
int grib_tool_new_filename_action(grib_runtime_options* options, const char* file)
2015-02-09 14:59:10 +00:00
{
2020-01-22 13:10:59 +00:00
int ret = 0;
2020-07-17 14:37:57 +00:00
printf("--- %s: processing %s\n", tool_name, file);
2020-07-13 18:09:48 +00:00
ret = grib_index_add_file(idx, file);
2020-01-22 13:10:59 +00:00
if (ret) {
2024-01-10 17:08:29 +00:00
fprintf(stderr, "Error: %s\n", grib_get_error_message(ret));
2020-01-22 13:10:59 +00:00
exit(ret);
}
return 0;
2014-07-06 13:18:40 +00:00
}
2020-01-22 13:10:59 +00:00
int grib_tool_new_file_action(grib_runtime_options* options, grib_tools_file* file)
2015-02-09 14:59:10 +00:00
{
return 0;
2014-07-06 13:18:40 +00:00
}
2015-02-09 14:59:10 +00:00
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
return 0;
2014-07-06 13:18:40 +00:00
}
2015-02-09 14:59:10 +00:00
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
return 0;
2014-07-06 13:18:40 +00:00
}
2015-02-09 14:59:10 +00:00
int grib_tool_finalise_action(grib_runtime_options* options)
{
2017-09-01 16:52:05 +00:00
grib_index_key* the_keys;
grib_string_list* values;
int first;
if (compress_index) {
grib_index_compress(idx);
}
printf("--- %s: keys included in the index file %s:\n",
2020-07-17 14:37:57 +00:00
tool_name, options->outfile->name);
printf("--- ");
2020-01-22 13:10:59 +00:00
first = 1;
the_keys = idx->keys;
2017-09-01 16:52:05 +00:00
while (the_keys) {
2020-01-22 13:10:59 +00:00
if (!first)
printf(", ");
printf("%s", the_keys->name);
the_keys = the_keys->next;
first = 0;
}
printf("\n");
2020-01-22 13:10:59 +00:00
the_keys = idx->keys;
2017-09-01 16:52:05 +00:00
while (the_keys) {
2020-01-22 13:10:59 +00:00
printf("--- %s = { ", the_keys->name);
values = the_keys->values;
first = 1;
while (values) {
2020-01-22 13:10:59 +00:00
if (!first)
printf(", ");
printf("%s", values->value);
first = 0;
values = values->next;
}
printf(" }\n");
2020-01-22 13:10:59 +00:00
the_keys = the_keys->next;
}
printf("--- %d message(s) indexed\n", idx->count);
if (idx->count)
2020-01-22 13:10:59 +00:00
grib_index_write(idx, options->outfile->name);
grib_index_delete(idx);
return 0;
2014-07-06 13:18:40 +00:00
}
int grib_no_handle_action(grib_runtime_options* options, int err)
{
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "\t\t\"ERROR: unreadable message\"\n");
return 0;
}