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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-08-02 10:07:57 +00:00
|
|
|
* Description: test setting the number of bits per value before decoding and then encoding;
|
|
|
|
* setting the wrong number of bpv should result in an error
|
2013-03-25 12:04:10 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-01 11:00:29 +00:00
|
|
|
#include "grib_api_internal.h"
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
static double compare_double_absolute(double a, double b, double tolerance)
|
2019-08-02 10:07:57 +00:00
|
|
|
{
|
2020-01-29 12:05:02 +00:00
|
|
|
double ret = 0;
|
|
|
|
double d = fabs(a - b);
|
2019-08-02 10:07:57 +00:00
|
|
|
if (d > tolerance) {
|
2020-01-29 12:05:02 +00:00
|
|
|
ret = d;
|
2019-08-02 10:07:57 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
static void usage(const char* prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s input\n", prog);
|
2019-08-02 10:07:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2019-08-02 10:07:57 +00:00
|
|
|
static int check_error_code(int err)
|
|
|
|
{
|
|
|
|
if (err == GRIB_INVALID_BPV || err == GRIB_DECODING_ERROR)
|
|
|
|
return GRIB_SUCCESS;
|
|
|
|
return GRIB_INVALID_ARGUMENT;
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int err = 0, i;
|
|
|
|
size_t values_len = 0;
|
|
|
|
double* values = NULL;
|
|
|
|
char error_msg[100];
|
2019-08-02 10:07:57 +00:00
|
|
|
const double tolerance = 1e-5;
|
2020-01-29 12:05:02 +00:00
|
|
|
size_t slong = sizeof(long) * 8;
|
|
|
|
grib_handle* h;
|
|
|
|
char* filename;
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if (argc < 2) usage(argv[0]);
|
|
|
|
filename = argv[1];
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < 255; i++) {
|
|
|
|
FILE* in = fopen(filename, "rb");
|
2022-08-01 11:00:29 +00:00
|
|
|
Assert(in);
|
2020-01-29 12:05:02 +00:00
|
|
|
h = grib_handle_new_from_file(0, in, &err);
|
2022-08-01 11:00:29 +00:00
|
|
|
Assert(h);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
|
|
|
/* get the size of the values array*/
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_get_size(h, "values", &values_len), 0);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
err = grib_get_double_array(h, "values", values, &values_len);
|
2019-08-02 10:07:57 +00:00
|
|
|
if (compare_double_absolute(values[0], 2.7900000000e+02, tolerance) != 0)
|
2020-01-29 12:05:02 +00:00
|
|
|
printf("oops i=%d values[0]=%g\n", i, values[0]);
|
2019-08-02 10:07:57 +00:00
|
|
|
if (compare_double_absolute(values[1], 2.7996093750e+02, tolerance) != 0)
|
2020-01-29 12:05:02 +00:00
|
|
|
printf("oops i=%d values[1]=%g\n", i, values[1]);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_long(h, "bitsPerValue", i), 0);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
err = grib_set_double_array(h, "values", values, values_len);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
|
|
|
free(values);
|
|
|
|
values = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if encoding of the data fails when the number of bpv
|
|
|
|
* is not supported; bpv allowed on decoding is bpv < size of long
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i < slong && err == 0) {
|
|
|
|
/* do nothing */
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else if (i >= slong && check_error_code(err) == GRIB_SUCCESS) {
|
2019-08-02 10:07:57 +00:00
|
|
|
/* do nothing */
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-10 19:18:43 +00:00
|
|
|
snprintf(error_msg, sizeof(error_msg), "Error decoding when bpv=%d. Error message:%s", i, grib_get_error_message(err));
|
2019-08-02 10:07:57 +00:00
|
|
|
perror(error_msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
values = (double*)malloc(values_len * sizeof(double));
|
|
|
|
err = grib_get_double_array(h, "values", values, &values_len);
|
2019-08-02 10:07:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check if decoding of the data fails when the number of bpv
|
|
|
|
* is not supported; bpv allowed on decoding is bpv <= size of long
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i < slong && err == 0) {
|
|
|
|
/* do nothing */
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else if (i >= slong && check_error_code(err) == GRIB_SUCCESS) {
|
2019-08-02 10:07:57 +00:00
|
|
|
/* do nothing */
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-10 19:18:43 +00:00
|
|
|
snprintf(error_msg, sizeof(error_msg), "Error decoding when bpv=%d. Error message:%s", i, grib_get_error_message(err));
|
2019-08-02 10:07:57 +00:00
|
|
|
perror(error_msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
free(values);
|
|
|
|
values = 0;
|
|
|
|
grib_handle_delete(h);
|
|
|
|
h = 0;
|
2019-08-02 10:07:57 +00:00
|
|
|
fclose(in);
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 16:47:23 +00:00
|
|
|
grib_context_delete(grib_context_get_default());
|
2019-08-02 10:07:57 +00:00
|
|
|
return 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|