From c59047f34fcde239cd5e234846e4682b63c78732 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 20 Dec 2024 19:26:40 +0000 Subject: [PATCH] eckit::geo: exception handling and macros --- src/geo/GeoIterator.cc | 18 ++++++++++++------ src/geo/GeoIterator.h | 7 ------- src/geo/iterator/grib_iterator.cc | 10 ++++++++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/geo/GeoIterator.cc b/src/geo/GeoIterator.cc index e15d53406..04fd6969f 100644 --- a/src/geo/GeoIterator.cc +++ b/src/geo/GeoIterator.cc @@ -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(grib_context_malloc(h_->context, nv_ * sizeof(double))); - ECCODES_ASSERT(data_ != nullptr); + //long numberOfPoints = 0; + //grib_get_long_internal(h, "numberOfPoints", &numberOfPoints); - auto size = nv_; - CODES_CHECK(codes_get_double_array(h_, "values", data_, &size), ""); - ECCODES_ASSERT(nv_ == size); + if (flags_ & GRIB_GEOITERATOR_NO_VALUES) { + data_ = nullptr; + } else { + data_ = static_cast(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; diff --git a/src/geo/GeoIterator.h b/src/geo/GeoIterator.h index fefc5aca5..0f8396b9d 100644 --- a/src/geo/GeoIterator.h +++ b/src/geo/GeoIterator.h @@ -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: @@ -47,5 +41,4 @@ private: geo_iterator::Iterator* create() const override; }; - } // namespace eccodes::geo diff --git a/src/geo/iterator/grib_iterator.cc b/src/geo/iterator/grib_iterator.cc index 9f0be23eb..5c41b9be2 100644 --- a/src/geo/iterator/grib_iterator.cc +++ b/src/geo/iterator/grib_iterator.cc @@ -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(ch), flags); + try { + i->iterator = new eccodes::geo::GeoIterator(const_cast(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