ECC-1520: grib_filter/bufr_filter: Provide way of reading environment variables

This commit is contained in:
Shahram Najm 2023-02-02 10:38:18 +00:00
parent b1d5fe14fb
commit 8a0073aeb9
2 changed files with 50 additions and 10 deletions

View File

@ -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(")");
}

View File

@ -242,6 +242,29 @@ EOF
diff $tempRef $tempOut
echo "Test environment variables"
# -----------------------------------------
input="${samp_dir}/GRIB2.tmpl"
cat >$tempFilt <<EOF
transient cds = environment_variable(CDS);
if (cds == 0) {
print "Either CDS is undefined or defined but equal to 0";
} else {
print "CDS is defined and equal to [cds]";
}
EOF
# No env var or zero
${tools_dir}/grib_filter $tempFilt $input > $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"