eccodes/examples/C/grib_iterator.c

95 lines
2.9 KiB
C
Raw Normal View History

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.
*/
/*
* C Implementation: grib_iterator
2013-03-25 12:04:10 +00:00
*
* Description: how to use an iterator on lat/lon/values for GRIB messages
2013-03-25 12:04:10 +00:00
*/
#include <stdio.h>
#include <stdlib.h>
2017-02-06 16:46:28 +00:00
#include "eccodes.h"
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
static void usage(const char* prog)
{
printf("Usage: %s grib_file\n", prog);
2014-06-18 16:14:01 +00:00
exit(1);
2013-03-25 12:04:10 +00:00
}
2014-06-18 16:14:01 +00:00
int main(int argc, char** argv)
{
FILE* in = NULL;
2020-01-22 14:57:43 +00:00
int err = 0;
double lat, lon, value;
double missingValue = 1.0e36; /* a value out of range */
2020-01-22 14:57:43 +00:00
int n = 0;
long bitmapPresent = 0;
2020-01-22 14:57:43 +00:00
char* filename = NULL;
2014-06-18 16:14:01 +00:00
2021-11-30 12:44:37 +00:00
/* message handle. Required in all the ecCodes calls acting on a message.*/
2020-01-22 14:57:43 +00:00
codes_handle* h = NULL;
2021-11-30 12:44:37 +00:00
/* iterator on lat/lon/values.*/
2020-01-22 14:57:43 +00:00
codes_iterator* iter = NULL;
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
if (argc != 2) usage(argv[0]);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
filename = argv[1];
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
in = fopen(filename, "rb");
if (!in) {
2020-05-14 19:21:31 +00:00
fprintf(stderr, "Error: unable to open file %s\n", filename);
2014-06-18 16:14:01 +00:00
return 1;
}
2021-11-30 12:44:37 +00:00
/* loop on all the messages in a file.*/
2020-01-22 14:57:43 +00:00
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
2021-11-30 12:44:37 +00:00
/* check of errors after reading a message. */
2020-01-22 14:57:43 +00:00
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
2014-06-18 16:14:01 +00:00
2021-11-30 12:44:37 +00:00
/* check if a bitmap applies */
CODES_CHECK(codes_get_long(h, "bitmapPresent", &bitmapPresent), 0);
if (bitmapPresent) {
2021-11-30 12:44:37 +00:00
/* set the double representing the missing value in the field. */
/* choose a missingValue that does not correspond to any real value in the data array */
CODES_CHECK(codes_set_double(h, "missingValue", missingValue), 0);
}
2014-06-18 16:14:01 +00:00
2021-11-30 12:44:37 +00:00
/* a new iterator on lat/lon/values is created from the message handle h */
2020-01-22 14:57:43 +00:00
iter = codes_grib_iterator_new(h, 0, &err);
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
2014-06-18 16:14:01 +00:00
n = 0;
2021-11-30 12:44:37 +00:00
/* loop on all the lat/lon/values. */
2020-01-22 14:57:43 +00:00
while (codes_grib_iterator_next(iter, &lat, &lon, &value)) {
2021-11-30 12:44:37 +00:00
/* now print lat and lon, */
2020-01-22 14:57:43 +00:00
printf("- %d - lat=%f lon=%f value=", n, lat, lon);
2014-06-18 16:14:01 +00:00
/* decide what to print if a missing value is found. */
if (bitmapPresent && value == missingValue) printf("missing\n");
2014-06-18 16:14:01 +00:00
/* and print the value if is not missing. */
2020-01-22 14:57:43 +00:00
else
printf("%f\n", value);
2014-06-18 16:14:01 +00:00
n++;
}
2021-11-30 12:44:37 +00:00
/* at the end the iterator is deleted to free memory. */
codes_grib_iterator_delete(iter);
2014-06-18 16:14:01 +00:00
2021-11-30 12:44:37 +00:00
/* at the end the codes_handle is deleted to free memory */
codes_handle_delete(h);
2014-06-18 16:14:01 +00:00
}
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
fclose(in);
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}