2014-09-22 14:58:31 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2014-09-22 14:58:31 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-09-22 15:51:27 +00:00
|
|
|
* C Implementation: ensemble_mean
|
|
|
|
*
|
|
|
|
* Description: index a GRIB file, select a specific parameter and compute ensemble mean.
|
2014-09-22 14:58:31 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-23 10:16:21 +00:00
|
|
|
#include "eccodes.h"
|
2014-09-22 14:58:31 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
int main(int argc, char* argv[])
|
2014-09-22 14:58:31 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i, j;
|
2020-01-22 14:57:43 +00:00
|
|
|
int count = 0;
|
|
|
|
size_t paramIdSize, numberSize, values_len = 0;
|
2014-09-22 14:58:31 +00:00
|
|
|
char** paramId;
|
|
|
|
long* number;
|
|
|
|
double* values;
|
2020-01-22 14:57:43 +00:00
|
|
|
double* result = NULL;
|
|
|
|
double min = 1e13, max = -1e13, avg = 0;
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_index* index;
|
2020-01-22 14:57:43 +00:00
|
|
|
codes_handle* h = NULL;
|
2017-02-06 16:46:28 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
if (argc < 2) return 1;
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* create index of file contents for paramId and number */
|
2020-01-22 14:57:43 +00:00
|
|
|
index = codes_index_new_from_file(0, argv[1], "paramId,number", &ret);
|
|
|
|
CODES_CHECK(ret, 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* get size of "paramId" list */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_index_get_size(index, "paramId", ¶mIdSize), 0);
|
|
|
|
printf("grib contains %lu different parameters\n", paramIdSize);
|
2014-09-22 14:58:31 +00:00
|
|
|
/* allocate memory for "paramId" list */
|
2020-01-22 14:57:43 +00:00
|
|
|
paramId = (char**)malloc(paramIdSize * sizeof(char*));
|
2014-09-22 14:58:31 +00:00
|
|
|
/* get list of "paramId" */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_index_get_string(index, "paramId", paramId, ¶mIdSize), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* get size of ensemble number list */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_index_get_size(index, "number", &numberSize), 0);
|
|
|
|
printf("GRIB contains %lu different ensemble members\n", numberSize);
|
2014-09-22 14:58:31 +00:00
|
|
|
/* allocate memory for ensemble number list */
|
2020-01-22 14:57:43 +00:00
|
|
|
number = (long*)malloc(numberSize * sizeof(long));
|
2014-09-22 14:58:31 +00:00
|
|
|
/* get list of ensemble numbers */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_index_get_long(index, "number", number, &numberSize), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* select T850 with paramId 130 */
|
2014-12-23 10:16:21 +00:00
|
|
|
CODES_CHECK(codes_index_select_string(index, "paramId", "130"), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* loop over all members */
|
|
|
|
for (i = 0; i < numberSize; i++) {
|
|
|
|
count++;
|
|
|
|
/* select an individual ensemble number */
|
2014-12-23 10:16:21 +00:00
|
|
|
CODES_CHECK(codes_index_select_long(index, "number", number[i]), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
2014-09-22 15:51:27 +00:00
|
|
|
/* create handle for next GRIB message */
|
2020-01-22 14:57:43 +00:00
|
|
|
h = codes_handle_new_from_index(index, &ret);
|
2014-09-22 14:58:31 +00:00
|
|
|
if (ret) {
|
2014-12-23 10:16:21 +00:00
|
|
|
printf("Error: %s\n", codes_get_error_message(ret));
|
2014-09-22 14:58:31 +00:00
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the size of the values array */
|
2014-12-23 10:16:21 +00:00
|
|
|
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
2014-09-22 15:51:27 +00:00
|
|
|
/* allocate memory for the GRIB message */
|
2014-10-01 16:43:23 +00:00
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* allocate memory for result */
|
2020-01-22 14:57:43 +00:00
|
|
|
if (i == 0) {
|
|
|
|
result = (double*)calloc(values_len, sizeof(double));
|
2014-09-22 14:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get data values */
|
2014-12-23 10:16:21 +00:00
|
|
|
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
/* add up values */
|
|
|
|
for (j = 0; j < values_len; j++)
|
|
|
|
result[j] = result[j] + values[j];
|
|
|
|
|
2014-09-22 15:51:27 +00:00
|
|
|
/* free memory and GRIB message handle */
|
2014-09-22 14:58:31 +00:00
|
|
|
free(values);
|
2014-12-23 10:16:21 +00:00
|
|
|
CODES_CHECK(codes_handle_delete(h), 0);
|
2014-09-22 14:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("We considered %d ensemble members\n", count);
|
|
|
|
|
|
|
|
for (j = 0; j < values_len; j++)
|
|
|
|
result[j] = result[j] / count;
|
|
|
|
|
|
|
|
for (j = 0; j < values_len; j++) {
|
|
|
|
if (min > result[j]) {
|
|
|
|
min = result[j];
|
|
|
|
}
|
|
|
|
if (max < result[j]) {
|
|
|
|
max = result[j];
|
|
|
|
}
|
|
|
|
avg += result[j];
|
|
|
|
}
|
2020-01-22 14:57:43 +00:00
|
|
|
avg = avg / values_len;
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
printf("==============================================================================\n");
|
|
|
|
printf("Stats for ensemble mean of T850\n");
|
2020-01-22 14:57:43 +00:00
|
|
|
printf("Min: %f Max: %f Avg: %f\n", min, max, avg);
|
2014-09-22 14:58:31 +00:00
|
|
|
printf("==============================================================================\n");
|
|
|
|
|
|
|
|
/* finally free all other memory */
|
2020-01-22 14:57:43 +00:00
|
|
|
for (i = 0; i < paramIdSize; i++)
|
2014-09-22 14:58:31 +00:00
|
|
|
free(paramId[i]);
|
|
|
|
free(paramId);
|
|
|
|
free(number);
|
|
|
|
free(result);
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_index_delete(index);
|
2014-09-22 14:58:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|