eccodes/examples/C/grib_print_data.c

95 lines
2.6 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_print_data
2013-03-25 12:04:10 +00:00
*
* Description: prints all the data contained in a GRIB file
2013-03-25 12:04:10 +00:00
*
*/
#include <stdio.h>
#include <stdlib.h>
#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 filename\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)
{
2020-01-22 14:57:43 +00:00
int err = 0, i;
double* values = NULL;
double max, min, average;
size_t values_len = 0;
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
FILE* in = NULL;
2020-01-22 14:57:43 +00:00
char* filename;
codes_handle* h = NULL;
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
if (argc < 2) usage(argv[0]);
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;
}
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
/* create new handle from a message in a file*/
2015-02-12 17:34:01 +00:00
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
2014-06-18 16:14:01 +00:00
if (h == NULL) {
2020-05-14 19:21:31 +00:00
fprintf(stderr, "Error: unable to create handle from file %s\n", filename);
2021-11-25 16:10:17 +00:00
return 1;
2014-06-18 16:14:01 +00:00
}
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
/* get the size of the values array*/
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
values = (double*)malloc(values_len * sizeof(double));
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
/* get data values*/
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
for (i = 0; i < values_len; i++)
printf("%d %.10e\n", i, values[i]);
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
free(values);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_double(h, "max", &max), 0);
CODES_CHECK(codes_get_double(h, "min", &min), 0);
CODES_CHECK(codes_get_double(h, "average", &average), 0);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
printf("%d values found in %s\n", (int)values_len, filename);
printf("max=%.10e min=%.10e average=%.10e\n", max, min, average);
2013-03-25 12:04:10 +00:00
2017-01-05 13:50:08 +00:00
{
2021-11-30 12:44:37 +00:00
/* example of accessing specific elements from data values */
2020-01-22 14:57:43 +00:00
double vals_arr[3] = { 0, 0, 0 };
const int NUM = 3;
2017-05-02 16:59:04 +00:00
int index_arr[3];
2020-01-22 14:57:43 +00:00
index_arr[0] = 0; /* first element */
index_arr[1] = values_len / 2; /* middle element */
index_arr[2] = values_len - 1; /* last element */
2017-05-02 16:59:04 +00:00
2017-01-12 17:46:58 +00:00
CODES_CHECK(codes_get_double_elements(h, "values", index_arr, NUM, vals_arr), 0);
2020-01-22 14:57:43 +00:00
for (i = 0; i < NUM; ++i) {
2017-01-05 13:50:08 +00:00
printf("value at index %d = %.10e\n", index_arr[i], vals_arr[i]);
}
}
codes_handle_delete(h);
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
fclose(in);
return 0;
2013-03-25 12:04:10 +00:00
}