| """ |
| Weather Data Generator |
| Generate sample weather data for testing |
| """ |
|
|
| import random |
| from datetime import datetime, timedelta |
| from weather_data import WeatherData |
| from globals import DEFAULT_LOCATIONS |
|
|
| class DataGenerator: |
| def __init__(self): |
| self.locations = list(DEFAULT_LOCATIONS.keys()) |
| |
| def generate_temperature(self, base_temp=20): |
| """ |
| Generate realistic temperature |
| """ |
| variation = random.uniform(-10, 10) |
| return base_temp + variation |
| |
| def generate_humidity(self): |
| """ |
| Generate humidity percentage |
| """ |
| return random.uniform(30, 90) |
| |
| def generate_pressure(self): |
| """ |
| Generate atmospheric pressure |
| """ |
| return random.uniform(990, 1030) |
| |
| def generate_wind_speed(self): |
| """ |
| Generate wind speed |
| """ |
| return random.uniform(0, 60) |
| |
| def generate_wind_direction(self): |
| """ |
| Generate wind direction |
| """ |
| directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'] |
| return random.choice(directions) |
| |
| def generate_precipitation(self): |
| """ |
| Generate precipitation amount |
| """ |
| if random.random() < 0.7: |
| return 0 |
| return random.uniform(0, 30) |
| |
| def generate_conditions(self, temp, precip): |
| """ |
| Generate weather conditions description |
| """ |
| if precip > 10: |
| return "Rainy" |
| elif precip > 0: |
| return "Light Rain" |
| elif temp < 0: |
| return "Freezing" |
| elif temp > 30: |
| return "Hot" |
| elif random.random() < 0.3: |
| return "Cloudy" |
| else: |
| return "Clear" |
| |
| def generate_single_reading(self, location, timestamp): |
| """ |
| Generate a single weather reading |
| """ |
| weather = WeatherData(timestamp, location) |
| |
| |
| if location in ['Phoenix', 'Los Angeles']: |
| base_temp = 25 |
| elif location in ['Chicago', 'New York']: |
| base_temp = 15 |
| else: |
| base_temp = 20 |
| |
| temp = self.generate_temperature(base_temp) |
| humidity = self.generate_humidity() |
| pressure = self.generate_pressure() |
| wind_speed = self.generate_wind_speed() |
| wind_dir = self.generate_wind_direction() |
| precip = self.generate_precipitation() |
| |
| weather.set_temperature(temp) |
| weather.set_humidity(humidity) |
| weather.set_pressure(pressure) |
| weather.set_wind(wind_speed, wind_dir) |
| weather.set_precipitation(precip) |
| weather.conditions = self.generate_conditions(temp, precip) |
| |
| return weather |
| |
| def generate_dataset(self, days=7, readings_per_day=4): |
| """ |
| Generate a dataset of weather readings |
| """ |
| dataset = [] |
| start_date = datetime.now() - timedelta(days=days) |
| |
| for day in range(days): |
| for reading in range(readings_per_day): |
| timestamp = start_date + timedelta(days=day, hours=reading*6) |
| location = random.choice(self.locations) |
| |
| weather = self.generate_single_reading(location, timestamp) |
| dataset.append(weather) |
| |
| return dataset |
| |
| def generate_location_dataset(self, location, days=7): |
| """ |
| Generate dataset for specific location |
| """ |
| if location not in self.locations: |
| raise ValueError(f"Unknown location: {location}") |
| |
| dataset = [] |
| start_date = datetime.now() - timedelta(days=days) |
| |
| for day in range(days): |
| for hour in range(0, 24, 6): |
| timestamp = start_date + timedelta(days=day, hours=hour) |
| weather = self.generate_single_reading(location, timestamp) |
| dataset.append(weather) |
| |
| return dataset |
|
|