-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwind.py
53 lines (45 loc) · 1.64 KB
/
wind.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
import re
from aviation_weather.components import Component
from aviation_weather.exceptions import WindDecodeError
class Wind(Component):
# TODO: support conversion from KT to MPS (and vice versa)?
def __init__(self, raw):
"""Parse `raw` to create a new Wind object.
Args:
raw (str): The wind to be parsed.
Raises:
WindDecodeError: If `raw` could not be parsed.
"""
m = re.search(
r"\b(?P<direction>(?:\d{3}|VRB))(?P<speed>\d{2,3})(?:G(?P<gusts>\d{2,3}))?(?P<unit>(?:KT|MPS))"
r"(?: (?P<v_from>\d{3})V(?P<v_to>\d{3}))?\b",
raw
)
if not m:
raise WindDecodeError("Wind(%r) could not be parsed" % raw)
self.direction = m.group("direction")
if self.direction != "VRB":
self.direction = int(self.direction)
self.speed = int(m.group("speed"))
self.gusts = m.group("gusts")
if self.gusts:
self.gusts = int(self.gusts)
self.unit = m.group("unit")
self.variable = (m.group("v_from"), m.group("v_to"))
if self.variable == (None, None):
self.variable = None
else:
self.variable = (int(m.group("v_from")), int(m.group("v_to")))
@property
def raw(self):
if isinstance(self.direction, int):
raw = "%03d" % self.direction
else:
raw = self.direction
raw += "%02d" % self.speed
if self.gusts is not None:
raw += "G%02d" % self.gusts
raw += self.unit
if self.variable:
raw += " %03dV%03d" % self.variable
return raw