Add is_one_of functor

This commit is contained in:
shahramn 2024-04-21 16:21:35 +01:00
parent 19e2201d7c
commit 25bbb21950
3 changed files with 77 additions and 0 deletions

View File

@ -169,6 +169,46 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
return GRIB_SUCCESS;
}
if (STR_EQUAL(e->name, "is_one_of")) {
*lres = 0;
const char* keyName = grib_arguments_get_name(h, e->args, 0);
if (!keyName) return GRIB_INVALID_ARGUMENT;
int type = 0;
int err = grib_get_native_type(h, keyName, &type);
if (err) return err;
if (type == GRIB_TYPE_STRING) {
int n = 1; // skip the 1st argument which is the key
char keyValue[254] = {0,};
size_t len = sizeof(keyValue);
err = grib_get_string(h, keyName, keyValue, &len);
if (err) return err;
const char* sValue = 0;
while (( sValue = grib_arguments_get_string(h, e->args, n++)) != NULL ) {
if (STR_EQUAL(keyValue, sValue)) {
*lres = 1;
return GRIB_SUCCESS;
}
}
}
else if (type == GRIB_TYPE_LONG) {
long keyValue = 0;
err = grib_get_long(h, keyName, &keyValue);
if (err) return err;
int n = grib_arguments_get_count(e->args);
for (int i = 1; i < n; ++i) { // skip 1st argument which is the key
long lValue = grib_arguments_get_long(h, e->args, i);
if (keyValue == lValue) {
*lres = 1;
return GRIB_SUCCESS;
}
}
}
else if (type == GRIB_TYPE_DOUBLE) {
return GRIB_NOT_IMPLEMENTED;
}
return GRIB_SUCCESS;
}
if (STR_EQUAL(e->name, "gribex_mode_on")) {
*lres = h->context->gribex_mode_on ? 1 : 0;
return GRIB_SUCCESS;

View File

@ -105,6 +105,7 @@ if( HAVE_BUILD_TOOLS )
grib_packing_order
filter_substr
filter_size
filter_is_one_of
filter_is_in_list
filter_transient_darray
grib_uerra

36
tests/filter_is_one_of.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/sh
# (C) Copyright 2005- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
. ./include.ctest.sh
label="filter_is_one_of_test"
tempFilt=temp.$label.filt
tempOut=temp.$label.txt
input=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
grib_check_key_equals $input "shortName,paramId,typeOfLevel" "t 130 surface"
cat > $tempFilt <<EOF
transient xx1 = is_one_of(paramId, 0, 99, 44, 130); # last one should match
assert(xx1 == 1);
transient xx2 = is_one_of(paramId, 666); # no match
assert(xx2 == 0);
transient xx3 = is_one_of(shortName, "t", "q"); # First one matches
assert(xx3 == 1);
transient xx4 = is_one_of(typeOfLevel, "isobar", "surface" , "nomatch");
assert(xx4);
EOF
${tools_dir}/grib_filter $tempFilt $input
# Clean up
rm -f $tempFilt $tempOut