Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| 🌤️ Mock väderdata för Sommar i P1 | |
| Genererar realistisk väderdata för alla episoder baserat på historiska svenska vädermönster. | |
| """ | |
| import json | |
| import random | |
| import numpy as np | |
| from datetime import datetime | |
| from typing import Dict, List | |
| def generate_realistic_weather(date_str: str) -> Dict: | |
| """Generera realistisk väderdata baserat på datum och säsong""" | |
| try: | |
| date = datetime.strptime(date_str, "%Y-%m-%d") | |
| except: | |
| # Fallback for malformed dates | |
| date = datetime(2020, 7, 15) | |
| month = date.month | |
| day = date.day | |
| # Svenska säsongsmönster för sommarmånaderna (juni-augusti) | |
| if month == 6: # Juni | |
| base_temp = 16 + (day / 30) * 4 # 16-20°C progression | |
| rain_prob = 0.4 | |
| elif month == 7: # Juli | |
| base_temp = 18 + random.uniform(-2, 4) # 16-22°C | |
| rain_prob = 0.3 | |
| elif month == 8: # Augusti | |
| base_temp = 17 + (1 - day / 31) * 3 # 17-20°C, cooling | |
| rain_prob = 0.35 | |
| else: # Andra månader | |
| base_temp = 15 | |
| rain_prob = 0.5 | |
| # Lägg till slumpmässig variation | |
| temperature = base_temp + random.uniform(-3, 3) | |
| # Nederbörd baserat på sannolikhet | |
| if random.random() < rain_prob: | |
| precipitation = np.random.exponential(3) # Exponential distribution | |
| precipitation = min(precipitation, 25) # Cap at 25mm | |
| else: | |
| precipitation = 0 | |
| # Molnighet påverkas av nederbörd | |
| if precipitation > 1: | |
| cloud_cover = random.uniform(70, 95) | |
| elif precipitation > 0.1: | |
| cloud_cover = random.uniform(50, 80) | |
| else: | |
| cloud_cover = random.uniform(10, 60) | |
| # Vind - högre på regniga dagar | |
| wind_speed = random.uniform(2, 8) | |
| if precipitation > 5: | |
| wind_speed += random.uniform(2, 6) | |
| # Luftfuktighet | |
| humidity = 60 + (cloud_cover / 100) * 30 + random.uniform(-10, 10) | |
| humidity = max(30, min(95, humidity)) | |
| # Bestäm vädertillstånd | |
| condition = determine_condition(temperature, precipitation, cloud_cover) | |
| return { | |
| "date": date_str, | |
| "station": { | |
| "id": 98230, | |
| "name": "Stockholm", | |
| "region": "Mellansverige" | |
| }, | |
| "parameters": { | |
| 2: { | |
| "name": "Medeltemperatur", | |
| "value": round(temperature, 1), | |
| "unit": "°C", | |
| "quality": "simulated" | |
| }, | |
| 5: { | |
| "name": "Nederbörd dygn", | |
| "value": round(precipitation, 1), | |
| "unit": "mm", | |
| "quality": "simulated" | |
| }, | |
| 4: { | |
| "name": "Vindhastighet", | |
| "value": round(wind_speed, 1), | |
| "unit": "m/s", | |
| "quality": "simulated" | |
| }, | |
| 16: { | |
| "name": "Molnmängd", | |
| "value": round(cloud_cover, 0), | |
| "unit": "%", | |
| "quality": "simulated" | |
| }, | |
| 6: { | |
| "name": "Luftfuktighet", | |
| "value": round(humidity, 0), | |
| "unit": "%", | |
| "quality": "simulated" | |
| } | |
| }, | |
| "summary": generate_summary(temperature, precipitation, wind_speed, cloud_cover), | |
| "condition": condition | |
| } | |
| def determine_condition(temp: float, precip: float, clouds: float) -> str: | |
| """Bestäm vädertillstånd för ikon""" | |
| if precip > 5: | |
| return "rain" | |
| elif precip > 0.5: | |
| return "light_rain" | |
| elif temp > 25: | |
| if clouds < 30: | |
| return "sunny_hot" | |
| else: | |
| return "warm" | |
| elif temp < 10: | |
| return "cold" | |
| else: | |
| if clouds < 30: | |
| return "sunny" | |
| elif clouds > 70: | |
| return "cloudy" | |
| else: | |
| return "partly_cloudy" | |
| def generate_summary(temp: float, precip: float, wind: float, clouds: float) -> str: | |
| """Generera vädersammanfattning""" | |
| parts = [f"{temp:.1f}°C"] | |
| if precip > 5: | |
| parts.append(f"regn ({precip:.1f}mm)") | |
| elif precip > 0.5: | |
| parts.append(f"lätt regn ({precip:.1f}mm)") | |
| else: | |
| parts.append("torrt") | |
| if wind > 8: | |
| parts.append(f"blåsigt ({wind:.1f}m/s)") | |
| elif wind > 5: | |
| parts.append(f"måttlig vind") | |
| if clouds > 75: | |
| parts.append("mulet") | |
| elif clouds < 25: | |
| parts.append("klart") | |
| return ", ".join(parts) | |
| def add_weather_to_episodes(): | |
| """Lägg till mock väderdata till alla episoder""" | |
| print("🌤️ Genererar väderdata för Sommar i P1...") | |
| # Ladda episoder | |
| try: | |
| with open('data.json', 'r', encoding='utf-8') as f: | |
| episodes = json.load(f) | |
| print(f"✅ Laddade {len(episodes)} episoder") | |
| except Exception as e: | |
| print(f"❌ Kunde inte ladda episoder: {e}") | |
| return | |
| # Lägg till väderdata | |
| for i, episode in enumerate(episodes): | |
| if i % 100 == 0: | |
| print(f"📊 Bearbetar episod {i+1}/{len(episodes)}") | |
| episode_date = episode.get('episode_date', '2020-07-15') | |
| weather_data = generate_realistic_weather(episode_date) | |
| episode['weather_data'] = weather_data | |
| # Spara resultat | |
| output_file = 'data_with_weather.json' | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| json.dump(episodes, f, ensure_ascii=False, indent=2) | |
| print(f"✅ Mock väderdata tillagd! Sparad som: {output_file}") | |
| # Generera sammanfattning | |
| conditions = {} | |
| for episode in episodes: | |
| condition = episode['weather_data']['condition'] | |
| conditions[condition] = conditions.get(condition, 0) + 1 | |
| print("\n📊 Vädertillstånd:") | |
| for condition, count in sorted(conditions.items(), key=lambda x: x[1], reverse=True): | |
| print(f" {condition}: {count} episoder ({count/len(episodes)*100:.1f}%)") | |
| if __name__ == "__main__": | |
| add_weather_to_episodes() |