2015-02-01 20:53:53 +00:00
|
|
|
/*
|
2018-01-02 11:01:36 +00:00
|
|
|
* Copyright 2005-2018 ECMWF.
|
2015-02-01 20:53:53 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
|
|
|
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
2018-01-02 11:01:36 +00:00
|
|
|
static int fail_on_error = 1;
|
2015-02-01 20:53:53 +00:00
|
|
|
|
2017-09-01 16:52:05 +00:00
|
|
|
static void usage(const char* prog)
|
2015-02-01 20:53:53 +00:00
|
|
|
{
|
2018-01-02 11:01:36 +00:00
|
|
|
printf("Usage: %s [-v] [-f] infile1 infile2 ... \n",prog);
|
2015-02-01 20:53:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-02-02 22:00:32 +00:00
|
|
|
static int count_messages(FILE* in, int message_type, unsigned long *count)
|
2015-02-01 20:53:53 +00:00
|
|
|
{
|
|
|
|
void* mesg=NULL;
|
|
|
|
size_t size=0;
|
|
|
|
off_t offset=0;
|
2015-02-02 22:00:32 +00:00
|
|
|
int err=GRIB_SUCCESS;
|
2015-02-01 20:53:53 +00:00
|
|
|
typedef void* (*wmo_read_proc)(FILE *, int, size_t *, off_t *, int *);
|
|
|
|
wmo_read_proc wmo_read = NULL;
|
|
|
|
grib_context* c=grib_context_get_default();
|
|
|
|
|
|
|
|
if (!in) return 1;
|
|
|
|
/* printf("message_type=%d\n", message_type); */
|
|
|
|
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;
|
2019-03-19 15:36:25 +00:00
|
|
|
else if (message_type == CODES_GTS) wmo_read=wmo_read_gts_from_file_malloc;
|
2015-02-01 20:53:53 +00:00
|
|
|
else wmo_read=wmo_read_any_from_file_malloc;
|
2018-01-02 11:01:36 +00:00
|
|
|
|
|
|
|
if (fail_on_error)
|
|
|
|
{
|
|
|
|
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)++;
|
|
|
|
}
|
|
|
|
}
|
2015-02-01 20:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,char* argv[])
|
|
|
|
{
|
2015-02-02 22:00:32 +00:00
|
|
|
FILE* infh = NULL;
|
2015-02-01 20:53:53 +00:00
|
|
|
char* filename;
|
2015-02-02 22:00:32 +00:00
|
|
|
int i, verbose=0;
|
2018-01-02 11:01:36 +00:00
|
|
|
int err=0, files_processed=0;
|
2015-02-02 22:00:32 +00:00
|
|
|
unsigned long count_total=0, count_curr=0;
|
2015-02-01 20:53:53 +00:00
|
|
|
int message_type = 0; /* GRIB, BUFR etc */
|
|
|
|
|
|
|
|
if (argc <2) usage(argv[0]);
|
|
|
|
|
|
|
|
if (strstr(argv[0], "grib_count")) message_type = CODES_GRIB;
|
|
|
|
if (strstr(argv[0], "bufr_count")) message_type = CODES_BUFR;
|
2019-03-19 15:36:25 +00:00
|
|
|
if (strstr(argv[0], "gts_count")) message_type = CODES_GTS;
|
2015-02-01 20:53:53 +00:00
|
|
|
|
2015-02-02 22:00:32 +00:00
|
|
|
count_total=0;
|
2015-02-01 20:53:53 +00:00
|
|
|
for (i=1;i<argc;i++) {
|
2015-02-02 22:00:32 +00:00
|
|
|
if (strcmp(argv[i], "-v")==0) {
|
|
|
|
verbose = 1;
|
2018-01-02 11:01:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(argv[i], "-f")==0) {
|
|
|
|
fail_on_error = 0;
|
2015-02-02 22:00:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-01 20:53:53 +00:00
|
|
|
filename=argv[i];
|
2018-02-01 11:00:58 +00:00
|
|
|
if (strcmp(filename,"-")==0)
|
|
|
|
infh=stdin;
|
|
|
|
else
|
2019-03-14 12:34:41 +00:00
|
|
|
infh=fopen(filename,"rb");
|
2015-02-01 20:53:53 +00:00
|
|
|
if (!infh) {
|
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-01-02 11:01:36 +00:00
|
|
|
files_processed=1; /* At least one file processed */
|
2015-02-02 22:00:32 +00:00
|
|
|
count_curr=0;
|
|
|
|
err=count_messages(infh, message_type, &count_curr);
|
2018-01-02 11:01:36 +00:00
|
|
|
if (err && fail_on_error) {
|
2017-06-28 15:14:35 +00:00
|
|
|
fprintf(stderr,"Invalid message(s) found in %s", filename);
|
|
|
|
if (count_curr>0) fprintf(stderr," (got as far as %lu)", count_curr);
|
|
|
|
fprintf(stderr,"\n");
|
2015-02-01 20:53:53 +00:00
|
|
|
exit(err);
|
2015-02-02 22:00:32 +00:00
|
|
|
#ifdef DONT_EXIT_ON_BAD_APPLE
|
|
|
|
/* If we did not want to fail but warn and continue */
|
|
|
|
fclose(infh);
|
|
|
|
continue;
|
|
|
|
#endif
|
2015-02-01 20:53:53 +00:00
|
|
|
}
|
2015-02-02 22:00:32 +00:00
|
|
|
if (verbose) printf ("%7lu %s\n", count_curr, filename);
|
|
|
|
count_total += count_curr;
|
2015-02-01 20:53:53 +00:00
|
|
|
|
|
|
|
fclose(infh);
|
|
|
|
}
|
2018-01-02 11:01:36 +00:00
|
|
|
|
|
|
|
if (!files_processed) usage(argv[0]);
|
2015-02-02 22:00:32 +00:00
|
|
|
if (verbose) {
|
|
|
|
printf("%7lu %s\n", count_total, "total");
|
|
|
|
} else {
|
|
|
|
printf("%lu\n", count_total);
|
|
|
|
}
|
2015-02-01 20:53:53 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|