AI_Smart_Grid_System / app_backend.py
rehanafzal's picture
Update app_backend.py
b24634c verified
raw
history blame
4.93 kB
# 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!")
# code2
# import pandas as pd
# import numpy as np
# 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 data
# def generate_synthetic_data():
# time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
# return pd.DataFrame({
# "timestamp": time_index,
# "total_power_consumption_mw": np.random.randint(200, 500, len(time_index)),
# "grid_generation_mw": np.random.randint(100, 300, len(time_index)),
# "storage_utilization_mw": np.random.randint(50, 150, len(time_index)),
# })
# # Generate storage data
# def generate_storage_data():
# return {
# "wind": 5,
# "solar": 7,
# "turbine": 10,
# "total_stored_kwh": 2000
# }
# # Export functions for use in Streamlit
# if __name__ == "__main__":
# print("Backend ready!")
# code 3
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# Function to fetch weather data remains unchanged
# 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,
"power_consumption_mw": np.random.randint(50, 200, len(time_index)),
"grid_generation_mw": np.random.randint(30, 150, len(time_index)),
"storage_utilization_mw": np.random.randint(10, 50, len(time_index)),
"grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
})
# Generate synthetic storage data
def generate_storage_data():
wind_storage = np.random.randint(5, 15)
solar_storage = np.random.randint(7, 20)
turbine_storage = np.random.randint(10, 25)
total_storage = wind_storage + solar_storage + turbine_storage
return {
"wind_storage_mw": wind_storage,
"solar_storage_mw": solar_storage,
"turbine_storage_mw": turbine_storage,
"total_storage_mw": total_storage
}
# Generate synthetic trade data
def generate_trade_data():
countries = ["Country A", "Country B", "Country C"]
exports = np.random.randint(10, 50, len(countries))
imports = np.random.randint(5, 30, len(countries))
return pd.DataFrame({
"country": countries,
"exports_mw": exports,
"imports_mw": imports
})
# Updated optimization recommendation
def optimize_load(demand, generation, storage):
if generation + storage >= demand:
return "Grid is Stable with Current Supply"
elif demand - (generation + storage) < 20:
return "Activate Backup or Optimize Load"
else:
return "Immediate Action Required: Adjust Load or Increase Generation"
# Export functions
if __name__ == "__main__":
print("Backend ready for enhanced dashboard!")