Skip to content

Commit

Permalink
GribHandle starting from an offset
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisspyB committed Feb 19, 2024
1 parent bf90fa5 commit e02d9fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/metkit/codes/GribHandle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ GribHandle::GribHandle(eckit::DataHandle& handle):
handle_ = h;
}

GribHandle::GribHandle(eckit::DataHandle& handle, eckit::Offset offset):
handle_(nullptr),
owned_(true) {

codes_handle* h = nullptr;
int err = 0;

FILE* f = handle.openf();
ASSERT(f);

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

CODES_CALL(err);
ASSERT(h);
handle_ = h;

// XXX: Part of a workaround to sync handle and f on linux.
// XXX: This needs to be investigated further.
handle.seek(ftello(f));
fclose(f); // XXX: The other constructor does not close the file, why do we?
}

GribHandle::~GribHandle() noexcept(false) {
if (handle_ && owned_) {
CODES_CALL(codes_handle_delete(handle_));
Expand Down
3 changes: 3 additions & 0 deletions src/metkit/codes/GribHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class GribHandle : private eckit::NonCopyable {
/// constructor creating a grib_handle from a DataHandle
explicit GribHandle(eckit::DataHandle&);

// Constructor creating a grib_handle from a DataHandle starting from a given offset
explicit GribHandle(eckit::DataHandle&, eckit::Offset);

/// destructor will delete the grib_handle if we own it
~GribHandle() noexcept(false);

Expand Down

0 comments on commit e02d9fb

Please sign in to comment.