Skip to content

Commit

Permalink
eckit::geo: exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Dec 19, 2024
1 parent 87d8023 commit 26be541
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
24 changes: 15 additions & 9 deletions src/geo/GeoIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,29 @@ int GeoIterator::init(grib_handle*, grib_arguments*)
NOTIMP;
}


// The C public API for this does not have a way of returning an error,
// So any exception thrown by eckit will is fatal!
int GeoIterator::next(double* lat, double* lon, double* val) const
{
if (iter_ == end_) {
return 0; // (false)
}
try {
const auto p = *iter_;
const auto& q = std::get<eckit::geo::PointLonLat>(p);

const auto p = *iter_;
const auto& q = std::get<eckit::geo::PointLonLat>(p);
*lat = q.lat;
*lon = q.lon;
if (val != nullptr && data_ != nullptr) {
*val = data_[iter_->index()];
}

*lat = q.lat;
*lon = q.lon;
if (val != nullptr && data_ != nullptr) {
*val = data_[iter_->index()];
++iter_;
}
catch(std::exception& e) {
grib_context_log(h_->context, GRIB_LOG_FATAL, "GeoIterator::next: Exception thrown (%s)", e.what());
return 0;
}

++iter_;
return 1; // (true)
}

Expand Down
4 changes: 2 additions & 2 deletions src/geo/iterator/grib_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int
grib_iterator* i = (grib_iterator*)grib_context_malloc_clear(ch->context, sizeof(grib_iterator));

#if defined(HAVE_ECKIT_GEO)
static const auto* eckit_geo = codes_getenv("ECCODES_ECKIT_GEO");
if (eckit_geo != nullptr && strcmp(eckit_geo, "1") == 0) {
const int eckit_geo = ch->context->eckit_geo; // check environment variable
if (eckit_geo) {
struct InitMain
{
InitMain()
Expand Down
1 change: 1 addition & 0 deletions src/grib_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ struct grib_context
int bufr_multi_element_constant_arrays;
int grib_data_quality_checks;
int single_precision;
int eckit_geo;
FILE* log_stream;
grib_trie* classes;
grib_trie* lists;
Expand Down
9 changes: 7 additions & 2 deletions src/grib_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ static grib_context default_grib_context = {
0, /* bufr_multi_element_constant_arrays */
0, /* grib_data_quality_checks */
0, /* single_precision */
0, /* eckit_geo */
0, /* log_stream */
0, /* classes */
0, /* lists */
Expand Down Expand Up @@ -406,18 +407,22 @@ grib_context* grib_context_get_default()
const char* bufr_multi_element_constant_arrays = NULL;
const char* grib_data_quality_checks = NULL;
const char* single_precision = NULL;
const char* eckit_geo = NULL;
const char* file_pool_max_opened_files = NULL;

#ifdef ENABLE_FLOATING_POINT_EXCEPTIONS
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
#endif

write_on_fail = codes_getenv("ECCODES_GRIB_WRITE_ON_FAIL");
bufrdc_mode = getenv("ECCODES_BUFRDC_MODE_ON");
bufr_set_to_missing_if_out_of_range = getenv("ECCODES_BUFR_SET_TO_MISSING_IF_OUT_OF_RANGE");
bufr_multi_element_constant_arrays = getenv("ECCODES_BUFR_MULTI_ELEMENT_CONSTANT_ARRAYS");
grib_data_quality_checks = getenv("ECCODES_GRIB_DATA_QUALITY_CHECKS");
single_precision = getenv("ECCODES_SINGLE_PRECISION");
file_pool_max_opened_files = getenv("ECCODES_FILE_POOL_MAX_OPENED_FILES");
eckit_geo = getenv("ECCODES_ECKIT_GEO");
// The following had an equivalent env. var in grib_api
write_on_fail = codes_getenv("ECCODES_GRIB_WRITE_ON_FAIL");
large_constant_fields = codes_getenv("ECCODES_GRIB_LARGE_CONSTANT_FIELDS");
no_abort = codes_getenv("ECCODES_NO_ABORT");
debug = codes_getenv("ECCODES_DEBUG");
Expand All @@ -429,7 +434,6 @@ grib_context* grib_context_get_default()
no_spd = codes_getenv("ECCODES_GRIB_NO_SPD");
keep_matrix = codes_getenv("ECCODES_GRIB_KEEP_MATRIX");
show_hour_stepunit = codes_getenv("ECCODES_GRIB_HOURLY_STEPS_WITH_UNITS");
file_pool_max_opened_files = getenv("ECCODES_FILE_POOL_MAX_OPENED_FILES");

/* On UNIX, when we read from a file we get exactly what is in the file on disk.
* But on Windows a file can be opened in binary or text mode. In binary mode the system behaves exactly as in UNIX.
Expand Down Expand Up @@ -564,6 +568,7 @@ grib_context* grib_context_get_default()
default_grib_context.bufr_multi_element_constant_arrays = bufr_multi_element_constant_arrays ? atoi(bufr_multi_element_constant_arrays) : 0;
default_grib_context.grib_data_quality_checks = grib_data_quality_checks ? atoi(grib_data_quality_checks) : 0;
default_grib_context.single_precision = single_precision ? atoi(single_precision) : 0;
default_grib_context.eckit_geo = eckit_geo ? atoi(eckit_geo) : 0;
default_grib_context.file_pool_max_opened_files = file_pool_max_opened_files ? atoi(file_pool_max_opened_files) : DEFAULT_FILE_POOL_MAX_OPENED_FILES;
}

Expand Down

0 comments on commit 26be541

Please sign in to comment.