Skip to content

Commit 303a8ea

Browse files
committed
Filter: Allow 'size' accessor to be used in IF statements
1 parent 02f9a17 commit 303a8ea

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/grib_expression_class_functor.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
9292
return ret;
9393
}
9494

95+
if (STR_EQUAL(e->name, "size")) {
96+
*lres = 0;
97+
const char* p = grib_arguments_get_name(h, e->args, 0);
98+
if (p) {
99+
size_t size = 0;
100+
int err = grib_get_size(h, p, &size);
101+
if (err) return err;
102+
*lres = (long)size;
103+
return GRIB_SUCCESS;
104+
}
105+
return GRIB_INVALID_ARGUMENT;
106+
}
107+
95108
if (STR_EQUAL(e->name, "missing")) {
96109
const char* p = grib_arguments_get_name(h, e->args, 0);
97110
if (p) {

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ if( HAVE_BUILD_TOOLS )
104104
grib_ifsParam
105105
grib_packing_order
106106
filter_substr
107+
filter_size
107108
filter_is_in_list
108109
filter_transient_darray
109110
grib_uerra

tests/filter_size.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
# (C) Copyright 2005- ECMWF.
3+
#
4+
# This software is licensed under the terms of the Apache Licence Version 2.0
5+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
#
7+
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8+
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9+
#
10+
11+
. ./include.ctest.sh
12+
13+
label="filter_size_test"
14+
tempFilt=temp.$label.filt
15+
tempOut=temp.$label.txt
16+
17+
cat > $tempFilt <<EOF
18+
if (size(pl) == 64) {
19+
print "size of pl is 64";
20+
}
21+
if (size(pl) == 128) {
22+
print "size of pl is 128";
23+
}
24+
EOF
25+
26+
${tools_dir}/grib_filter $tempFilt $ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl > $tempOut
27+
grep "size of pl is 64" $tempOut
28+
29+
${tools_dir}/grib_filter $tempFilt $ECCODES_SAMPLES_PATH/reduced_gg_pl_64_grib2.tmpl > $tempOut
30+
grep "size of pl is 128" $tempOut
31+
32+
# Clean up
33+
rm -f $tempFilt $tempOut

0 commit comments

Comments
 (0)