diff --git a/eccodes_config.h.in b/eccodes_config.h.in index 4432f9a10..29877a3d9 100644 --- a/eccodes_config.h.in +++ b/eccodes_config.h.in @@ -115,6 +115,7 @@ #cmakedefine HAVE_GEOGRAPHY #cmakedefine HAVE_MEMFS +#cmakedefine HAVE_FORTRAN #ifdef HAVE_MEMFS #undef ECCODES_DEFINITION_PATH diff --git a/src/codes_util.cc b/src/codes_util.cc index 9e7507c23..31e301999 100644 --- a/src/codes_util.cc +++ b/src/codes_util.cc @@ -404,3 +404,72 @@ int compute_scaled_value_and_scale_factor( } return err; } + +int codes_is_feature_enabled(const char* feature) +{ + int aec_enabled = 0; + int jpg_enabled = 0; // JasPer and/or OpenJPEG + int png_enabled = 0; + int memfs_enabled = 0; + int posix_threads_enabled = 0; + int omp_threads_enabled = 0; + int netcdf_enabled = 0; + int fortran_enabled = 0; + +#if defined(HAVE_LIBAEC) || defined(HAVE_AEC) + aec_enabled = 1; +#endif +#if HAVE_JPEG + #if HAVE_LIBJASPER + jpg_enabled = 1; + #endif + #if HAVE_LIBOPENJPEG + jpg_enabled = 1; + #endif +#endif +#if defined(HAVE_LIBPNG) + png_enabled = 1; +#endif +#if defined(HAVE_MEMFS) + memfs_enabled = 1; +#endif +#if GRIB_PTHREADS + posix_threads_enabled = 1; +#endif +#if GRIB_OMP_THREADS + omp_threads_enabled= 1; +#endif +#if defined(HAVE_NETCDF) + netcdf_enabled = 1; +#endif +#if defined(HAVE_FORTRAN) + fortran_enabled = 1; +#endif + + if (STR_EQUAL(feature, "AEC") || STR_EQUAL(feature, "CCSDS")) { + return aec_enabled; + } + if (STR_EQUAL(feature, "JPG") || STR_EQUAL(feature, "JPEG")) { + return jpg_enabled; + } + if (STR_EQUAL(feature, "PNG")) { + return png_enabled; + } + if (STR_EQUAL(feature, "MEMFS")) { + return memfs_enabled; + } + if (STR_EQUAL(feature, "ECCODES_THREADS")) { + return posix_threads_enabled; + } + if (STR_EQUAL(feature, "ECCODES_OMP_THREADS")) { + return omp_threads_enabled; + } + if (STR_EQUAL(feature, "NETCDF")) { + return netcdf_enabled; + } + if (STR_EQUAL(feature, "FORTRAN")) { + return fortran_enabled; + } + + return 0; +} diff --git a/src/eccodes.h b/src/eccodes.h index 2c5aff7e2..0b017d6ab 100644 --- a/src/eccodes.h +++ b/src/eccodes.h @@ -1400,6 +1400,9 @@ int codes_get_product_kind(const codes_handle* h, ProductKind* product_kind); int codes_check_message_header(const void* bytes, size_t length, ProductKind product); int codes_check_message_footer(const void* bytes, size_t length, ProductKind product); +/* Features enabled */ +int codes_is_feature_enabled(const char* feature); + /* --------------------------------------- */ #define CODES_UTIL_GRID_SPEC_REGULAR_LL GRIB_UTIL_GRID_SPEC_REGULAR_LL diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index d7479e960..38de12e4b 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -857,6 +857,7 @@ long convert_to_minutes(long step, long stepUnits); bool is_sorted_ascending(double arr[], size_t n); bool is_sorted_descending(double arr[], size_t n); int compute_scaled_value_and_scale_factor(double input, int64_t scaled_value_max, int64_t scale_factor_max, int64_t* ret_value, int64_t* ret_factor); +int codes_is_feature_enabled(const char* feature); /* grib_util.cc */ grib_handle* grib_util_sections_copy(grib_handle* hfrom, grib_handle* hto, int what, int* err); diff --git a/tests/unit_tests.cc b/tests/unit_tests.cc index eedafca1b..5b89bda82 100644 --- a/tests/unit_tests.cc +++ b/tests/unit_tests.cc @@ -282,6 +282,7 @@ static void test_concept_condition_strings() char result[1024] = {0,}; grib_context* context = NULL; grib_handle* h = grib_handle_new_from_samples(context, "GRIB2"); + if (!h) return; err = get_concept_condition_string(h, "typeOfLevel", NULL, result); Assert(!err); @@ -762,6 +763,7 @@ void test_codes_context_set_debug() grib_context_set_debug(context, -1); grib_handle* h = grib_handle_new_from_samples(context, "GRIB2"); + if (!h) return; err = grib_set_long(h, "paramId", 167); Assert(!err); @@ -777,6 +779,27 @@ void test_codes_context_set_debug() grib_context_set_debug(context, 0); } +void test_is_feature_enabled() +{ + printf("Running %s ...\n", __func__); + const char* features[] = { + "AEC", + "MEMFS", + "JPG", + "PNG", + "ECCODES_THREADS", + "ECCODES_OMP_THREADS", + "NETCDF", + "non-existent-feature", + "FORTRAN", + NULL}; + for (int i = 0; features[i]; ++i) { + const char* f = features[i]; + printf("\tFeature %s enabled?\t%d\n", f, codes_is_feature_enabled(f)); + } +} + + int main(int argc, char** argv) { printf("Doing unit tests. ecCodes version = %ld\n", grib_get_api_version()); @@ -841,6 +864,7 @@ int main(int argc, char** argv) test_grib2_select_PDTN(); test_grib2_choose_PDTN(); + test_is_feature_enabled(); return 0; }