NCEPLIBS-bufr 11.7.1
debufr.c
Go to the documentation of this file.
1
5#include <ctype.h>
6#include <string.h>
7#include <stdio.h>
8#include <libgen.h>
9#include <unistd.h>
10
11#include "bufrlib.h"
12#include "cobfl.h"
13
14#ifdef UNDERSCORE
15#define fdebufr fdebufr_
16#define prtusage prtusage_
17#endif
18
19void fdebufr( char *, char *, f77int *, char *, char *, char *, char *, char *,
20 size_t, size_t, size_t, size_t, size_t, size_t, size_t );
21void prtusage( char * );
22
23#define MXFLEN 500
24#define MXFLEN_TBLFIL (MXFLEN+50)
25
40void prtusage( char *prgnam ) {
41 printf( "\nUSAGE:\n" );
42 printf( " %s [-v] [-h] [-b] [-c] [-m] [-o outfile] [-t tabledir] [-f tablefil] [-p prmstg] bufrfile\n\n", prgnam );
43 printf( "\nWHERE:\n" );
44 printf( " -v prints program version information and exits\n\n" );
45 printf( " -h prints program help and usage information and exits\n\n" );
46 printf( " -b specifies the \"basic\" option, meaning that only the\n" );
47 printf( " information in Sections 0-3 will be decoded from each\n" );
48 printf( " BUFR message in the bufrfile, and no attempt will be\n" );
49 printf( " made to decode the data in Section 4\n\n" );
50 printf( " -c specifies that code and flag table meanings should not\n" );
51 printf( " be read from master BUFR tables and included in the output;\n" );
52 printf( " otherwise this feature is enabled by default\n\n" );
53 printf( " -m specifies that master BUFR tables will be used to\n" );
54 printf( " decode the data messages in the file, regardless of\n" );
55 printf( " whether it contains any embedded DX BUFR table messages.\n" );
56 printf( " This option can be used to view the actual contents of\n" );
57 printf( " DX BUFR table messages, which otherwise would not be\n" );
58 printf( " printed in the output listing.\n\n" );
59 printf( " outfile [path/]name of file to contain verbose output listing.\n" );
60 printf( " The default is \"bufrfilename.debufr.out\" in the current\n" );
61 printf( " working directory, where bufrfilename is the basename of\n" );
62 printf( " the bufrfile (i.e. bufrfile with any preceding [path/]\n" );
63 printf( " removed).\n\n" );
64 printf( " tabledir [path/]name of directory containing tables to be used\n" );
65 printf( " for decoding. This directory contains the DX BUFR tables\n" );
66 printf( " file to be used (if one was specified via the -f option),\n" );
67 printf( " or it may contain all of the master BUFR tables when these\n" );
68 printf( " are being used to decode a file. If unspecified, the\n" );
69 printf( " default directory location is\n" );
70 printf( " \"/home/jeffa/build/install/tables\"\n\n" );
71 printf( " tablefil file within tabledir containing DX BUFR tables to be used\n" );
72 printf( " for decoding.\n\n" );
73 printf( " prmstg string of comma-separated PARAMETER=VALUE pairs, up to a\n" );
74 printf( " maximum of 20. For each pair, the dynamic allocation\n" );
75 printf( " PARAMETER will be set to VALUE within the underlying\n" );
76 printf( " BUFRLIB software, overriding the default value that would\n" );
77 printf( " otherwise be used. A complete list of parameters that can\n" );
78 printf( " be dynamically sized is included within the documentation\n" );
79 printf( " for BUFRLIB function isetprm.\n\n" );
80 printf( " bufrfile [path/]name of BUFR file to be decoded\n\n" );
81}
82
174int main( int argc, char *argv[ ] ) {
175
176 int ch;
177
178 char basic = 'N';
179 char forcemt = 'N';
180 char cfms = 'Y';
181 char io = 'r';
182 char tbldir[MXFLEN] =
183 "/home/jeffa/build/install/tables";
184 char outfile[MXFLEN];
185 char wkstr[MXFLEN];
186 char wkstr2[MXFLEN];
187 char tblfil[MXFLEN_TBLFIL];
188 char prmstg[300] = "NULLPSTG";
189 char bvstr[9] = " ";
190
191 unsigned short ii;
192
193 f77int lentd;
194
195 /*
196 ** Get and process the valid options from the command line:
197 */
198 wkstr[0] = '\0'; /* initialize to empty string */
199 outfile[0] = '\0'; /* initialize to empty string */
200 while ( ( ch = getopt ( argc, argv, "vhbcmo:t:f:p:" ) ) != EOF ) {
201 switch ( ch ) {
202 case 'v':
203 bvers ( bvstr, sizeof(bvstr) );
204 /* append a trailing NULL to bvstr for printf */
205 for ( ii = 0; ii < sizeof(bvstr); ii++ ) {
206 if ( ( bvstr[ii] != '.' ) && ( !isdigit(bvstr[ii]) ) ) {
207 bvstr[ii] = '\0';
208 break;
209 }
210 }
211 printf( "This is debufr v3.1.1, built with BUFRLIB v%s\n",
212 bvstr );
213 return 0;
214 case 'h':
215 printf( "\nPROGRAM %s\n", argv[0] );
216 printf( "\nABSTRACT: This program decodes a BUFR file and generates a verbose\n" );
217 printf( " listing of the contents. If a DX BUFR tables file is specified\n" );
218 printf( " (using the -f option) or if the specified BUFR file contains an\n" );
219 printf( " embedded DX BUFR tables message as the first message in the file,\n" );
220 printf( " then this DX BUFR tables information is used to decode the data\n" );
221 printf( " messages in the file. Otherwise, or whenever the -m option is\n" );
222 printf( " specified, master BUFR tables are read and used to decode the\n" );
223 printf( " data messages in the file.\n" );
224 prtusage( argv[0] );
225 return 0;
226 break;
227 case 'b':
228 basic = 'Y';
229 break;
230 case 'm':
231 forcemt = 'Y';
232 break;
233 case 'c':
234 cfms = 'N';
235 break;
236 case 'o':
237 strcpy ( outfile, optarg );
238 break;
239 case 't':
240 strcpy ( tbldir, optarg );
241 break;
242 case 'f':
243 strcpy ( wkstr, optarg );
244 break;
245 case 'p':
246 strcpy ( prmstg, optarg );
247 break;
248 }
249 }
250
251 /*
252 ** There should be one remaining command line argument specifying the
253 ** input BUFR file.
254 */
255 if ( (optind+1) != argc ) {
256 printf( "\nERROR: You must specify an input BUFR file to be decoded!\n" );
257 prtusage( argv[0] );
258 return -1;
259 }
260
261 /*
262 ** Open the input BUFR file.
263 */
264 cobfl( argv[optind], &io );
265
266 /*
267 ** Check whether a DX tables file was specified.
268 */
269 if ( strlen( wkstr ) > 0 ) {
270 sprintf( tblfil, "%s%c%s", tbldir, '/', wkstr );
271 }
272 else {
273 strcpy( tblfil, "NULLFILE" );
274 }
275
276 /*
277 ** Check whether an output file was specified. If not, make a default
278 ** filename in the current working directory using the basename of the
279 ** input BUFR file.
280 */
281 if ( strlen( outfile ) == 0 ) {
282 strcpy( wkstr2, argv[optind] );
283 strcpy( outfile, basename( wkstr2 ) );
284 strcat( outfile, ".debufr.out" );
285 }
286
287 /*
288 ** Confirm that the output directory is writeable.
289 */
290 strcpy( wkstr2, outfile );
291 strcpy( wkstr, dirname( wkstr2 ) );
292 if ( access( wkstr, W_OK ) != 0 ) {
293 printf( "\nERROR: Cannot write output file to directory %s\n",
294 ( strcmp( wkstr, "." ) == 0 ? getcwd( wkstr2, MXFLEN ) : wkstr ) );
295 prtusage( argv[0] );
296 return -1;
297 }
298
299 /*
300 ** Read and decode each message from the input BUFR file.
301 */
302 lentd = (f77int) strlen(tbldir);
303 fdebufr( outfile, tbldir, &lentd, tblfil, prmstg, &basic, &forcemt, &cfms,
304 strlen(outfile), strlen(tbldir), strlen(tblfil), strlen(prmstg), 1, 1, 1 );
305
306 /*
307 ** Close the input BUFR file.
308 */
309 ccbfl( );
310
311 return 0;
312}
Define signatures to enable a number of BUFRLIB subprograms to be called directly from C application ...
subroutine bvers(CVERSTR)
This subroutine returns a character string containing the version number of the BUFRLIB software.
Definition: bvers.f:34
void ccbfl(void)
This subroutine closes all system files that were opened via previous calls to subroutine cobfl().
Definition: ccbfl.c:20
void cobfl(char *bfl, char *io)
This subroutine opens a new file for reading or writing BUFR messages via a C language interface.
Definition: cobfl.c:82
Define signatures and declare variables for reading or writing BUFR messages via a C language interfa...
int main(int argc, char *argv[])
This program decodes a BUFR file and generates a verbose listing of the contents.
Definition: debufr.c:174
void prtusage(char *)
This function prints program usage information to standard output.
Definition: debufr.c:40
subroutine fdebufr(ofile, tbldir, lentd, tblfil, prmstg, basic, forcemt, cfms)
This subroutine reads, decodes, and generates a verbose output listing of the contents of every BUFR ...
Definition: debufr.f:88