Added codes_count.c from which we create 3 executables: codes_count, grib_count and bufr_count

This commit is contained in:
Shahram Najm 2015-02-01 20:53:53 +00:00
parent 5e659515a7
commit cc3237e578
3 changed files with 100 additions and 5 deletions

View File

@ -7,10 +7,10 @@ ecbuild_add_library( TARGET grib_tools
# tools binaries # tools binaries
list( APPEND grib_tools_bins list( APPEND grib_tools_bins
codes_info codes_info codes_count
grib_histogram grib_filter grib_ls grib_dump grib_merge grib_histogram grib_filter grib_ls grib_dump grib_merge
grib2ppm grib_set grib_get grib_get_data grib_copy grib2ppm grib_set grib_get grib_get_data grib_copy
grib_compare parser grib_count grib_index_build grib_to_netcdf grib_compare parser grib_index_build grib_to_netcdf
bufr_filter bufr_ls bufr_dump bufr_set bufr_get bufr_filter bufr_ls bufr_dump bufr_set bufr_get
bufr_copy bufr_compare bufr_index_build bufr_copy bufr_compare bufr_index_build
taf_dump taf_ls taf_get taf_filter taf_dump taf_ls taf_get taf_filter
@ -42,6 +42,17 @@ foreach( tool ${grib_tools_bins_ECMWF_ONLY} )
LIBS grib_tools ) LIBS grib_tools )
endforeach() endforeach()
# Count tools. Same source code, different executable names
ecbuild_add_executable( TARGET grib_count
SOURCES codes_count.c
CONDITION ECCODES_INSTALL_ECMWF_TOOLS
LIBS grib_tools )
ecbuild_add_executable( TARGET bufr_count
SOURCES codes_count.c
CONDITION ECCODES_INSTALL_ECMWF_TOOLS
LIBS grib_tools )
# grib_list_keys
ecbuild_add_executable( TARGET grib_list_keys ecbuild_add_executable( TARGET grib_list_keys
SOURCES list_keys.c SOURCES list_keys.c
CONDITION ECCODES_INSTALL_ECMWF_TOOLS CONDITION ECCODES_INSTALL_ECMWF_TOOLS

View File

@ -13,11 +13,11 @@ libgrib_tools_la_SOURCES = grib_tools.c \
dist_bin_SCRIPTS = bufr_compare_dir dist_bin_SCRIPTS = bufr_compare_dir
bin_PROGRAMS = grib_histogram big2gribex \ bin_PROGRAMS = grib_histogram big2gribex \
codes_info grib_filter grib_ls grib_dump grib_merge \ codes_info codes_count grib_filter grib_ls grib_dump grib_merge \
grib2ppm grib_set grib_get grib_get_data grib_copy grib_repair \ grib2ppm grib_set grib_get grib_get_data grib_copy grib_repair \
grib_compare grib_list_keys parser grib_count grib_index_build grib1to2 \ grib_compare grib_list_keys parser grib_count grib_index_build grib1to2 \
gg_sub_area_check grib_to_netcdf grib_to_json \ gg_sub_area_check grib_to_netcdf grib_to_json \
bufr_filter bufr_ls bufr_dump bufr_set bufr_get \ bufr_count bufr_filter bufr_ls bufr_dump bufr_set bufr_get \
bufr_copy bufr_compare bufr_index_build \ bufr_copy bufr_compare bufr_index_build \
taf_dump taf_ls taf_get taf_filter \ taf_dump taf_ls taf_get taf_filter \
metar_dump metar_ls metar_get metar_filter metar_dump metar_ls metar_get metar_filter
@ -32,7 +32,9 @@ grib_merge_SOURCES = grib_merge.c
grib_to_netcdf_SOURCES = grib_to_netcdf.c grib_to_netcdf_SOURCES = grib_to_netcdf.c
grib_to_json_SOURCES = grib_to_json.c grib_to_json_SOURCES = grib_to_json.c
grib_dump_SOURCES = grib_dump.c grib_dump_SOURCES = grib_dump.c
grib_count_SOURCES = grib_count.c grib_count_SOURCES = codes_count.c
bufr_count_SOURCES = codes_count.c
codes_count_SOURCES = codes_count.c
grib_histogram_SOURCES = grib_histogram.c grib_histogram_SOURCES = grib_histogram.c
parser_SOURCES = parser.c parser_SOURCES = parser.c
grib_list_keys_SOURCES = list_keys.c grib_list_keys_SOURCES = list_keys.c

82
tools/codes_count.c Normal file
View File

@ -0,0 +1,82 @@
/*
* Copyright 2005-2015 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"
void usage(char* prog)
{
printf("usage: %s infile1 infile2 ... \n",prog);
exit(1);
}
static int check_file(FILE* in, int message_type, long *count)
{
void* mesg=NULL;
size_t size=0;
off_t offset=0;
int err=0;
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;
else wmo_read=wmo_read_any_from_file_malloc;
while ( (mesg=wmo_read(in,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) {
grib_context_free(c,mesg);
(*count)++;
}
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
return err;
}
int main(int argc,char* argv[])
{
FILE* infh;
char* filename;
int i;
int err=0;
long n=0,nn=0;
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;
n=0;
for (i=1;i<argc;i++) {
filename=argv[i];
infh=fopen(filename,"r");
if (!infh) {
perror(filename);
exit(1);
}
nn=0;
err=check_file(infh, message_type, &nn);
if (err!=0) {
fprintf(stderr,"Invalid message(s) found in %s\n", filename);
exit(err);
}
n+=nn;
fclose(infh);
}
printf("%ld\n",n);
return 0;
}