1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* (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.
*/
/*
* C Implementation: grib_index
*
* Description: How to create and use an index to access GRIB messages from a file
*
*/
#include "eccodes.h"
static void usage(const char* prog)
{
printf("usage: %s gribfile indexfile\n", prog);
exit(1);
}
int main(int argc, char* argv[])
{
codes_index* index = NULL;
codes_handle* h = NULL;
char* inputfile = NULL;
char* indexfile = 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 = sizeof(oshortName);
int ret = 0, count = 0;
if (argc != 3) usage(argv[0]);
inputfile = argv[1];
indexfile = argv[2];
printf("indexing...\n");
/* create an index given set of keys*/
index = codes_index_new(0, "shortName,level,number,step", &ret);
if (ret) {
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
/* indexes a file */
ret = codes_index_add_file(index, inputfile);
if (ret) {
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
printf("end indexing...\n");
/* get the number of distinct values of "step" in the index */
CODES_CHECK(codes_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 */
CODES_CHECK(codes_index_get_long(index, "step", steps, &stepSize), 0);
printf("stepSize=%zu\n", stepSize);
for (i = 0; i < stepSize; i++)
printf("%ld ", steps[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index, "level", &levelSize), 0);
levels = (long*)malloc(sizeof(long) * levelSize);
if (!levels) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_long(index, "level", levels, &levelSize), 0);
printf("levelSize=%zu\n", levelSize);
for (i = 0; i < levelSize; i++)
printf("%ld ", levels[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index, "number", &numberSize), 0);
numbers = (long*)malloc(sizeof(long) * numberSize);
if (!numbers) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_long(index, "number", numbers, &numberSize), 0);
printf("numberSize=%zu\n", numberSize);
for (i = 0; i < numberSize; i++)
printf("%ld ", numbers[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index, "shortName", &shortNameSize), 0);
shortName = (char**)malloc(sizeof(char*) * shortNameSize);
if (!shortName) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_string(index, "shortName", shortName, &shortNameSize), 0);
printf("shortNameSize=%zu\n", 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] */
codes_index_select_string(index, "shortName", shortName[i]);
for (l = 0; l < levelSize; l++) {
/* select the GRIB with level=levels[l] */
codes_index_select_long(index, "level", levels[l]);
for (j = 0; j < numberSize; j++) {
/* select the GRIB with number=numbers[j] */
codes_index_select_long(index, "number", numbers[j]);
for (k = 0; k < stepSize; k++) {
/* select the GRIB with step=steps[k] */
codes_index_select_long(index, "step", steps[k]);
/* create a new codes_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 = codes_handle_new_from_index(index, &ret)) != NULL) {
count++;
if (ret) {
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
lenshortName = sizeof(oshortName);
codes_get_string(h, "shortName", oshortName, &lenshortName);
codes_get_long(h, "level", &olevel);
codes_get_long(h, "number", &onumber);
codes_get_long(h, "step", &ostep);
printf("shortName=%s ", oshortName);
printf("level=%ld ", olevel);
printf("number=%ld ", onumber);
printf("step=%ld \n", ostep);
codes_handle_delete(h);
}
if (ret && ret != GRIB_END_OF_INDEX) {
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
}
}
}
}
printf(" %d messages selected\n", count);
codes_index_write(index, indexfile);
codes_index_delete(index);
return 0;
}
|