File: grib_from_gridspec.cc

package info (click to toggle)
eccodes 2.46.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 154,956 kB
  • sloc: cpp: 163,970; ansic: 26,310; sh: 22,006; f90: 6,854; perl: 6,361; python: 5,352; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 279; xml: 183; awk: 66
file content (214 lines) | stat: -rw-r--r-- 6,846 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * (C) Copyright 2025- 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.
 */


#include <vector>

#include "eckit/geo/Exceptions.h"
#include "eckit/spec/Custom.h"
#include "eckit/testing/Test.h"
#include "eckit/types/FloatCompare.h"

#include "eccodes/eccodes.h"
#include "eccodes/geo/GribFromSpec.h"

#define CHECK(a) CODES_CHECK(a, nullptr)


bool get_string(const grib_handle* h, const char* key, std::string& value)
{
    char buffer[10240];
    size_t size = sizeof(buffer);
    ASSERT(h);
    int err = codes_get_string(h, key, buffer, &size);

    if (err == CODES_NOT_FOUND) {
        return false;
    }

    ASSERT(err == 0);

    value = buffer;
    return true;
}


void set_string(grib_handle* h, const char* key, const std::string& value)
{
    size_t length = value.length();
    CHECK(codes_set_string(h, key, value.c_str(), &length));
}


CASE("grid: O2")
{
    const ::eckit::spec::Custom spec{ { "grid", "o2" } };

    for (const auto* name : {
             "GRIB1",
             "GRIB2",
         }) {
        auto* sample = codes_grib_handle_new_from_samples(nullptr, name);
        EXPECT(sample != nullptr);

        auto* h = eccodes::geo::GribFromSpec::set(sample, spec);
        EXPECT(h != nullptr);

        long valid = 0;
        set_string(h, "messageValidityChecks", "grid");
        CHECK(codes_get_long(h, "isMessageValid", &valid));
        EXPECT(valid == 1);

        std::string type;
        get_string(h, "gridType", type);
        EXPECT(type == "reduced_gg");

        std::string gridName;
        get_string(h, "gridName", gridName);
        EXPECT(gridName == "O2");

        long N = 0;
        CHECK(codes_get_long(h, "N", &N));
        EXPECT(N == 2);

        long numberOfDataPoints = 0;
        CHECK(codes_get_long(h, "numberOfDataPoints", &numberOfDataPoints));
        EXPECT(numberOfDataPoints == 88);

        size_t pl_size = 0;
        CHECK(codes_get_size(h, "pl", &pl_size));

        std::vector<long> pl(pl_size, 0);
        auto size = pl_size;
        CHECK(codes_get_long_array(h, "pl", pl.data(), &size));
        ASSERT(pl_size == size);

        std::vector<long> pl_expected{ 20L, 24, 24, 20 };
        EXPECT(pl == pl_expected);

        std::vector<double> area(4);
        CHECK(codes_get_double(h, "latitudeOfFirstGridPointInDegrees", &area[0]));
        CHECK(codes_get_double(h, "longitudeOfFirstGridPointInDegrees", &area[1]));
        CHECK(codes_get_double(h, "latitudeOfLastGridPointInDegrees", &area[2]));
        CHECK(codes_get_double(h, "longitudeOfLastGridPointInDegrees", &area[3]));

        EXPECT(eckit::types::is_strictly_greater(90., area[0]));
        EXPECT(eckit::types::is_approximately_equal(area[1], 0.));
        EXPECT(eckit::types::is_strictly_greater(area[2], -90.));
        EXPECT(eckit::types::is_strictly_greater(360., area[3]));

        codes_handle_delete(h);
    }
}


CASE("grid: 1/1")
{
    const ::eckit::spec::Custom spec{ { "grid", "1/1" } };

    for (const auto* name : {
             "GRIB1",
             "GRIB2",
         }) {
        auto* sample = codes_grib_handle_new_from_samples(nullptr, name);
        EXPECT(sample != nullptr);

        auto* h = eccodes::geo::GribFromSpec::set(sample, spec);
        EXPECT(h != nullptr);

        long valid = 0;
        set_string(h, "messageValidityChecks", "grid");
        CHECK(codes_get_long(h, "isMessageValid", &valid));
        EXPECT(valid == 1);

        std::string type;
        get_string(h, "gridType", type);
        EXPECT(type == "regular_ll");

        long Ni                 = 0;
        long Nj                 = 0;
        long numberOfDataPoints = 0;
        CHECK(codes_get_long(h, "Ni", &Ni));
        CHECK(codes_get_long(h, "Nj", &Nj));
        CHECK(codes_get_long(h, "numberOfDataPoints", &numberOfDataPoints));

        EXPECT(Ni * Nj == numberOfDataPoints);

        std::vector<double> area(4);
        CHECK(codes_get_double(h, "latitudeOfFirstGridPointInDegrees", &area[0]));
        CHECK(codes_get_double(h, "longitudeOfFirstGridPointInDegrees", &area[1]));
        CHECK(codes_get_double(h, "latitudeOfLastGridPointInDegrees", &area[2]));
        CHECK(codes_get_double(h, "longitudeOfLastGridPointInDegrees", &area[3]));

        EXPECT(eckit::types::is_approximately_equal(area[0], 90.));
        EXPECT(eckit::types::is_approximately_equal(area[1], 0.));
        EXPECT(eckit::types::is_approximately_equal(area[2], -90.));
        EXPECT(eckit::types::is_approximately_equal(area[3], 360. - 1., 0.5 * 1e-6));

        codes_handle_delete(h);
    }
}


CASE("grid: reduced_ll")
{
    const ::eckit::spec::Custom spec{ { "type", "reduced_ll" }, { "pl", std::vector<long>{ 0, 10, 0 } }, { "area", std::vector<double>{ 90., 0., -90., 360. } } };

    for (const auto* name : {
             "GRIB1",
             "GRIB2",
         }) {
        auto* sample = codes_grib_handle_new_from_samples(nullptr, name);
        EXPECT(sample != nullptr);

        auto* h = eccodes::geo::GribFromSpec::set(sample, spec);
        EXPECT(h != nullptr);

        long valid = 0;
        set_string(h, "messageValidityChecks", "grid");
        CHECK(codes_get_long(h, "isMessageValid", &valid));
        EXPECT(valid == 1);

        std::string type;
        get_string(h, "gridType", type);
        EXPECT(type == "reduced_ll");

        long Nj                 = 3;
        long numberOfDataPoints = 10;
        CHECK(codes_get_long(h, "Nj", &Nj));
        CHECK(codes_get_long(h, "numberOfDataPoints", &numberOfDataPoints));

        std::vector<double> area(4);
        CHECK(codes_get_double(h, "latitudeOfFirstGridPointInDegrees", &area[0]));
        CHECK(codes_get_double(h, "longitudeOfFirstGridPointInDegrees", &area[1]));
        CHECK(codes_get_double(h, "latitudeOfLastGridPointInDegrees", &area[2]));
        CHECK(codes_get_double(h, "longitudeOfLastGridPointInDegrees", &area[3]));

        EXPECT(eckit::types::is_approximately_equal(area[0], 90.));
        EXPECT(eckit::types::is_approximately_equal(area[1], 0.));
        EXPECT(eckit::types::is_approximately_equal(area[2], -90.));
        EXPECT(eckit::types::is_approximately_equal(area[3], 324.));

        codes_handle_delete(h);
    }
}


int main(int argc, char* argv[])
{
    const char* ev_name = "ECCODES_ECKIT_GEO";
    const char* ev_val = getenv(ev_name);
    if (ev_val && atol(ev_val) != 0) {
        return eckit::testing::run_tests(argc, argv);
    }
    printf("%s: This test is disabled (env. variable %s is not set)", argv[0], ev_name);
    return 0;
}