def test_wind(self): """Tests converting wind data into a spoken string""" for *wind, vardir, spoken in ( ("", "", "", None, "unknown"), ( "360", "12", "20", ["340", "020"], "three six zero (variable three four zero to zero two zero) at 12kt gusting to 20kt", ), ("000", "00", "", None, "Calm"), ("VRB", "5", "12", None, "Variable at 5kt gusting to 12kt"), ( "270", "10", "", ["240", "300"], "two seven zero (variable two four zero to three zero zero) at 10kt", ), ): wind = [ core.make_number(v, literal=(not i)) for i, v in enumerate(wind) ] if vardir: vardir = [ core.make_number(i, speak=i, literal=True) for i in vardir ] self.assertEqual(speech.wind(*wind, vardir), "Winds " + spoken)
def test_taf(self): """ Tests end-to-end TAF translation """ units = structs.Units(**static.core.NA_UNITS) line_data = { "altimeter": core.make_number("29.92", "2992"), "clouds": [core.make_cloud("BKN015CB")], "icing": ["611005"], "other": [], "turbulence": ["540553"], "visibility": core.make_number("3"), "wind_direction": core.make_number("360"), "wind_gust": core.make_number("20"), "wind_shear": "WS020/07040KT", "wind_speed": core.make_number("12"), "wx_codes": get_wx_codes(["+RA"])[1], } line_data.update({ k: "" for k in ( "raw", "end_time", "start_time", "transition_start", "probability", "type", "flight_rules", "sanitized", ) }) data = {"max_temp": "TX20/1518Z", "min_temp": "TN00/00", "remarks": ""} data.update({ k: "" for k in ("raw", "station", "time", "start_time", "end_time") }) data = structs.TafData(forecast=[structs.TafLineData(**line_data)], **data) line_trans = structs.TafLineTrans( altimeter="29.92 inHg (1013 hPa)", clouds="Broken layer at 1500ft (Cumulonimbus) - Reported AGL", icing="Light icing from 10000ft to 15000ft", turbulence= "Occasional moderate turbulence in clouds from 5500ft to 8500ft", visibility="3sm (4.8km)", wind_shear="Wind shear 2000ft from 070 at 40kt", wind="N-360 at 12kt gusting to 20kt", wx_codes="Heavy Rain", ) trans = structs.TafTrans( forecast=[line_trans], max_temp="Maximum temperature of 20°C (68°F) at 15-18:00Z", min_temp="Minimum temperature of 0°C (32°F) at 00:00Z", remarks={}, ) translated = translate.taf.translate_taf(data, units) self.assertIsInstance(translated, structs.TafTrans) for line in translated.forecast: self.assertIsInstance(line, structs.TafLineTrans) self.assertEqual(translated, trans)
def test_make_number(self): """ Tests Number dataclass generation from a number string """ self.assertIsNone(core.make_number("")) for num, value, spoken in ( ("1", 1, "one"), ("1.5", 1.5, "one point five"), ("060", 60, "six zero"), ("M10", -10, "minus one zero"), ("P6SM", None, "greater than six"), ("M1/4", None, "less than one quarter"), ): number = core.make_number(num) self.assertEqual(number.repr, num) self.assertEqual(number.value, value) self.assertEqual(number.spoken, spoken) for num, value, spoken, nmr, dnm, norm in ( ("1/4", 0.25, "one quarter", 1, 4, "1/4"), ("5/2", 2.5, "two and one half", 5, 2, "2 1/2"), ("3/4", 0.75, "three quarters", 3, 4, "3/4"), ("5/4", 1.25, "one and one quarter", 5, 4, "1 1/4"), ("11/4", 1.25, "one and one quarter", 5, 4, "1 1/4"), ): number = core.make_number(num) self.assertEqual(number.value, value) self.assertEqual(number.spoken, spoken) self.assertEqual(number.numerator, nmr) self.assertEqual(number.denominator, dnm) self.assertEqual(number.normalized, norm) self.assertEqual(core.make_number("1234", "A1234").repr, "A1234") number = core.make_number("040", speak="040") self.assertEqual(number.value, 40) self.assertEqual(number.spoken, "zero four zero")
def test_taf_line(self): """Tests converting TAF line data into into a single spoken string""" units = structs.Units(**static.core.NA_UNITS) line = { "altimeter": parse_altimeter("2992"), "clouds": [core.make_cloud("BKN015CB")], "end_time": core.make_timestamp("1206"), "icing": ["611005"], "other": [], "start_time": core.make_timestamp("1202"), "transition_start": None, "turbulence": ["540553"], "type": "FROM", "visibility": core.make_number("3"), "wind_direction": core.make_number("360"), "wind_gust": core.make_number("20"), "wind_shear": "WS020/07040KT", "wind_speed": core.make_number("12"), "wx_codes": get_wx_codes(["+RA"])[1], } line.update({ k: None for k in ("flight_rules", "probability", "raw", "sanitized") }) line = structs.TafLineData(**line) spoken = ( "From 2 to 6 zulu, Winds three six zero at 12kt gusting to 20kt. " "Wind shear 2000ft from zero seven zero at 40kt. Visibility three miles. " "Altimeter two nine point nine two. Heavy Rain. " "Broken layer at 1500ft (Cumulonimbus). " "Occasional moderate turbulence in clouds from 5500ft to 8500ft. " "Light icing from 10000ft to 15000ft") ret = speech.taf_line(line, units) self.assertIsInstance(ret, str) self.assertEqual(ret, spoken)
def _location(item: str) -> Location: """ Convert a location element to a Location object """ items = item.split() if not items: return None station, direction, distance = None, None, None if len(items) == 1: ilen = len(item) # MLB if ilen < 5: station = item # MKK360002 or KLGA220015 elif ilen in (9, 10) and item[-6:].isdigit(): station, direction, distance = item[:-6], item[-6:-3], item[-3:] # 10 WGON elif items[0].isdigit(): station, direction, distance = items[1][-3:], items[1][:-3], items[0] # GON 270010 elif items[1].isdigit(): station, direction, distance = items[0], items[1][:3], items[1][3:] # Convert non-null elements if direction: direction = core.make_number(direction) if distance: distance = core.make_number(distance) return Location(item, station, direction, distance)
def test_make_number_speech(self): """Tests Number generation speech overrides""" number = core.make_number("040", speak="040") self.assertEqual(number.value, 40) self.assertEqual(number.spoken, "zero four zero") number = core.make_number("100", literal=True) self.assertEqual(number.value, 100) self.assertEqual(number.spoken, "one zero zero")
def parse(rmk: str) -> RemarksData: """Finds temperature and dewpoint decimal values from the remarks""" rmkdata = {} for item in rmk.split(): if len(item) in [5, 9] and item[0] == "T" and item[1:].isdigit(): rmkdata["temperature_decimal"] = core.make_number( _tdec(item[1:5], None)) rmkdata["dewpoint_decimal"] = core.make_number( _tdec(item[5:], None)) return RemarksData(**rmkdata)
def test_metar(self): """ Tests end-to-end METAR translation """ units = structs.Units(**static.core.NA_UNITS) data = { "altimeter": core.make_number("29.92", "2992"), "clouds": [core.make_cloud("BKN015CB")], "dewpoint": core.make_number("M01"), "other": [], "temperature": core.make_number("03"), "visibility": core.make_number("3"), "wind_direction": core.make_number("360"), "wind_gust": core.make_number("20"), "wind_speed": core.make_number("12"), "wind_variable_direction": [ core.make_number("340"), core.make_number("020"), ], "wx_codes": get_wx_codes(["+RA"])[1], } data.update({ k: "" for k in ( "raw", "remarks", "station", "time", "flight_rules", "remarks_info", "runway_visibility", "sanitized", ) }) data = structs.MetarData(**data) trans = structs.MetarTrans( altimeter="29.92 inHg (1013 hPa)", clouds="Broken layer at 1500ft (Cumulonimbus) - Reported AGL", dewpoint="-1°C (30°F)", remarks={}, temperature="3°C (37°F)", visibility="3sm (4.8km)", wind="N-360 (variable 340 to 020) at 12kt gusting to 20kt", wx_codes="Heavy Rain", ) translated = translate.metar.translate_metar(data, units) self.assertIsInstance(translated, structs.MetarTrans) self.assertEqual(translated, trans)
def test_type_and_times(self): """Tests line start from type, time, and probability values""" for type, *times, prob, spoken in ( (None, None, None, None, ""), ("FROM", "2808", "2815", None, "From 8 to 15 zulu,"), ("FROM", "2822", "2903", None, "From 22 to 3 zulu,"), ("BECMG", "3010", None, None, "At 10 zulu becoming"), ( "PROB", "1303", "1305", "30", r"From 3 to 5 zulu, there's a 30% chance for", ), ( "INTER", "1303", "1305", "45", r"From 3 to 5 zulu, there's a 45% chance for intermittent", ), ("INTER", "2423", "2500", None, "From 23 to midnight zulu, intermittent"), ("TEMPO", "0102", "0103", None, "From 2 to 3 zulu, temporary"), ): times = [core.make_timestamp(time) for time in times] if prob is not None: prob = core.make_number(prob) ret = speech.type_and_times(type, *times, prob) self.assertIsInstance(ret, str) self.assertEqual(ret, spoken)
def parse_lines(lines: [str], units: Units, use_na: bool = True) -> [dict]: """ Returns a list of parsed line dictionaries """ parsed_lines = [] prob = "" while lines: raw_line = lines[0].strip() line = sanitize_line(raw_line) # Remove prob from the beginning of a line if line.startswith("PROB"): # Add standalone prob to next line if len(line) == 6: prob = line line = "" # Add to current line elif len(line) > 6: prob = line[:6] line = line[6:].strip() if line: parsed_line = (parse_na_line if use_na else parse_in_line)(line, units) for key in ("start_time", "end_time"): parsed_line[key] = core.make_timestamp(parsed_line[key]) parsed_line["probability"] = core.make_number(prob[4:]) parsed_line["raw"] = raw_line if prob: parsed_line[ "sanitized"] = prob + " " + parsed_line["sanitized"] prob = "" parsed_lines.append(parsed_line) lines.pop(0) return parsed_lines
def _thunder(line: str, size: int = 3) -> list: """ Parse thunder line into Number tuples """ ret = [] previous = None for item in _split_line(line, size=size, prefix=5, strip=" /"): if not item: ret.append(None) elif previous: ret.append((previous, core.make_number(item))) previous = None else: ret.append(None) previous = core.make_number(item) return ret
def get_temp_and_dew(wxdata: str) -> ([str], Number, Number): """ Returns the report list and removed temperature and dewpoint strings """ for i, item in reversed(list(enumerate(wxdata))): if "/" in item: # ///07 if item[0] == "/": item = "/" + item.lstrip("/") # 07/// elif item[-1] == "/": item = item.rstrip("/") + "/" tempdew = item.split("/") if len(tempdew) != 2: continue valid = True for j, temp in enumerate(tempdew): if temp in ["MM", "XX"]: tempdew[j] = "" elif not core.is_possible_temp(temp): valid = False break if valid: wxdata.pop(i) return (wxdata, *[core.make_number(t) for t in tempdew]) return wxdata, None, None
def parse_in(report: str, issued: date = None) -> (MetarData, Units): """ Parser for the International METAR variant """ units = Units(**IN_UNITS) resp = {"raw": report} resp["sanitized"], resp["remarks"], data = sanitize(report) data, resp["station"], resp["time"] = core.get_station_and_time(data) data, resp["runway_visibility"] = get_runway_visibility(data) if "CAVOK" not in data: data, resp["clouds"] = core.get_clouds(data) ( data, resp["wind_direction"], resp["wind_speed"], resp["wind_gust"], resp["wind_variable_direction"], ) = core.get_wind(data, units) data, resp["altimeter"] = get_altimeter(data, units, "IN") if "CAVOK" in data: resp["visibility"] = core.make_number("CAVOK") resp["clouds"] = [] data.remove("CAVOK") else: data, resp["visibility"] = core.get_visibility(data, units) data, resp["temperature"], resp["dewpoint"] = get_temp_and_dew(data) condition = core.get_flight_rules(resp["visibility"], core.get_ceiling(resp["clouds"])) resp["other"], resp["wx_codes"] = get_wx_codes(data) resp["flight_rules"] = FLIGHT_RULES[condition] resp["remarks_info"] = remarks.parse(resp["remarks"]) resp["time"] = core.make_timestamp(resp["time"], target_date=issued) return MetarData(**resp), units
def parse_in_line(line: str, units: Units) -> {str: str}: """ Parser for the International TAF forcast variant """ wxdata = core.dedupe(line.split()) wxdata = core.sanitize_report_list(wxdata) retwx = {"sanitized": " ".join(wxdata)} ( wxdata, retwx["type"], retwx["start_time"], retwx["end_time"], ) = get_type_and_times(wxdata) wxdata, retwx["wind_shear"] = get_wind_shear(wxdata) ( wxdata, retwx["wind_direction"], retwx["wind_speed"], retwx["wind_gust"], _, ) = core.get_wind(wxdata, units) if "CAVOK" in wxdata: retwx["visibility"] = core.make_number("CAVOK") retwx["clouds"] = [] wxdata.pop(wxdata.index("CAVOK")) else: wxdata, retwx["visibility"] = core.get_visibility(wxdata, units) wxdata, retwx["clouds"] = core.get_clouds(wxdata) ( retwx["other"], retwx["altimeter"], retwx["icing"], retwx["turbulence"], ) = get_alt_ice_turb(wxdata) return retwx
def _numbers( line: str, size: int = 3, prefix: str = "", postfix: str = "", decimal: int = None, literal: bool = False, special: dict = None, ) -> List[Number]: """Parse line into Number objects Prefix, postfix, and decimal location are applied to value, not repr Decimal is applied after prefix and postfix """ ret = [] for item in _split_line(line, size=size): value = None if item: value = prefix + item + postfix if decimal is not None: if abs(decimal) > len(value): value = value.zfill(abs(decimal)) value = value[:decimal] + "." + value[decimal:] ret.append(core.make_number(value, repr=item, literal=literal, special=special)) return ret
def parse_in_line(line: str, units: Units) -> Dict[str, str]: """Parser for the International TAF forcast variant""" data = core.dedupe(line.split()) data = sanitization.sanitize_report_list(data, remove_clr_and_skc=False) ret = {"sanitized": " ".join(data)} ( data, ret["type"], ret["start_time"], ret["end_time"], ret["transition_start"], ) = get_type_and_times(data) data, ret["wind_shear"] = get_wind_shear(data) ( data, ret["wind_direction"], ret["wind_speed"], ret["wind_gust"], _, ) = core.get_wind(data, units) if "CAVOK" in data: ret["visibility"] = core.make_number("CAVOK") ret["clouds"] = [] data.pop(data.index("CAVOK")) else: data, ret["visibility"] = core.get_visibility(data, units) data, ret["clouds"] = core.get_clouds(data) ( ret["other"], ret["altimeter"], ret["icing"], ret["turbulence"], ) = get_alt_ice_turb(data) return ret
def _altitude(item: str) -> "Number|str": """ Convert reporting altitude to a Number or string """ if item.isdigit(): return core.make_number(item) return item
def _numbers(line: str, size: int = 3, postfix: str = "") -> ["Number"]: """ Parse line into Number objects """ return [ core.make_number(item + postfix if item else item, repr=item) for item in _split_line(line, size=size) ]
def test_get_alt_ice_turb(self): """Tests that report global altimeter, icing, and turbulence get removed""" for wx, *data in ( (["1"], "", [], []), (["1", "512345", "612345"], "", ["612345"], ["512345"]), (["QNH1234", "1", "612345"], core.make_number("1234"), ["612345"], []), ): self.assertEqual(taf.get_alt_ice_turb(wx), (["1"], *data))
def test_parse(self): """Tests generating RemarksData from a remarks string""" for rmk, data in ( ("", (None, None)), ("T09870123", ("12.3", "98.7")), ("RMK AO2 SLP141 T02670189 $", ("18.9", "26.7")), ): data = [core.make_number(d) for d in data] self.assertEqual(remarks.parse(rmk), structs.RemarksData(*data))
def test_make_number(self): """Tests Number dataclass generation from a number string""" self.assertIsNone(core.make_number("")) for num, value, spoken in ( ("1", 1, "one"), ("1.5", 1.5, "one point five"), ("060", 60, "six zero"), ("300", 300, "three hundred"), ("25000", 25000, "two five thousand"), ("M10", -10, "minus one zero"), ("P6SM", None, "greater than six"), ("M1/4", None, "less than one quarter"), ): number = core.make_number(num) self.assertIsInstance(number, Number) self.assertEqual(number.repr, num) self.assertEqual(number.value, value) self.assertEqual(number.spoken, spoken) self.assertEqual(core.make_number("1234", "A1234").repr, "A1234")
def test_wind(self): """Tests that wind values are translating into a single string""" for *wind, vardir, translation in ( ("", "", "", None, ""), ( "360", "12", "20", ["340", "020"], "N-360 (variable 340 to 020) at 12kt gusting to 20kt", ), ("000", "00", "", None, "Calm"), ("VRB", "5", "12", None, "Variable at 5kt gusting to 12kt"), ("270", "10", "", ["240", "300"], "W-270 (variable 240 to 300) at 10kt"), ): wind = [core.make_number(i) for i in wind] if vardir: vardir = [core.make_number(i) for i in vardir] self.assertEqual(translate.base.wind(*wind, vardir), translation)
def test_metar(self): """ Tests converting METAR data into into a single spoken string """ units = structs.Units(**static.core.NA_UNITS) data = { "altimeter": parse_altimeter("2992"), "clouds": [core.make_cloud("BKN015CB")], "dewpoint": core.make_number("M01"), "other": [], "temperature": core.make_number("03"), "visibility": core.make_number("3"), "wind_direction": core.make_number("360"), "wind_gust": core.make_number("20"), "wind_speed": core.make_number("12"), "wind_variable_direction": [ core.make_number("340"), core.make_number("020", speak="020"), ], "wx_codes": get_wx_codes(["+RA"])[1], } data.update({ k: None for k in ( "raw", "remarks", "station", "time", "flight_rules", "remarks_info", "runway_visibility", "sanitized", ) }) data = structs.MetarData(**data) spoken = ( "Winds three six zero (variable three four zero to zero two zero) " "at 12kt gusting to 20kt. Visibility three miles. " "Temperature three degrees Celsius. Dew point minus one degree Celsius. " "Altimeter two nine point nine two. Heavy Rain. " "Broken layer at 1500ft (Cumulonimbus)") ret = speech.metar(data, units) self.assertIsInstance(ret, str) self.assertEqual(ret, spoken)
def test_shared(self): """Tests availability of shared values between the METAR and TAF translations""" units = structs.Units(**static.core.NA_UNITS) data = structs.SharedData( altimeter=core.make_number("2992"), clouds=[core.make_cloud("OVC060")], flight_rules="", other=[], sanitized="", visibility=core.make_number("10"), wind_direction=core.make_number("0"), wind_gust=core.make_number("0"), wind_speed=core.make_number("0"), wx_codes=get_wx_codes(["RA"])[1], ) trans = translate.base.current_shared(data, units) self.assertIsInstance(trans, dict) for key in ("altimeter", "clouds", "visibility", "wx_codes"): self.assertIn(key, trans) self.assertTrue(bool(trans[key]))
def _find_floor_ceiling(items: List[str]) -> Tuple[List[str], dict]: """Extracts the floor and ceiling from item list""" ret = {"floor": None, "ceiling": None} for i, item in enumerate(items): hloc = item.find("-") # TRACE RIME 070-090 if hloc > -1 and item[:hloc].isdigit() and item[hloc + 1:].isdigit(): for key, val in zip(("floor", "ceiling"), items.pop(i).split("-")): ret[key] = core.make_number(val) break # CONT LGT CHOP BLO 250 if item in _DIR_SIG: ret[_DIR_SIG[item]] = core.make_number(items[i + 1]) items = items[:i] break # LGT RIME 025 if item.isdigit(): num = core.make_number(item) ret["floor"], ret["ceiling"] = num, num break return items, ret
def test_temperature(self): """Tests temperature translation and conversion""" for temp, unit, translation in ( ("20", "F", "20°F (-7°C)"), ("M20", "F", "-20°F (-29°C)"), ("20", "C", "20°C (68°F)"), ("M20", "C", "-20°C (-4°F)"), ("", "F", ""), ): self.assertEqual( translate.base.temperature(core.make_number(temp), unit), translation )
def parse_altimeter(value: str) -> Number: """Parse an altimeter string into a Number""" if not value or len(value) < 4: return None # QNH3003INS if len(value) >= 7 and value.endswith("INS"): return core.make_number(value[-7:-5] + "." + value[-5:-3], value, literal=True) number = value.replace(".", "") # Q1000/10 if "/" in number: number = number.split("/")[0] if number.startswith("QNH"): number = "Q" + number[1:] if not (len(number) in (4, 5) and number[-4:].isdigit()): return None number = number.lstrip("AQ") if number[0] in ("2", "3"): number = number[:2] + "." + number[2:] elif number[0] not in ("0", "1"): return None return core.make_number(number, value, number, literal=True)
def test_temperature(self): """Tests converting a temperature into a spoken string""" for temp, unit, spoken in ( ("", "F", "unknown"), ("20", "F", "two zero degrees Fahrenheit"), ("M20", "F", "minus two zero degrees Fahrenheit"), ("20", "C", "two zero degrees Celsius"), ("1", "C", "one degree Celsius"), ): self.assertEqual( speech.temperature("Temp", core.make_number(temp), unit), "Temp " + spoken, )
def test_altimeter(self): """Tests altimeter translation and conversion""" for alt, repr, unit, translation in ( ("", "", "hPa", ""), ("1020", "1020", "hPa", "1020 hPa (30.12 inHg)"), ("0999", "0999", "hPa", "999 hPa (29.50 inHg)"), ("1012", "1012", "hPa", "1012 hPa (29.88 inHg)"), ("30.00", "3000", "inHg", "30.00 inHg (1016 hPa)"), ("29.92", "2992", "inHg", "29.92 inHg (1013 hPa)"), ("30.05", "3005", "inHg", "30.05 inHg (1018 hPa)"), ): self.assertEqual( translate.base.altimeter(core.make_number(alt, repr), unit), translation )
def test_visibility(self): """Tests converting visibility distance into a spoken string""" for vis, unit, spoken in ( ("", "m", "unknown"), ("0000", "m", "zero kilometers"), ("2000", "m", "two kilometers"), ("0900", "m", "point nine kilometers"), ("P6", "sm", "greater than six miles"), ("M1/4", "sm", "less than one quarter of a mile"), ("3/4", "sm", "three quarters of a mile"), ("3/2", "sm", "one and one half miles"), ("3", "sm", "three miles"), ): self.assertEqual(speech.visibility(core.make_number(vis), unit), "Visibility " + spoken)