2013-03-25 12:04:10 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2013-03-25 12:04:10 +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.h"
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
static void usage(const char* prog)
|
|
|
|
{
|
|
|
|
printf("usage: %s infile\n", prog);
|
2014-07-29 15:50:33 +00:00
|
|
|
exit(1);
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char* argv[])
|
2014-07-29 15:50:33 +00:00
|
|
|
{
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_index* index = NULL;
|
|
|
|
grib_handle* h = NULL;
|
|
|
|
char* infile = NULL;
|
|
|
|
long *steps, *levels, *numbers; /* arrays */
|
2020-02-10 10:31:36 +00:00
|
|
|
char** shortNames = NULL;
|
2020-01-29 12:05:02 +00:00
|
|
|
int i, j, k, l;
|
|
|
|
size_t stepSize, levelSize, shortNameSize, numberSize;
|
|
|
|
long oStep, oLevel, oNumber;
|
2015-11-27 18:04:40 +00:00
|
|
|
char oShortName[200];
|
2020-01-29 12:05:02 +00:00
|
|
|
size_t lenShortName = 200;
|
|
|
|
int ret = 0, count = 0;
|
2014-07-29 15:50:33 +00:00
|
|
|
|
|
|
|
if (argc != 2) usage(argv[0]);
|
2020-01-29 12:05:02 +00:00
|
|
|
infile = argv[1];
|
2014-07-29 15:50:33 +00:00
|
|
|
|
|
|
|
printf("indexing...\n");
|
|
|
|
|
2015-03-02 17:22:07 +00:00
|
|
|
/* Create an index given set of keys*/
|
2020-01-29 12:05:02 +00:00
|
|
|
index = grib_index_new(0, "shortName,level,number,step", &ret);
|
|
|
|
if (ret) {
|
|
|
|
printf("error: %s\n", grib_get_error_message(ret));
|
|
|
|
exit(ret);
|
|
|
|
}
|
2015-03-02 17:22:07 +00:00
|
|
|
|
|
|
|
/* Indexes a file */
|
2020-01-29 12:05:02 +00:00
|
|
|
ret = grib_index_add_file(index, infile);
|
|
|
|
if (ret) {
|
|
|
|
printf("error: %s\n", grib_get_error_message(ret));
|
|
|
|
exit(ret);
|
|
|
|
}
|
2015-03-02 17:22:07 +00:00
|
|
|
printf("end indexing...\n");
|
|
|
|
|
|
|
|
/* get the number of distinct values of "step" in the index */
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_index_get_size(index, "step", &stepSize), 0);
|
|
|
|
steps = (long*)malloc(sizeof(long) * stepSize);
|
2015-03-02 17:22:07 +00:00
|
|
|
if (!steps) exit(1);
|
|
|
|
/* get the list of distinct steps from the index */
|
|
|
|
/* the list is in ascending order */
|
2020-01-29 12:05:02 +00:00
|
|
|
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]);
|
2015-03-02 17:22:07 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/*same as for "step"*/
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_index_get_size(index, "level", &levelSize), 0);
|
|
|
|
levels = (long*)malloc(sizeof(long) * levelSize);
|
2015-03-02 17:22:07 +00:00
|
|
|
if (!levels) exit(1);
|
|
|
|
/*same as for "step"*/
|
2020-01-29 12:05:02 +00:00
|
|
|
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]);
|
2015-03-02 17:22:07 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/*same as for "step"*/
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_index_get_size(index, "number", &numberSize), 0);
|
|
|
|
numbers = (long*)malloc(sizeof(long) * numberSize);
|
2015-03-02 17:22:07 +00:00
|
|
|
if (!numbers) exit(1);
|
|
|
|
/*same as for "step"*/
|
2020-01-29 12:05:02 +00:00
|
|
|
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]);
|
2015-03-02 17:22:07 +00:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/*same as for "step"*/
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_index_get_size(index, "shortName", &shortNameSize), 0);
|
2020-02-10 10:31:36 +00:00
|
|
|
shortNames = (char**)malloc(sizeof(char*) * shortNameSize);
|
|
|
|
if (!shortNames) exit(1);
|
2015-03-02 17:22:07 +00:00
|
|
|
/*same as for "step"*/
|
2020-02-10 10:31:36 +00:00
|
|
|
GRIB_CHECK(grib_index_get_string(index, "shortName", shortNames, &shortNameSize), 0);
|
2020-01-29 12:05:02 +00:00
|
|
|
printf("shortNameSize=%ld\n", (long)shortNameSize);
|
|
|
|
for (i = 0; i < shortNameSize; i++)
|
2020-02-10 10:31:36 +00:00
|
|
|
printf("%s ", shortNames[i]);
|
2015-03-02 17:22:07 +00:00
|
|
|
printf("\n");
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
count = 0;
|
2015-03-02 17:22:07 +00:00
|
|
|
/* nested loops on the keys values of the index */
|
|
|
|
/* different order of the nested loops doesn't affect performance*/
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < shortNameSize; i++) {
|
2020-02-10 10:31:36 +00:00
|
|
|
/* select the GRIB with shortName=shortNames[i] */
|
|
|
|
grib_index_select_string(index, "shortName", shortNames[i]);
|
2015-03-02 17:22:07 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (l = 0; l < levelSize; l++) {
|
2020-06-01 19:21:53 +00:00
|
|
|
/* select the GRIB with level=levels[l] */
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_index_select_long(index, "level", levels[l]);
|
2015-03-02 17:22:07 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (j = 0; j < numberSize; j++) {
|
2020-06-01 19:21:53 +00:00
|
|
|
/* select the GRIB with number=numbers[j] */
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_index_select_long(index, "number", numbers[j]);
|
2015-03-02 17:22:07 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (k = 0; k < stepSize; k++) {
|
2020-06-01 19:21:53 +00:00
|
|
|
/* select the GRIB with step=steps[k] */
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_index_select_long(index, "step", steps[k]);
|
2015-03-02 17:22:07 +00:00
|
|
|
|
|
|
|
/* create a new grib_handle from the index with the constraints
|
2020-06-01 19:21:53 +00:00
|
|
|
imposed by the select statements. It is a loop because
|
|
|
|
in the index there could be more than one GRIB with those
|
|
|
|
constraints */
|
2020-01-29 12:05:02 +00:00
|
|
|
while ((h = grib_handle_new_from_index(index, &ret)) != NULL) {
|
2015-03-02 17:22:07 +00:00
|
|
|
count++;
|
2020-01-29 12:05:02 +00:00
|
|
|
if (ret) {
|
2020-06-01 19:21:53 +00:00
|
|
|
printf("error: %s\n", grib_get_error_message(ret));
|
2020-01-29 12:05:02 +00:00
|
|
|
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);
|
2015-03-02 17:22:07 +00:00
|
|
|
grib_handle_delete(h);
|
2014-07-29 15:50:33 +00:00
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
if (ret && ret != GRIB_END_OF_INDEX) {
|
|
|
|
printf("error: %s\n", grib_get_error_message(ret));
|
|
|
|
exit(ret);
|
|
|
|
}
|
2014-07-29 15:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
printf(" %d messages selected\n", count);
|
2015-03-02 17:22:07 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_index_write(index, "out.gribidx");
|
2015-03-02 17:22:07 +00:00
|
|
|
grib_index_delete(index);
|
2020-02-10 10:31:36 +00:00
|
|
|
free(levels);
|
|
|
|
free(numbers);
|
|
|
|
free(steps);
|
|
|
|
for (i = 0; i < shortNameSize; i++)
|
|
|
|
free(shortNames[i]);
|
|
|
|
free(shortNames);
|
2014-07-29 15:50:33 +00:00
|
|
|
|
2020-12-22 16:47:23 +00:00
|
|
|
grib_context_delete(grib_context_get_default());
|
2020-08-25 12:23:24 +00:00
|
|
|
|
2014-07-29 15:50:33 +00:00
|
|
|
return 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|