File size: 3,210 Bytes
3f046ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | """
Weather Data Class
Represents weather measurements and data
"""
from datetime import datetime
class WeatherData:
def __init__(self, timestamp, location):
self.timestamp = timestamp
self.location = location
self.temperature = None
self.humidity = None
self.pressure = None
self.wind_speed = None
self.wind_direction = None
self.precipitation = None
self.conditions = None
def set_temperature(self, temp_celsius):
"""
Set temperature in Celsius
"""
if temp_celsius < -100 or temp_celsius > 60:
raise ValueError("Temperature out of valid range")
self.temperature = temp_celsius
def get_temperature_fahrenheit(self):
"""
Get temperature in Fahrenheit
"""
if self.temperature is None:
return None
return (self.temperature * 9/5) + 32
def set_humidity(self, humidity_percent):
"""
Set humidity percentage
"""
if humidity_percent < 0 or humidity_percent > 100:
raise ValueError("Humidity must be between 0 and 100")
self.humidity = humidity_percent
def set_pressure(self, pressure_hpa):
"""
Set atmospheric pressure in hPa
"""
if pressure_hpa < 800 or pressure_hpa > 1100:
raise ValueError("Pressure out of valid range")
self.pressure = pressure_hpa
def set_wind(self, speed_kmh, direction):
"""
Set wind speed and direction
"""
if speed_kmh < 0:
raise ValueError("Wind speed cannot be negative")
self.wind_speed = speed_kmh
self.wind_direction = direction
def set_precipitation(self, precip_mm):
"""
Set precipitation in millimeters
"""
if precip_mm < 0:
raise ValueError("Precipitation cannot be negative")
self.precipitation = precip_mm
def is_extreme_weather(self):
"""
Check if conditions are extreme
"""
extreme = False
if self.temperature and (self.temperature < -20 or self.temperature > 40):
extreme = True
if self.wind_speed and self.wind_speed > 80:
extreme = True
if self.precipitation and self.precipitation > 50:
extreme = True
return extreme
def to_dict(self):
"""
Convert to dictionary
"""
return {
'timestamp': str(self.timestamp),
'location': self.location,
'temperature_c': self.temperature,
'temperature_f': self.get_temperature_fahrenheit(),
'humidity': self.humidity,
'pressure': self.pressure,
'wind_speed': self.wind_speed,
'wind_direction': self.wind_direction,
'precipitation': self.precipitation,
'conditions': self.conditions,
'extreme': self.is_extreme_weather()
}
def __str__(self):
return f"Weather at {self.location} on {self.timestamp}: {self.temperature}C, {self.humidity}% humidity"
|