bufr2synop 0.24.0
bufrdeco_json.c
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013-2022 by Guillermo Ballester Valor *
3 * gbv@ogimet.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20/*! \file bufrdeco_json.c
21 \brief This file includes the code to get data from a BUFR report using bufrdeco library
22*/
23#include "bufrdeco.h"
24
27char ENTRADA[256];
28int PRINT_JSON_SEC0; /*!< If != 0 Prints Sec 0 information in json format */
29int PRINT_JSON_SEC1; /*!< If != 0 Prints Sec 1 information in json format */
30int PRINT_JSON_SEC2; /*!< If != 0 Prints Sec 2 information in json format */
31int PRINT_JSON_SEC3; /*!< If != 0 Prints Sec 3 information in json format */
32int PRINT_JSON_SEC4; /*!< If != 0 Prints Sec 4 data in json format */
33int PRINT_JSON_EXPANDED_TREE; /*!< If != 0 Prints expanded tree in json format */
34int FIRST_SUBSET; /*!< First subset to parse */
35int LAST_SUBSET; /*!< Last subset to parse */
36
37void print_usage ( void )
38{
39 printf ( "Usage: \n" );
40 printf ( "bufrdeco_test -i input_file [-h][more optional args...]\n" );
41 printf ( " -h Print this help\n" );
42 printf ( " -i Input file. Complete input path file for bufr file\n" );
43 printf ( " -J. Information, tree and data in json format. Equivalent to option -E01234\n" );
44 printf ( " -S first..last . Print only results for subsets in range first..last (First subset available is 0). Default is all subsets\n" );
45 printf ( " -T. Print expanded tree in json format\n" );
46 printf ( " -X. Extract first BUFR buffer found in input file (from first 'BUFR' item to next '7777')\n" );
47 printf ( " -0. Prints BUFR Sec 0 information in json format\n" );
48 printf ( " -1. Prints BUFR Sec 1 information in json format\n" );
49 printf ( " -2. Prints BUFR Sec 2 information in json format\n" );
50 printf ( " -3. Prints BUFR Sec 3 information in json format\n" );
51 printf ( " -4. Prints BUFR data in json format\n" );
52
53}
54
55/*!
56 \fn int read_args( int _argc, char * _argv[])
57 \brief read the arguments from stdio
58 \param _argc number of arguments passed
59 \param _argv array of arguments
60
61 Returns 1 if succcess, -1 othewise
62*/
63int read_args ( int _argc, char * _argv[] )
64{
65 int iopt;
66 char *c, aux[128];
67
68 // Default values
69 EXTRACT = 0;
75
76 /*
77 Read input options
78 */
79 while ( ( iopt = getopt ( _argc, _argv, "hi:JXT01234" ) ) !=-1 )
80 switch ( iopt )
81 {
82 case 'X':
83 EXTRACT = 1;
84 break;
85
86 case '0':
88 break;
89
90 case '1':
92 break;
93
94 case '2':
96 break;
97
98 case '3':
100 break;
101
102 case '4':
103 PRINT_JSON_SEC4 = 1;
104 break;
105
106 case 'T':
108 break;
109
110 case 'J':
111 PRINT_JSON_SEC0 = 1;
112 PRINT_JSON_SEC1 = 1;
113 PRINT_JSON_SEC2 = 1;
114 PRINT_JSON_SEC3 = 1;
115 PRINT_JSON_SEC4 = 1;
117 break;
118
119 case 'S':
120 // Process subset sequence to show
121 if ( strlen ( optarg ) < 128 &&
122 strspn ( optarg, "0123456789." ) == strlen ( optarg ) )
123 {
124 if ( strstr ( optarg, ".." ) != NULL )
125 {
126 strcpy ( aux, optarg );
127 c = strstr ( aux, ".." );
128 if ( strlen ( c ) > 2 )
129 {
130 LAST_SUBSET = atoi ( c + 2 );
131 }
132 *c = '\0';
133 if ( aux[0] )
134 {
135 FIRST_SUBSET = atoi ( aux );
136 }
137 }
138 else
139 {
140 FIRST_SUBSET = atoi ( optarg );
142 }
143 }
144 break;
145 case 'i':
146 if ( strlen ( optarg ) < 256 )
147 strcpy ( ENTRADA, optarg );
148 break;
149
150 case 'h':
151 default:
152 print_usage();
153 exit ( EXIT_SUCCESS );
154 }
155
156 if ( ENTRADA[0] == '\0' )
157 {
158 printf ( "read_args(): It is needed an input file. Use -i option\n" );
159 return -1;
160 }
161 return 1;
162}
163
164// set the bit mask according with input arguments
166{
167 if ( PRINT_JSON_SEC0 )
169
170 if ( PRINT_JSON_SEC1 )
172
173 if ( PRINT_JSON_SEC2 )
175
176 if ( PRINT_JSON_SEC3 )
178
179 if ( PRINT_JSON_SEC4 )
181
184
185 return 0;
186}
187
188int main ( int argc, char *argv[] )
189{
190 size_t subset, first_subset, last_subset;
192
193 if ( read_args ( argc, argv ) < 0 )
194 exit ( EXIT_FAILURE );
195
196 bufrdeco_init ( &BUFR );
197
198 // sets the bit mask according to readed args
200
201 // Check read file
202 if ( EXTRACT )
203 {
205 {
206 printf ( "%s", BUFR.error );
207 exit ( EXIT_FAILURE );
208 }
209 }
210 else if ( bufrdeco_read_bufr ( &BUFR, ENTRADA ) )
211 {
212 printf ( "%s", BUFR.error );
213 exit ( EXIT_FAILURE );
214 }
215
216 if ( bufrdeco_parse_tree ( &BUFR ) )
217 {
218 printf ( "# %s", BUFR.error );
219 bufrdeco_close ( &BUFR );
220 exit ( EXIT_FAILURE );
221 }
222
225
226 first_subset = FIRST_SUBSET;
227 last_subset = LAST_SUBSET;
228
229 // Fix first and last subset
230 if ( first_subset >= BUFR.sec3.subsets )
231 {
232 fprintf ( BUFR.err, "Error, first subset to parse is greater than subset number in BUFR\n" );
233 exit ( EXIT_FAILURE );
234 }
235
236 if ( last_subset < first_subset )
237 last_subset = BUFR.sec3.subsets - 1;
238
239
240 for ( subset = first_subset; subset <= last_subset ; subset++ )
241 {
242 if ( ( seq = bufrdeco_get_target_subset_sequence_data ( subset, &BUFR ) ) == NULL )
243 {
244 printf ( "# %s", BUFR.error );
245 bufrdeco_close ( &BUFR );
246 exit ( EXIT_FAILURE );
247 }
248 }
249
250 //printf ( "So far so good !!\n" );
251 bufrdeco_close ( &BUFR );
252 exit ( EXIT_SUCCESS );
253}
254
int PRINT_JSON_EXPANDED_TREE
Definition: bufrdeco_json.c:33
int main(int argc, char *argv[])
int PRINT_JSON_SEC4
Definition: bufrdeco_json.c:32
int PRINT_JSON_SEC1
Definition: bufrdeco_json.c:29
char ENTRADA[256]
Definition: bufrdeco_json.c:27
int PRINT_JSON_SEC2
Definition: bufrdeco_json.c:30
int PRINT_JSON_SEC0
Definition: bufrdeco_json.c:28
int read_args(int _argc, char *_argv[])
read the arguments from stdio
Definition: bufrdeco_json.c:63
int set_bufrdeco_mask(struct bufrdeco *b)
int PRINT_JSON_SEC3
Definition: bufrdeco_json.c:31
struct bufrdeco BUFR
Definition: bufrdeco_json.c:25
void print_usage(void)
Definition: bufrdeco_json.c:37
int FIRST_SUBSET
Definition: bufrdeco_json.c:34
int EXTRACT
Definition: bufrdeco_json.c:26
int LAST_SUBSET
Definition: bufrdeco_json.c:35
int bufrdeco_close(struct bufrdeco *b)
Free all allocated memory. Needed when no more task to do with bufrdeco library.
Definition: bufrdeco.c:259
int bufrdeco_parse_tree(struct bufrdeco *b)
Parse the tree of descriptors without expand the replicators.
Definition: bufrdeco.c:120
struct bufrdeco_subset_sequence_data * bufrdeco_get_target_subset_sequence_data(buf_t nset, struct bufrdeco *b)
Prepare the struct bufrdeco to get data from the solicited subset.
Definition: bufrdeco.c:396
int bufrdeco_init(struct bufrdeco *b)
Inits and allocate memory for a struct bufrdeco.
Definition: bufrdeco.c:138
Include header file for bufrdeco library.
#define BUFRDECO_OUTPUT_JSON_EXPANDED_TREE
Bit mask to the member mask of struct bufrdeco to print bufr expanded tree of descriptors.
Definition: bufrdeco.h:305
buf_t bufrdeco_print_json_tree(struct bufrdeco *b)
#define BUFRDECO_OUTPUT_JSON_SUBSET_DATA
Bit mask to the member mask of struct bufrdeco to print bufr subset data in json format.
Definition: bufrdeco.h:299
#define BUFRDECO_OUTPUT_JSON_SEC2
Bit mask to the member mask of struct bufrdeco to print bufr info for SEC 2 in json format.
Definition: bufrdeco.h:287
#define BUFRDECO_OUTPUT_JSON_SEC3
Bit mask to the member mask of struct bufrdeco to print bufr info for SEC 3 in json format.
Definition: bufrdeco.h:293
#define BUFRDECO_OUTPUT_JSON_SEC0
Bit mask to the member mask of struct bufrdeco to print info for SEC 0 in json format.
Definition: bufrdeco.h:275
#define BUFRDECO_OUTPUT_JSON_SEC1
Bit mask to the member mask of struct bufrdeco to print info for SEC 1 in json format.
Definition: bufrdeco.h:281
int bufrdeco_extract_bufr(struct bufrdeco *b, char *filename)
Read file and try to find a bufr report inserted in. Once found do the same that bufrdeco_read_file()
int bufrdeco_read_bufr(struct bufrdeco *b, char *filename)
Read bufr file and does preliminary and first decode pass.
Definition: bufrdeco_read.c:41
uint32_t subsets
Definition: bufrdeco.h:810
Contains all the information for a subset in a expanded squence This is a version to use with bufrdec...
Definition: bufrdeco.h:458
This struct contains all needed data to parse and decode a BUFR file.
Definition: bufrdeco.h:965
uint32_t mask
Definition: bufrdeco.h:966
FILE * err
Definition: bufrdeco.h:985
struct bufr_sec3 sec3
Definition: bufrdeco.h:971
char error[1024]
Definition: bufrdeco.h:983