File size: 3,274 Bytes
4e21788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c93df57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e21788
c93df57
 
 
 
4e21788
 
 
 
c93df57
 
 
4e21788
c93df57
 
 
 
 
 
 
 
 
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
# import pandas as pd
# import numpy as np
# import plotly.express as px
# from datetime import datetime, timedelta
# import requests

# # Function to fetch real-time weather data
# def fetch_weather(api_key, location):
#     url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
#     response = requests.get(url).json()
#     if response["cod"] == 200:
#         return {
#             "temperature": response["main"]["temp"],
#             "wind_speed": response["wind"]["speed"],
#             "weather": response["weather"][0]["description"]
#         }
#     return None

# # Generate synthetic grid data
# def generate_synthetic_data():
#     time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
#     return pd.DataFrame({
#         "timestamp": time_index,
#         "total_consumption_kwh": np.random.randint(200, 500, len(time_index)),
#         "grid_generation_kwh": np.random.randint(150, 400, len(time_index)),
#         "storage_usage_kwh": np.random.randint(50, 150, len(time_index)),
#         "solar_output_kw": np.random.randint(50, 150, len(time_index)),
#         "wind_output_kw": np.random.randint(30, 120, len(time_index)),
#         "grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
#     })

# # Load optimization recommendation
# def optimize_load(demand, solar, wind):
#     renewable_supply = solar + wind
#     if renewable_supply >= demand:
#         return "Grid Stable"
#     return "Use Backup or Adjust Load"

# # Export functions for use in Streamlit
# if __name__ == "__main__":
#     print("Backend ready!")


import pandas as pd
import numpy as np
import plotly.express as px
from datetime import datetime, timedelta
import requests

# Function to fetch real-time weather data
def fetch_weather(api_key, location):
    url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
    response = requests.get(url).json()
    if response["cod"] == 200:
        return {
            "temperature": response["main"]["temp"],
            "wind_speed": response["wind"]["speed"],
            "weather": response["weather"][0]["description"]
        }
    return None

# Generate synthetic grid data in MW (for generation) and kWh (for load)
def generate_synthetic_data():
    time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
    return pd.DataFrame({
        "timestamp": time_index,
        "load_demand_mw": np.random.uniform(0.2, 0.5, len(time_index)),  # Load demand in MW
        "solar_output_mw": np.random.uniform(0.05, 0.15, len(time_index)),  # Solar output in MW
        "wind_output_mw": np.random.uniform(0.03, 0.12, len(time_index)),  # Wind output in MW
        "battery_storage_kwh": np.random.randint(100, 500, len(time_index)),  # Battery storage in kWh
        "grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
    })

# Load optimization recommendation in MW
def optimize_load(demand, solar, wind):
    renewable_supply = solar + wind
    if renewable_supply >= demand:
        return "Grid Stable"
    return "Use Backup or Adjust Load"

# Export functions for use in Streamlit
if __name__ == "__main__":
    print("Backend ready!")