avwx.parsing.translate.remarks

Remarks data translation handlers.

 1"""Remarks data translation handlers."""
 2
 3from __future__ import annotations
 4
 5from typing import TYPE_CHECKING
 6
 7if TYPE_CHECKING:
 8    from avwx.structs import Number, PressureTendency, RemarksData
 9
10
11def temp_minmax(label: str, code: Number) -> str:
12    """Translate a minimum or maximum temperature value."""
13    return f"6-hour {label} temperature {code.value}°C"
14
15
16def pressure_tendency(pressure: PressureTendency, unit: str = "mb") -> str:
17    """Translate a pressure outlook value.
18
19    Ex: "50123" -> 12.3 mb: Increasing, then decreasing
20    """
21    value = f"{pressure.change} {unit} - {pressure.tendency}"
22    return f"3-hour pressure difference: +/- {value}"
23
24
25def precip(label: str, code: Number, unit: str = "in") -> str:
26    """Translate a labelled precipitation value."""
27    return f"Precipitation in the last {label}: {code.value} {unit}"
28
29
30def sunshine_duration(code: Number, unit: str = "minutes") -> str:
31    """Translate a sunlight duration value."""
32    return f"Duration of sunlight: {code.value} {unit}"
33
34
35def snow_depth(code: Number, unit: str = "in") -> str:
36    """Translate a snow accumulation value."""
37    return f"Snow accumulation: {code.value} {unit}"
38
39
40def sea_level_pressure(code: Number) -> str:
41    """Translate a sea level pressure value."""
42    return f"Sea level pressure: {code.value} hPa"
43
44
45def remarks_data(data: RemarksData) -> dict[str, str]:
46    """Extract translations from parsed remarks data."""
47    ret = {}
48    if data.temperature_decimal and data.dewpoint_decimal:
49        temp, dew = data.temperature_decimal, data.dewpoint_decimal
50        key = f"T{temp.repr}{dew.repr}"
51        ret[key] = f"Temperature {temp.value}°C and dewpoint {dew.value}°C"
52    if data.minimum_temperature_24 and data.maximum_temperature_24:
53        minimum, maximum = data.minimum_temperature_24, data.maximum_temperature_24
54        key = f"4{minimum.repr}{maximum.repr}"
55        ret[key] = f"24-hour temperature: max {maximum.value} min {minimum.value}"
56    if data.maximum_temperature_6:
57        key = data.maximum_temperature_6.repr
58        ret[key] = temp_minmax("maximum", data.maximum_temperature_6)
59    if data.minimum_temperature_6:
60        key = data.minimum_temperature_6.repr
61        ret[key] = temp_minmax("minimum", data.minimum_temperature_6)
62    if data.precip_36_hours:
63        ret[data.precip_36_hours.repr] = precip("3/6 hours", data.precip_36_hours)
64    if data.precip_24_hours:
65        ret[data.precip_24_hours.repr] = precip("24 hours", data.precip_24_hours)
66    if data.precip_hourly:
67        ret[data.precip_hourly.repr] = precip("hour", data.precip_hourly)
68    if data.pressure_tendency:
69        ret[data.pressure_tendency.repr] = pressure_tendency(data.pressure_tendency)
70    if data.sunshine_minutes:
71        ret[data.sunshine_minutes.repr] = sunshine_duration(data.sunshine_minutes)
72    if data.snow_depth:
73        ret[data.snow_depth.repr] = snow_depth(data.snow_depth)
74    if data.sea_level_pressure:
75        ret[data.sea_level_pressure.repr] = sea_level_pressure(data.sea_level_pressure)
76    return ret
77
78
79def translate(raw: str | None, data: RemarksData | None) -> dict[str, str]:
80    """Translate elements in the remarks string."""
81    if not (raw and data):
82        return {}
83    # Add static codes
84    ret = {code.repr: code.value for code in data.codes}
85    # Add features from the parsed remarks data
86    ret |= remarks_data(data)
87    return ret
def temp_minmax(label: str, code: avwx.structs.Number) -> str:
12def temp_minmax(label: str, code: Number) -> str:
13    """Translate a minimum or maximum temperature value."""
14    return f"6-hour {label} temperature {code.value}°C"

Translate a minimum or maximum temperature value.

def pressure_tendency(pressure: avwx.structs.PressureTendency, unit: str = 'mb') -> str:
17def pressure_tendency(pressure: PressureTendency, unit: str = "mb") -> str:
18    """Translate a pressure outlook value.
19
20    Ex: "50123" -> 12.3 mb: Increasing, then decreasing
21    """
22    value = f"{pressure.change} {unit} - {pressure.tendency}"
23    return f"3-hour pressure difference: +/- {value}"

Translate a pressure outlook value.

Ex: "50123" -> 12.3 mb: Increasing, then decreasing

def precip(label: str, code: avwx.structs.Number, unit: str = 'in') -> str:
26def precip(label: str, code: Number, unit: str = "in") -> str:
27    """Translate a labelled precipitation value."""
28    return f"Precipitation in the last {label}: {code.value} {unit}"

Translate a labelled precipitation value.

def sunshine_duration(code: avwx.structs.Number, unit: str = 'minutes') -> str:
31def sunshine_duration(code: Number, unit: str = "minutes") -> str:
32    """Translate a sunlight duration value."""
33    return f"Duration of sunlight: {code.value} {unit}"

Translate a sunlight duration value.

def snow_depth(code: avwx.structs.Number, unit: str = 'in') -> str:
36def snow_depth(code: Number, unit: str = "in") -> str:
37    """Translate a snow accumulation value."""
38    return f"Snow accumulation: {code.value} {unit}"

Translate a snow accumulation value.

def sea_level_pressure(code: avwx.structs.Number) -> str:
41def sea_level_pressure(code: Number) -> str:
42    """Translate a sea level pressure value."""
43    return f"Sea level pressure: {code.value} hPa"

Translate a sea level pressure value.

def remarks_data(data: avwx.structs.RemarksData) -> dict[str, str]:
46def remarks_data(data: RemarksData) -> dict[str, str]:
47    """Extract translations from parsed remarks data."""
48    ret = {}
49    if data.temperature_decimal and data.dewpoint_decimal:
50        temp, dew = data.temperature_decimal, data.dewpoint_decimal
51        key = f"T{temp.repr}{dew.repr}"
52        ret[key] = f"Temperature {temp.value}°C and dewpoint {dew.value}°C"
53    if data.minimum_temperature_24 and data.maximum_temperature_24:
54        minimum, maximum = data.minimum_temperature_24, data.maximum_temperature_24
55        key = f"4{minimum.repr}{maximum.repr}"
56        ret[key] = f"24-hour temperature: max {maximum.value} min {minimum.value}"
57    if data.maximum_temperature_6:
58        key = data.maximum_temperature_6.repr
59        ret[key] = temp_minmax("maximum", data.maximum_temperature_6)
60    if data.minimum_temperature_6:
61        key = data.minimum_temperature_6.repr
62        ret[key] = temp_minmax("minimum", data.minimum_temperature_6)
63    if data.precip_36_hours:
64        ret[data.precip_36_hours.repr] = precip("3/6 hours", data.precip_36_hours)
65    if data.precip_24_hours:
66        ret[data.precip_24_hours.repr] = precip("24 hours", data.precip_24_hours)
67    if data.precip_hourly:
68        ret[data.precip_hourly.repr] = precip("hour", data.precip_hourly)
69    if data.pressure_tendency:
70        ret[data.pressure_tendency.repr] = pressure_tendency(data.pressure_tendency)
71    if data.sunshine_minutes:
72        ret[data.sunshine_minutes.repr] = sunshine_duration(data.sunshine_minutes)
73    if data.snow_depth:
74        ret[data.snow_depth.repr] = snow_depth(data.snow_depth)
75    if data.sea_level_pressure:
76        ret[data.sea_level_pressure.repr] = sea_level_pressure(data.sea_level_pressure)
77    return ret

Extract translations from parsed remarks data.

def translate(raw: str | None, data: avwx.structs.RemarksData | None) -> dict[str, str]:
80def translate(raw: str | None, data: RemarksData | None) -> dict[str, str]:
81    """Translate elements in the remarks string."""
82    if not (raw and data):
83        return {}
84    # Add static codes
85    ret = {code.repr: code.value for code in data.codes}
86    # Add features from the parsed remarks data
87    ret |= remarks_data(data)
88    return ret

Translate elements in the remarks string.