Testing: check surface types too. Cater for keys not present

This commit is contained in:
Shahram Najm 2021-02-11 15:02:11 +00:00
parent b26ad13104
commit dd5d513a86
1 changed files with 71 additions and 35 deletions

View File

@ -8,38 +8,62 @@
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
/*
* Check GRIB2 parameter concept file e.g. shortName.def, paramId.def
*/
#include <assert.h>
#include "grib_api_internal.h"
typedef struct grib_expression_long
{
typedef struct grib_expression_long {
grib_expression base;
/* Members defined in long */
long value;
} grib_expression_long;
typedef struct grib_expression_functor
{
typedef struct grib_expression_functor {
grib_expression base;
/* Members defined in functor */
char* name;
grib_arguments* args;
} grib_expression_functor;
typedef struct grib_expression_string
{
typedef struct grib_expression_string {
grib_expression base;
/* Members defined in string */
char* value;
} grib_expression_string;
static int type_of_surface_missing(const char* value)
{
/* Surface Type is Code Table 4.5 in which 255 is the same as missing */
if (strcmp(value, "missing") == 0 || strcmp(value, "255") == 0) {
return 1;
}
return 0;
}
static int scale_factor_missing(const char* value)
{
/* Scale factor is one octet so 255 is the same as missing */
if (strcmp(value, "missing") == 0 || strcmp(value, "255") == 0) {
return 1;
}
return 0;
}
static int grib_check_param_concepts(const char* key, const char* filename)
{
grib_concept_value* concept_value = grib_parse_concept_file(NULL, filename);
if (!concept_value)
return GRIB_IO_PROBLEM;
while (concept_value) {
grib_concept_condition* concept_condition = concept_value->conditions;
int scaleFactor1Missing = 0, scaleFactor2Missing = 0;
int scaledValue1Missing = 0, scaledValue2Missing = 0;
/* Convention:
* -1 key not present
* 0 key present and not missing
* 1 key present and missing
*/
int scaleFactor1Missing = -1, scaleFactor2Missing = -1;
int scaledValue1Missing = -1, scaledValue2Missing = -1;
int type1Missing = -1, type2Missing = -1;
int err = 0;
/* concept_value->name is the value of the key e.g. 151163 */
while (concept_condition) {
@ -47,42 +71,54 @@ static int grib_check_param_concepts(const char* key, const char* filename)
grib_expression* expression = concept_condition->expression;
const char* condition_name = concept_condition->name;
/* condition_name is discipline, parameterCategory etc. */
if (strcmp(expression->cclass->name,"long")==0) {
if (strcmp(expression->cclass->name, "long") == 0) {
grib_expression_long* el = (grib_expression_long*)expression;
sprintf(condition_value, "%ld", el->value);
} else if(strcmp(expression->cclass->name,"functor")==0) {
}
else if (strcmp(expression->cclass->name, "functor") == 0) {
grib_expression_functor* ef = (grib_expression_functor*)expression;
sprintf(condition_value, "%s", ef->name);
} else if(strcmp(expression->cclass->name,"string")==0) {
}
else if (strcmp(expression->cclass->name, "string") == 0) {
grib_expression_string* es = (grib_expression_string*)expression;
sprintf(condition_value, "%s", es->value);
} else {
}
else {
Assert(0);
}
if (strcmp(condition_name, "scaleFactorOfFirstFixedSurface")==0) {
/* scale factor is one octet so 255 is the same as missing */
if (strcmp(condition_value,"missing")==0 || strcmp(condition_value,"255")==0)
scaleFactor1Missing = 1;
if (strcmp(condition_name, "typeOfFirstFixedSurface") == 0) {
type1Missing = type_of_surface_missing(condition_value);
}
if (strcmp(condition_name, "scaleFactorOfSecondFixedSurface")==0) {
if (strcmp(condition_value,"missing")==0 || strcmp(condition_value,"255")==0)
scaleFactor2Missing = 1;
if (strcmp(condition_name, "typeOfSecondFixedSurface") == 0) {
type2Missing = type_of_surface_missing(condition_value);
}
if (strcmp(condition_name, "scaleFactorOfFirstFixedSurface") == 0) {
scaleFactor1Missing = scale_factor_missing(condition_value);
}
if (strcmp(condition_name, "scaleFactorOfSecondFixedSurface") == 0) {
scaleFactor2Missing = scale_factor_missing(condition_value);
}
if (strcmp(condition_name, "scaledValueOfFirstFixedSurface") == 0) {
scaledValue1Missing = (strcmp(condition_value, "missing") == 0);
}
if (strcmp(condition_name, "scaledValueOfSecondFixedSurface") == 0) {
scaledValue2Missing = (strcmp(condition_value, "missing") == 0);
}
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 (type1Missing == 1 && scaleFactor1Missing == 0 && scaledValue1Missing == 0) err = 1;
if (type2Missing == 1 && scaleFactor2Missing == 0 && scaledValue2Missing == 0) err = 1;
if (scaleFactor1Missing == 1 && scaledValue1Missing == 0) err = 1;
if (scaleFactor1Missing == 0 && scaledValue1Missing == 1) err = 1;
if (scaleFactor2Missing == 1 && scaledValue2Missing == 0) err = 1;
if (scaleFactor2Missing == 0 && scaledValue2Missing == 1) err = 1;
if (err) {
fprintf(stderr, "Error: Mismatched scale factor, scaled value keys for %s='%s'.\n"
" If the scale factor is missing so should the scaled value and vice versa\n",
fprintf(stderr,
"Error: Mismatched type of surface, scale factor, scaled value keys for %s='%s'.\n"
" If the type of surface is missing so should its scaled keys\n"
" If the scale factor is missing so should the scaled value and vice versa\n",
key, concept_value->name);
return GRIB_INVALID_KEY_VALUE;
}
@ -93,10 +129,10 @@ static int grib_check_param_concepts(const char* key, const char* filename)
int main(int argc, char** argv)
{
int err = 0;
const char* concepts_key = argv[1];
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;