2015-03-25 17:39:58 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2015-03-25 17:39:58 +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: bufr_read_scatterometer
|
|
|
|
*
|
|
|
|
* Description: how to read data for a given beam from scatterometer BUFR messages.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-02-06 16:46:28 +00:00
|
|
|
/*
|
2015-03-25 17:39:58 +00:00
|
|
|
* Please note that scatterometer data can be encoded in various ways in BUFR. Therefore the code
|
|
|
|
* below might not work directly for other types of messages than the one used in the
|
|
|
|
* example. It is advised to use bufr_dump to understand the structure of the messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "eccodes.h"
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
int main(int argc, char* argv[])
|
2015-03-25 17:39:58 +00:00
|
|
|
{
|
|
|
|
FILE* in = NULL;
|
|
|
|
|
|
|
|
/* 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-25 17:39:58 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
double *lat = NULL, *lon = NULL, *bscatter = NULL;
|
|
|
|
long numObs = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
int i, err = 0;
|
|
|
|
int cnt = 0;
|
2015-06-01 15:59:48 +00:00
|
|
|
const char* infile = "../../data/bufr/asca_139.bufr";
|
2015-03-25 17:39:58 +00:00
|
|
|
char key_name[128];
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
in = fopen(infile, "rb");
|
2015-03-25 17:39:58 +00:00
|
|
|
if (!in) {
|
|
|
|
printf("ERROR: unable to open file %s\n", infile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
2015-03-25 17:39:58 +00:00
|
|
|
if (h == NULL) {
|
2020-01-22 14:57:43 +00:00
|
|
|
printf("Error: unable to create handle for message %d\n", cnt);
|
2015-03-25 17:39:58 +00:00
|
|
|
cnt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
printf("message: %d\n", cnt);
|
2015-03-25 17:39:58 +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);
|
2017-02-06 16:46:28 +00:00
|
|
|
|
2015-03-25 17:39:58 +00:00
|
|
|
/* The BUFR file contains a single message with 2016 subsets in a compressed form.
|
|
|
|
* It means each subset has exactly the same structure: they store one location with
|
2017-02-06 16:46:28 +00:00
|
|
|
* several beams and one backscatter value in each beam.
|
|
|
|
*
|
|
|
|
* To print the backScatter values for beamIdentifier=2 from all the subsets
|
2015-03-25 17:39:58 +00:00
|
|
|
* we will simply access the key by condition (see below) */
|
2017-02-06 16:46:28 +00:00
|
|
|
|
2015-03-25 17:39:58 +00:00
|
|
|
/* Get the total number of subsets. */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numObs), 0);
|
2015-03-25 17:39:58 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
printf("Number of values: %ld\n", numObs);
|
2017-02-06 16:46:28 +00:00
|
|
|
|
2015-03-25 17:39:58 +00:00
|
|
|
/* Get latitude */
|
2020-01-22 14:57:43 +00:00
|
|
|
sprintf(key_name, "latitude");
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Check the size (including all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
|
|
|
|
if (len != numObs) {
|
|
|
|
printf("inconsistent number of %s values found!\n", key_name);
|
2015-03-25 17:39:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:54:32 +00:00
|
|
|
/* Allocate memory for the values to be read. Each
|
|
|
|
* parameter must have the same number of values. */
|
2020-01-22 14:57:43 +00:00
|
|
|
lat = (double*)malloc(numObs * sizeof(double));
|
2018-07-12 16:54:32 +00:00
|
|
|
|
2015-03-25 17:39:58 +00:00
|
|
|
/* Get the values (from all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_double_array(h, key_name, lat, &len), 0);
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Get longitude */
|
2020-01-22 14:57:43 +00:00
|
|
|
sprintf(key_name, "longitude");
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Check the size (including all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
|
|
|
|
if (len != numObs) {
|
|
|
|
printf("inconsistent number of %s values found!\n", key_name);
|
2015-03-25 17:39:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the values (from all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
lon = (double*)malloc(numObs * sizeof(double));
|
|
|
|
CODES_CHECK(codes_get_double_array(h, key_name, lon, &len), 0);
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Get backScatter for beam two. We use an access by condition for this key. */
|
2020-01-22 14:57:43 +00:00
|
|
|
sprintf(key_name, "/beamIdentifier=2/backscatter");
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Check the size (including all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
|
|
|
|
if (len != numObs) {
|
|
|
|
printf("inconsistent number of %s values found!\n", key_name);
|
2015-03-25 17:39:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the values (from all the subsets) */
|
2020-01-22 14:57:43 +00:00
|
|
|
bscatter = (double*)malloc(numObs * sizeof(double));
|
|
|
|
CODES_CHECK(codes_get_double_array(h, key_name, bscatter, &len), 0);
|
2015-03-25 17:39:58 +00:00
|
|
|
|
|
|
|
/* Print the values */
|
|
|
|
printf("pixel lat lon backscatter \n");
|
|
|
|
printf("-------------------------------\n");
|
2020-01-22 14:57:43 +00:00
|
|
|
for (i = 0; i < numObs; i++) {
|
|
|
|
printf("%4d %.3f %.3f %.3f \n", i + 1, lat[i], lon[i], bscatter[i]);
|
2015-03-25 17:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete handle */
|
|
|
|
codes_handle_delete(h);
|
|
|
|
|
|
|
|
/* Release memory */
|
|
|
|
free(lat);
|
|
|
|
free(lon);
|
|
|
|
free(bscatter);
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
return 0;
|
|
|
|
}
|