ECC-407: Add option to bufr_count/grib_count to count valid messages

This commit is contained in:
Shahram Najm 2018-01-02 11:01:36 +00:00
parent 0cfda760ca
commit ff3a2eded4
5 changed files with 67 additions and 23 deletions

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
# Copyright 2005-2017 ECMWF. # Copyright 2005-2018 ECMWF.
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 # This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
@ -10,13 +10,32 @@
. ./include.sh . ./include.sh
#set -x # Enter data dir
#Enter data dir
cd ${data_dir}/bufr cd ${data_dir}/bufr
input=syno_multi.bufr
# counting messages # Counting valid messages
count=`${tools_dir}/bufr_count syno_multi.bufr` # ------------------------
#[ "$count" = "3" ] count=`${tools_dir}/bufr_count $input`
count=`${tools_dir}/codes_count syno_multi.bufr`
[ "$count" = "3" ] [ "$count" = "3" ]
count=`${tools_dir}/codes_count $input`
[ "$count" = "3" ]
# Files with invalid (unreadable) messages
# -----------------------------------------
temp=$input.truncated
# Remove last 4 bytes of multi message BUFR file
head --bytes=-4 $input > $temp
set +e
# Without -f, bufr_count should fail
${tools_dir}/bufr_count $temp
status=$?
set -e
[ $status -ne 0 ]
# With -f should count the valid messages
count=`${tools_dir}/bufr_count -f $temp`
[ "$count" = "2" ]
rm -f $temp

View File

@ -1,12 +1,11 @@
NAME bufr_count NAME bufr_count
DESCRIPTION DESCRIPTION
Print the total number of BUFR messages in the given files. Print the total number of BUFR messages in the given files.
USAGE USAGE
bufr_count [options] file file ... bufr_count [options] bufr_file bufr_file ...
OPTIONS OPTIONS
-v Verbose mode. The number of messages is given for each file. -v Verbose mode. The number of messages is given for each file.
-f Force. Force the execution not to fail on error.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2005-2017 ECMWF. * Copyright 2005-2018 ECMWF.
* *
* This software is licensed under the terms of the Apache Licence Version 2.0 * This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
@ -9,10 +9,11 @@
*/ */
#include "grib_api_internal.h" #include "grib_api_internal.h"
static int fail_on_error = 1;
static void usage(const char* prog) static void usage(const char* prog)
{ {
printf("usage: %s [-v] infile1 infile2 ... \n",prog); printf("Usage: %s [-v] [-f] infile1 infile2 ... \n",prog);
exit(1); exit(1);
} }
@ -31,10 +32,30 @@ static int count_messages(FILE* in, int message_type, unsigned long *count)
if (message_type == CODES_GRIB) wmo_read=wmo_read_grib_from_file_malloc; if (message_type == CODES_GRIB) wmo_read=wmo_read_grib_from_file_malloc;
else if (message_type == CODES_BUFR) wmo_read=wmo_read_bufr_from_file_malloc; else if (message_type == CODES_BUFR) wmo_read=wmo_read_bufr_from_file_malloc;
else wmo_read=wmo_read_any_from_file_malloc; else wmo_read=wmo_read_any_from_file_malloc;
while ( (mesg=wmo_read(in,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) { if (fail_on_error)
grib_context_free(c,mesg); {
(*count)++; while ( (mesg=wmo_read(in,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) {
grib_context_free(c,mesg);
(*count)++;
}
}
else
{
int done = 0;
while(!done) {
mesg = wmo_read(in, 0, &size, &offset, &err);
/*printf("Count so far=%ld, mesg=%x, err=%d (%s)\n", *count, mesg, err, grib_get_error_message(err));*/
if (!mesg) {
if (err == GRIB_END_OF_FILE || err == GRIB_PREMATURE_END_OF_FILE) {
done = 1; /* reached the end */
}
}
if (mesg && !err) {
grib_context_free(c,mesg);
(*count)++;
}
}
} }
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS; if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
@ -47,7 +68,7 @@ int main(int argc,char* argv[])
FILE* infh = NULL; FILE* infh = NULL;
char* filename; char* filename;
int i, verbose=0; int i, verbose=0;
int err=0; int err=0, files_processed=0;
unsigned long count_total=0, count_curr=0; unsigned long count_total=0, count_curr=0;
int message_type = 0; /* GRIB, BUFR etc */ int message_type = 0; /* GRIB, BUFR etc */
@ -60,7 +81,10 @@ int main(int argc,char* argv[])
for (i=1;i<argc;i++) { for (i=1;i<argc;i++) {
if (strcmp(argv[i], "-v")==0) { if (strcmp(argv[i], "-v")==0) {
verbose = 1; verbose = 1;
if (argc <3) usage(argv[0]); continue;
}
if (strcmp(argv[i], "-f")==0) {
fail_on_error = 0;
continue; continue;
} }
filename=argv[i]; filename=argv[i];
@ -69,10 +93,10 @@ int main(int argc,char* argv[])
perror(filename); perror(filename);
exit(1); exit(1);
} }
files_processed=1; /* At least one file processed */
count_curr=0; count_curr=0;
err=count_messages(infh, message_type, &count_curr); err=count_messages(infh, message_type, &count_curr);
if (err) { if (err && fail_on_error) {
fprintf(stderr,"Invalid message(s) found in %s", filename); fprintf(stderr,"Invalid message(s) found in %s", filename);
if (count_curr>0) fprintf(stderr," (got as far as %lu)", count_curr); if (count_curr>0) fprintf(stderr," (got as far as %lu)", count_curr);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
@ -88,6 +112,8 @@ int main(int argc,char* argv[])
fclose(infh); fclose(infh);
} }
if (!files_processed) usage(argv[0]);
if (verbose) { if (verbose) {
printf("%7lu %s\n", count_total, "total"); printf("%7lu %s\n", count_total, "total");
} else { } else {

View File

@ -8,4 +8,4 @@ USAGE
OPTIONS OPTIONS
-v Verbose mode. The number of messages is given for each file. -v Verbose mode. The number of messages is given for each file.
-f Force. Force the execution not to fail on error.

View File

@ -8,4 +8,4 @@ USAGE
OPTIONS OPTIONS
-v Verbose mode. The number of messages is given for each file. -v Verbose mode. The number of messages is given for each file.
-f Force. Force the execution not to fail on error.