Skip to content

Commit 999a204

Browse files
committed
Fix gribhandle on Mac for file with multiple messages
1 parent 6a4ab3a commit 999a204

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/metkit/codes/GribHandle.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ GribHandle::GribHandle(eckit::DataHandle& handle, eckit::Offset offset):
9494
FILE* f = handle.openf();
9595
ASSERT(f);
9696

97-
handle.seek(offset);
97+
fseek(f, offset, SEEK_SET);
9898

9999
h = codes_handle_new_from_file(0, f, PRODUCT_GRIB, &err);
100100

tests/CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ endif()
1717

1818
ecbuild_get_test_multidata( TARGET grib_get_data
1919
DIRNAME grib_api/data
20-
NAMES latlon.grib )
20+
NAMES latlon.grib
21+
synthetic_2msgs.grib )
2122

2223
ecbuild_get_test_multidata( TARGET metkit_get_odb_data
2324
NAMES multiodb.odb
@@ -61,6 +62,15 @@ ecbuild_add_test( TARGET metkit_test_odbsplitter
6162
NO_AS_NEEDED
6263
LIBS metkit )
6364

65+
ecbuild_add_test( TARGET metkit_test_gribhandle
66+
CONDITION HAVE_GRIB
67+
SOURCES test_gribhandle.cc
68+
INCLUDES "${ECKIT_INCLUDE_DIRS}" "${ECCODES_INCLUDE_DIR}"
69+
LIBS metkit
70+
TEST_DEPENDS grib_get_data
71+
NO_AS_NEEDED
72+
ENVIRONMENT "${metkit_env}")
73+
6474
list(APPEND testFileSuffixes typesfactory expand param_axis steprange_axis time hypercube type_levelist )
6575

6676
foreach(test IN LISTS testFileSuffixes)

tests/test_gribhandle.cc

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* (C) Copyright 1996- ECMWF.
3+
*
4+
* This software is licensed under the terms of the Apache Licence Version 2.0
5+
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
* In applying this licence, ECMWF does not waive the privileges and immunities
7+
* granted to it by virtue of its status as an intergovernmental organisation
8+
* nor does it submit to any jurisdiction.
9+
*/
10+
11+
/// @date Apr 2024
12+
/// @author Christopher Bradley
13+
14+
#include "eccodes.h"
15+
16+
#include "eckit/testing/Test.h"
17+
#include "eckit/io/FileHandle.h"
18+
19+
#include "metkit/codes/GribHandle.h"
20+
#include "metkit/codes/GribAccessor.h"
21+
22+
using namespace eckit::testing;
23+
24+
namespace metkit {
25+
namespace grib {
26+
namespace test {
27+
28+
//-----------------------------------------------------------------------------
29+
30+
// Test that a gribhandle will point to the correct message in a file, given an offset.
31+
CASE( "File with two messages" ) {
32+
33+
// The test file has two messages of different packing types, with some junk data in between.
34+
eckit::PathName path("synthetic_2msgs.grib");
35+
36+
off_t* offsets;
37+
grib_context* c = nullptr;
38+
int n = 0;
39+
int err = codes_extract_offsets_malloc(c, path.asString().c_str(), PRODUCT_GRIB, &offsets, &n, 1);
40+
EXPECT(!err);
41+
EXPECT(n == 2);
42+
43+
GribAccessor<std::string> packingType("packingType");
44+
std::vector<std::string> expected = {"grid_simple", "grid_ccsds"};
45+
46+
eckit::FileHandle dh(path);
47+
dh.openForRead();
48+
49+
for (int i = 0; i < n; i++) {
50+
GribHandle h(dh, offsets[i]);
51+
EXPECT(packingType(h) == expected[i]);
52+
}
53+
54+
free(offsets);
55+
dh.close();
56+
}
57+
58+
//-----------------------------------------------------------------------------
59+
60+
} // namespace test
61+
} // namespace grib
62+
} // namespace metkit
63+
64+
int main(int argc, char **argv)
65+
{
66+
return run_tests ( argc, argv );
67+
}

0 commit comments

Comments
 (0)