From 2e461f0ef3fd2de2248359dc72adb514abad7b69 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sun, 9 Jul 2023 15:18:24 +0100 Subject: [PATCH] BUFR: Allow decoding of expandedDescriptors as a string array --- ...essor_class_bufrdc_expanded_descriptors.cc | 12 ++++--- ...rib_accessor_class_expanded_descriptors.cc | 31 ++++++++++++++++++- tests/bufr_filter_misc.sh | 19 ++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/grib_accessor_class_bufrdc_expanded_descriptors.cc b/src/grib_accessor_class_bufrdc_expanded_descriptors.cc index 2bb5d648f..8476f4708 100644 --- a/src/grib_accessor_class_bufrdc_expanded_descriptors.cc +++ b/src/grib_accessor_class_bufrdc_expanded_descriptors.cc @@ -169,24 +169,26 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) static int unpack_string_array(grib_accessor* a, char** buffer, size_t* len) { + int err = 0; grib_accessor* descriptors = 0; size_t l = 0; long lenall = 0; size_t i = 0; long* v = 0; char buf[25] = {0,}; - grib_context* c = a->context; + grib_context* c = a->context; descriptors = get_accessor(a); if (!descriptors) return GRIB_NOT_FOUND; - grib_value_count(a, &lenall); + err = grib_value_count(a, &lenall); + if (err) return err; l = lenall; - if (l > *len) - return GRIB_ARRAY_TOO_SMALL; + if (l > *len) return GRIB_ARRAY_TOO_SMALL; v = (long*)grib_context_malloc_clear(c, sizeof(long) * l); - grib_unpack_long(descriptors, v, &l); + err = grib_unpack_long(descriptors, v, &l); + if (err) return err; for (i = 0; i < l; i++) { snprintf(buf, sizeof(buf), "%06ld", v[i]); diff --git a/src/grib_accessor_class_expanded_descriptors.cc b/src/grib_accessor_class_expanded_descriptors.cc index a339882d0..7a69b82b2 100644 --- a/src/grib_accessor_class_expanded_descriptors.cc +++ b/src/grib_accessor_class_expanded_descriptors.cc @@ -25,6 +25,7 @@ CLASS = accessor SUPER = grib_accessor_class_long IMPLEMENTS = unpack_long;pack_long IMPLEMENTS = unpack_double +IMPLEMENTS = unpack_string_array IMPLEMENTS = init;dump;destroy IMPLEMENTS = value_count; get_native_type MEMBERS = const char* unexpandedDescriptors @@ -55,6 +56,7 @@ static int get_native_type(grib_accessor*); static int pack_long(grib_accessor*, const long* val, size_t* len); static int unpack_double(grib_accessor*, double* val, size_t* len); static int unpack_long(grib_accessor*, long* val, size_t* len); +static int unpack_string_array(grib_accessor*, char**, size_t* len); static int value_count(grib_accessor*, long*); static void destroy(grib_context*, grib_accessor*); static void dump(grib_accessor*, grib_dumper*); @@ -107,7 +109,7 @@ static grib_accessor_class _grib_accessor_class_expanded_descriptors = { 0, /* pack_string */ 0, /* unpack_string */ 0, /* pack_string_array */ - 0, /* unpack_string_array */ + &unpack_string_array, /* unpack_string_array */ 0, /* pack_bytes */ 0, /* unpack_bytes */ 0, /* pack_expression */ @@ -805,6 +807,33 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) return GRIB_SUCCESS; } +static int unpack_string_array(grib_accessor* a, char** buffer, size_t* len) +{ + int err = 0; + long* v = NULL; + char buf[25] = {0,}; + long llen = 0; + size_t i = 0, size = 0; + grib_context* c = a->context; + + err = grib_value_count(a, &llen); + if (err) return err; + + size = llen; + v = (long*)grib_context_malloc_clear(c, sizeof(long) * size); + err = grib_unpack_long(a, v, &size); + if (err) return err; + + for (i = 0; i < size; i++) { + snprintf(buf, sizeof(buf), "%06ld", v[i]); + buffer[i] = grib_context_strdup(c, buf); + } + *len = size; + grib_context_free(c,v); + + return GRIB_NOT_IMPLEMENTED; +} + static int pack_long(grib_accessor* a, const long* val, size_t* len) { grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; diff --git a/tests/bufr_filter_misc.sh b/tests/bufr_filter_misc.sh index 6e2171a4f..cda4ce799 100755 --- a/tests/bufr_filter_misc.sh +++ b/tests/bufr_filter_misc.sh @@ -1460,5 +1460,24 @@ res=`${tools_dir}/bufr_get -s unpack=1 -p temperature $f.out` [ "$res" = "-101" ] +# Decode expandedDescriptors as array of string +cat > $fRules < $fLog +fRef=temp.$label.ref +cat > $fRef <