From 598f552ef1f133a516b9a707bdc92b36457d88e6 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 10 Feb 2021 16:03:11 +0000 Subject: [PATCH] Testing: check grib2 parameter concepts (scale factor/scaled value pair) --- tests/CMakeLists.txt | 1 + tests/grib_check_param_concepts.c | 90 ++++++++++++++++++++++++++++++ tests/grib_check_param_concepts.sh | 17 ++++-- 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/grib_check_param_concepts.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3281011f5..9d7bfc8f5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,6 +23,7 @@ list(APPEND test_bins gauss_sub grib_nearest_test grib_util_set_spec + grib_check_param_concepts grib_local_MeteoFrance grib_2nd_order_numValues grib_optimize_scaling diff --git a/tests/grib_check_param_concepts.c b/tests/grib_check_param_concepts.c new file mode 100644 index 000000000..75c8d81ee --- /dev/null +++ b/tests/grib_check_param_concepts.c @@ -0,0 +1,90 @@ +/* + * (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 +#include "grib_api_internal.h" + +typedef struct grib_expression_long +{ + grib_expression base; + /* Members defined in long */ + long value; +} grib_expression_long; + +typedef struct grib_expression_functor +{ + grib_expression base; + /* Members defined in functor */ + char* name; + grib_arguments* args; +} grib_expression_functor; + +static int grib_check_param_concepts(const char* key, const char* filename) +{ + grib_concept_value* concept_value = grib_parse_concept_file(NULL, filename); + while (concept_value) { + grib_concept_condition* concept_condition = concept_value->conditions; + int scaleFactor1Missing = 0, scaleFactor2Missing = 0; + int scaledValue1Missing = 0, scaledValue2Missing = 0; + int err = 0; + /* printf("\nconcept_value->name=%s\n", concept_value->name); // A given paramId e.g. 151163 */ + while (concept_condition) { + char condition_value[512] = {0,}; + grib_expression* expression = concept_condition->expression; + const char* condition_name = concept_condition->name; + /* printf(" condition_name->name=%s", condition_name); // e.g. discipline, parameterCategory */ + if(strcmp(expression->cclass->name,"long")==0) { + grib_expression_long* e = (grib_expression_long*)expression; + sprintf(condition_value, "%ld", e->value); + } else if(strcmp(expression->cclass->name,"functor")==0) { + grib_expression_functor* e = (grib_expression_functor*)expression; + sprintf(condition_value, "%s", e->name); + } else { + Assert(0); + } + if (strcmp(condition_name, "scaleFactorOfFirstFixedSurface")==0 && strcmp(condition_value,"missing")==0) + scaleFactor1Missing = 1; + if (strcmp(condition_name, "scaleFactorOfSecondFixedSurface")==0 && strcmp(condition_value,"missing")==0) + scaleFactor2Missing = 1; + if (strcmp(condition_name, "scaledValueOfFirstFixedSurface")==0 && strcmp(condition_value,"missing")==0) + scaledValue1Missing = 1; + if (strcmp(condition_name, "scaledValueOfSecondFixedSurface")==0 && strcmp(condition_value,"missing")==0) + scaledValue2Missing = 1; + + concept_condition = concept_condition->next; + } + /* Now check the scale factor/value pairs */ + if (scaleFactor1Missing && !scaledValue1Missing) err = 1; + if (!scaleFactor1Missing && scaledValue1Missing) err = 1; + if (scaleFactor2Missing && !scaledValue2Missing) err = 1; + if (!scaleFactor2Missing && scaledValue2Missing) err = 1; + if (err) { + fprintf(stderr, "Error: mismatched scale factor, scaled value keys for %s=%s\n", key, concept_value->name); + return 1; + } + concept_value = concept_value->next; + } + return 0; /* ALL OK */ +} + +int main(int argc, char** argv) +{ + int err = 0; + const char* concepts_key = argv[1]; + const char* concepts_filename = argv[2]; + + assert(argc == 3); + err = grib_check_param_concepts(concepts_key, concepts_filename); + if (err) return err; + + printf("ALL OK\n"); + + return 0; +} diff --git a/tests/grib_check_param_concepts.sh b/tests/grib_check_param_concepts.sh index e781c2827..e4d4090a3 100755 --- a/tests/grib_check_param_concepts.sh +++ b/tests/grib_check_param_concepts.sh @@ -10,12 +10,21 @@ . ./include.sh -REDIRECT=/dev/null - -# This script will check the following concept files: -# name.def paramId.def shortName.def units.def cfVarName.def +# +# This script will do various checks on the concepts files # +# First check the GRIB2 paramId.def on its own +# -------------------------------------------- +$EXEC ${test_dir}/grib_check_param_concepts paramId $ECCODES_DEFINITION_PATH/grib2/paramId.def +$EXEC ${test_dir}/grib_check_param_concepts paramId $ECCODES_DEFINITION_PATH/grib2/localConcepts/ecmf/paramId.def + +$EXEC ${test_dir}/grib_check_param_concepts shortName $ECCODES_DEFINITION_PATH/grib2/shortName.def +$EXEC ${test_dir}/grib_check_param_concepts shortName $ECCODES_DEFINITION_PATH/grib2/localConcepts/ecmf/shortName.def + + +# Check the group: name.def paramId.def shortName.def units.def cfVarName.def +# ---------------------------------------------------------------------------- # Check whether the Test::More Perl module is available set +e perl -e 'use Test::More;'