File: grib_bpv_limit.cc

package info (click to toggle)
eccodes 2.45.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,456 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 278; xml: 183; awk: 66
file content (111 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * (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.
 */

/*
 *  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
 */

#include "grib_api_internal.h"

static double compare_double_absolute(double a, double b, double tolerance)
{
    double ret = 0;
    double d   = fabs(a - b);
    if (d > tolerance) ret = d;
    return ret;
}

static int check_error_code(int err)
{
    if (err == GRIB_INVALID_BPV || err == GRIB_DECODING_ERROR)
        return GRIB_SUCCESS;
    return GRIB_INVALID_ARGUMENT;
}

int main(int argc, char** argv)
{
    int err = 0, i;
    size_t values_len = 0;
    const double tolerance = 1e-5;
    size_t slong = sizeof(long) * 8;

    ECCODES_ASSERT(argc == 2);
    char* filename = argv[1];

    for (i = 0; i < 255; i++) {
        FILE* in = fopen(filename, "rb");
        ECCODES_ASSERT(in);
        grib_handle* h = grib_handle_new_from_file(0, in, &err);
        ECCODES_ASSERT(h);

        /* get the size of the values array*/
        GRIB_CHECK(grib_get_size(h, "values", &values_len), 0);

        double* values = (double*)malloc(values_len * sizeof(double));

        err = grib_get_double_array(h, "values", values, &values_len);
        if (compare_double_absolute(values[0], 2.7900000000e+02, tolerance) != 0)
            printf("oops i=%d  values[0]=%g\n", i, values[0]);
        if (compare_double_absolute(values[1], 2.7996093750e+02, tolerance) != 0)
            printf("oops i=%d  values[1]=%g\n", i, values[1]);

        GRIB_CHECK(grib_set_long(h, "bitsPerValue", i), 0);

        err = grib_set_double_array(h, "values", values, values_len);

        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 */
        }
        else if (i >= slong && check_error_code(err) == GRIB_SUCCESS) {
            /* do nothing  */
        }
        else {
            fprintf(stderr, "Error decoding when bpv=%d. Error message:%s\n", i, grib_get_error_message(err));
            return 1;
        }

        values = (double*)malloc(values_len * sizeof(double));
        err    = grib_get_double_array(h, "values", values, &values_len);

        /*
         * 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 */
        }
        else if (i >= slong && check_error_code(err) == GRIB_SUCCESS) {
            /* do nothing  */
        }
        else {
            fprintf(stderr, "Error decoding when bpv=%d. Error message:%s\n", i, grib_get_error_message(err));
            return 1;
        }

        free(values);
        values = 0;
        grib_handle_delete(h);
        h = 0;
        fclose(in);
    }

    grib_context_delete(grib_context_get_default());
    return 0;
}