From d5cd7960c770692bfcfb4986343f45bde66a2053 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 9 Mar 2022 16:54:29 +0000 Subject: [PATCH] Debugging: Utility function to track GRIB packing/repacking issues --- src/grib_value.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/grib_value.c b/src/grib_value.c index 9c508f68b..eeae905fa 100644 --- a/src/grib_value.c +++ b/src/grib_value.c @@ -27,6 +27,27 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } +/* Debug utility function to track GRIB packing/repacking issues */ +static void print_debug_info__set_double_array(grib_handle* h, const char* func, const char* name, const double* val, size_t length) +{ + size_t N = 5, i = 0; + double minVal = DBL_MAX, maxVal = -DBL_MAX; + Assert( h->context->debug ); + + if (length <= N) + N = length; + fprintf(stderr, "ECCODES DEBUG %s key=%s %lu values (", func, name, (unsigned long)length); + for (i = 0; i < N; ++i) + fprintf(stderr, "%g, ", val[i]); + if (N >= length) fprintf(stderr, " ) "); + else fprintf(stderr, "...) "); + for (i = 0; i < length; ++i) { + if (val[i] < minVal) minVal = val[i]; + if (val[i] > maxVal) maxVal = val[i]; + } + fprintf(stderr, "min=%g, max=%g\n",minVal,maxVal); +} + int grib_set_expression(grib_handle* h, const char* name, grib_expression* e) { grib_accessor* a = grib_find_accessor(h, name); @@ -708,8 +729,9 @@ int grib_set_double_array_internal(grib_handle* h, const char* name, const doubl { int ret = 0; - if (h->context->debug) - fprintf(stderr, "ECCODES DEBUG grib_set_double_array_internal key=%s %ld values\n", name, (long)length); + if (h->context->debug) { + print_debug_info__set_double_array(h, "grib_set_double_array_internal", name, val, length); + } if (length == 0) { grib_accessor* a = grib_find_accessor(h, name); @@ -732,26 +754,7 @@ static int __grib_set_double_array(grib_handle* h, const char* name, const doubl size_t i = 0; if (h->context->debug) { - size_t N = 5; - /*double minVal = DBL_MAX, maxVal = -DBL_MAX;*/ - if (length <= N) - N = length; - fprintf(stderr, "ECCODES DEBUG grib_set_double_array key=%s %ld values (", name, (long)length); - for (i = 0; i < N; ++i) - fprintf(stderr, " %g,", val[i]); - if (N >= length) fprintf(stderr, " )\n"); - else fprintf(stderr, " ... )\n"); - /* - * if (N >= length) - * fprintf(stderr, " )\t"); - * else - * fprintf(stderr, " ... )\t"); - * for (i = 0; i < length; ++i) { - * if (val[i] < minVal) minVal = val[i]; - * if (val[i] > maxVal) maxVal = val[i]; - * } - * fprintf(stderr, "min=%g, max=%g\n",minVal,maxVal); - */ + print_debug_info__set_double_array(h, "__grib_set_double_array", name, val, length); } if (length == 0) {