1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_api.h"
12 
usage(char * prog)13 static void usage(char* prog)
14 {
15     printf("usage: %s infile\n", prog);
16     exit(1);
17 }
18 
main(int argc,char * argv[])19 int main(int argc, char* argv[])
20 {
21     grib_index* index = NULL;
22     grib_handle* h    = NULL;
23     long *steps, *levels, *numbers;
24     char** shortNames = NULL;
25     int i, j, k, l;
26     size_t stepSize, levelSize, shortNameSize, numberSize;
27     long ostep, olevel, onumber;
28     char oshortName[200];
29     size_t lenshortName = 200;
30     int ret = 0, count = 0;
31 
32     if (argc != 2) usage(argv[0]);
33 
34     printf("indexing...\n");
35 
36     index = grib_index_read(0, "out.gribidx", &ret);
37     GRIB_CHECK(ret, 0);
38 
39     printf("end indexing...\n");
40 
41     /* get the number of distinct values of "step" in the index */
42     GRIB_CHECK(grib_index_get_size(index, "step", &stepSize), 0);
43     steps = (long*)malloc(sizeof(long) * stepSize);
44     if (!steps) exit(1);
45     /* get the list of distinct steps from the index */
46     /* the list is in ascending order */
47     GRIB_CHECK(grib_index_get_long(index, "step", steps, &stepSize), 0);
48     printf("stepSize=%ld\n", (long)stepSize);
49     for (i = 0; i < stepSize; i++)
50         printf("%ld ", steps[i]);
51     printf("\n");
52 
53     /*same as for "step"*/
54     GRIB_CHECK(grib_index_get_size(index, "level", &levelSize), 0);
55     levels = (long*)malloc(sizeof(long) * levelSize);
56     if (!levels) exit(1);
57     /*same as for "step"*/
58     GRIB_CHECK(grib_index_get_long(index, "level", levels, &levelSize), 0);
59     printf("levelSize=%ld\n", (long)levelSize);
60     for (i = 0; i < levelSize; i++)
61         printf("%ld ", levels[i]);
62     printf("\n");
63 
64     /*same as for "step"*/
65     GRIB_CHECK(grib_index_get_size(index, "number", &numberSize), 0);
66     numbers = (long*)malloc(sizeof(long) * numberSize);
67     if (!numbers) exit(1);
68     /*same as for "step"*/
69     GRIB_CHECK(grib_index_get_long(index, "number", numbers, &numberSize), 0);
70     printf("numberSize=%ld\n", (long)numberSize);
71     for (i = 0; i < numberSize; i++)
72         printf("%ld ", numbers[i]);
73     printf("\n");
74 
75     /*same as for "step"*/
76     GRIB_CHECK(grib_index_get_size(index, "shortName", &shortNameSize), 0);
77     shortNames = (char**)malloc(sizeof(char*) * shortNameSize);
78     if (!shortNames) exit(1);
79     /*same as for "step"*/
80     GRIB_CHECK(grib_index_get_string(index, "shortName", shortNames, &shortNameSize), 0);
81     printf("shortNameSize=%ld\n", (long)shortNameSize);
82     for (i = 0; i < shortNameSize; i++)
83         printf("%s ", shortNames[i]);
84     printf("\n");
85 
86     count = 0;
87     /* nested loops on the keys values of the index */
88     /* different order of the nested loops doesn't affect performance*/
89     for (i = 0; i < shortNameSize; i++) {
90         /* select the grib with shortName=shortName[i] */
91         grib_index_select_string(index, "shortName", shortNames[i]);
92 
93         for (l = 0; l < levelSize; l++) {
94             /* select the grib with level=level[i] */
95             grib_index_select_long(index, "level", levels[l]);
96 
97             for (j = 0; j < numberSize; j++) {
98                 /* select the grib with number=number[i] */
99                 grib_index_select_long(index, "number", numbers[j]);
100 
101                 for (k = 0; k < stepSize; k++) {
102                     /* select the grib with step=step[i] */
103                     grib_index_select_long(index, "step", steps[k]);
104 
105                     /* create a new grib_handle from the index with the constraints
106                      * imposed by the select statements. It is a loop because
107                      * in the index there could be more than one grib with those
108                      * constrants */
109                     while ((h = grib_handle_new_from_index(index, &ret)) != NULL) {
110                         count++;
111                         if (ret) {
112                             printf("error: %d\n", ret);
113                             exit(ret);
114                         }
115                         lenshortName = 200;
116                         grib_get_string(h, "shortName", oshortName, &lenshortName);
117                         grib_get_long(h, "level", &olevel);
118                         grib_get_long(h, "number", &onumber);
119                         grib_get_long(h, "step", &ostep);
120                         printf("shortName=%s ", oshortName);
121                         printf("level=%ld ", olevel);
122                         printf("number=%ld ", onumber);
123                         printf("step=%ld \n", ostep);
124                         grib_handle_delete(h);
125                     }
126                     if (ret != GRIB_END_OF_INDEX) {
127                         printf("error: %s\n", grib_get_error_message(ret));
128                         exit(ret);
129                     }
130                 }
131             }
132         }
133     }
134     printf("  %d messages selected\n", count);
135     free(levels);
136     free(steps);
137     free(numbers);
138     for (i = 0; i < shortNameSize; i++)
139         free(shortNames[i]);
140     free(shortNames);
141     grib_index_delete(index);
142 
143     return 0;
144 }
145