eccodes/examples/C/grib_list.c

103 lines
3.1 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.
*/
/*
2015-03-18 14:35:13 +00:00
* C Implementation: grib_list
2013-03-25 12:04:10 +00:00
*
* Description: how to get values using keys.
*
*/
#include <stdio.h>
#include <assert.h>
#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)
{
int err = 0;
size_t i = 0;
size_t count;
size_t size;
long numberOfContributingSpectralBands;
long values[1024];
long new_values[1024];
2020-01-22 14:57:43 +00:00
FILE* in = NULL;
2017-06-21 18:21:58 +00:00
const char* filename = "../../data/satellite.grib";
2020-01-22 14:57:43 +00:00
codes_handle* h = NULL;
2014-06-18 16:14:01 +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 input file %s\n", filename);
2014-06-18 16:14:01 +00:00
return 1;
}
/* 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
}
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "numberOfContributingSpectralBands", &numberOfContributingSpectralBands), 0);
2014-06-18 16:14:01 +00:00
assert(numberOfContributingSpectralBands == 3);
2021-11-30 12:44:37 +00:00
/* shrink NB to 2 */
2014-06-18 16:14:01 +00:00
numberOfContributingSpectralBands = 2;
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long(h, "numberOfContributingSpectralBands", numberOfContributingSpectralBands), 0);
2014-06-18 16:14:01 +00:00
2021-11-30 12:44:37 +00:00
/* expand NB to 9 */
2014-06-18 16:14:01 +00:00
numberOfContributingSpectralBands = 9;
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long(h, "numberOfContributingSpectralBands", numberOfContributingSpectralBands), 0);
2014-06-18 16:14:01 +00:00
/* get as a long*/
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "numberOfContributingSpectralBands", &numberOfContributingSpectralBands), 0);
printf("numberOfContributingSpectralBands=%ld\n", numberOfContributingSpectralBands);
2014-06-18 16:14:01 +00:00
/* get as a long*/
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_size(h, "scaledValueOfCentralWaveNumber", &count), 0);
printf("count=%zu\n", count);
2014-06-18 16:14:01 +00:00
2020-01-22 14:57:43 +00:00
assert(count < sizeof(values) / sizeof(values[0]));
2014-06-18 16:14:01 +00:00
size = count;
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long_array(h, "scaledValueOfCentralWaveNumber", values, &size), 0);
2014-06-18 16:14:01 +00:00
assert(size == count);
2020-01-22 14:57:43 +00:00
for (i = 0; i < count; i++) {
printf("scaledValueOfCentralWaveNumber %zu = %ld\n", i, values[i]);
2020-01-22 14:57:43 +00:00
if (i == 0) assert(values[i] == 26870);
if (i == 1) assert(values[i] == 9272);
2014-06-18 16:14:01 +00:00
}
2020-01-22 14:57:43 +00:00
for (i = 0; i < count; i++)
values[i] = i + 1000;
2014-06-18 16:14:01 +00:00
size = count;
/* size--; */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long_array(h, "scaledValueOfCentralWaveNumber", values, size), 0);
2014-06-18 16:14:01 +00:00
assert(size == count);
/* check what we set */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long_array(h, "scaledValueOfCentralWaveNumber", new_values, &size), 0);
2014-06-18 16:14:01 +00:00
assert(size == count);
2020-01-22 14:57:43 +00:00
for (i = 0; i < count; i++) {
printf("Now scaledValueOfCentralWaveNumber %zu = %ld\n", i, new_values[i]);
2020-01-22 14:57:43 +00:00
assert(new_values[i] == (i + 1000));
2014-06-18 16:14:01 +00:00
}
codes_handle_delete(h);
2014-06-18 16:14:01 +00:00
fclose(in);
return 0;
2013-03-25 12:04:10 +00:00
}