eccodes/examples/C/iterator.c

87 lines
2.4 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2015-01-05 15:45:46 +00:00
* Copyright 2005-2015 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: iterator
*
* Description: how to use an iterator on lat/lon/values.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eccodes.h"
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
void usage(const char* prog) {
printf("Usage: %s grib_file\n",prog);
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;
int err = 0;
double lat,lon,value,missingValue=0;
int n=0;
char* filename = NULL;
/* Message handle. Required in all the grib_api calls acting on a message.*/
codes_handle *h = NULL;
2014-06-18 16:14:01 +00:00
/* Iterator on lat/lon/values.*/
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
2014-06-18 16:14:01 +00:00
filename=argv[1];
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
in = fopen(filename,"r");
if(!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* Loop on all the messages in a file.*/
while ((h = codes_handle_new_from_file(0,in,&err)) != NULL ) {
2014-06-18 16:14:01 +00:00
/* Check of errors after reading a message. */
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
2014-06-18 16:14:01 +00:00
/* Get the double representing the missing value in the field. */
CODES_CHECK(codes_get_double(h,"missingValue",&missingValue),0);
2014-06-18 16:14:01 +00:00
/* A new iterator on lat/lon/values is created from the message handle h. */
iter=codes_iterator_new(h,0,&err);
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
2014-06-18 16:14:01 +00:00
n = 0;
/* Loop on all the lat/lon/values. */
while(codes_iterator_next(iter,&lat,&lon,&value)) {
2014-06-18 16:14:01 +00:00
/* You can now print lat and lon, */
printf("- %d - lat=%f lon=%f value=",n,lat,lon);
/* decide what to print if a missing value is found. */
if (value == missingValue ) printf("missing\n");
/* and print the value if is not missing. */
else printf("%f\n",value);
n++;
}
/* At the end the iterator is deleted to free memory. */
codes_iterator_delete(iter);
2014-06-18 16:14:01 +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
}