rehanafzal commited on
Commit
fbf2a5a
·
verified ·
1 Parent(s): 1d2c4f6

Update app_backend.py

Browse files
Files changed (1) hide show
  1. app_backend.py +78 -78
app_backend.py CHANGED
@@ -45,99 +45,99 @@
45
  # code2
46
 
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # import pandas as pd
49
  # import numpy as np
50
  # from datetime import datetime, timedelta
51
- # import requests
52
 
53
- # # Function to fetch real-time weather data
54
- # def fetch_weather(api_key, location):
55
- # url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
56
- # response = requests.get(url).json()
57
- # if response["cod"] == 200:
58
- # return {
59
- # "temperature": response["main"]["temp"],
60
- # "wind_speed": response["wind"]["speed"],
61
- # "weather": response["weather"][0]["description"]
62
- # }
63
- # return None
64
 
65
- # # Generate synthetic data
66
  # def generate_synthetic_data():
67
  # time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
68
  # return pd.DataFrame({
69
  # "timestamp": time_index,
70
- # "total_power_consumption_mw": np.random.randint(200, 500, len(time_index)),
71
- # "grid_generation_mw": np.random.randint(100, 300, len(time_index)),
72
- # "storage_utilization_mw": np.random.randint(50, 150, len(time_index)),
 
73
  # })
74
 
75
- # # Generate storage data
76
  # def generate_storage_data():
 
 
 
 
77
  # return {
78
- # "wind": 5,
79
- # "solar": 7,
80
- # "turbine": 10,
81
- # "total_stored_kwh": 2000
82
  # }
83
 
84
- # # Export functions for use in Streamlit
85
- # if __name__ == "__main__":
86
- # print("Backend ready!")
87
-
88
-
89
- # code 3
90
- import pandas as pd
91
- import numpy as np
92
- from datetime import datetime, timedelta
93
-
94
- # Function to fetch weather data remains unchanged
95
-
96
- # Generate synthetic grid data
97
- def generate_synthetic_data():
98
- time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
99
- return pd.DataFrame({
100
- "timestamp": time_index,
101
- "power_consumption_mw": np.random.randint(50, 200, len(time_index)),
102
- "grid_generation_mw": np.random.randint(30, 150, len(time_index)),
103
- "storage_utilization_mw": np.random.randint(10, 50, len(time_index)),
104
- "grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
105
- })
106
-
107
- # Generate synthetic storage data
108
- def generate_storage_data():
109
- wind_storage = np.random.randint(5, 15)
110
- solar_storage = np.random.randint(7, 20)
111
- turbine_storage = np.random.randint(10, 25)
112
- total_storage = wind_storage + solar_storage + turbine_storage
113
- return {
114
- "wind_storage_mw": wind_storage,
115
- "solar_storage_mw": solar_storage,
116
- "turbine_storage_mw": turbine_storage,
117
- "total_storage_mw": total_storage
118
- }
119
-
120
- # Generate synthetic trade data
121
- def generate_trade_data():
122
- countries = ["Country A", "Country B", "Country C"]
123
- exports = np.random.randint(10, 50, len(countries))
124
- imports = np.random.randint(5, 30, len(countries))
125
- return pd.DataFrame({
126
- "country": countries,
127
- "exports_mw": exports,
128
- "imports_mw": imports
129
- })
130
 
131
- # Updated optimization recommendation
132
- def optimize_load(demand, generation, storage):
133
- if generation + storage >= demand:
134
- return "Grid is Stable with Current Supply"
135
- elif demand - (generation + storage) < 20:
136
- return "Activate Backup or Optimize Load"
137
- else:
138
- return "Immediate Action Required: Adjust Load or Increase Generation"
139
 
140
- # Export functions
141
- if __name__ == "__main__":
142
- print("Backend ready for enhanced dashboard!")
143
 
 
45
  # code2
46
 
47
 
48
+ import pandas as pd
49
+ import numpy as np
50
+ from datetime import datetime, timedelta
51
+ import requests
52
+
53
+ # Function to fetch real-time weather data
54
+ def fetch_weather(api_key, location):
55
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
56
+ response = requests.get(url).json()
57
+ if response["cod"] == 200:
58
+ return {
59
+ "temperature": response["main"]["temp"],
60
+ "wind_speed": response["wind"]["speed"],
61
+ "weather": response["weather"][0]["description"]
62
+ }
63
+ return None
64
+
65
+ # Generate synthetic data
66
+ def generate_synthetic_data():
67
+ time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
68
+ return pd.DataFrame({
69
+ "timestamp": time_index,
70
+ "total_power_consumption_mw": np.random.randint(200, 500, len(time_index)),
71
+ "grid_generation_mw": np.random.randint(100, 300, len(time_index)),
72
+ "storage_utilization_mw": np.random.randint(50, 150, len(time_index)),
73
+ })
74
+
75
+ # Generate storage data
76
+ def generate_storage_data():
77
+ return {
78
+ "wind": 5,
79
+ "solar": 7,
80
+ "turbine": 10,
81
+ "total_stored_kwh": 2000
82
+ }
83
+
84
+ # Export functions for use in Streamlit
85
+ if __name__ == "__main__":
86
+ print("Backend ready!")
87
+
88
+
89
+ # code 3
90
  # import pandas as pd
91
  # import numpy as np
92
  # from datetime import datetime, timedelta
 
93
 
94
+ # # Function to fetch weather data remains unchanged
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # # Generate synthetic grid data
97
  # def generate_synthetic_data():
98
  # time_index = pd.date_range(start=datetime.now(), periods=24, freq="H")
99
  # return pd.DataFrame({
100
  # "timestamp": time_index,
101
+ # "power_consumption_mw": np.random.randint(50, 200, len(time_index)),
102
+ # "grid_generation_mw": np.random.randint(30, 150, len(time_index)),
103
+ # "storage_utilization_mw": np.random.randint(10, 50, len(time_index)),
104
+ # "grid_health": np.random.choice(["Good", "Moderate", "Critical"], len(time_index))
105
  # })
106
 
107
+ # # Generate synthetic storage data
108
  # def generate_storage_data():
109
+ # wind_storage = np.random.randint(5, 15)
110
+ # solar_storage = np.random.randint(7, 20)
111
+ # turbine_storage = np.random.randint(10, 25)
112
+ # total_storage = wind_storage + solar_storage + turbine_storage
113
  # return {
114
+ # "wind_storage_mw": wind_storage,
115
+ # "solar_storage_mw": solar_storage,
116
+ # "turbine_storage_mw": turbine_storage,
117
+ # "total_storage_mw": total_storage
118
  # }
119
 
120
+ # # Generate synthetic trade data
121
+ # def generate_trade_data():
122
+ # countries = ["Country A", "Country B", "Country C"]
123
+ # exports = np.random.randint(10, 50, len(countries))
124
+ # imports = np.random.randint(5, 30, len(countries))
125
+ # return pd.DataFrame({
126
+ # "country": countries,
127
+ # "exports_mw": exports,
128
+ # "imports_mw": imports
129
+ # })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
+ # # Updated optimization recommendation
132
+ # def optimize_load(demand, generation, storage):
133
+ # if generation + storage >= demand:
134
+ # return "Grid is Stable with Current Supply"
135
+ # elif demand - (generation + storage) < 20:
136
+ # return "Activate Backup or Optimize Load"
137
+ # else:
138
+ # return "Immediate Action Required: Adjust Load or Increase Generation"
139
 
140
+ # # Export functions
141
+ # if __name__ == "__main__":
142
+ # print("Backend ready for enhanced dashboard!")
143