Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gribhandle on Mac for file with multiple messages #45

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/metkit/codes/GribHandle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ GribHandle::GribHandle(eckit::DataHandle& handle, eckit::Offset offset):
FILE* f = handle.openf();
ASSERT(f);

handle.seek(offset);
fseek(f, offset, SEEK_SET);

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

Expand Down
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ endif()

ecbuild_get_test_multidata( TARGET grib_get_data
DIRNAME grib_api/data
NAMES latlon.grib )
NAMES latlon.grib
synthetic_2msgs.grib )

ecbuild_get_test_multidata( TARGET metkit_get_odb_data
NAMES multiodb.odb
Expand Down Expand Up @@ -61,6 +62,15 @@ ecbuild_add_test( TARGET metkit_test_odbsplitter
NO_AS_NEEDED
LIBS metkit )

ecbuild_add_test( TARGET metkit_test_gribhandle
CONDITION HAVE_GRIB
SOURCES test_gribhandle.cc
INCLUDES "${ECKIT_INCLUDE_DIRS}" "${ECCODES_INCLUDE_DIR}"
LIBS metkit
TEST_DEPENDS grib_get_data
NO_AS_NEEDED
ENVIRONMENT "${metkit_env}")

list(APPEND testFileSuffixes typesfactory expand param_axis steprange_axis time hypercube type_levelist )

foreach(test IN LISTS testFileSuffixes)
Expand Down
67 changes: 67 additions & 0 deletions tests/test_gribhandle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* (C) Copyright 1996- 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.
*/

/// @date Apr 2024
/// @author Christopher Bradley

#include "eccodes.h"

#include "eckit/testing/Test.h"
#include "eckit/io/FileHandle.h"

#include "metkit/codes/GribHandle.h"
#include "metkit/codes/GribAccessor.h"

using namespace eckit::testing;

namespace metkit {
namespace grib {
namespace test {

//-----------------------------------------------------------------------------

// Test that a gribhandle will point to the correct message in a file, given an offset.
CASE( "File with two messages" ) {

// The test file has two messages of different packing types, with some junk data in between.
eckit::PathName path("synthetic_2msgs.grib");

off_t* offsets;
grib_context* c = nullptr;
int n = 0;
int err = codes_extract_offsets_malloc(c, path.asString().c_str(), PRODUCT_GRIB, &offsets, &n, 1);
EXPECT(!err);
EXPECT(n == 2);

GribAccessor<std::string> packingType("packingType");
std::vector<std::string> expected = {"grid_simple", "grid_ccsds"};

eckit::FileHandle dh(path);
dh.openForRead();

for (int i = 0; i < n; i++) {
GribHandle h(dh, offsets[i]);
EXPECT(packingType(h) == expected[i]);
}

free(offsets);
dh.close();
}

//-----------------------------------------------------------------------------

} // namespace test
} // namespace grib
} // namespace metkit

int main(int argc, char **argv)
{
return run_tests ( argc, argv );
}
Loading