Skip to content

Commit ffda07d

Browse files
committed
Add abs functor (integer absolute value function)
1 parent c32c4c5 commit ffda07d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/grib_expression_class_functor.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,24 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
8686
{
8787
grib_expression_functor* e = (grib_expression_functor*)g;
8888

89-
if (strcmp(e->name, "lookup") == 0) {
89+
if (STR_EQUAL(e->name, "lookup")) {
9090
return GRIB_SUCCESS;
9191
}
9292

93-
if (strcmp(e->name, "new") == 0) {
93+
if (STR_EQUAL(e->name, "new")) {
9494
*lres = h->loader != NULL;
9595
return GRIB_SUCCESS;
9696
}
9797

98-
if (strcmp(e->name, "missing") == 0) {
98+
if (STR_EQUAL(e->name, "abs")) {
99+
grib_expression* exp = grib_arguments_get_expression(h, e->args, 0);
100+
long lval = 0;
101+
int ret = grib_expression_evaluate_long(h, exp, &lval);
102+
*lres = abs(lval);
103+
return ret;
104+
}
105+
106+
if (STR_EQUAL(e->name, "missing")) {
99107
const char* p = grib_arguments_get_name(h, e->args, 0);
100108
if (p) {
101109
long val = 0;
@@ -122,7 +130,7 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
122130
return GRIB_SUCCESS;
123131
}
124132

125-
if (strcmp(e->name, "defined") == 0) {
133+
if (STR_EQUAL(e->name, "defined")) {
126134
const char* p = grib_arguments_get_name(h, e->args, 0);
127135

128136
if (p) {
@@ -134,7 +142,7 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
134142
return GRIB_SUCCESS;
135143
}
136144

137-
if (strcmp(e->name, "environment_variable") == 0) {
145+
if (STR_EQUAL(e->name, "environment_variable")) {
138146
// ECC-1520: This implementation has some limitations:
139147
// 1. Cannot distinguish between environment variable NOT SET
140148
// and SET but equal to 0
@@ -154,12 +162,12 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
154162
return GRIB_SUCCESS;
155163
}
156164

157-
if (strcmp(e->name, "changed") == 0) {
165+
if (STR_EQUAL(e->name, "changed")) {
158166
*lres = 1;
159167
return GRIB_SUCCESS;
160168
}
161169

162-
if (strcmp(e->name, "gribex_mode_on") == 0) {
170+
if (STR_EQUAL(e->name, "gribex_mode_on")) {
163171
*lres = h->context->gribex_mode_on ? 1 : 0;
164172
return GRIB_SUCCESS;
165173
}

tests/grib_filter.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ grep "unable to get rubbish as string" $tempOut
398398
grep "unable to get garbage as string" $tempOut
399399

400400

401+
# Use of "abs"
402+
cat >$tempFilt <<EOF
403+
meta abs_twice_bsf evaluate( abs(binaryScaleFactor * 2) );
404+
assert(abs_twice_bsf == 20);
405+
EOF
406+
${tools_dir}/grib_filter $tempFilt $ECCODES_SAMPLES_PATH/GRIB2.tmpl
407+
408+
401409
# Clean up
402410
rm -f $tempGrib $tempFilt $tempOut $tempRef
403411
rm -f ${data_dir}/formatint.rules ${data_dir}/binop.rules

0 commit comments

Comments
 (0)