eccodes/tests/grib_ecc-1467.cc

85 lines
3.1 KiB
C++
Raw Permalink Normal View History

2022-12-30 15:53:10 +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.
*/
2023-03-13 19:57:36 +00:00
2024-03-04 20:45:39 +00:00
//
// ECC-1467: Support data values array decoded as "floats" (single-precision)
//
#include <math.h>
2022-12-30 15:53:10 +00:00
#include "eccodes.h"
#include "grib_api_internal.h"
2022-12-30 15:53:10 +00:00
int main(int argc, char** argv)
{
int err = 0;
2023-03-13 19:57:36 +00:00
float* fvalues = NULL; // data values as floats
double* dvalues = NULL; // data values as doubles
size_t values_len = 0; // number of data points
size_t i = 0;
2022-12-30 15:53:10 +00:00
double abs_error = 0;
2023-03-13 19:57:36 +00:00
const double max_abs_error = 1e-03;
const double tolerance = 1e-03;
2024-01-19 12:36:30 +00:00
double dmin, dmax, dval;
float fval;
2022-12-30 15:53:10 +00:00
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(argc == 2);
2024-03-04 18:52:22 +00:00
const char* filename = argv[1];
printf("Opening %s\n", filename);
2024-03-04 18:52:22 +00:00
FILE* in = fopen(filename, "rb");
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(in);
2022-12-30 15:53:10 +00:00
2024-03-04 18:52:22 +00:00
codes_handle* h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(h);
2022-12-30 15:53:10 +00:00
2024-01-19 12:36:30 +00:00
CODES_CHECK(codes_get_float(h, "referenceValue", &fval), 0);
CODES_CHECK(codes_get_double(h, "referenceValue", &dval), 0);
printf("dval = %g, fval = %g\n", dval, fval);
2022-12-30 15:53:10 +00:00
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
fvalues = (float*)malloc(values_len * sizeof(float));
dvalues = (double*)malloc(values_len * sizeof(double));
2023-03-06 14:41:23 +00:00
CODES_CHECK(codes_get_float_array(h, "values", fvalues, &values_len), 0);
2023-03-08 16:13:17 +00:00
CODES_CHECK(codes_get_double_array(h, "values", dvalues, &values_len), 0);
2022-12-30 15:53:10 +00:00
for (i = 0; i < values_len; i++) {
abs_error = fabs(dvalues[i] - (double)fvalues[i]);
if (abs_error > max_abs_error) {
2023-03-13 19:57:36 +00:00
fprintf(stderr, "ERROR:\n\tfvalue %e\n\tdvalue %e\n\terror %e\n\tmax_abs_error %e\n",
fvalues[i], dvalues[i], abs_error, max_abs_error);
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(!"Absolute error test failed\n");
2022-12-30 15:53:10 +00:00
}
dmin = dvalues[i] >= 0 ? dvalues[i] / (1 + tolerance) : dvalues[i] * (1 + tolerance);
dmax = dvalues[i] >= 0 ? dvalues[i] * (1 + tolerance) : dvalues[i] / (1 + tolerance);
fval = fvalues[i];
if (!((dmin <= fval) && (fval <= dmax))) {
2024-03-04 20:45:39 +00:00
fprintf(stderr, "Error: dvalue: %f, fvalue: %f\n", dvalues[i], fvalues[i]);
2023-05-09 12:57:31 +00:00
fprintf(stderr, "\tmin < fvalue < max = %.20e < %.20e < %.20e FAILED\n",
dmin, fvalues[i], dmax);
2023-05-09 12:57:31 +00:00
fprintf(stderr, "\tfvalue - min = %.20e (%s)\n",
fvalues[i] - dmin, fvalues[i] - dmin >= 0 ? "OK" : "FAILED (should be positive)");
2023-05-09 12:57:31 +00:00
fprintf(stderr, "\tmax - fvalue = %.20e (%s)\n",
dmax - fvalues[i], dmax - fvalues[i] >= 0 ? "OK" : "FAILED (should be positive)");
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(!"Relative tolerance test failed\n");
2022-12-30 15:53:10 +00:00
}
}
free(fvalues);
free(dvalues);
2022-12-30 15:53:10 +00:00
codes_handle_delete(h);
fclose(in);
2022-12-30 15:53:10 +00:00
return 0;
}