File: bufr_compare.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 (394 lines) | stat: -rwxr-xr-x 11,392 bytes parent folder | download | duplicates (2)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/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

# Enter data dir
cd ${data_dir}/bufr

# Define a common label for all the tmp files
label="bufr_compare_test"

# Create log file
fLog=temp.${label}".log"
rm -f $fLog
touch $fLog

# Define tmp bufr file
fBufrTmp=temp.${label}".bufr"
fBufrInput1=temp1.in.${label}".bufr"
fBufrInput2=temp2.in.${label}".bufr"

# Define filter rules file
fRules=temp.${label}.filter

#----------------------------------------------------
# Test: comparing same files
#----------------------------------------------------
f="syno_1.bufr"
echo "Test: comparing the same files" >> $fLog
echo "file: $f" >> $fLog
${tools_dir}/bufr_compare $f $f >> $fLog

#----------------------------------------------------
# Test: comparing two completely different files
#----------------------------------------------------
set +e
f1="syno_1.bufr"
f2="aaen_55.bufr"
echo "Test: comparing two completely different files" >> $fLog
echo "file: $f" >> $fLog
${tools_dir}/bufr_compare -v $f1 $f2 >> $fLog
if [ $? -eq 0 ]; then
   echo "ERROR: bufr_compare should have failed if files are completely different" >&2
   exit 1
fi
set -e

# Namespace options
set +e
${tools_dir}/bufr_compare -c ls:n $f1 $f2 >> $fLog
statusA=$?
${tools_dir}/bufr_compare -a -c ls:n $f1 $f2 >> $fLog
statusB=$?
set -e
[ $statusA -ne 0 ]
[ $statusB -ne 0 ]


#----------------------------------------------------
# Test: comparing with and without the -b switch
#----------------------------------------------------
f="syno_1.bufr"
echo "Test: comparing with and without the -b switch" >> $fLog
echo "file: $f" >> $fLog

# Alter a key in the file 
${tools_dir}/bufr_set -s dataCategory=2 $f ${fBufrTmp} >> $fLog

set +e
${tools_dir}/bufr_compare $f ${fBufrTmp}>> $fLog
if [ $? -eq 0 ]; then
   echo "ERROR: bufr_compare should have failed if files are different" >&2
   exit 1
fi
set -e

# Now compare with -b switch. No difference should be found.
${tools_dir}/bufr_compare -b dataCategory $f ${fBufrTmp}>> $fLog

#----------------------------------------------------
# Test: comparing with the -r switch
#----------------------------------------------------
# Create a bufr file with various message types
#cat syno_multi.bufr temp_101.bufr > $fBufrInput1 
#cat temp_101.bufr syno_multi.bufr > $fBufrInput2

#set +e
#${tools_dir}/bufr_compare ${fBufrInput1} ${fBufrInput2} >> $fLog
#if [ $? -eq 0 ]; then
#   echo "ERROR: bufr_compare should have failed if the message order in the files is different" >&2
#   exit 1
#fi
#set -e
#${tools_dir}/bufr_compare -r ${fBufrInput1} ${fBufrInput2}>> $fLog

#----------------------------------------------------
# Change subCentre and compare
#----------------------------------------------------
echo "Test: Change subCentre and compare" >> $fLog
${tools_dir}/bufr_set -s bufrHeaderSubCentre=12 aaen_55.bufr $fBufrTmp
set +e
${tools_dir}/bufr_compare aaen_55.bufr $fBufrTmp > $fLog 2>&1
status=$?
set -e
[ $status -eq 1 ]
fgrep -q "[bufrHeaderSubCentre]: [70] != [12]" $fLog

#----------------------------------------------------
# First argument of bufr_compare is a directory (error)
#----------------------------------------------------
echo "Test: First argument of bufr_compare is a directory (error)" >> $fLog
temp_dir=tempdir.${label}
mkdir -p $temp_dir
set +e
${tools_dir}/bufr_compare $temp_dir aaen_55.bufr >/dev/null
status=$?
set -e
[ $status -eq 1 ]
rm -fr $temp_dir

#----------------------------------------------------
# Second argument of bufr_compare is a directory
#----------------------------------------------------
echo "Test: Second argument of bufr_compare is a directory" >> $fLog
temp_dir=tempdir.${label}
mkdir -p $temp_dir
infile=aaen_55.bufr
cp $infile $temp_dir
${tools_dir}/bufr_compare $infile $temp_dir >/dev/null
rm -fr $temp_dir

#----------------------------------------------------
# Compare attributes
#----------------------------------------------------
echo "Test: Compare attributes" >> $fLog
set +e
${tools_dir}/bufr_compare amv2_87.bufr amv3_87.bufr > $fLog 2>&1
status=$?
set -e
[ $status -eq 1 ]
grep -q "#1#pressure->percentConfidence" $fLog
grep -q "#1#windDirection->percentConfidence" $fLog
grep -q "#1#windSpeed->percentConfidence" $fLog
grep -q "#1#coldestClusterTemperature->percentConfidence" $fLog

#----------------------------------------------------
# Header-only mode
#----------------------------------------------------
echo "Test: Header-only mode" >> $fLog
f="syno_1.bufr"
cat > $fRules <<EOF
 set unpack=1;
 set relativeHumidity=27;
 set horizontalVisibility=1500;
 set pack=1;
 write;
EOF
${tools_dir}/codes_bufr_filter -o $fBufrTmp $fRules $f
# Header keys have not changed
${tools_dir}/bufr_compare -H $f $fBufrTmp

#----------------------------------------------------
# Compare two-way (symmetric mode)
#----------------------------------------------------
echo "Test: Compare two-way (symmetric mode)" >> $fLog
f=$ECCODES_SAMPLES_PATH/BUFR3.tmpl
# Add a local section
${tools_dir}/bufr_set -s section2Present=1 $f $fBufrTmp
# Compare A with B will pass
${tools_dir}/bufr_compare $f $fBufrTmp
# Compare with -2 option
set +e
${tools_dir}/bufr_compare -2 -v $f $fBufrTmp > $fLog 2>&1
status=$?
set -e
[ $status -eq 1 ]
grep Swapping $fLog

#----------------------------------------------------
# ECC-656: using relative comparison (-R) with 'all'
#----------------------------------------------------
echo "Test: ECC-656: using relative comparison (-R) with 'all'" >> $fLog
f='airc_142.bufr'
echo 'set unpack=1;set airTemperature=228; set height=1.037e+04; set pack=1; write;' |\
    ${tools_dir}/codes_bufr_filter -o $fBufrTmp - $f
${tools_dir}/bufr_compare -R airTemperature=0.004,height=0.001 $f $fBufrTmp
${tools_dir}/bufr_compare -R all=0.004 $f $fBufrTmp

#--------------------------------------------------------------------
# ECC-658: apply relative comparison (-R) to all ranks of a given key
#--------------------------------------------------------------------
echo "Test: ECC-658: apply relative comparison (-R) to all ranks of a given key" >> $fLog
f='PraticaTemp.bufr'
${tools_dir}/codes_bufr_filter -o $fBufrTmp - $f <<EOF
 set unpack=1;
 set #1#airTemperature=288.41;
 set #2#airTemperature=286.15;
 set #3#airTemperature=280.95;
 set #4#airTemperature=280.32;
 set #5#airTemperature=280.43;
 set pack=1;
 write;
EOF
# The relative differences are around 3.5e-5. Suppress all instances
${tools_dir}/bufr_compare -R airTemperature=4e-5 $f $fBufrTmp

#--------------------------------------------------------------------
# -d option
#--------------------------------------------------------------------
echo "Test: -d option" >> $fLog
f='PraticaTemp.bufr'
${tools_dir}/codes_bufr_filter -o $fBufrTmp - $f <<EOF
 set unpack=1;
 set #1#airTemperature=288.41;
 set pack=1;
 write;
EOF
set +e
${tools_dir}/bufr_compare -d $f $fBufrTmp
status=$?
set -e
[ $status -eq 1 ]
[ -f "error1_1.bufr" ]
[ -f "error2_1.bufr" ]
rm -f error1_1.bufr error2_1.bufr

#--------------------------------------------------------------------
# ECC-1283: string arrays
#--------------------------------------------------------------------
sample=$ECCODES_SAMPLES_PATH/BUFR4.tmpl
fBufrTmp1=temp1.${label}".bufr"
fBufrTmp2=temp2.${label}".bufr"

${tools_dir}/codes_bufr_filter -o $fBufrTmp1 - $sample <<EOF
 set numberOfSubsets = 3;
 set compressedData = 1;
 set unexpandedDescriptors = { 1015 };
 set stationOrSiteName = { "Black", "Rose", "Immortal" };
 set pack=1;
 write;
EOF
${tools_dir}/codes_bufr_filter -o $fBufrTmp2 - $sample <<EOF
 set numberOfSubsets = 3;
 set compressedData = 1;
 set unexpandedDescriptors = { 1015 };
 set stationOrSiteName = { "Black", "Rose", "Mortal" };
 set pack=1;
 write;
EOF
set +e
${tools_dir}/bufr_compare $fBufrTmp1 $fBufrTmp2 >$fLog
status=$?
set -e
[ $status -eq 1 ]
grep -q "string \[stationOrSiteName\] 1 out of 3 different" $fLog

${tools_dir}/bufr_compare -b stationOrSiteName $fBufrTmp1 $fBufrTmp2
rm -f $fBufrTmp1 $fBufrTmp2

# Comparing empty string with 'missing'
${tools_dir}/codes_bufr_filter -o $fBufrTmp1 - $sample <<EOF
 set numberOfSubsets = 3;
 set compressedData = 1;
 set unexpandedDescriptors = { 1015 };
 set stationOrSiteName = { "", "y", "x" };
 set pack=1;
 write;
EOF
${tools_dir}/codes_bufr_filter -o $fBufrTmp2 - $sample <<EOF
 set numberOfSubsets = 3;
 set compressedData = 1;
 set unexpandedDescriptors = { 1015 };
 # stationOrSiteName not set so all entries 'missing'
 set pack=1;
 write;
EOF
export ECCODES_BUFR_MULTI_ELEMENT_CONSTANT_ARRAYS=1
set +e
${tools_dir}/bufr_compare $fBufrTmp1 $fBufrTmp2 >$fLog
status=$?
set -e
[ $status -eq 1 ]
grep -q "string \[stationOrSiteName\] 2 out of 3 different" $fLog

${tools_dir}/bufr_compare -b stationOrSiteName $fBufrTmp1 $fBufrTmp2
unset ECCODES_BUFR_MULTI_ELEMENT_CONSTANT_ARRAYS
rm -f $fBufrTmp1 $fBufrTmp2

# Through index
# -------------
tempIndex1=temp.$label.1.idx
tempIndex2=temp.$label.2.idx
f=$ECCODES_SAMPLES_PATH/BUFR3_local.tmpl
${tools_dir}/bufr_set -s ident:s=66611 $f $fBufrTmp
${tools_dir}/bufr_index_build -N -o $tempIndex1 $f
${tools_dir}/bufr_index_build -N -o $tempIndex2 $fBufrTmp

set +e
${tools_dir}/bufr_compare  $tempIndex1 $tempIndex2
status=$?
set -e
[ $status -eq 1 ]

${tools_dir}/bufr_compare -bident -v $tempIndex1 $tempIndex2
rm -f $tempIndex1 $tempIndex2

# Fail to unpack
# ---------------
bufr1=vos308014_v3_26.bufr
bufr2=aaen_55.bufr
set +e
${tools_dir}/bufr_compare $bufr1 $bufr2 > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "Failed to unpack 1st message" $fLog

set +e
${tools_dir}/bufr_compare $bufr2 $bufr1 > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "Failed to unpack 2nd message" $fLog

# ----------------------------------------
# Summary mode (-f)
# ----------------------------------------
set +e
${tools_dir}/bufr_compare -f aaen_55.bufr aben_55.bufr > $fLog 2>&1
status=$?
set -e
[ $status -eq 1 ]
grep -q "Summary of different key values" $fLog

# More messages in 2nd file
count1=$(${tools_dir}/bufr_count syno_4.bufr)
count2=$(${tools_dir}/bufr_count syno_multi.bufr)
[ $count1 = 1 ]
[ $count2 = 3 ]
set +e
${tools_dir}/bufr_compare -f syno_4.bufr syno_multi.bufr > $fLog 2>&1
status=$?
set -e
[ $status -eq 1 ]
grep -q "Different number of messages" $fLog


# ----------------------------------------
# Unreadable message
# ----------------------------------------
echo BUFR > $fBufrTmp
set +e
${tools_dir}/bufr_compare $fBufrTmp $fBufrTmp > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "unreadable message" $fLog

# Options
# ----------
f1="aaen_55.bufr"
f2="aaen_55.bufr"
set +e
${tools_dir}/bufr_compare -H -c edition $f1 $f2 > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "options are incompatible" $fLog

set +e
${tools_dir}/bufr_compare -a edition $f1 $f2 > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]
grep -q "a option requires -c option" $fLog

set +e
${tools_dir}/bufr_compare nosuchfile $f1 > $fLog 2>&1
status=$?
set -e
[ $status -ne 0 ]



# Clean up
# -------------
rm -f $fLog $fBufrTmp $fBufrInput1 $fBufrInput2 $fRules