Skip to content

Commit

Permalink
eckit::geo: exception handling and macros
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Dec 20, 2024
1 parent 2af6a89 commit c59047f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/geo/GeoIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ GeoIterator::GeoIterator(grib_handle* h, unsigned long flags) :
CODES_CHECK(codes_get_size(h_, "values", &nv_), "");
ECCODES_ASSERT(nv_ > 0);

data_ = (flags_ & GRIB_GEOITERATOR_NO_VALUES) ? nullptr : static_cast<double*>(grib_context_malloc(h_->context, nv_ * sizeof(double)));
ECCODES_ASSERT(data_ != nullptr);

auto size = nv_;
CODES_CHECK(codes_get_double_array(h_, "values", data_, &size), "");
ECCODES_ASSERT(nv_ == size);
//long numberOfPoints = 0;
//grib_get_long_internal(h, "numberOfPoints", &numberOfPoints);

if (flags_ & GRIB_GEOITERATOR_NO_VALUES) {
data_ = nullptr;
} else {
data_ = static_cast<double*>(grib_context_malloc(h_->context, nv_ * sizeof(double)));
ECCODES_ASSERT(data_ != nullptr);
auto size = nv_;
CODES_CHECK(codes_get_double_array(h_, "values", data_, &size), "");
// Check numberOfPoints equals nv_
// if not, throw an exception
}
}


int GeoIterator::init(grib_handle*, grib_arguments*)
{
NOTIMP;
Expand Down
7 changes: 0 additions & 7 deletions src/geo/GeoIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@

#include "eckit/geo/Grid.h"

// eccodes macros conflict with eckit
#ifdef Assert
#undef Assert
#endif

#include "geo/iterator/grib_iterator.h"


namespace eccodes::geo
{


class GeoIterator : public geo_iterator::Iterator
{
public:
Expand All @@ -47,5 +41,4 @@ class GeoIterator : public geo_iterator::Iterator
geo_iterator::Iterator* create() const override;
};


} // namespace eccodes::geo
10 changes: 8 additions & 2 deletions src/geo/iterator/grib_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int
}
}
} static const init_main;

i->iterator = new eccodes::geo::GeoIterator(const_cast<grib_handle*>(ch), flags);
try {
i->iterator = new eccodes::geo::GeoIterator(const_cast<grib_handle*>(ch), flags);
}
catch(std::exception& e) {
grib_context_log(ch->context, GRIB_LOG_ERROR, "grib_iterator_new: Exception thrown (%s)", e.what());
*error = GRIB_GEOCALCULUS_PROBLEM;
return NULL;
}
}
else
#endif
Expand Down

0 comments on commit c59047f

Please sign in to comment.