eccodes/tests/grib_ecc-1431.cpp

210 lines
7.2 KiB
C++
Raw Normal View History

2022-08-01 15:16:08 +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.
*/
2022-07-28 14:16:21 +00:00
#include "grib_api_internal.h"
2022-07-29 09:53:55 +00:00
#include <eccodes.h>
#include <string.h>
#include <iostream>
#include <iomanip>
2022-07-29 09:53:55 +00:00
#include <vector>
#include <string>
#include <random>
#include <limits>
2022-08-01 14:32:27 +00:00
#include <cmath>
2022-07-29 09:53:55 +00:00
typedef std::numeric_limits<double> dbl;
typedef std::numeric_limits<float> flt;
2022-07-29 09:53:55 +00:00
2022-08-01 15:16:08 +00:00
struct Range
{
double min;
double max;
};
2022-07-28 14:16:21 +00:00
2022-08-01 14:32:27 +00:00
2022-08-01 15:16:08 +00:00
//
// Test: This test doesn't accept errors after decompression.
//
void test_full()
{
std::vector<Range> ranges;
2022-08-01 15:16:08 +00:00
ranges.push_back({ flt::max() / 10, flt::max() });
ranges.push_back({ flt::max() / 100, flt::max() / 10 });
ranges.push_back({ 1e+9, 1e+10 });
ranges.push_back({ 1e+0, 1e+1 });
ranges.push_back({ 1e-1, 1e-0 });
ranges.push_back({ 1e-2, 1e-1 });
ranges.push_back({ 1e-11, 1e-10 });
ranges.push_back({ flt::min() * 10, flt::min() * 100 });
ranges.push_back({ flt::min(), flt::min() * 10 });
ranges.push_back({ flt::min(), flt::max() });
2022-07-29 09:53:55 +00:00
std::vector<size_t> values_lens = {
2022-08-01 15:16:08 +00:00
1,
10,
100,
100000,
2022-07-29 09:53:55 +00:00
};
int err;
2022-07-29 09:53:55 +00:00
std::default_random_engine re;
size_t grid_simple_values_len = 0;
2022-08-01 15:16:08 +00:00
size_t grid_ccsds_values_len = 0;
std::string packing_type = "";
size_t size = 0;
2022-07-29 09:53:55 +00:00
2022-08-01 15:16:08 +00:00
for (const size_t in_values_len : values_lens) {
for (const Range range : ranges) {
Assert(range.min < range.max);
std::cout << "Testing: "
<< "n_vals : " << std::setw(7) << in_values_len
<< " range(" << std::scientific << std::setw(12) << range.min << ", " << std::setw(12) << range.max << ") "
<< std::endl;
std::uniform_real_distribution<double>
unif(range.min, range.max);
2022-07-29 09:53:55 +00:00
2022-08-01 15:16:08 +00:00
codes_handle* handle = codes_grib_handle_new_from_samples(0, "reduced_gg_pl_128_grib2");
double* in_values = new double[in_values_len];
2022-07-29 09:53:55 +00:00
double* grid_simple_values = new double[in_values_len];
2022-08-01 15:16:08 +00:00
double* grid_ccsds_values = new double[in_values_len];
2022-07-29 09:53:55 +00:00
grid_simple_values_len = in_values_len;
2022-08-01 15:16:08 +00:00
grid_ccsds_values_len = in_values_len;
2022-07-29 09:53:55 +00:00
2022-08-01 15:16:08 +00:00
// Initialize with random values
2022-07-29 09:53:55 +00:00
for (size_t i = 0; i < in_values_len; ++i) {
in_values[i] = unif(re);
}
2022-08-01 15:16:08 +00:00
// Convert original values to quantized values, by grid_simple
2022-07-29 09:53:55 +00:00
CODES_CHECK(codes_set_double_array(handle, "values", in_values, in_values_len), 0);
CODES_CHECK(codes_get_double_array(handle, "values", grid_simple_values, &grid_simple_values_len), 0);
Assert(in_values_len == grid_simple_values_len);
2022-08-01 15:16:08 +00:00
// Test grid_ccsds
2022-07-29 09:53:55 +00:00
packing_type = "grid_ccsds";
2022-08-01 15:16:08 +00:00
size = packing_type.size();
2022-07-29 12:23:49 +00:00
CODES_CHECK(codes_set_string(handle, "packingType", packing_type.c_str(), &size), 0);
2022-07-29 09:53:55 +00:00
if ((err = codes_set_double_array(handle, "values", grid_simple_values, grid_simple_values_len)) != 0) {
Assert(!"CCSDS encoding failed");
}
if ((err = codes_get_double_array(handle, "values", grid_ccsds_values, &grid_ccsds_values_len)) != 0) {
Assert(!"CCSDS decoding failed");
}
Assert(grid_ccsds_values_len == grid_simple_values_len);
2022-08-01 15:16:08 +00:00
// Test buffers
2022-07-31 13:05:29 +00:00
for (size_t i = 0; i < grid_ccsds_values_len; ++i) {
2022-07-29 09:53:55 +00:00
if (grid_ccsds_values[i] != grid_simple_values[i]) {
std::cout.precision(dbl::max_digits10);
2022-08-01 15:16:08 +00:00
std::cout << "Test failed: " << grid_ccsds_values[i] << " != " << grid_simple_values[i] << std::endl;
2022-07-29 09:53:55 +00:00
Assert(0);
}
}
codes_handle_delete(handle);
delete[] in_values;
delete[] grid_simple_values;
delete[] grid_ccsds_values;
}
}
2022-07-28 14:16:21 +00:00
}
2022-07-29 09:53:55 +00:00
2022-08-01 14:32:27 +00:00
2022-08-01 15:16:08 +00:00
//
// Test: This test accepts tolerates errors. Values after decompression must be within a certain user defined range.
//
void test_simplified()
{
2022-08-01 14:32:27 +00:00
std::vector<Range> ranges;
2022-08-01 15:16:08 +00:00
ranges.push_back({ flt::max() / 10, flt::max() });
ranges.push_back({ flt::max() / 100, flt::max() / 10 });
ranges.push_back({ 1e+9, 1e+10 });
ranges.push_back({ 1e+0, 1e+1 });
ranges.push_back({ 1e-1, 1e-0 });
ranges.push_back({ 1e-2, 1e-1 });
ranges.push_back({ 1e-11, 1e-10 });
ranges.push_back({ flt::min() * 10, flt::min() * 100 });
ranges.push_back({ flt::min(), flt::min() * 10 });
ranges.push_back({ flt::min(), flt::max() });
2022-08-01 14:32:27 +00:00
std::vector<size_t> values_lens = {
2022-08-01 15:16:08 +00:00
1,
10,
100,
100000,
2022-08-01 14:32:27 +00:00
10000000,
};
int err;
std::default_random_engine re;
size_t grid_ccsds_values_len = 0;
2022-08-01 15:16:08 +00:00
std::string packing_type = "";
size_t size = 0;
2022-08-01 14:32:27 +00:00
2022-08-01 15:16:08 +00:00
for (const size_t in_values_len : values_lens) {
for (const Range range : ranges) {
2022-08-01 14:32:27 +00:00
Assert(range.min < range.max);
std::cout << "Testing: " << in_values_len << " " << range.min << " " << range.max << std::endl;
std::uniform_real_distribution<double> unif(range.min, range.max);
2022-08-01 15:16:08 +00:00
codes_handle* handle = codes_grib_handle_new_from_samples(0, "reduced_gg_pl_128_grib2");
double* in_values = new double[in_values_len];
2022-08-01 14:32:27 +00:00
double* grid_ccsds_values = new double[in_values_len];
grid_ccsds_values_len = in_values_len;
2022-08-01 15:16:08 +00:00
// Initialize with random values
2022-08-01 14:32:27 +00:00
for (size_t i = 0; i < in_values_len; ++i) {
in_values[i] = unif(re);
}
2022-08-01 15:16:08 +00:00
// Test grid_ccsds
2022-08-03 07:54:38 +00:00
packing_type = "grid_ccsds";
2022-08-01 15:16:08 +00:00
size = packing_type.size();
2022-08-01 14:32:27 +00:00
if ((err = codes_set_double_array(handle, "values", in_values, in_values_len)) != 0) {
Assert(!"CCSDS encoding failed");
}
CODES_CHECK(codes_set_string(handle, "packingType", packing_type.c_str(), &size), 0);
2022-08-01 14:32:27 +00:00
if ((err = codes_get_double_array(handle, "values", grid_ccsds_values, &grid_ccsds_values_len)) != 0) {
Assert(!"CCSDS decoding failed");
}
Assert(grid_ccsds_values_len == in_values_len);
2022-08-01 15:16:08 +00:00
// Test buffers
2022-08-01 14:32:27 +00:00
double tolerance = 0.000001;
for (size_t i = 0; i < grid_ccsds_values_len; ++i) {
if (!((grid_ccsds_values[i] < (in_values[i] * (1 + tolerance))) &&
2022-08-01 15:16:08 +00:00
grid_ccsds_values[i] > (in_values[i] / (1 + tolerance)))) {
2022-08-01 14:32:27 +00:00
std::cout.precision(dbl::max_digits10);
2022-08-01 15:16:08 +00:00
std::cout << "Test failed: " << grid_ccsds_values[i] << " != " << in_values[i] << std::endl;
2022-08-01 14:32:27 +00:00
Assert(0);
}
}
codes_handle_delete(handle);
delete[] in_values;
delete[] grid_ccsds_values;
}
}
}
2022-08-01 15:16:08 +00:00
int main(int argc, char** argv)
{
//test_simplified();
test_full();
2022-08-01 14:32:27 +00:00
return 0;
}