File: BUFR_Snippets.md

package info (click to toggle)
eccodes-python 2%3A2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,048 kB
  • sloc: python: 7,752; ansic: 280; sh: 94; makefile: 81; cpp: 30
file content (463 lines) | stat: -rw-r--r-- 12,221 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# BUFR Snippets

This document seeks to compile a collection of code snippets demonstrating common
(but also less common) BUFR-handling tasks, utilising both the low-level and the
high-level interface.

## Reading from file

### The low-level way

Read a single message:

```python
bufr = codes_bufr_new_from_file(file)
```

Iterate over all messages in the file:

```python
while bufr := codes_bufr_new_from_file(file):
    ...
    codes_release(bufr)
```

### The high-level way

Read a single message:

```python
bufr = BUFRMessage(file)
```

Iterate over all messages in the file:

```python
for bufr in FileReader(path, eccodes.CODES_PRODUCT_BUFR):
    ...
```

We can also use Python's `with` statement (aka context manager) to ensure
that `BUFRMessage` resources are properly released after its use:

```python
for bufr in FileReader(path, eccodes.CODES_PRODUCT_BUFR):
    with bufr:
        ...
```

## Accessing items

### The low-level way

Get value of a header key:

```python
value = codes_get(bufr, 'numberOfSubsets')
```

Get value of a data key:

```python
codes_set(bufr, 'unpack', 1)
value = codes_get_array(bufr, 'latitude')
```

### The high-level way

Get value of a header key:

```python
value = bufr['numberOfSubsets']
```

Get value of a data key:

```python
value = bufr['latitude']
```

Note that we didn't need to do explicit unpacking in order to access data section keys.
This was done for us automatically.
However, had we had a needed for a more precise control over when and where the message
gets unpacked, we could do that manually by calling the `unpack()` method:

```python
bufr.unpack()
# guaranteed to be in the unpacked state henceforth
```

After the first invocation, subsequent calls to `unpack()` will have no effect.

## Iterating over items

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
it = codes_bufr_keys_iterator_new(bufr)
while codes_bufr_keys_iterator_next(it):
    key = codes_bufr_keys_iterator_get_name(it)
    try:
        value = codes_get(bufr, key)
    except ArrayTooSmallError:
        value = codes_get_array(bufr, key)
    ...
codes_bufr_keys_iterator_release(it)
```

### The high-level way

```python
for key, value in bufr.items():
    ...
```

## Iterating over data items only

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
it = codes_bufr_keys_iterator_new(bufr)
while codes_bufr_keys_iterator_next(it):
    key = codes_bufr_keys_iterator_get_name(it)
    if codes_bufr_key_is_header(bufr, key):
        continue
    try:
        value = codes_get(bufr, key)
    except ArrayTooSmallError:
        value = codes_get_array(bufr, key)
    ...
codes_bufr_keys_iterator_release(it)
```

### The high-level way

```python
for key, value in bufr.data.items():
    ...
```

## Creating a new message from samples

### The low-level way

Note that 'numberOfSubsets', 'compressedData' and 'input...ReplicationFactor' must
be set _before_ the 'unexpandedDescriptor' key!

```python
bufr = codes_bufr_new_from_samples('BUFR4_local')
codes_set('masterTablesVersionNumber', 18)
codes_set('numberOfSubsets', 5)
codes_set('compressedData', 1)
codes_set_array('inputShortDelayedDescriptorReplicationFactor', [1, 1, 1, 1, 1, 1])
codes_set_array('unexpandedDescriptors', [311010])
```

### The high-level way

The same restrictions apply in the high-level case:

```python
bufr = BUFRMessage('BUFR4_local')
bufr['masterTablesVersionNumber'] = 18
bufr['numberOfSubsets'] = 5
bufr['compressedData'] = 1
bufr['inputShortDelayedDescriptorReplicationFactor'] = [1, 1, 1, 1, 1, 1]
bufr['unexpandedDescriptors'] = [311010]
```

(In the future version of eccodes-python we are planning to relax some of these restrictions.
Specifically, the need to set 'input...ReplicationFactor' upfront.)

## Creating a message copy with ECMWF's local section 2

### The low-level way

```python
old = codes_bufr_new_from_file(foreign_file)
new = codes_bufr_new_from_samples('BUFR4_local_satellite')
# Copy header keys (section 1 only)
it = codes_bufr_keys_iterator_new(old)
codes_skip_read_only(it)
while codes_bufr_keys_iterator_next(it):
    key = codes_bufr_keys_iterator_get_name(it)
    if key == 'unexpandedDescriptors':
        break
    value = codes_get(old, key)
    codes_set(new, value)
# Copy template-related keys
codes_set(old, 'unpack', 1)
value = codes_get_array(old, 'dataPresentIndicator')
codes_set_array(new, 'inputDataPresentIndicator', value)
value = codes_get_array(old, 'delayedDescriptorReplicationFactor')
codes_set_array(new, 'inputDelayedDescriptorReplicationFactor', value)
value = codes_get_array(old, 'unexpandedDescriptors')
codes_set_array(new, 'unexpandedDescriptors', value)
# Copy data keys
codes_copy_data(old, new)
# Set section 2 header keys
...
value = codes_get_array(old, 'latitude')'
codes_set(new, 'localLatitude1', min(value))
codes_set(new, 'localLatitude2', max(value))
...
codes_set(new, 'satelliteID', 123)
```

### The high-level way

```python
old = BUFRMessage(foreign_file)
new = BUFRMessage('BUFR4_local_satellite')
old.copy_to(new)
new['satelliteID'] = 123
```
Note that unlike in the low-level example, we don't need to set every section 2 key
explicitly. The only exception is the 'satelliteID' (or, in the case of uncompressed
messages, the 'ident' key).
All other section 2 keys are set automatically on message packing.

## Iterating over subsets of uncompressed multi-subset messages

### The low-level way

```python
subset_count = codes_get(bufr, 'numberOfSubsets')
codes_set(bufr, 'unpack', 1)
for n in range(1, subset_count + 1):
    print(f'Working on subset {n}')
    block_number = codes_get(bufr, f'/subsetNumber={n}/blockNumber')
    ...
    # BUT THIS IS CURRENTLY NOT ALLOWED!!!
    # pressure1 = codes_get(bufr, f'/subsetNumber={n}/#1#pressure')
    ...
```

### The high-level way

```python
for n, subset in enumerate(bufr.data, start=1):
    print(f'Working on subset {n}')
    block_number = subset['blockNumber']
    ...
    pressure1 = subset['#1#pressure']
    ...
```

## Working with uncompressed multi-subset messages with nested replications

The following is an example of a wind profiler BUFR message (see tests/sample-data/rwp\_jma.bufr).
Each subset in this uncompressed BUFR message represents a unique station.
Within each subset there is one static block at the top which contains some
common metadata, followed by a doubly-nested delayed replication block with
the actual profile data.
The outer replication corresponds to profiles measured at different times, and
the inner replication contains the individual level data for each of the profiles.
The tricky part about this message is that the number of profiles and the number
of levels varies across stations, which means we can't navigate the structure by doing a simple
rank arithmetic.
We have to employ more involved calculations in order to navigate the structure correctly.

### The low-level way

```python
subset_count = codes_get(bufr, 'numberOfSubsets')
codes_set(bufr, 'unpack', 1)
for n in range(1, subset_count + 1):
    codes_set(bufr, 'extractSubset', n)
    codes_set(bufr, 'doExtractSubsets', 1)
    sbufr = codes_clone(bufr)
    codes_set(sbufr, 'unpack', 1)
    # Station info
    ...
    latitude = codes_get(sbufr, 'latitude')
    ...
    factors = codes_get_array(sbufr, 'delayedDescriptorReplicationFactor')
    station_counts = factors[0]
    level_counts = factors[1:]
    assert len(level_counts) == station_counts
    level_offsets = [0] + list(itertools.accumulate(level_counts[:-1]))
    us = codes_get_array(sbufr, 'u')
    vs = codes_get_array(sbufr, 'u')
    ...
    for prof, (off, len) in enumerate(zip(level_offsets, level_counts)):
        # Profile info
        ...
        hour = codes_get_array(sbufr, 'hour')[prof]
        ...
        # Profile data
        ...
        u = np.ma.masked_equal(us[off:off+len], CODES_MISSING_DOUBLE)
        v = np.ma.masked_equal(vs[off:off+len], CODES_MISSING_DOUBLE)
        ...
    codes_release(sbufr)
```

### The high-level way

With the high-level interface, navigating the hierarchical structure is much simpler:

```python
for subset in bufr.data:
    station_info, station_profiles = subset
    # Station info
    ...
    latitude = station_info['latitude']
    ...
    for (profile_info, profile_data) in station_profiles:
        # Profile info
        ...
        hour = profile_info['hour']
        ...
        # Profile data
        ...
        u = profile_data['u']
        ...
```

## Extracting subsets by index

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
codes_set(bufr, 'extractSubsetList',  [2, 4, 8])
codes_set(bufr, 'doExtractSubsets', 1)
new = codes_clone(bufr)
```

### The high-level way

```python
new = bufr.copy(subsets=[1, 3, 7])
```
Note that unlike in the low-level example where we used 1-based subset
numbers, here we use subset indices which are 0-based!

## Extracting subsets by range / slice

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
codes_set(bufr, 'extractSubsetIntervalStart', 2)
codes_set(bufr, 'extractSubsetIntervalEnd',   8)
codes_set(bufr, 'doExtractSubsets', 1)
new = codes_clone(bufr)
```

### The high-level way

```python
new = bufr.copy(subsets=range(1, 9))
```
or
```python
new = bufr.copy(subsets=slice(1, 9))
```
Note that the bounds in the high-level example follow standard Python
convention, whereas in the low-level example they are 1-based and inclusive!

## Extracting subsets by date & time

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
start = datetime(2000, 2, 4, 8, 16)
end = start + timedelta(seconds=32)
for suffix, value in zip(('Start', 'End'), (start, end)):
    codes_set(bufr, 'extractDateTimeYear'   + suffix, value.year)
    codes_set(bufr, 'extractDateTimeMonth'  + suffix, value.month)
    codes_set(bufr, 'extractDateTimeDay'    + suffix, value.day)
    codes_set(bufr, 'extractDateTimeHour'   + suffix, value.hour)
    codes_set(bufr, 'extractDateTimeMinute' + suffix, value.minute)
    codes_set(bufr, 'extractDateTimeSecond' + suffix, value.second)
codes_set(bufr, 'doExtractDateTime', 1)
new = codes_clone(bufr)
```

### The high-level way

```python
start = datetime(2000, 2, 4, 8, 16)
end = start + timedelta(seconds=32)
new = bufr.copy(subsets=slice(start, end))
```
Note that the bounds of datetime slices are inclusive on both sides!

## Extracting subsets within a lat-lon area

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
codes_set(bufr, 'extractAreaNorthLatitude', 64.0)
codes_set(bufr, 'extractAreaSouthLatitude', 32.0)
codes_set(bufr, 'extractAreaEastLongitude', 16.0)
codes_set(bufr, 'extractAreaWestLongitude',  8.0)
codes_set(bufr, 'doExtractArea', 1)
new = codes_clone(bufr)
```

### The high-level way

Unlike in the previous examples, there is no built-in option to extract subsets by area.
However, we can pass an arbitrary boolean mask argument to the `copy()`
method, which provides unlimited flexibility:

```python
lat = bufr['latitude']
lon = bufr['longitude']
lat_mask = np.logical_and(lat <= 64.0, lat >= 32.0)
lon_mask = np.logical_and(lon <= 16.0, lon >=  8.0)
mask = np.logical_and(lat_mask, lon_mask)
new = bufr.copy(subsets=mask)
```

## Thinning subsets

### The low-level way

```python
codes_set(bufr, 'unpack', 1)
codes_set(bufr, 'simpleThinningSkip', 4)
codes_set(bufr, 'doSimpleThinning', 1)
new = codes_clone(bufr)
```
This will extract subset numbers 1, 6, 11, etc.

### The high-level way

Although the high-level interface doesn't provide bespoke option for
the thinning, we can achieve the same by using slices:

```python
new = bufr.copy(subsets=slice(None, None, 5))
```
Note that the slice step must be 1 larger than the thinning step to get the
same result.

## Writing to file

### The low-level way

```python
file = open('output.bufr', 'wb')
codes_set(bufr, 'pack', 1)
codes_write(bufr, file)
```

### The high-level way

```python
file = open('output.bufr', 'wb')
bufr.write_to(file)
```
Notice that in contrast to the low-level example, we didn't need to
pack the message explicitly before writing.
This was done for us automatically.
But, if needed, packing can also be triggered manually at any time by calling
the `pack()` method.