File: grib_data_quality_checks.sh

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 (240 lines) | stat: -rwxr-xr-x 8,244 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/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

# ---------------------------------------------------------
# Tests for data quality checks
# ---------------------------------------------------------
label="grib_data_quality_test"
tempOut=temp.1.${label}.out
tempLog=temp.1.${label}.log
tempErr=temp.${label}.err
tempGrib1=temp.${label}.grib1
tempGrib2=temp.${label}.grib2

sample_g1=$ECCODES_SAMPLES_PATH/GRIB1.tmpl
sample_g2=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
sample_ccsds=$ECCODES_SAMPLES_PATH/ccsds_grib2.tmpl

# Start with clean environment
unset ECCODES_GRIB_DATA_QUALITY_CHECKS
unset ECCODES_EXTRA_DEFINITION_PATH

echo "Data quality checks enabled. Packing samples should work"
# -------------------------------------------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
${tools_dir}/grib_copy -r $sample_g1 /dev/null
${tools_dir}/grib_copy -r $sample_g2 /dev/null
unset ECCODES_GRIB_DATA_QUALITY_CHECKS

# These input files are 2m temperature with min=221.76 and max=311.619
input1=${data_dir}/reduced_gaussian_surface.grib1
input2=${data_dir}/reduced_gaussian_surface.grib2
grib_check_key_equals $input1 paramId 167
grib_check_key_equals $input2 paramId 167

echo "Data quality checks disabled. Create huge values for temperature..."
# --------------------------------------------------------------------------
${tools_dir}/grib_set -s scaleValuesBy=100 $input1 $tempOut
${tools_dir}/grib_set -s scaleValuesBy=100 $input2 $tempOut

echo "Data quality checks enabled. Repacking should fail..."
# -----------------------------------------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
export ECCODES_DEBUG=-1
set +e
${tools_dir}/grib_copy -r $tempOut /dev/null  2>$tempErr
status=$?
set -e
[ $status -ne 0 ]
grep -q 'more than the allowable limit' $tempErr
unset ECCODES_DEBUG


echo "Data quality checks enabled but only as a warning. Repacking should pass..."
# --------------------------------------------------------------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=2
${tools_dir}/grib_copy -r $tempOut /dev/null  2>$tempErr
grep -q 'more than the allowable limit' $tempErr


echo "Data quality checks enabled. Scaling should fail..."
# --------------------------------------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
set +e
${tools_dir}/grib_set -s scaleValuesBy=100 $input1 $tempOut 2>$tempErr
status=$?
set -e
[ $status -ne 0 ]
grep -q 'GRIB1 simple packing: unable to set values' $tempErr
grep -q 'allowable limit' $tempErr

set +e
${tools_dir}/grib_set -s scaleValuesBy=100 $input2 $tempOut 2>$tempErr
status=$?
set -e
[ $status -ne 0 ]
grep -q 'GRIB2 simple packing: unable to set values' $tempErr
grep -q 'allowable limit' $tempErr


echo "Test limits which are doubles..."
# -------------------------------------
pid=262140 # has limits -3.5 and +3.5
${tools_dir}/grib_set -s paramId=$pid $input2 $tempGrib2
minval2=`${tools_dir}/grib_get -p param_value_min:d $tempGrib2`
maxval2=`${tools_dir}/grib_get -p param_value_max:d $tempGrib2`
[ "$minval2" = "-3.5" ]
[ "$maxval2" = "3.5"  ]

# Decode as strings
grib_check_key_equals $tempGrib2 'param_value_min:s,param_value_max:s' '-3.5 3.5'

set +e
${tools_dir}/grib_set -s scaleValuesBy=1.1 $tempGrib2 $tempOut 2>$tempErr
stat2=$?
set -e
[ $stat2 -ne 0 ]

# Should succeed. Change paramId first and then scale all values down
${tools_dir}/grib_set -s paramId=$pid,scaleValuesBy=0.01 $input2 $tempOut

echo "Test close to the limit..."
# ---------------------------------
# The GRIB2 sample has max values of 273. We need to use 1 for this test
${tools_dir}/grib_set -s paramId=$pid,values=1 $sample_g2 $tempGrib2
${tools_dir}/grib_set -s scaleValuesBy=3 $tempGrib2 $tempOut # OK
set +e
${tools_dir}/grib_set -s scaleValuesBy=3.6 $tempGrib2 $tempOut
status=$?
set -e
[ $status -ne 0 ]


# echo "Test limits with steps..."
# # -----------------------------
input1=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib1.tmpl
# # This sets the minimum to 1.1 but this should work for step=0
# ${tools_dir}/grib_set -s step=0,paramId=121,scaleValuesBy=1.1 $input1 $tempOut

# # But it must fail when step > 0
# set +e
# ${tools_dir}/grib_set -s step=6,paramId=121,scaleValuesBy=1.1 $input1 $tempOut
# status=$?
# set -e
# [ $status -ne 0 ]


echo "Override the defaults..."
# ------------------------------
tempDir=tempdir.$label
rm -rf $tempDir
mkdir -p $tempDir
# Change limits for 2m temperature (grid-point) and Temperature (spectral)
cat > $tempDir/param_limits.def <<EOF
 constant default_min_val = -1e9 : double_type, hidden;
 constant default_max_val = +1e9 : double_type, hidden;
 concept param_value_min(default_min_val) {
    0   = { paramId=167; }
    273 = { paramId=130; }
 } : double_type, hidden;
 concept param_value_max(default_max_val) {
    40000 = { paramId=167; }
    273   = { paramId=130; }
 } : double_type, hidden;
EOF


# Set limits based on a more complex condition
# ---------------------------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
export ECCODES_EXTRA_DEFINITION_PATH=$test_dir/$tempDir
cat > $tempDir/param_limits.def <<EOF
 constant default_min_val = -1e9 : double_type, hidden;
 constant default_max_val = +1e9 : double_type, hidden;
 concept param_value_min(default_min_val) {
    0 = { paramId=260509; }
 } : double_type, hidden;
 concept param_value_max(default_max_val) {
    400  = { paramId=260509; }
    1001 = { paramId=260509; one=(step % 2 == 0 && step > 4); }
 } : double_type, hidden;
EOF
# Step of 12 satisfies the condition: it is even and > 4
# The GRIB2 sample has max values of 273. We need to use 1 for this test
${tools_dir}/grib_set -s paramId=260509,step=12,values=1,scaleValuesBy=1000 $sample_g2 $tempGrib2

# Step of 0 doesn't satisfy the condition so will use 400
# The GRIB2 sample has max values of 273. We need to use 1 for this test
set +e
${tools_dir}/grib_set -s paramId=260509,values=1,scaleValuesBy=1000 $sample_g2 $tempGrib2
status=$?
set -e
[ $status -ne 0 ]
unset ECCODES_EXTRA_DEFINITION_PATH


# ECC-2141: Disable data quality checks when setting packingType
# --------------------------------------------------------------
if [ $HAVE_AEC -eq 1 ]; then
   unset ECCODES_GRIB_DATA_QUALITY_CHECKS
   input=${data_dir}/reduced_gaussian_surface.grib2
   grib_check_key_equals $input packingType grid_simple
   ${tools_dir}/grib_set -s scaleValuesBy=100 $input $tempOut
   ECCODES_GRIB_DATA_QUALITY_CHECKS=1 ${tools_dir}/grib_set -s packingType=grid_ccsds $tempOut $tempGrib2 > $tempLog 2>&1
   if [ -s $tempLog ]; then
      # -s = True if file exists and has a size greater than zero
      echo "Error: No output should have been generated!"
      exit 1
   fi
   grib_check_key_equals $tempGrib2 packingType grid_ccsds
fi


# Check CCSDS encoding too
# -------------------------
if [ $HAVE_AEC -eq 1 ]; then
   export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
   set +e
   ${tools_dir}/grib_set -s scaleValuesBy=1000 $sample_ccsds $tempGrib2 2>$tempErr
   status=$?
   set -e
   [ $status -ne 0 ]
fi

# Invalid shortName/name
# -------------------------
export ECCODES_GRIB_DATA_QUALITY_CHECKS=1
input2=${data_dir}/reduced_gaussian_surface.grib2
${tools_dir}/grib_set -s discipline=254 $input2 $tempOut
grib_check_key_equals $tempOut 'shortName' 'unknown'
set +e
${tools_dir}/grib_set -s scaleValuesBy=2  $tempOut $tempGrib2 2>$tempErr
status=$?
set -e
[ $status -ne 0 ]
grep -q "Invalid metadata: shortName='unknown'" $tempErr

# Invalid name (ECC-793)
${tools_dir}/grib_set -s paramId=129080 $input2 $tempOut 2>$tempErr
grib_check_key_equals $tempOut 'name' 'Experimental product'
# Repacking causes the values to be set
set +e
${tools_dir}/grib_set -r -s paramId=129080 $input2 $tempOut 2>$tempErr
status=$?
set -e
[ $status -ne 0 ]
grep -q "Invalid metadata: name='Experimental product'" $tempErr


# Clean up
rm -rf $tempDir
rm -f $tempOut $tempErr $tempGrib1 $tempGrib2 $tempLog