2013-03-25 12:04:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-11-19 17:56:05 +00:00
|
|
|
#include <assert.h>
|
2014-12-23 10:16:21 +00:00
|
|
|
#include "eccodes.h"
|
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;
|
2022-12-29 17:54:30 +00:00
|
|
|
float* fvalues = NULL;
|
|
|
|
double* dvalues = NULL;
|
2020-01-22 14:57:43 +00:00
|
|
|
size_t values_len = 0;
|
2022-12-29 17:54:30 +00:00
|
|
|
size_t i = 0;
|
2013-11-19 17:56:05 +00:00
|
|
|
|
2022-12-29 17:54:30 +00:00
|
|
|
double daverage = 0;
|
|
|
|
float faverage = 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
FILE* in = NULL;
|
2022-12-29 17:30:01 +00:00
|
|
|
const char* filename = "../../data/sample.grib2";
|
2020-01-22 14:57:43 +00:00
|
|
|
codes_handle* h = NULL;
|
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);
|
2013-11-19 17:56:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2015-05-26 13:40:27 +00:00
|
|
|
/* create new handle from the first message in the file*/
|
2015-02-12 17:34:01 +00:00
|
|
|
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
|
2013-11-19 17:56:05 +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);
|
2015-05-26 13:40:27 +00:00
|
|
|
return 1;
|
2013-11-19 17:56:05 +00:00
|
|
|
}
|
2015-05-26 13:40:27 +00:00
|
|
|
fclose(in);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2013-11-19 17:56:05 +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
|
|
|
|
2022-12-29 17:54:30 +00:00
|
|
|
fvalues = (float*)malloc(values_len * sizeof(float));
|
|
|
|
dvalues = (double*)malloc(values_len * sizeof(double));
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2013-11-19 17:56:05 +00:00
|
|
|
/* get data values*/
|
2022-12-29 17:54:30 +00:00
|
|
|
CODES_CHECK(codes_get_float_array(h, "values", fvalues, &values_len), 0);
|
|
|
|
CODES_CHECK(codes_get_double_array(h, "values", dvalues, &values_len), 0);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2022-12-29 17:54:30 +00:00
|
|
|
daverage = 0;
|
|
|
|
faverage = 0;
|
2022-12-29 17:30:01 +00:00
|
|
|
for (i = 0; i < values_len; i++){
|
|
|
|
//printf("%f\n",values[i]);
|
2022-12-29 17:54:30 +00:00
|
|
|
faverage += fvalues[i];
|
|
|
|
daverage += dvalues[i];
|
2022-12-29 17:30:01 +00:00
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2022-12-29 17:54:30 +00:00
|
|
|
daverage /= (double)values_len;
|
|
|
|
faverage /= (double)values_len;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2022-12-29 17:54:30 +00:00
|
|
|
free(dvalues);
|
|
|
|
free(fvalues);
|
|
|
|
printf("There are %d values, double average is %g\n", (int)values_len, daverage);
|
|
|
|
printf("There are %d values, float average is %f\n", (int)values_len, faverage);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_handle_delete(h);
|
2015-05-26 13:40:27 +00:00
|
|
|
|
2013-11-19 17:56:05 +00:00
|
|
|
return 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|