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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-03-17 14:21:14 +00:00
|
|
|
* C Implementation: grib_iterator
|
2013-03-25 12:04:10 +00:00
|
|
|
*
|
2015-03-17 14:21:14 +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;
|
2021-11-30 12:44:37 +00:00
|
|
|
double missingValue = 1e+20; /* a value out of range */
|
2020-01-22 14:57:43 +00:00
|
|
|
int n = 0;
|
2021-02-11 22:04:00 +00:00
|
|
|
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 */
|
2021-02-11 22:04:00 +00:00
|
|
|
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 */
|
2021-02-11 22:04:00 +00:00
|
|
|
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. */
|
2021-02-11 22:04:00 +00:00
|
|
|
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. */
|
2015-02-11 14:32:39 +00:00
|
|
|
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 */
|
2014-12-23 10:16:21 +00:00
|
|
|
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
|
|
|
}
|