| 1 | import logging |
| 2 | from datetime import datetime |
| 3 | from meteoclimatic import Station, Weather, Condition |
| 4 | from meteoclimatic.feed import FeedItemHelper |
| 5 | |
| 6 | |
| 7 | class Observation: |
| 8 | """ |
| 9 | A class representing the weather which is currently being observed from a |
| 10 | certain station. The station is represented by the encapsulated *Station* |
| 11 | object while the observed weather data are held by the encapsulated *Weather* |
| 12 | object. |
| 13 | |
| 14 | :param reception_time: Timestamp telling when the weather obervation has |
| 15 | been received from the station |
| 16 | :type reception_time: `datetime.datetime` |
| 17 | :param station: the *Station* relative to this observation |
| 18 | :type station: *Station* |
| 19 | :param weather: the *Weather* relative to this observation |
| 20 | :type weather: *Weather* |
| 21 | :returns: an *Observation* instance |
| 22 | :raises: *ValueError* when negative values are provided as reception time |
| 23 | """ |
| 24 | |
| 25 | _feed_datetime_format = "%a, %d %b %Y %H:%M:%S %z" |
| 26 | |
| 27 | def __init__(self, reception_time: datetime, station: Station, weather: Weather): |
| 28 | """Initialize the class.""" |
| 29 | if not isinstance(reception_time, datetime): |
| 30 | raise ValueError( |
| 31 | "reception_time is not an instance of datetime.datetime") |
| 32 | self.reception_time = reception_time |
| 33 | |
| 34 | if not isinstance(station, Station): |
| 35 | raise ValueError( |
| 36 | "station is not an instance of meteoclimatic.Station") |
| 37 | self.station = station |
| 38 | |
| 39 | if not isinstance(weather, Weather): |
| 40 | raise ValueError( |
| 41 | "weather is not an instance of meteoclimatic.Weather") |
| 42 | self.weather = weather |
| 43 | |
| 44 | @classmethod |
| 45 | def from_feed_item(cls, feed_item): |
| 46 | """ |
| 47 | Parses an *Observation* instance out of an RSS feed item. |
| 48 | :param feed_item: the input RSS feed item |
| 49 | :type feed_item: `bs4.element.Tag` |
| 50 | :returns: an *Observation* instance |
| 51 | :raises: *ValueError* if it is not possible to parse the data |
| 52 | """ |
| 53 | helper = FeedItemHelper(feed_item) |
| 54 | |
| 55 | station_name = feed_item.title.text |
| 56 | station_code = helper.get_text("station_code") |
| 57 | station_url = feed_item.link.text |
| 58 | station = Station(station_name, station_code, station_url) |
| 59 | |
| 60 | reception_time = datetime.strptime( |
| 61 | feed_item.pubDate.text, cls._feed_datetime_format) |
| 62 | |
| 63 | condition_str = helper.get_text("condition") |
| 64 | try: |
| 65 | condition = Condition(condition_str) |
| 66 | except ValueError: |
| 67 | logging.info( |
| 68 | "Unrecognized condidition '%s', using literal value instead of meteoclimatic.Condition" % (condition_str, )) |
| 69 | condition = condition_str |
| 70 | temp_current = helper.get_float("temp_current") |
| 71 | temp_max = helper.get_float("temp_max") |
| 72 | temp_min = helper.get_float("temp_min") |
| 73 | humidity_current = helper.get_float("humidity_current") |
| 74 | humidity_max = helper.get_float("humidity_max") |
| 75 | humidity_min = helper.get_float("humidity_min") |
| 76 | pressure_current = helper.get_float("pressure_current") |
| 77 | pressure_max = helper.get_float("pressure_max") |
| 78 | pressure_min = helper.get_float("pressure_min") |
| 79 | wind_current = helper.get_float("wind_current") |
| 80 | wind_max = helper.get_float("wind_max") |
| 81 | wind_bearing = helper.get_float("wind_bearing") |
| 82 | rain = helper.get_float("rain") |
| 83 | wind_max = helper.get_float("wind_max") |
| 84 | |
| 85 | weather = Weather(reception_time, condition, |
| 86 | temp_current, temp_max, temp_min, |
| 87 | humidity_current, humidity_max, humidity_min, |
| 88 | pressure_current, pressure_max, pressure_min, |
| 89 | wind_current, wind_max, wind_bearing, |
| 90 | rain) |
| 91 | |
| 92 | return cls(reception_time, station, weather) |
| 93 | |
| 94 | def __eq__(self, other): |
| 95 | if not isinstance(other, Observation): |
| 96 | return NotImplemented |
| 97 | prop_names = list(self.__dict__) |
| 98 | for prop in prop_names: |
| 99 | if self.__dict__[prop] != other.__dict__[prop]: |
| 100 | return False |
| 101 | return True |
| 102 | |
| 103 | def __repr__(self): |
| 104 | return "%s(%r)" % (self.__class__, self.__dict__) |