|
1 | 1 | """ |
2 | 2 | ############################################################################### |
3 | | -# Library to tranlate weather codes to human readable description text\n |
4 | | -# seeAlso: https://github.com/open-meteo/open-meteo/issues/287\n |
5 | | -#------------------------------------------------------------------------------\n |
6 | | -# Author: Michael Oberdorf\n |
7 | | -# Date: 2025-04-11\n |
8 | | -# Last modified by: Michael Oberdorf\n |
9 | | -# Last modified at: 2025-07-01\n |
| 3 | +# Library to translate weather codes to human readable description text |
| 4 | +# seeAlso: https://github.com/open-meteo/open-meteo/issues/287 |
| 5 | +#------------------------------------------------------------------------------ |
| 6 | +# Author: Michael Oberdorf |
| 7 | +# Date: 2025-04-11 |
| 8 | +# Last modified by: Michael Oberdorf |
| 9 | +# Last modified at: 2026-01-11 |
10 | 10 | ###############################################################################\n |
11 | 11 | """ |
12 | 12 |
|
13 | 13 | __author__ = "Michael Oberdorf <info@oberdorf-itc.de>" |
14 | 14 | __status__ = "production" |
15 | | -__date__ = "2025-07-01" |
16 | | -__version_info__ = ("0", "1", "1") |
| 15 | +__date__ = "2026-01-11" |
| 16 | +__version_info__ = ("1", "0", "0") |
17 | 17 | __version__ = ".".join(__version_info__) |
18 | 18 |
|
19 | | -__all__ = ["translate_weather_code"] |
20 | | - |
21 | | -_WEATHER_CODES = { |
22 | | - 0: "Clear sky", |
23 | | - 1: "Mainly clear", |
24 | | - 2: "Partly cloudy", |
25 | | - 3: "Cloudy", |
26 | | - 45: "Fog", |
27 | | - 48: "Freezing Fog and depositing rime fog", |
28 | | - 51: "Drizzle: Light intensity", |
29 | | - 53: "Drizzle: Moderate intensity", |
30 | | - 55: "Drizzle: Dense intensity", |
31 | | - 56: "Freezing Drizzle: Light intensity", |
32 | | - 57: "Freezing Drizzle: Dense intensity", |
33 | | - 61: "Rain: Slight intensity", |
34 | | - 63: "Rain: Moderate intensity", |
35 | | - 65: "Rain: Heavy intensity", |
36 | | - 66: "Freezing Rain: Light intensity", |
37 | | - 67: "Freezing Rain: Heavy intensity", |
38 | | - 71: "Snow fall: Slight intensity", |
39 | | - 73: "Snow fall: Moderate intensity", |
40 | | - 75: "Snow fall: Heavy intensity", |
41 | | - 77: "Snow Grains", |
42 | | - 80: "Rain showers: Slight intensity", |
43 | | - 81: "Rain showers: Moderate intensity", |
44 | | - 82: "Rain showers: Heavy intensity", |
45 | | - 85: "Snow showers: Slight intensity", |
46 | | - 86: "Snow showers: Heavy intensity", |
47 | | - 95: "Thunderstorm: Slight or moderate", |
48 | | - 96: "Thunderstorm with slight hail", |
49 | | - 99: "Thunderstorm with hail", |
50 | | - 100: "is not used", |
51 | | - 101: "Tornado", |
52 | | - 102: "Tropical storm", |
53 | | - 103: "Hurricane", |
54 | | -} |
55 | | - |
56 | | - |
57 | | -def translate_weather_code(code: int) -> str: |
| 19 | +__all__ = ["WeatherCodes"] |
| 20 | + |
| 21 | +import json |
| 22 | +import logging |
| 23 | +import os |
| 24 | + |
| 25 | + |
| 26 | +class WeatherCodes: |
58 | 27 | """ |
59 | | - Translate weather code to human readable description\n |
60 | | - :param code: Weather code\n |
61 | | - :return: Human readable description\n |
| 28 | + Class to translate weather codes to human readable description |
62 | 29 | """ |
63 | | - return _WEATHER_CODES.get(code, "Unknown weather code") |
| 30 | + |
| 31 | + __translations_path = os.path.join(__path__[0], "translations") |
| 32 | + |
| 33 | + def __init__(self, language: str = "en"): |
| 34 | + self.logger = logging.getLogger(__name__) |
| 35 | + |
| 36 | + # identify the weather code translation file |
| 37 | + weather_code_translation_file = os.path.join(self.__translations_path, f"{language}.json") |
| 38 | + if not os.path.isfile(weather_code_translation_file): |
| 39 | + self.logger.warning( |
| 40 | + f"Weather code translation file for language '{language}' not found. Falling back to default language 'en'." |
| 41 | + ) |
| 42 | + weather_code_translation_file = os.path.join(self.__translations_path, "en.json") |
| 43 | + |
| 44 | + # load the weather code translations |
| 45 | + with open(weather_code_translation_file, encoding="utf-8") as f: |
| 46 | + self.weather_codes = json.load(f) |
| 47 | + |
| 48 | + def translate(self, code: int) -> str: |
| 49 | + """ |
| 50 | + Translate weather code to human readable description |
| 51 | +
|
| 52 | + :param code: Weather code |
| 53 | + :return: Human readable description |
| 54 | + """ |
| 55 | + # transform the code to int and str to fit to the json keys |
| 56 | + code = str(int(code)) |
| 57 | + self.logger.debug(f"Translating weather code {code}") |
| 58 | + translation = self.weather_codes.get(code, "Unknown weather code") |
| 59 | + self.logger.debug(f"Weather code {code} translated to '{translation}'") |
| 60 | + return translation |
0 commit comments