Exemplo n.º 1
0
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
Exemplo n.º 2
0
 def test_get_temp_min_and_max(self):
     """
     Tests that temp max and min times are extracted and assigned properly
     """
     for wx, *temps in (
         (["1"], "", ""),
         (["1", "TX12/1316Z", "TNM03/1404Z"], "TX12/1316Z", "TNM03/1404Z"),
         (["1", "TM03/1404Z", "T12/1316Z"], "TX12/1316Z", "TNM03/1404Z"),
     ):
         self.assertEqual(_core.get_temp_min_and_max(wx), (["1"], *temps))
Exemplo n.º 3
0
 def test_get_temp_min_and_max(self):
     """
     Tests that temp max and min times are extracted and assigned properly
     """
     for wx, *temps in (
         (['1'], '', ''),
         (['1', 'TX12/1316Z', 'TNM03/1404Z'], 'TX12/1316Z', 'TNM03/1404Z'),
         (['1', 'TM03/1404Z', 'T12/1316Z'], 'TX12/1316Z', 'TNM03/1404Z'),
     ):
         self.assertEqual(_core.get_temp_min_and_max(wx), (['1'], *temps))