forked from ecmwf/eccodes-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufr.py
97 lines (75 loc) · 3.2 KB
/
bufr.py
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
"""
Classes for handling BUFR with a high level interface.
``BufrFiles`` can be treated mostly as regular files and used as context
managers, as can ``BufrMessages``. Each of these classes destructs itself and
any child instances appropriately.
Author: Daniel Lee, DWD, 2016
"""
from .. import eccodes
from .codesmessage import CodesMessage
from .codesfile import CodesFile
class BufrMessage(CodesMessage):
__doc__ = "\n".join(CodesMessage.__doc__.splitlines()[4:]).format(
prod_type="BUFR", classname="BufrMessage", parent="BufrFile", alias="bufr"
)
product_kind = eccodes.CODES_PRODUCT_BUFR
# Arguments included explicitly to support introspection
# TODO: Can we get this to work with an index?
def __init__(self, codes_file=None, clone=None, sample=None, headers_only=False):
"""
Open a message and inform the GRIB file that it's been incremented.
The message is taken from ``codes_file``, cloned from ``clone`` or
``sample``, or taken from ``index``, in that order of precedence.
"""
super(self.__class__, self).__init__(codes_file, clone, sample, headers_only)
# self._unpacked = False
# def get(self, key, ktype=None):
# """Return requested value, unpacking data values if necessary."""
# # TODO: Only do this if accessing arrays that need unpacking
# if not self._unpacked:
# self.unpacked = True
# return super(self.__class__, self).get(key, ktype)
# def missing(self, key):
# """
# Report if key is missing.#
#
# Overloaded due to confusing behaviour in ``codes_is_missing`` (SUP-1874).
# """
# return not bool(eccodes.codes_is_defined(self.codes_id, key))
def unpack(self):
"""Decode data section"""
eccodes.codes_set(self.codes_id, "unpack", 1)
def pack(self):
"""Encode data section"""
eccodes.codes_set(self.codes_id, "pack", 1)
def keys(self, namespace=None):
# self.unpack()
# return super(self.__class__, self).keys(namespace)
iterator = eccodes.codes_bufr_keys_iterator_new(self.codes_id)
keys = []
while eccodes.codes_bufr_keys_iterator_next(iterator):
key = eccodes.codes_bufr_keys_iterator_get_name(iterator)
keys.append(key)
eccodes.codes_bufr_keys_iterator_delete(iterator)
return keys
# @property
# def unpacked(self):
# return self._unpacked
# @unpacked.setter
# def unpacked(self, val):
# eccodes.codes_set(self.codes_id, "unpack", val)
# self._unpacked = val
# def __setitem__(self, key, value):
# """Set item and pack BUFR."""
# if not self._unpacked:
# self.unpacked = True
# super(self.__class__, self).__setitem__(key, value)
# eccodes.codes_set(self.codes_id, "pack", True)
def copy_data(self, destMsg):
"""Copy data values from this message to another message"""
return eccodes.codes_bufr_copy_data(self.codes_id, destMsg.codes_id)
class BufrFile(CodesFile):
__doc__ = "\n".join(CodesFile.__doc__.splitlines()[4:]).format(
prod_type="BUFR", classname="BufrFile", alias="bufr"
)
MessageClass = BufrMessage