eccodes/examples/C/bufr_attributes.c

141 lines
5.4 KiB
C
Raw Permalink Normal View History

/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
*
* 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: bufr_attributes
*
* Description: how to read attributes of keys in BUFR messages.
*
*/
#include "eccodes.h"
2020-01-22 14:57:43 +00:00
int main(int argc, char* argv[])
{
FILE* in = NULL;
2015-03-17 13:36:47 +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;
2015-03-17 13:36:47 +00:00
2020-01-22 14:57:43 +00:00
char* units = NULL;
char* unitsPercent = NULL;
long longVal;
double doubleVal;
2020-01-22 14:57:43 +00:00
size_t len = 0;
int err = 0;
int cnt = 0;
2017-06-21 18:21:58 +00:00
const char* infile = "../../data/bufr/syno_multi.bufr";
2020-01-22 14:57:43 +00:00
in = fopen(infile, "rb");
if (!in) {
2020-05-14 19:21:31 +00:00
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
2015-03-17 13:36:47 +00:00
2021-11-13 17:40:27 +00:00
/* loop over the messages in the BUFR file */
2020-01-22 14:57:43 +00:00
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
2020-05-14 19:21:31 +00:00
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
2015-03-17 13:36:47 +00:00
2020-01-22 14:57:43 +00:00
printf("message: %d\n", cnt);
2015-03-17 13:36:47 +00:00
2017-02-06 16:46:28 +00:00
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
2015-03-17 13:36:47 +00:00
/* ----------------------------------------------------------------
We will read the value and all the attributes available for
the 2m temperature.
2017-02-06 16:46:28 +00:00
-------------------------------------------------------------------*/
2015-03-17 13:36:47 +00:00
/* get the value as double */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal), 0);
printf(" airTemperatureAt2M: %.2f\n", doubleVal);
2015-03-17 13:36:47 +00:00
/* get the element's code (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->code", &longVal), 0);
printf(" airTemperatureAt2M->code: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's units (see BUFR code table B) */
2015-03-17 13:36:47 +00:00
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "airTemperatureAt2M->units", &len), 0);
2020-01-22 14:57:43 +00:00
units = (char*)malloc(len * sizeof(char));
2015-03-17 13:36:47 +00:00
/* get the values*/
codes_get_string(h, "airTemperatureAt2M->units", units, &len);
printf(" airTemperatureAt2M->units: %s\n", units);
2015-03-17 13:36:47 +00:00
/* get the element's scale (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->scale", &longVal), 0);
printf(" airTemperatureAt2M->scale: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's reference (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->reference", &longVal), 0);
printf(" airTemperatureAt2M->reference: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's width (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->width", &longVal), 0);
printf(" airTemperatureAt2M->width: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
2017-02-06 16:46:28 +00:00
/* -----------------------------------------------------------------
The 2m temperature data element in this message has an associated
field: percentConfidence. Its value and attributes can be accessed
in a similar manner as was shown above for 2m temperature.
----------------------------------------------------------------- */
2015-03-17 13:36:47 +00:00
/* get the value as long */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's code (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->code", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->code: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's units (see BUFR code table B) */
2015-03-17 13:36:47 +00:00
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "airTemperatureAt2M->percentConfidence->units", &len), 0);
2020-01-22 14:57:43 +00:00
unitsPercent = (char*)malloc(len * sizeof(char));
2015-03-17 13:36:47 +00:00
/* get the values*/
codes_get_string(h, "airTemperatureAt2M->percentConfidence->units", unitsPercent, &len);
printf(" airTemperatureAt2M->percentConfidence->units: %s\n", unitsPercent);
2015-03-17 13:36:47 +00:00
/* get the element's scale (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->scale", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->scale: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's reference (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->reference", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->reference: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
/* get the element's width (see BUFR code table B) */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->width", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->width: %ld\n", longVal);
2015-03-17 13:36:47 +00:00
2017-02-06 16:46:28 +00:00
/* free allocated arrays */
free(units);
free(unitsPercent);
2015-03-17 13:36:47 +00:00
/* delete handle */
codes_handle_delete(h);
2015-03-17 13:36:47 +00:00
cnt++;
}
2015-03-17 13:36:47 +00:00
fclose(in);
return 0;
}