Added grib_index as C example

This commit is contained in:
Shahram Najm 2015-03-18 13:48:30 +00:00
parent 6fe515c665
commit e05a7aaae2
4 changed files with 206 additions and 15 deletions

1
.gitignore vendored
View File

@ -98,6 +98,7 @@ examples/C/grib_get_keys
examples/C/get_data
examples/C/get_product_kind
examples/C/grib_iterator
examples/C/grib_index
examples/C/grib_keys_iterator
examples/C/large_grib1
examples/C/list

View File

@ -13,6 +13,7 @@ list( APPEND test_bins
grib_get_keys
grib_print_data
grib_set_keys
grib_index
set_missing
grib_keys_iterator
grib_set_data

View File

@ -1,22 +1,74 @@
AM_CFLAGS = @WARN_PEDANTIC@ @WERROR@
TESTS = grib_iterator.sh grib_get_keys.sh grib_print_data.sh grib_set_keys.sh \
grib_keys_iterator.sh grib_multi.sh grib_multi_write.sh grib_precision.sh \
list.sh large_grib1.sh get_data.sh sections_copy.sh \
set_missing.sh grib_clone.sh grib_set_pv.sh \
check_gaussian_grids.sh bufr_attributes.sh bufr_clone.sh bufr_expanded.sh \
bufr_get_keys.sh bufr_read_header.sh bufr_read_synop.sh \
bufr_set_keys.sh bufr_subset.sh bufr_keys_iterator.sh bufr_missing.sh \
bufr_read_temp.sh get_product_kind.sh
TESTS = grib_iterator.sh \
grib_get_keys.sh \
grib_print_data.sh \
grib_set_keys.sh \
grib_keys_iterator.sh \
grib_multi.sh \
grib_multi_write.sh \
grib_precision.sh \
list.sh \
large_grib1.sh \
get_data.sh \
sections_copy.sh \
set_missing.sh \
grib_clone.sh \
grib_set_pv.sh \
check_gaussian_grids.sh \
bufr_attributes.sh \
bufr_clone.sh \
bufr_expanded.sh \
bufr_get_keys.sh \
bufr_read_header.sh \
bufr_read_synop.sh \
bufr_set_keys.sh \
bufr_subset.sh \
bufr_keys_iterator.sh \
bufr_missing.sh \
bufr_read_temp.sh \
get_product_kind.sh
noinst_PROGRAMS = grib_nearest grib_set_bitmap grib_iterator grib_get_keys grib_print_data \
grib_set_keys set_missing grib_keys_iterator \
grib_set_data mars_param values_check box grib_multi multi2 grib_multi_write grib_precision \
grib_set_pv list sections_copy large_grib1 get_data grib_iterator_bitmap grib_clone new_sample \
check_gaussian_grid grib_ensemble_index points \
bufr_attributes bufr_clone bufr_expanded bufr_get_keys bufr_read_header bufr_read_synop \
bufr_get_keys bufr_subset bufr_keys_iterator bufr_set_keys bufr_missing bufr_read_temp \
noinst_PROGRAMS = grib_nearest \
grib_set_bitmap \
grib_iterator \
grib_get_keys \
grib_print_data \
grib_set_keys \
set_missing \
grib_keys_iterator \
grib_set_data \
grib_index \
mars_param \
values_check \
box \
grib_multi \
multi2 \
grib_multi_write \
grib_precision \
grib_set_pv \
list \
sections_copy \
large_grib1 \
get_data \
grib_iterator_bitmap \
grib_clone new_sample \
check_gaussian_grid \
grib_ensemble_index \
points \
bufr_attributes \
bufr_clone \
bufr_expanded \
bufr_get_keys \
bufr_read_header \
bufr_read_synop \
bufr_get_keys \
bufr_subset \
bufr_keys_iterator \
bufr_set_keys \
bufr_missing \
bufr_read_temp \
get_product_kind
#bin_PROGRAMS = points
@ -31,6 +83,7 @@ grib_get_keys_SOURCES = grib_get_keys.c
grib_print_data_SOURCES = grib_print_data.c
grib_set_keys_SOURCES = grib_set_keys.c
set_missing_SOURCES = set_missing.c
grib_index_SOURCES = grib_index.c
grib_set_bitmap_SOURCES = grib_set_bitmap.c
grib_multi_SOURCES = grib_multi.c
multi2_SOURCES = multi2.c

136
examples/C/grib_index.c Normal file
View File

@ -0,0 +1,136 @@
/*
* 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.h"
void usage(char* prog) {
printf("usage: %s infile\n",prog);
exit(1);
}
int main(int argc,char* argv[])
{
grib_index* index=NULL;
grib_handle* h=NULL;
char* infile=NULL;
char* outfile=NULL;
long *steps,*levels,*numbers; /* arrays */
char** shortName=NULL;
int i,j,k,l;
size_t stepSize,levelSize,shortNameSize,numberSize;
long ostep,olevel,onumber;
char oshortName[200];
size_t lenshortName=200;
int ret=0,count=0;
if (argc != 2) usage(argv[0]);
infile=argv[1];
outfile=argv[2];
printf("indexing...\n");
/* Create an index given set of keys*/
index=grib_index_new(0,"shortName,level,number,step",&ret);
if (ret) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);}
/* Indexes a file */
ret=grib_index_add_file(index,infile);
if (ret) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);}
printf("end indexing...\n");
/* get the number of distinct values of "step" in the index */
GRIB_CHECK(grib_index_get_size(index,"step",&stepSize),0);
steps=(long*)malloc(sizeof(long)*stepSize);
if (!steps) exit(1);
/* get the list of distinct steps from the index */
/* the list is in ascending order */
GRIB_CHECK(grib_index_get_long(index,"step",steps,&stepSize),0);
printf("stepSize=%ld\n",(long)stepSize);
for (i=0;i<stepSize;i++) printf("%ld ",steps[i]);
printf("\n");
/*same as for "step"*/
GRIB_CHECK(grib_index_get_size(index,"level",&levelSize),0);
levels=(long*)malloc(sizeof(long)*levelSize);
if (!levels) exit(1);
/*same as for "step"*/
GRIB_CHECK(grib_index_get_long(index,"level",levels,&levelSize),0);
printf("levelSize=%ld\n",(long)levelSize);
for (i=0;i<levelSize;i++) printf("%ld ",levels[i]);
printf("\n");
/*same as for "step"*/
GRIB_CHECK(grib_index_get_size(index,"number",&numberSize),0);
numbers=(long*)malloc(sizeof(long)*numberSize);
if (!numbers) exit(1);
/*same as for "step"*/
GRIB_CHECK(grib_index_get_long(index,"number",numbers,&numberSize),0);
printf("numberSize=%ld\n",(long)numberSize);
for (i=0;i<numberSize;i++) printf("%ld ",numbers[i]);
printf("\n");
/*same as for "step"*/
GRIB_CHECK(grib_index_get_size(index,"shortName",&shortNameSize),0);
shortName=(char**)malloc(sizeof(char*)*shortNameSize);
if (!shortName) exit(1);
/*same as for "step"*/
GRIB_CHECK(grib_index_get_string(index,"shortName",shortName,&shortNameSize),0);
printf("shortNameSize=%ld\n",(long)shortNameSize);
for (i=0;i<shortNameSize;i++) printf("%s ",shortName[i]);
printf("\n");
count=0;
/* nested loops on the keys values of the index */
/* different order of the nested loops doesn't affect performance*/
for (i=0;i<shortNameSize;i++) {
/* select the GRIB with shortName=shortName[i] */
grib_index_select_string(index,"shortName",shortName[i]);
for (l=0;l<levelSize;l++) {
/* select the GRIB with level=levels[i] */
grib_index_select_long(index,"level",levels[l]);
for (j=0;j<numberSize;j++) {
/* select the GRIB with number=numbers[i] */
grib_index_select_long(index,"number",numbers[j]);
for (k=0;k<stepSize;k++) {
/* select the GRIB with step=steps[i] */
grib_index_select_long(index,"step",steps[k]);
/* create a new grib_handle from the index with the constraints
imposed by the select statements. It is a loop because
in the index there could be more than one GRIB with those
constraints */
while ((h=grib_handle_new_from_index(index,&ret))!=NULL){
count++;
if (ret) {printf("error: %d\n",ret); exit(ret);}
lenshortName=200;
grib_get_string(h,"shortName",oshortName,&lenshortName);
grib_get_long(h,"level",&olevel);
grib_get_long(h,"number",&onumber);
grib_get_long(h,"step",&ostep);
printf("shortName=%s ",oshortName);
printf("level=%ld ",olevel);
printf("number=%ld ",onumber);
printf("step=%ld \n",ostep);
grib_handle_delete(h);
}
if (ret && ret!=GRIB_END_OF_INDEX ) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);}
}
}
}
}
printf(" %d messages selected\n",count);
grib_index_write(index,"out.gribidx");
grib_index_delete(index);
return 0;
}