From 8a0073aeb9cfd2aca4439f3a7a9f4010d3349417 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 2 Feb 2023 10:38:18 +0000 Subject: [PATCH] ECC-1520: grib_filter/bufr_filter: Provide way of reading environment variables --- src/grib_expression_class_functor.cc | 37 ++++++++++++++++++++-------- tests/grib_filter.sh | 23 +++++++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/grib_expression_class_functor.cc b/src/grib_expression_class_functor.cc index 8a36b68c9..91f4cdcaf 100644 --- a/src/grib_expression_class_functor.cc +++ b/src/grib_expression_class_functor.cc @@ -88,9 +88,7 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres) { grib_expression_functor* e = (grib_expression_functor*)g; - /* - TODO: needs OO code here - */ + // TODO: needs OO code here if (strcmp(e->name, "lookup") == 0) { return GRIB_SUCCESS; } @@ -113,16 +111,15 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres) } err = grib_get_long_internal(h, p, &val); if (err) return err; - /* Note: This does not cope with keys like typeOfSecondFixedSurface - * which are codetable entries with values like 255: this value is - * not classed as 'missing'! - * (See ECC-594) - */ + // Note: This does not cope with keys like typeOfSecondFixedSurface + // which are codetable entries with values like 255: this value is + // not classed as 'missing'! + // (See ECC-594) *lres = (val == GRIB_MISSING_LONG); return GRIB_SUCCESS; } else { - /* No arguments means return the actual integer missing value */ + // No arguments means return the actual integer missing value *lres = GRIB_MISSING_LONG; } return GRIB_SUCCESS; @@ -140,6 +137,26 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres) return GRIB_SUCCESS; } + if (strcmp(e->name, "environment_variable") == 0) { + // ECC-1520: This implementation has some limitations: + // 1. Cannot distinguish between environment variable NOT SET + // and SET but equal to 0 + // 2. Cannot deal with string values + const char* p = grib_arguments_get_name(h, e->args, 0); + if (p) { + char* env = getenv(p); + if (env) { + long lval = 0; + if (string_to_long(env, &lval) == GRIB_SUCCESS) { + *lres = lval; + return GRIB_SUCCESS; + } + } + } + *lres = 0; + return GRIB_SUCCESS; + } + if (strcmp(e->name, "changed") == 0) { *lres = 1; return GRIB_SUCCESS; @@ -157,7 +174,7 @@ static void print(grib_context* c, grib_expression* g, grib_handle* f) { grib_expression_functor* e = (grib_expression_functor*)g; printf("%s(", e->name); - /*grib_expression_print(c,e->args,f);*/ + // grib_expression_print(c,e->args,f); printf(")"); } diff --git a/tests/grib_filter.sh b/tests/grib_filter.sh index e85d5727b..39636c60c 100755 --- a/tests/grib_filter.sh +++ b/tests/grib_filter.sh @@ -242,6 +242,29 @@ EOF diff $tempRef $tempOut +echo "Test environment variables" +# ----------------------------------------- +input="${samp_dir}/GRIB2.tmpl" +cat >$tempFilt < $tempOut +grep -q "undefined" $tempOut +CDS=0 ${tools_dir}/grib_filter $tempFilt $input > $tempOut +grep -q "defined but equal to 0" $tempOut +# Set to a non-zero integer +CDS=1 ${tools_dir}/grib_filter $tempFilt $input > $tempOut +grep -q "defined and equal to 1" $tempOut +CDS=-42 ${tools_dir}/grib_filter $tempFilt $input > $tempOut +grep -q "defined and equal to -42" $tempOut + + echo "Test IEEE float overflow" # ----------------------------------------- input="${samp_dir}/GRIB2.tmpl"