24 printf(
"\nUSAGE: %s [-v] [-h] [-g] bufrfile\n\n", prgnam );
26 printf(
" -v prints program version information and exits\n" );
27 printf(
" -h prints program help and usage information and exits\n" );
28 printf(
" -g preserves within each output file any GTS bulletin header and\n" );
29 printf(
" control characters associated with the corresponding BUFR message\n" );
30 printf(
" from the input file\n" );
31 printf(
" bufrfile [path/]name of input file containing one or more BUFR messages\n" );
32 printf(
" to be extracted into separate output files within the current\n" );
33 printf(
" working directory\n\n" );
34 printf(
"The output will be stored within the current working directory using the\n" );
35 printf(
"following filenames:\n\n" );
36 printf(
" (basename).xbfmg.out.000001\n" );
37 printf(
" (basename).xbfmg.out.000002\n" );
38 printf(
" (basename).xbfmg.out.000003\n" );
41 printf(
" (basename).xbfmg.out.(last#)\n\n" );
42 printf(
"where:\n\n" );
43 printf(
" (basename) = basename of bufrfile\n" );
44 printf(
" (last#) = total number of BUFR messages in bufrfile\n\n" );
95 int main(
int argc,
char *argv[] ) {
99 char *pc, *pmsg, *psb;
101 int save_GTSbull = 0;
103 char *outfile, *outfile_temp;
113 unsigned long i, filesize, noutfile;
118 while ( ( ch = getopt ( argc, argv,
"vgh" ) ) != EOF ) {
122 printf(
"This is xbfmg v4.0.0, built with NCEPLIBS-bufr v%s\n", bvstr );
128 printf(
"\nPROGRAM %s\n", argv[0] );
129 printf(
"\nABSTRACT: This program reads an input file containing one or more\n" );
130 printf(
" BUFR messages as given by the first argument. It then extracts each\n" );
131 printf(
" each individual BUFR message into its own separate output file within\n" );
132 printf(
" the current working directory.\n" );
142 if ( (optind+1) != argc ) {
143 printf(
"\nERROR: You must specify an input BUFR file of BUFR messages!\n" );
151 if ( stat( argv[optind], &fileinfo ) != 0 ) {
152 printf(
"\nERROR: Could not stat the file %s!\n", argv[optind] );
155 filesize = fileinfo.st_size;
160 if ( ( pc = malloc( filesize + 1 ) ) == NULL ) {
161 printf(
"\nERROR: Could not allocate memory for file %s!\n", argv[optind] );
168 if ( ( fp = fopen( argv[optind],
"rb" ) ) == NULL ) {
169 printf(
"\nERROR: Could not open input file %s!\n", argv[optind] );
173 for ( i = 0; i < filesize; i++ ) {
174 pc[i] = (char) fgetc( fp );
182 if ( ( outfile_temp = malloc( strlen( argv[optind] ) + 26 ) ) == NULL ) {
184 printf(
"\nERROR: Could not allocate memory for output file name template!\n" );
187 strcpy( outfile_temp, basename( argv[optind] ) );
188 strcat( outfile_temp,
".xbfmg.out" );
189 if ( ( outfile = malloc( strlen( outfile_temp ) + 26 ) ) == NULL ) {
191 printf(
"\nERROR: Could not allocate memory for output file names!\n" );
209 while ( ( ( pmsg - pc + 4 ) < filesize ) &&
210 ( ( *(pmsg) !=
'B' ) ||
211 ( *(pmsg + 1) !=
'U' ) ||
212 ( *(pmsg + 2) !=
'F' ) ||
213 ( *(pmsg + 3) !=
'R' ) ) ) {
214 if ( *pmsg ==
'\x01' ) psb = pmsg;
217 if ( ( pmsg - pc + 4 ) >= filesize ) {
219 free( outfile_temp );
227 sprintf( outfile,
"%s.%06lu", outfile_temp, ++noutfile );
228 if ( ( fp = fopen( outfile,
"wb" ) ) == NULL ) {
229 printf(
"\nERROR: Could not open output file %s!\n", outfile );
236 if ( save_GTSbull ) {
237 while ( psb < pmsg ) {
245 memcpy( &wkint, ( pmsg + 4 ), 3 );
251 if ( ( pmsg +
msglen - pc - 1 ) <= filesize ) {
252 for ( i = 1; i <=
msglen; i++ ) {
253 fputc( *pmsg++, fp );
260 if ( ( *(pmsg - 4) !=
'7' ) || ( *(pmsg - 3) !=
'7' ) ||
261 ( *(pmsg - 2) !=
'7' ) || ( *(pmsg - 1) !=
'7' ) ) {
262 printf(
"\nERROR: Could not find 7777 indicator in output file %s!\n",
269 if ( save_GTSbull ) {
void bvers_f(char *cverstr, int cverstr_len)
Get the version number of the NCEPLIBS-bufr software.
Enable a number of NCEPLIBS-bufr subprograms to be called from within the C part of the library.
#define VERS_STR_LEN
Size of a character string needed to store a library version number.
void wrdlen_f(void)
Determine important information about the local machine.
int iupb_f(int *mbay, int nbyt, int nbit)
Decode an integer value from an integer array.
integer, dimension(:), allocatable msglen
Length (in integers) of BUFR message most recently written to each output I/O stream.
int main(int argc, char *argv[])
This program splits a single file containing one or more BUFR messages into one or more BUFR files ea...
void prtusage(char *prgnam)
This function prints program usage information to standard output.