1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_tools.h"
12 
13 grib_option grib_options[] = {
14     /*  {id, args, help}, on, command_line, value*/
15     { "S", 0, 0, 1, 0, 0 },
16     { "O", 0, "Octet mode. WMO documentation style dump.\n", 0, 1, 0 },
17     { "D", 0, 0, 0, 1, 0 },
18     { "d", 0, "Print all data values.\n", 0, 1, 0 },
19     { "C", 0, 0, 0, 1, 0 },
20     { "t", 0, 0, 0, 1, 0 },
21     { "H", 0, 0, 0, 1, 0 },
22     { "a", 0, 0, 0, 1, 0 },
23     { "w:", 0, 0, 0, 1, 0 },
24     { "M", 0, 0, 0, 1, 0 },
25     { "T:", 0, 0, 1, 0, "F" },
26     { "7", 0, 0, 0, 1, 0 },
27     { "V", 0, 0, 0, 1, 0 },
28     { "q", 0, 0, 1, 0, 0 },
29     { "x", 0, 0, 0, 1, 0 }
30 };
31 
32 const char* tool_description = "Dump the content of a TAF file in different formats.";
33 const char* tool_name        = "taf_dump";
34 const char* tool_usage       = "[options] file file ...";
35 
36 int grib_options_count = sizeof(grib_options) / sizeof(grib_option);
37 
main(int argc,char * argv[])38 int main(int argc, char* argv[])
39 {
40     return grib_tool(argc, argv);
41 }
42 
grib_tool_before_getopt(grib_runtime_options * options)43 int grib_tool_before_getopt(grib_runtime_options* options)
44 {
45     return 0;
46 }
47 
grib_tool_init(grib_runtime_options * options)48 int grib_tool_init(grib_runtime_options* options)
49 {
50     int opt = grib_options_on("C") + grib_options_on("O") + grib_options_on("D");
51 
52     options->dump_mode = "default";
53 
54 
55     if (opt > 1) {
56         printf("%s: simultaneous C/O/D options not allowed\n", tool_name);
57         exit(1);
58     }
59 
60     if (grib_options_on("O")) {
61         options->dump_mode  = "wmo";
62         options->dump_flags = GRIB_DUMP_FLAG_CODED | GRIB_DUMP_FLAG_OCTET | GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
63     }
64 
65     if (grib_options_on("D")) {
66         options->dump_mode  = "debug";
67         options->dump_flags = GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
68     }
69 
70     if (grib_options_on("J")) {
71         options->dump_mode  = "json";
72         options->dump_flags = GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
73     }
74 
75     if (grib_options_on("X")) {
76         options->dump_mode  = "xml";
77         options->dump_flags = GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
78     }
79 
80     if (grib_options_on("a"))
81         options->dump_flags |= GRIB_DUMP_FLAG_ALIASES;
82 
83     if (grib_options_on("t"))
84         options->dump_flags |= GRIB_DUMP_FLAG_TYPE;
85 
86     if (grib_options_on("H"))
87         options->dump_flags |= GRIB_DUMP_FLAG_HEXADECIMAL;
88 
89     if (grib_options_on("d"))
90         options->dump_flags |= GRIB_DUMP_FLAG_ALL_DATA;
91 
92     return 0;
93 }
94 
grib_tool_new_filename_action(grib_runtime_options * options,const char * file)95 int grib_tool_new_filename_action(grib_runtime_options* options, const char* file)
96 {
97     return 0;
98 }
99 
grib_tool_new_file_action(grib_runtime_options * options,grib_tools_file * file)100 int grib_tool_new_file_action(grib_runtime_options* options, grib_tools_file* file)
101 {
102     char tmp[1024];
103     if (!options->current_infile->name)
104         return 0;
105     sprintf(tmp, "FILE: %s ", options->current_infile->name);
106     if (!grib_options_on("C") && !grib_options_on("J") && !grib_options_on("X"))
107         fprintf(stdout, "***** %s\n", tmp);
108     return 0;
109 }
110 
grib_tool_new_handle_action(grib_runtime_options * options,grib_handle * h)111 int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
112 {
113     long length = 0;
114     char tmp[1024];
115     char identifier[100];
116     size_t idlen = 100;
117     int i;
118     if (grib_get_long(h, "totalLength", &length) != GRIB_SUCCESS)
119         length = -9999;
120 
121     for (i = 0; i < options->print_keys_count; i++)
122         grib_set_flag(h, options->print_keys[i].name, GRIB_ACCESSOR_FLAG_DUMP);
123 
124     sprintf(tmp, "MESSAGE %d ( length=%ld )", options->handle_count, length);
125     if (!grib_options_on("C") && !grib_options_on("X") && !grib_options_on("J"))
126         fprintf(stdout, "#==============   %-38s   ==============\n", tmp);
127     if (!strcmp(options->dump_mode, "default")) {
128         GRIB_CHECK_NOLINE(grib_get_string(h, "identifier", identifier, &idlen), 0);
129         printf("%s {\n", identifier);
130     }
131 
132     grib_dump_content(h, stdout, options->dump_mode, options->dump_flags, 0);
133 
134     if (!strcmp(options->dump_mode, "default"))
135         printf("}\n");
136     return 0;
137 }
138 
grib_tool_skip_handle(grib_runtime_options * options,grib_handle * h)139 int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
140 {
141     grib_handle_delete(h);
142     return 0;
143 }
144 
grib_tool_print_key_values(grib_runtime_options * options,grib_handle * h)145 void grib_tool_print_key_values(grib_runtime_options* options, grib_handle* h)
146 {
147     grib_print_key_values(options, h);
148 }
149 
grib_tool_finalise_action(grib_runtime_options * options)150 int grib_tool_finalise_action(grib_runtime_options* options)
151 {
152     return 0;
153 }
154 
grib_no_handle_action(grib_runtime_options * options,int err)155 int grib_no_handle_action(grib_runtime_options* options, int err)
156 {
157     fprintf(dump_file, "\t\t\"ERROR: unreadable message\"\n");
158     return 0;
159 }
160