BUFR: Allow decoding of expandedDescriptors as a string array

This commit is contained in:
Shahram Najm 2023-07-09 15:18:24 +01:00
parent 3926e03f7d
commit 2e461f0ef3
3 changed files with 56 additions and 6 deletions

View File

@ -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) static int unpack_string_array(grib_accessor* a, char** buffer, size_t* len)
{ {
int err = 0;
grib_accessor* descriptors = 0; grib_accessor* descriptors = 0;
size_t l = 0; size_t l = 0;
long lenall = 0; long lenall = 0;
size_t i = 0; size_t i = 0;
long* v = 0; long* v = 0;
char buf[25] = {0,}; char buf[25] = {0,};
grib_context* c = a->context; grib_context* c = a->context;
descriptors = get_accessor(a); descriptors = get_accessor(a);
if (!descriptors) return GRIB_NOT_FOUND; if (!descriptors) return GRIB_NOT_FOUND;
grib_value_count(a, &lenall); err = grib_value_count(a, &lenall);
if (err) return err;
l = lenall; l = lenall;
if (l > *len) if (l > *len) return GRIB_ARRAY_TOO_SMALL;
return GRIB_ARRAY_TOO_SMALL;
v = (long*)grib_context_malloc_clear(c, sizeof(long) * l); 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++) { for (i = 0; i < l; i++) {
snprintf(buf, sizeof(buf), "%06ld", v[i]); snprintf(buf, sizeof(buf), "%06ld", v[i]);

View File

@ -25,6 +25,7 @@ CLASS = accessor
SUPER = grib_accessor_class_long SUPER = grib_accessor_class_long
IMPLEMENTS = unpack_long;pack_long IMPLEMENTS = unpack_long;pack_long
IMPLEMENTS = unpack_double IMPLEMENTS = unpack_double
IMPLEMENTS = unpack_string_array
IMPLEMENTS = init;dump;destroy IMPLEMENTS = init;dump;destroy
IMPLEMENTS = value_count; get_native_type IMPLEMENTS = value_count; get_native_type
MEMBERS = const char* unexpandedDescriptors 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 pack_long(grib_accessor*, const long* val, size_t* len);
static int unpack_double(grib_accessor*, double* 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_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 int value_count(grib_accessor*, long*);
static void destroy(grib_context*, grib_accessor*); static void destroy(grib_context*, grib_accessor*);
static void dump(grib_accessor*, grib_dumper*); static void dump(grib_accessor*, grib_dumper*);
@ -107,7 +109,7 @@ static grib_accessor_class _grib_accessor_class_expanded_descriptors = {
0, /* pack_string */ 0, /* pack_string */
0, /* unpack_string */ 0, /* unpack_string */
0, /* pack_string_array */ 0, /* pack_string_array */
0, /* unpack_string_array */ &unpack_string_array, /* unpack_string_array */
0, /* pack_bytes */ 0, /* pack_bytes */
0, /* unpack_bytes */ 0, /* unpack_bytes */
0, /* pack_expression */ 0, /* pack_expression */
@ -805,6 +807,33 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
return GRIB_SUCCESS; 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) static int pack_long(grib_accessor* a, const long* val, size_t* len)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;

View File

@ -1460,5 +1460,24 @@ res=`${tools_dir}/bufr_get -s unpack=1 -p temperature $f.out`
[ "$res" = "-101" ] [ "$res" = "-101" ]
# Decode expandedDescriptors as array of string
cat > $fRules <<EOF
print "[expandedDescriptors:s]";
EOF
${tools_dir}/codes_bufr_filter $fRules airc_142.bufr > $fLog
fRef=temp.$label.ref
cat > $fRef <<EOF
001006 002061 004001 004002 004003 004004 004005 005001
006001 008004 007002 012001 011001 011002 011031 011032
011033 020041 222000 031031 031031 031031 031031 031031
031031 031031 031031 031031 031031 031031 031031 031031
031031 031031 031031 031031 031031 001031 001032 033007
033007 033007 033007 033007 033007 033007 033007 033007
033007 033007 033007 033007 033007 033007 033007 033007
033007
EOF
diff $fRef $fLog
rm -f $fRef
# Clean up # Clean up
rm -f ${f}.log ${f}.log.ref ${f}.out $fLog $fRules rm -f ${f}.log ${f}.log.ref ${f}.out $fLog $fRules