eccodes/tools/codes_count.cc

199 lines
5.7 KiB
C++
Raw Normal View History

/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
*
* 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"
2021-02-07 22:06:42 +00:00
static int fail_on_error = 1;
2020-07-17 14:37:57 +00:00
static const char* toolname = NULL;
2017-09-01 16:52:05 +00:00
static void usage(const char* prog)
{
2020-01-22 13:10:59 +00:00
printf("Usage: %s [-v] [-f] infile1 infile2 ... \n", prog);
exit(1);
}
// This version uses malloc and reads the whole contents of each message
static int count_messages_slow(FILE* in, int message_type, unsigned long* count)
{
void* mesg = NULL;
size_t size = 0;
off_t offset = 0;
int err = GRIB_SUCCESS;
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 (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_GTS)
wmo_read = wmo_read_gts_from_file_malloc;
else
wmo_read = wmo_read_any_from_file_malloc;
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) {
(*count)++;
}
grib_context_free(c, mesg);
}
}
if (err == GRIB_END_OF_FILE)
err = GRIB_SUCCESS;
if (mesg) grib_context_free(c, mesg);
return err;
}
2023-06-18 10:49:12 +00:00
// This version does not store the message contents (no malloc). Much faster
static int count_messages_fast(FILE* in, int message_type, unsigned long* count)
{
2020-01-22 13:10:59 +00:00
int err = GRIB_SUCCESS;
typedef int (*wmo_read_proc)(FILE* , void* , size_t*);
wmo_read_proc wmo_read = NULL;
2023-06-18 10:49:12 +00:00
unsigned char buffer[1000] = {0,};
size_t size = sizeof(buffer);
2020-01-22 13:10:59 +00:00
if (message_type == CODES_GRIB)
wmo_read = wmo_read_grib_from_file_fast;
2020-01-22 13:10:59 +00:00
else if (message_type == CODES_BUFR)
wmo_read = wmo_read_bufr_from_file_fast;
2020-01-22 13:10:59 +00:00
else if (message_type == CODES_GTS)
wmo_read = wmo_read_gts_from_file_fast;
2020-01-22 13:10:59 +00:00
else
wmo_read = wmo_read_any_from_file_fast;
2020-01-22 13:10:59 +00:00
if (fail_on_error) {
2023-06-18 10:49:12 +00:00
while ((err = wmo_read(in, buffer, &size)) == GRIB_SUCCESS) {
(*count)++;
}
}
2020-01-22 13:10:59 +00:00
else {
int done = 0;
2020-01-22 13:10:59 +00:00
while (!done) {
2023-06-18 10:49:12 +00:00
err = wmo_read(in, buffer, &size);
if (err) {
if (err == GRIB_END_OF_FILE || err == GRIB_PREMATURE_END_OF_FILE) {
done = 1; // reached the end
}
}
else {
(*count)++;
}
}
}
2020-01-22 13:10:59 +00:00
if (err == GRIB_END_OF_FILE)
err = GRIB_SUCCESS;
return err;
}
2020-01-22 13:10:59 +00:00
int main(int argc, char* argv[])
{
FILE* infh = NULL;
char* filename;
2020-01-22 13:10:59 +00:00
int i, verbose = 0;
int err = 0, files_processed = 0;
unsigned long count_total = 0, count_curr = 0;
int message_type = 0; // GRIB, BUFR etc
typedef int (*count_proc)(FILE*, int, unsigned long*);
count_proc do_count = count_messages_fast;
2020-07-17 14:37:57 +00:00
toolname = argv[0];
2020-01-22 13:10:59 +00:00
if (argc < 2)
2020-07-17 14:37:57 +00:00
usage(toolname);
2020-07-17 14:37:57 +00:00
if (strstr(toolname, "grib_count"))
2020-01-22 13:10:59 +00:00
message_type = CODES_GRIB;
2020-07-17 14:37:57 +00:00
if (strstr(toolname, "bufr_count"))
2020-01-22 13:10:59 +00:00
message_type = CODES_BUFR;
2020-07-17 14:37:57 +00:00
if (strstr(toolname, "gts_count"))
2020-01-22 13:10:59 +00:00
message_type = CODES_GTS;
2020-01-22 13:10:59 +00:00
count_total = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-v") == 0) {
verbose = 1;
continue;
}
2020-01-22 13:10:59 +00:00
if (strcmp(argv[i], "-f") == 0) {
fail_on_error = 0;
continue;
}
2020-01-22 13:10:59 +00:00
filename = argv[i];
if (path_is_directory(filename)) {
2020-07-17 14:37:57 +00:00
fprintf(stderr, "%s: ERROR: \"%s\": Is a directory\n", toolname, filename);
continue;
}
if (strcmp(filename, "-") == 0) {
2020-01-22 13:10:59 +00:00
infh = stdin;
do_count = count_messages_slow; // cannot do fseek on stdin
} else {
2020-01-22 13:10:59 +00:00
infh = fopen(filename, "rb");
}
if (!infh) {
perror(filename);
exit(1);
}
files_processed = 1; // At least one file processed
2020-01-22 13:10:59 +00:00
count_curr = 0;
err = do_count(infh, message_type, &count_curr);
if (err && fail_on_error) {
2020-01-22 13:10:59 +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");
exit(err);
#ifdef DONT_EXIT_ON_BAD_APPLE
// If we did not want to fail but warn and continue
fclose(infh);
continue;
#endif
}
2020-01-22 13:10:59 +00:00
if (verbose)
printf("%7lu %s\n", count_curr, filename);
count_total += count_curr;
fclose(infh);
}
2020-01-22 13:10:59 +00:00
if (!files_processed)
usage(argv[0]);
if (verbose) {
printf("%7lu %s\n", count_total, "total");
2020-01-22 13:10:59 +00:00
}
else {
printf("%lu\n", count_total);
}
return 0;
}