-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskycondition.py
51 lines (43 loc) · 1.43 KB
/
skycondition.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
import re
from aviation_weather.components import Component
from aviation_weather.exceptions import SkyConditionDecodeError
class SkyCondition(Component):
TYPES = {
"VV": "vertical visibility",
"SKC": "clear",
"CLR": "clear",
"NCD": "no cloud detected",
"FEW": "few",
"SCT": "scattered",
"BKN": "broken",
"OVC": "overcast"
}
def __init__(self, raw):
"""Parse `raw` to create a new SkyCondition object.
Args:
raw (str): The sky condition to be parsed.
Raises:
SkyConditionDecodeError: If `raw` could not be parsed.
"""
m = re.search(
r"\b(?P<type>(?:%(types)s))(?P<height>\d{3})?(?P<cb_tcu>CB|TCU)?\b" % {"types": "|".join(SkyCondition.TYPES)},
raw
)
if not m:
raise SkyConditionDecodeError("SkyCondition(%r) could not be parsed" % raw)
self.type = m.group("type")
self.height = m.group("height")
if self.height:
self.height = int(m.group("height")) * 100
self.cumulonimbus = m.group("cb_tcu") == "CB"
self.towering_cumulus = m.group("cb_tcu") == "TCU"
@property
def raw(self):
raw = self.type
if self.height:
raw += "%03d" % (self.height // 100)
if self.cumulonimbus:
raw += "CB"
if self.towering_cumulus:
raw += "TCU"
return raw