File: attributes.py

package info (click to toggle)
eccodes-python 2%3A2.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,048 kB
  • sloc: python: 7,750; ansic: 280; sh: 94; makefile: 81; cpp: 30
file content (45 lines) | stat: -rwxr-xr-x 1,404 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
#!/usr/bin/env python3

# Copyright 2022- 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.

# This example shows how to read key attributes.
#
# Usage: attributes.py input.bufr

# flake8: noqa: F403
# flake8: noqa: F405

import sys

from eccodes import *
from eccodes.highlevel import *

ATTRIBUTES = ["code", "units", "scale", "reference", "width"]  # See BUFR Code Table B


def run_example(input, output=sys.stdout):
    # Loop over the messages in the file
    for number, bufr in enumerate(FileReader(input, CODES_PRODUCT_BUFR), start=1):
        print(f"messageNumber: {number}", file=output)

        # Loop over all keys in the message
        for name in bufr.keys():
            # Print element's attributes. Attributes themselves are keys.
            for attribute in ATTRIBUTES:
                try:
                    key = f"{name}->{attribute}"
                    value = bufr[key]
                    print(f"{key}: {value}", file=output)
                except eccodes.KeyValueNotFoundError:
                    pass


if __name__ == "__main__":
    run_example(sys.argv[1])