def parse(station: str, report: str) -> (TafData, Units): """ Returns TafData and Units dataclasses with parsed data and their associated units """ if not report: return None, None valid_station(station) while len(report) > 3 and report[:4] in ("TAF ", "AMD ", "COR "): report = report[4:] retwx = { "end_time": None, "raw": report, "remarks": None, "start_time": None } report = _core.sanitize_report_string(report) _, station, time = _core.get_station_and_time(report[:20].split()) retwx["station"] = station retwx["time"] = _core.make_timestamp(time) report = report.replace(station, "") if time: report = report.replace(time, "").strip() if uses_na_format(station): use_na = True units = Units(**NA_UNITS) else: use_na = False units = Units(**IN_UNITS) # Find and remove remarks report, retwx["remarks"] = _core.get_taf_remarks(report) # Split and parse each line lines = _core.split_taf(report) parsed_lines = parse_lines(lines, units, use_na) # Perform additional info extract and corrections if parsed_lines: parsed_lines[-1]["other"], retwx["max_temp"], retwx[ "min_temp"] = _core.get_temp_min_and_max(parsed_lines[-1]["other"]) if not (retwx["max_temp"] or retwx["min_temp"]): parsed_lines[0]["other"], retwx["max_temp"], retwx[ "min_temp"] = _core.get_temp_min_and_max( parsed_lines[0]["other"]) # Set start and end times based on the first line start, end = parsed_lines[0]["start_time"], parsed_lines[0]["end_time"] parsed_lines[0]["end_time"] = None retwx["start_time"], retwx["end_time"] = start, end parsed_lines = _core.find_missing_taf_times(parsed_lines, start, end) parsed_lines = _core.get_taf_flight_rules(parsed_lines) # Extract Oceania-specific data if retwx["station"][0] == "A": parsed_lines[-1]["other"], retwx["alts"], retwx[ "temps"] = _core.get_oceania_temp_and_alt( parsed_lines[-1]["other"]) # Convert to dataclass retwx["forecast"] = [TafLineData(**line) for line in parsed_lines] return TafData(**retwx), units
def test_split_taf(self): """ Tests that TAF reports are split into the correct time periods """ for report, num in ( ("KJFK test", 1), ("KJFK test FM12345678 test", 2), ("KJFK test TEMPO test", 2), ("KJFK test TEMPO test TEMPO test", 3), ("KJFK test PROB30 test TEMPO test", 3), ("KJFK test PROB30 TEMPO test TEMPO test", 3), ): split = _core.split_taf(report) self.assertEqual(len(split), num) self.assertEqual(split[0], "KJFK test")