File: bufr_filter.sh

package info (click to toggle)
eccodes 2.44.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 150,256 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 274; xml: 183; awk: 66
file content (305 lines) | stat: -rwxr-xr-x 12,210 bytes parent folder | download | duplicates (3)
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
#!/bin/sh
set -e 

echo "-# The bufr_filter sequentially processes all bufr messages contained in the input files and applies the rules to each of them. \\n"

echo " Input messages can be written to the output by using the \"write\" statement. The write statement can be parameterised so that output "
echo " is sent to multiple files depending on key values used in the output file name. \\n"
echo " First we write a rules_file containing the following statement:\\n \\n"
echo "\\verbatim"
echo "write \"../data/split/[bufrHeaderCentre:i]_[dataCategory].bufr[editionNumber]\";"
echo "\\endverbatim\\n"
echo "Then we create the BUFR file multitype.bufr from three others: \\n"
echo "\\verbatim"
echo ">mkdir ../data/split \\n"
echo ">cat ../data/bufr/syno_1.bufr ../data/bufr/goes_87.bufr ../data/bufr/gosat.bufr > ../data/split/multitype.bufr \\n"
echo "\\endverbatim\\n"
echo "Applying this rules_file to the \"../data/split/multitype.bufr\" bufr file we obtain several files in the ../data/split directory containing "
echo " messages split according to their key values\\n "
echo "\\verbatim"

if [[ -d ../data/split ]] 
then
    rm -f ../data/split/* 
else
    mkdir ../data/split
fi

cat ../data/bufr/syno_1.bufr ../data/bufr/goes_87.bufr ../data/bufr/gosat.bufr > ../data/split/multitype.bufr


cat > rules_file <<EOF
write "../data/split/[bufrHeaderCentre:i]_[dataCategory].bufr[editionNumber]";
EOF

echo ">bufr_filter rules_file ../data/split/multitype.bufr"
echo ">ls ../data/split"

./bufr_filter rules_file ../data/split/multitype.bufr
ls ../data/split

echo "\\endverbatim\\n"

echo "-# The bufr <b>header</b> information can be accessed without unpacking the data. This rules_file: \\n"
echo "\\verbatim"
echo "print \"[bufrHeaderCentre] [bufrHeaderSubCentre] [masterTablesVersionNumber] [localTablesVersionNumber] [numberOfSubsets]\";"
echo "\\endverbatim\\n"
echo "will result in the following output: \\n"

echo "\\verbatim"
cat > rules_file <<EOF
print "[bufrHeaderCentre] [bufrHeaderSubCentre] [masterTablesVersionNumber] [localTablesVersionNumber] [numberOfSubsets]";
EOF

echo ">bufr_filter rules_file ../data/bufr/syno_multi.bufr"

./bufr_filter rules_file ../data/bufr/syno_multi.bufr

echo "\\endverbatim\\n"

# Unpack 

echo "-# To print values from the data section the messages have to be <b>unpacked</b>. To do that we need to set key <i>unpack</i> to 1. This rules_file: \\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "print \"block=[blockNumber] station=[stationNumber] lat=[latitude] lon=[longitude] t2=[airTemperatureAt2M]\";"
echo "\\endverbatim\\n"

echo "will print out some data values from the specified SYNOP bufr messages."

echo "\\verbatim"
cat > rules_file <<EOF
set unpack=1;
print "block=[blockNumber] station=[stationNumber] lat=[latitude] lon=[longitude] t2=[airTemperatureAt2M]";
EOF

echo ">bufr_filter rules_file ../data/bufr/syno_multi.bufr"

./bufr_filter rules_file ../data/bufr/syno_multi.bufr

echo "\\endverbatim\\n"

# Tranzient keys

echo "-# bufr_filter allows defining new keys with the <b>transient</b> keyword. \\n"
echo " We will further develop the previous example by creating a new key to combine the block number "
echo " and the station number into the full WMO station id: \\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "transient statid=1000*blockNumber+stationNumber;"
echo "print \"statid=[statid] lat=[latitude] lon=[longitude] t2=[airTemperatureAt2M]\";"
echo "\\endverbatim\\n"

echo "The result is:"

echo "\\verbatim"
cat > rules_file <<EOF
set unpack=1;
transient statid=1000*blockNumber+stationNumber;
print "statid=[statid] lat=[latitude] lon=[longitude] t2=[airTemperatureAt2M]";
EOF

echo ">bufr_filter rules_file ../data/bufr/syno_multi.bufr"

./bufr_filter rules_file ../data/bufr/syno_multi.bufr

echo "\\endverbatim\\n"
 
#Conditional statements

echo "-# We can use <b>conditional statements</b> in bufr_filter. The syntax is: \\n"
echo "\\verbatim"
echo "if ( condition ) { block of rules } else { block of rules }"
echo "\\endverbatim\\n"
echo "The condition can be made using ==,!= and joining single block conditions with || and &&. \\n"
echo "The statement can be any valid statement also another nested condition\\n"
echo "The rules_file below shows how to filter only SYNOP messages with a specific station id:"

echo "\\verbatim"
echo "set unpack=1;"
echo "transient statid=1000*blockNumber+stationNumber;"
echo ""
echo "if (dataCategory ==0 && statid == 1003) {"
echo "  write \"out.bufr\";"
echo "}"        
echo "\\endverbatim\\n"

#Switch

echo "-# The <b>switch</b> statement is an enhanced version of the if statement. Its syntax is the following:"
echo "\\verbatim"
echo "switch (key1) {"
echo "    case val1:"
echo "        # statements"
echo "    case val2:"
echo "        # statements"
echo "    default:"
echo "        # statements"
echo "}"
echo "\\endverbatim\\n"
echo "The value of the key given as argument to the switch statement is matched against the values specified in the case statements.\\n"
echo "If there is a match, then the statements corresponding to the matching case are executed.\\n"
echo "Otherwise, the default case is executed. The default case is mandatory if the case statements do not cover all the possibilities.\\n"
echo "The \"~\" operator can be used to match \"anything\".\\n\\n"


# Attributes 
 
echo "-# To access the keys' <b>attributes</b> use the -> operator. \\n"
echo " The example below prints the attributes of key <i>nonCoordinatePressure</i> from a SYNOP bufr message. \\n"

echo "\\verbatim"
echo "print \"nonCoordinatePressure=[nonCoordinatePressure] [nonCoordinatePressure->units]\";"
echo "print \"nonCoordinatePressure->code=[nonCoordinatePressure->code!06d]\";"
echo "print \"nonCoordinatePressure->scale=[nonCoordinatePressure->scale]\";"
echo "print \"nonCoordinatePressure->reference=[nonCoordinatePressure->reference]\";"
echo "print \"nonCoordinatePressure->width=[nonCoordinatePressure->width]\";"
echo "print \"nonCoordinatePressure->percentConfidence=[nonCoordinatePressure->percentConfidence] [nonCoordinatePressure->percentConfidence->units]\";"
echo "print \"nonCoordinatePressure->percentConfidence->code=[nonCoordinatePressure->percentConfidence->code!06d]\";"
echo "print \"nonCoordinatePressure->percentConfidence->scale=[nonCoordinatePressure->percentConfidence->scale]\";"
echo "print \"nonCoordinatePressure->percentConfidence->reference=[nonCoordinatePressure->percentConfidence->reference]\";"
echo "print \"nonCoordinatePressure->percentConfidence->width=[nonCoordinatePressure->percentConfidence->width]\";"
echo "\\endverbatim\\n"

echo "The result is:"

echo "\\verbatim"
cat > rules_file <<EOF
set unpack=1;
print "nonCoordinatePressure=[nonCoordinatePressure] [nonCoordinatePressure->units]";
print "nonCoordinatePressure->code=[nonCoordinatePressure->code!06d]";
print "nonCoordinatePressure->scale=[nonCoordinatePressure->scale]";
print "nonCoordinatePressure->reference=[nonCoordinatePressure->reference]";
print "nonCoordinatePressure->width=[nonCoordinatePressure->width]";
print "nonCoordinatePressure->percentConfidence=[nonCoordinatePressure->percentConfidence] [nonCoordinatePressure->percentConfidence->units]";
print "nonCoordinatePressure->percentConfidence->code=[nonCoordinatePressure->percentConfidence->code!06d]";
print "nonCoordinatePressure->percentConfidence->scale=[nonCoordinatePressure->percentConfidence->scale]";
print "nonCoordinatePressure->percentConfidence->reference=[nonCoordinatePressure->percentConfidence->reference]";
print "nonCoordinatePressure->percentConfidence->width=[nonCoordinatePressure->percentConfidence->width]";
EOF

echo ">bufr_filter rules_file ../data/bufr/syno_1.bufr"

./bufr_filter rules_file ../data/bufr/syno_1.bufr

echo "\\endverbatim\\n"

# Access by rank

echo "-# To <u>access keys by <b>rank</b></u> (i.e. by their occurrence in the message) use the # operator. \\n"
echo " The example below prints the value from the 4th occurrence of key <i>pressure</i> from a TEMP bufr message. "
echo " As a reference, we also print all the pressure values found in the message.\\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "print \"pressure=[#4#pressure] [#4#pressure->units]\";"
echo "print \"pressure=[pressure]\";"
echo "\\endverbatim\\n"

echo "The result is:"

echo "\\verbatim"
cat > rules_file <<EOF
set unpack=1;
print "pressure=[#4#pressure] [#4#pressure->units]";
print "pressure=[pressure]";
EOF

echo ">bufr_filter rules_file ../data/bufr/temp_101.bufr"

./bufr_filter rules_file ../data/bufr/temp_101.bufr

echo "\\endverbatim\\n"

# Access by condition

echo "-# It is possible to <u>access elements by <b>conditions</b></u> imposed on coordinate descriptors. \\n"
echo " The example below prints the temperature values on temperature significant levels from a TEMP bufr message."
echo " For temperature significant levels the key <i>verticalSoundingSignificance</i>=4 and this"
echo " is what we use in the condition:  \\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "print \"[/verticalSoundingSignificance=4/airTemperature]\";"
echo "\\endverbatim\\n"

echo "The result is:"

echo "\\verbatim"

cat > rules_file <<EOF
set unpack=1;
print "[/verticalSoundingSignificance=4/airTemperature]";
EOF

echo ">bufr_filter rules_file ../data/bufr/temp_101.bufr"

./bufr_filter rules_file ../data/bufr/temp_101.bufr

echo "\\endverbatim\\n"


# Access by condition 2

echo "-# Another example for accessing keys by condition is to read scatterometer data."
echo "File asca_139.bufr contains a single message with 2016 subsets in a compressed form."
echo "In this case each subset has exactly the same structure: they store one location with"
echo "several beams and one backscatter value in each beam. To print the backScatter values for beamIdentifier=2 from all the subsets" 
echo "we can simply define the condition like this: \\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "print \"/beamIdentifier=2/backscatter=[/beamIdentifier=2/backscatter]\";"
echo "\\endverbatim\\n"

echo "The result is:"
echo "\\verbatim"

cat > rules_file <<EOF
set unpack=1;
print "/beamIdentifier=2/backscatter=[/beamIdentifier=2/backscatter]";
EOF

echo ">bufr_filter rules_file ../data/bufr/asca_139.bufr"

./bufr_filter rules_file ../data/bufr/asca_139.bufr > tmp_file
head tmp_file
echo " and many more values ......"

echo "\\endverbatim\\n"

# Accessing subsets

echo "-# Accessing keys by condition provides the means to read individual <b>subsets</b> from uncompressed data."
echo "File synop_multi_subset.bufr contains a single message with several subsets in an uncompressed form."
echo "To access a given subset we can simply use key <b>subsetNumber</b> in the filter condition. The example below shows" 
echo "how to read the blockNumber, stationNumber and airTemperaturefor the first 3 subsets in the message: \\n"

echo "\\verbatim"
echo "set unpack=1;"
echo "print \"subsetNumber=1 blockNumber=[/subsetNumber=1/blockNumber] stationNumber=[/subsetNumber=1/stationNumber] airTempearture=[/subsetNumber=1/airTemperature]\";"
echo "print \"subsetNumber=2 blockNumber=[/subsetNumber=2/blockNumber] stationNumber=[/subsetNumber=2/stationNumber] airTempearture=[/subsetNumber=2/airTemperature]\";"
echo "print \"subsetNumber=3 blockNumber=[/subsetNumber=3/blockNumber] stationNumber=[/subsetNumber=3/stationNumber] airTempearture=[/subsetNumber=3/airTemperature]\";"
echo "\\endverbatim\\n"

echo "The result is:"
echo "\\verbatim"

cat > rules_file <<EOF
set unpack=1;
print "subsetNumber=1 blockNumber=[/subsetNumber=1/blockNumber] stationNumber=[/subsetNumber=1/stationNumber] airTempearture=[/subsetNumber=1/airTemperature]";
print "subsetNumber=2 blockNumber=[/subsetNumber=2/blockNumber] stationNumber=[/subsetNumber=2/stationNumber] airTempearture=[/subsetNumber=2/airTemperature]";
print "subsetNumber=3 blockNumber=[/subsetNumber=3/blockNumber] stationNumber=[/subsetNumber=3/stationNumber] airTempearture=[/subsetNumber=3/airTemperature]";
EOF

echo ">bufr_filter rules_file ../data/bufr/synop_multi_subset.bufr"

./bufr_filter rules_file ../data/bufr/synop_multi_subset.bufr > tmp_file
head tmp_file

echo "\\endverbatim\\n"

rm -f rules_file
rm -f tmp_file