Fake Weather Data with Faker

Creating realistic weather data for testing weather applications or data analysis

from faker import Faker
import random
from datetime import datetime, timedelta

fake = Faker()

def generate_weather_data(start_date, num_days):
    weather_data = []
    current_date = start_date
    for _ in range(num_days):
        temp_celsius = random.uniform(-10, 35)
            "date": current_date.isoformat(),
            "temperature_celsius": round(temp_celsius, 1),
            "temperature_fahrenheit": round((temp_celsius * 9/5) + 32, 1),
            "humidity": random.randint(30, 100),
            "wind_speed_kmh": round(random.uniform(0, 100), 1),
            "wind_direction": random.choice(["N", "NE", "E", "SE", "S", "SW", "W", "NW"]),
            "precipitation_mm": round(random.uniform(0, 50), 1),
            "condition": random.choice(["Sunny", "Partly Cloudy", "Cloudy", "Rainy", "Thunderstorm", "Snowy"]),
            "uv_index": random.randint(0, 11),
            "air_quality_index": random.randint(0, 500)
        current_date += timedelta(days=1)
    return weather_data

# Generate weather data for the next 7 days
start_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
weather_forecast = generate_weather_data(start_date, 7)

# Print the generated weather data
for day in weather_forecast:
    print(f"Date: {day['date']}")
    print(f"Temperature: {day['temperature_celsius']}°C / {day['temperature_fahrenheit']}°F")
    print(f"Humidity: {day['humidity']}%")
    print(f"Wind: {day['wind_speed_kmh']} km/h, Direction: {day['wind_direction']}")
    print(f"Precipitation: {day['precipitation_mm']} mm")
    print(f"Condition: {day['condition']}")
    print(f"UV Index: {day['uv_index']}")
    print(f"Air Quality Index: {day['air_quality_index']}")