File: grib_check_param_concepts.cc

package info (click to toggle)
eccodes 2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,404 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 283; xml: 183; awk: 66
file content (190 lines) | stat: -rw-r--r-- 8,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * (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.
 */

/*
 * Check GRIB2 parameter concept file e.g. shortName.def, paramId.def
 */

#include "expression/Long.h"
#include "expression/Functor.h"
#include "expression/String.h"
#include "expression/Binop.h"
#include "grib_api_internal.h"

static int type_of_surface_missing(const char* name, const char* value)
{
    /* Surface Type is Code Table 4.5 in which 255 is the same as missing */
    if (strcmp(value, "255") == 0) {
        return 1;
    }
    /* Beware of problems where we put 'missing()' for a code table key! */
    if (strncmp(value, "missing", 7) == 0) {
        fprintf(stderr, "Invalid value for %s Code Table entry: '%s'\n", name, value);
        ECCODES_ASSERT(0);
    }
    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;
}

#define STAT_PROC_MAX_VAL 103 // increase this with new Code Table 4.10 entries

/*
 * key      = paramId or shortName
 * filename = paramId.def or shortName.def
*/
static int grib_check_param_concepts(const char* key, const char* filename)
{
    int isLocal = 0;
    int count = 0;
    grib_concept_value* concept_value = grib_parse_concept_file(NULL, filename);
    if (!concept_value)
        return GRIB_IO_PROBLEM;

    if (strstr(filename, "/localConcepts/")) {
        isLocal = 1;
    }
    while (concept_value) {
        count++;
        if (strlen(concept_value->name) == 0) {
            fprintf(stderr, "%s %s: Empty concept value (count=%d)\n", key, concept_value->name, count);
            ECCODES_ASSERT(0);
        }
        grib_concept_condition* concept_condition = concept_value->conditions;
        /* 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 or sst) */
        if (strcmp(key, "cfVarName")==0) {
            ECCODES_ASSERT( strlen(concept_value->name) > 0 );
            if ( isdigit(concept_value->name[0]) || strcmp(concept_value->name, "~")==0 ) {
                fprintf(stderr, "%s %s: Invalid cfVarName in file %s\n",
                        key, concept_value->name, filename);
                return GRIB_INVALID_KEY_VALUE;
            }
        }
        while (concept_condition) {
            char condition_value[512] = {0, };
            eccodes::Expression* expression = concept_condition->expression;
            if(expression) {
                const char* condition_name  = concept_condition->name;
                /* printf("%s\n", concept_value->name); */
                /* condition_name is discipline, parameterCategory etc. */
                if (strcmp(expression->class_name(), "long") == 0) {
                    eccodes::expression::Long* el = dynamic_cast<eccodes::expression::Long*>(expression);
                    ECCODES_ASSERT(el);
                    long value;
                    el->evaluate_long(NULL, &value);
                    snprintf(condition_value, sizeof(condition_value), "%ld", value);
                }
                else if (strcmp(expression->class_name(), "functor") == 0) {
                    eccodes::expression::Functor* ef = dynamic_cast<eccodes::expression::Functor*>(expression);
                    ECCODES_ASSERT(ef);
                    snprintf(condition_value, sizeof(condition_value), "%s", ef->name());
                }
                else if (strcmp(expression->class_name(), "string") == 0) {
                    eccodes::expression::String* es = dynamic_cast<eccodes::expression::String*>(expression);
                    ECCODES_ASSERT(es);
                    int error;
                    const char* value = es->evaluate_string(NULL, NULL, NULL, &error);
                    snprintf(condition_value, sizeof(condition_value), "%s", value);
                }
                else if (strcmp(expression->class_name(), "binop") == 0) {
                    eccodes::expression::Binop* eb = dynamic_cast<eccodes::expression::Binop*>(expression);
                    ECCODES_ASSERT(eb);
                    eb->print(0, 0, stdout);
                    printf("\n");
                    // Not yet implemented
                }
                else {
                    fprintf(stderr, "%s %s: Unknown class name: '%s'\n",
                            key, concept_value->name, expression->class_name());
                    ECCODES_ASSERT(0);
                }
                if (!isLocal && strcmp(condition_name, "localTablesVersion") == 0) {
                    fprintf(stderr, "%s %s: Cannot have localTablesVersion key in WMO file %s!\n",
                            key, concept_value->name, filename);
                    return GRIB_INVALID_KEY_VALUE;
                }
                if (strcmp(condition_name, "typeOfStatisticalProcessing") == 0) {
                    long lValue = atol(condition_value);
                    if (lValue > STAT_PROC_MAX_VAL || lValue < 0) {
                        fprintf(stderr, "Bad value for %s in %s=%s in file %s\n",
                                condition_name, key, concept_value->name, filename);
                        return GRIB_INVALID_KEY_VALUE;
                    }
                }
                if (strcmp(condition_name, "typeOfFirstFixedSurface") == 0) {
                    type1Missing = type_of_surface_missing(condition_name, condition_value);
                }
                if (strcmp(condition_name, "typeOfSecondFixedSurface") == 0) {
                    type2Missing = type_of_surface_missing(condition_name, 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 = (strncmp(condition_value, "missing", 7) == 0);
                }
                if (strcmp(condition_name, "scaledValueOfSecondFixedSurface") == 0) {
                    scaledValue2Missing = (strncmp(condition_value, "missing", 7) == 0);
                }
            }
            concept_condition = concept_condition->next;
        }
        /* Now check the scale factor/value pairs */
        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 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;
        }
        concept_value = concept_value->next;
    }
    return GRIB_SUCCESS;
}

int main(int argc, char** argv)
{
    int err                       = 0;
    const char* concepts_key      = argv[1];
    const char* concepts_filename = argv[2];

    ECCODES_ASSERT(argc == 3);
    err = grib_check_param_concepts(concepts_key, concepts_filename);
    if (err) return err;

    printf("ALL OK\n");

    return 0;
}