From a2adea7b812462b51b07607ced8bf4fd0aecca07 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 9 Dec 2023 17:13:02 +0000 Subject: [PATCH] API: Allow setting code table keys as missing via codes_set_string --- examples/C/grib_set_missing.c | 2 ++ examples/C/grib_set_missing.sh | 4 ++++ src/grib_accessor_class_codetable.cc | 4 ++++ src/grib_accessor_class_long.cc | 2 +- src/grib_api_internal.h | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/C/grib_set_missing.c b/examples/C/grib_set_missing.c index 2b8b036e4..82f698975 100644 --- a/examples/C/grib_set_missing.c +++ b/examples/C/grib_set_missing.c @@ -62,6 +62,8 @@ int main(int argc, char** argv) /* Can also set to MISSING via codes_set_string */ CODES_CHECK(codes_set_string(h, "scaleFactorOfFirstFixedSurface", "missing", &str_len), 0); CODES_CHECK(codes_set_string(h, "scaledValueOfFirstFixedSurface", "missing", &str_len), 0); + /* Set a Code Table key to missing via codes_set_string */ + CODES_CHECK(codes_set_string(h, "typeOfFirstFixedSurface", "missing", &str_len), 0); /* see GRIB-490 */ CODES_CHECK(codes_get_long(h, "Ni", &Ni), 0); diff --git a/examples/C/grib_set_missing.sh b/examples/C/grib_set_missing.sh index baef5d073..9b1314f9b 100755 --- a/examples/C/grib_set_missing.sh +++ b/examples/C/grib_set_missing.sh @@ -20,6 +20,10 @@ if [ -f "${tools_dir}/grib_get" ]; then [ "$sf" = "MISSING" ] sf=`${tools_dir}/grib_get -p scaledValueOfFirstFixedSurface $tempGrib` [ "$sf" = "MISSING" ] + + # Code Table key being set to missing + sf=`${tools_dir}/grib_get -p typeOfFirstFixedSurface:i, $tempGrib` + [ "$sf" = "255" ] fi rm -f $tempGrib diff --git a/src/grib_accessor_class_codetable.cc b/src/grib_accessor_class_codetable.cc index a14f13178..453c97306 100644 --- a/src/grib_accessor_class_codetable.cc +++ b/src/grib_accessor_class_codetable.cc @@ -737,6 +737,10 @@ static int pack_string(grib_accessor* a, const char* buffer, size_t* len) return grib_pack_long(a, &lValue, &l); } + if (STR_EQUAL_NOCASE(buffer, "missing")) { + return pack_missing(a); + } + grib_accessor_codetable* self = (grib_accessor_codetable*)a; grib_codetable* table; long i; diff --git a/src/grib_accessor_class_long.cc b/src/grib_accessor_class_long.cc index 7fa0e5846..92431f812 100644 --- a/src/grib_accessor_class_long.cc +++ b/src/grib_accessor_class_long.cc @@ -258,7 +258,7 @@ static int pack_string(grib_accessor* a, const char* val, size_t* len) long v = 0; /* The converted value */ // ECC-1722 - if (strcmp_nocase(val, "missing")==0) { + if (STR_EQUAL_NOCASE(val, "missing")) { return pack_missing(a); } diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index b48aed6ff..06e4ef451 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -201,6 +201,8 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type); /* Return true if two strings are equal */ #define STR_EQUAL(a, b) (strcmp((a), (b)) == 0) +/* Return true if two strings are equal, ignoring case */ +#define STR_EQUAL_NOCASE(a, b) (strcmp_nocase((a), (b)) == 0) #include "grib_api.h"