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 the xbfmg utility, 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" );
204 while ( ( ( pmsg - pc + 4 ) < filesize ) &&
205 ( ( *(pmsg) !=
'B' ) ||
206 ( *(pmsg + 1) !=
'U' ) ||
207 ( *(pmsg + 2) !=
'F' ) ||
208 ( *(pmsg + 3) !=
'R' ) ) ) {
209 if ( *pmsg ==
'\x01' ) psb = pmsg;
212 if ( ( pmsg - pc + 4 ) >= filesize ) {
214 free( outfile_temp );
222 sprintf( outfile,
"%s.%06lu", outfile_temp, ++noutfile );
223 if ( ( fp = fopen( outfile,
"wb" ) ) == NULL ) {
224 printf(
"\nERROR: Could not open output file %s!\n", outfile );
231 if ( save_GTSbull ) {
232 while ( psb < pmsg ) {
240 memcpy( wkint, pmsg, 8 );
246 if ( ( pmsg +
msglen - pc - 1 ) <= filesize ) {
247 for ( i = 1; i <=
msglen; i++ ) {
248 fputc( *pmsg++, fp );
255 if ( ( *(pmsg - 4) !=
'7' ) || ( *(pmsg - 3) !=
'7' ) ||
256 ( *(pmsg - 2) !=
'7' ) || ( *(pmsg - 1) !=
'7' ) ) {
257 printf(
"\nERROR: Could not find 7777 indicator in output file %s!\n",
264 if ( save_GTSbull ) {
Enable a number of NCEPLIBS-bufr subprograms to be called from within C and C++ application programs.
#define VERS_STR_LEN
Size of a character string needed to store a library version number.
int iupbs01_f(int *bufr, char *mnemonic)
Read a data value from Section 0 or Section 1 of a BUFR message.
void bvers_f(char *cverstr, int cverstr_len)
Get the version number of the NCEPLIBS-bufr software.
integer, dimension(:), allocatable msglen
Length (in integers) of BUFR message most recently written to each output file ID.
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.