Skip to content

Commit

Permalink
start to support weather types
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgryan committed Nov 9, 2024
1 parent d1fc5e6 commit d56d647
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 8 deletions.
28 changes: 25 additions & 3 deletions src/detaf/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dataclasses import dataclass, field
from enum import Enum
from collections import namedtuple
from detaf import wx


class Change(str, Enum):
Expand Down Expand Up @@ -52,7 +53,11 @@ class Cloud:
height: int


Phenomenon = Visibility | Wind
class Wx(str, Enum):
NO_SIGNIFICANT_WEATHER = "NSW"


Phenomenon = Visibility | Wind | Cloud


@dataclass
Expand Down Expand Up @@ -187,7 +192,7 @@ def parse_change(tokens, cursor=0):


def parse_phenomenon(tokens, cursor=0):
for parser in [parse_visibility, parse_wind, parse_cloud]:
for parser in [parse_visibility, parse_wind, parse_cloud, parse_nsw, parse_wx]:
phenomenon, cursor = parser(tokens, cursor)
if phenomenon:
return phenomenon, cursor
Expand Down Expand Up @@ -240,7 +245,24 @@ def parse_cloud(tokens, cursor=0):
return Cloud(description, height), cursor + 1
else:
return None, cursor



def parse_nsw(tokens, cursor=0):
token = peek(tokens, cursor)
if token == Wx.NO_SIGNIFICANT_WEATHER:
return Wx.NO_SIGNIFICANT_WEATHER, cursor + 1
else:
return None, cursor


def parse_wx(tokens, cursor=0):
token = peek(tokens, cursor)
obj = wx.parse(token)
if obj:
return obj, cursor + 1
else:
return None, cursor


def peek(tokens, cursor):
try:
Expand Down
88 changes: 88 additions & 0 deletions src/detaf/wx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from dataclasses import dataclass
from enum import Enum


class Proximity(str, Enum):
VICINITY = "VC"


class Intensity(str, Enum):
LIGHT = "-"
HEAVY = "+"
MODERATE = ""


class Descriptor(str, Enum):
PATCHES = "BC"
BLOWING = "BL"
LOW_DRIFTING = "DR"
FREEZING = "FZ"
SHALLOW = "MI"
PARTIAL = "PR"
SHOWER = "SH"
THUNDERSTORM = "TS"


class Precipitation(str, Enum):
DRIZZLE = "DZ"
HAIL = "GR"
SMALL_HAIL = "GS"
ICE_CRYSTALS = "IC"
ICE_PELLETS = "PL"
RAIN = "RA"
SNOW_GRAINS = "SG"
SNOW = "SN"
UNKNOWN_PRECIPITATION = "UP"


class Obscuration(str, Enum):
MIST = "BR"
WIDESPREAD_DUST = "DU"
FOG = "FG"
SMOKE = "FU"
HAZE = "HZ"
SAND = "SA"
VOLCANIC_ASH = "VA"


class Other(str, Enum):
DUSTSTORM = "DS"
FUNNEL_CLOUD = "FC"
SAND_WHIRLS = "PO"
SQUALLS = "SQ"
SANDSTORM = "SS"


@dataclass
class Wx:
proximity: Proximity | None = None
intensity: Intensity | None = None
descriptor: Descriptor | None = None
precipitation: Precipitation | None = None
obscuration: Obscuration | None = None
other: Other | None = None


def parse(token: str) -> Wx | None:
index = 0

# Intensity
intensity = None
for key in Intensity:
if token[index:].startswith(key):
intensity = key
index += len(key)
break

# Precipitation
precipitation = None
for key in Precipitation:
if token[index:].startswith(key):
precipitation = key
index += len(key)
break

if precipitation is None:
return None
else:
return Wx(intensity=intensity, precipitation=precipitation)
28 changes: 23 additions & 5 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import detaf
from detaf import wx


def test_integration():
Expand Down Expand Up @@ -64,16 +65,22 @@ def test_integration_eigw():
phenomena=[
detaf.Wind(140, 10),
detaf.Visibility(4000),
# TODO: other phenomena
wx.Wx(
intensity="-",
precipitation="DZ"
),
detaf.Cloud("BKN", 700)
]
),
# BECMG 0818/0820 9999 NSW SCT010 BKN015
detaf.WeatherCondition(
period=((8, 18), (8, 20)),
change=detaf.Change.BECMG,
phenomena=[
detaf.Visibility(9999)
# TODO: other phenomena
detaf.Visibility(9999),
detaf.Wx.NO_SIGNIFICANT_WEATHER,
detaf.Cloud("SCT", 1000),
detaf.Cloud("BKN", 1500)
]
),
# BECMG 0901/0903 15005KT
Expand Down Expand Up @@ -106,8 +113,12 @@ def test_integration_eigw():
change=detaf.Change.TEMPO,
probability=30,
phenomena=[
detaf.Visibility(4000)
# TODO: other phenomena
detaf.Visibility(4000),
wx.Wx(
intensity="-",
precipitation="DZ"
),
detaf.Cloud("BKN", 800)
]
),
]
Expand Down Expand Up @@ -174,3 +185,10 @@ def test_parse_weather_conditions(bulletin, expected):
def test_parse_visibility(bulletin, expected):
taf = detaf.parse(bulletin)
assert taf.weather_conditions[0].phenomena == expected


def test_parse_wx():
assert wx.parse("-DZ") == wx.Wx(
intensity=wx.Intensity.LIGHT,
precipitation=wx.Precipitation.DRIZZLE
)

0 comments on commit d56d647

Please sign in to comment.