Umar4321 commited on
Commit
dff1ff3
·
verified ·
1 Parent(s): b79dedd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import plotly.express as px
5
+
6
+ # ----------------------------
7
+ # Helper functions
8
+ # ----------------------------
9
+
10
+ def calculate_daily_load(appliances):
11
+ return (appliances['Power (W)'] *
12
+ appliances['Qty'] *
13
+ appliances['Hours/day']).sum()
14
+
15
+ def calculate_panels_needed(daily_load_wh, panel_watt, psh, perf_ratio, coverage):
16
+ energy_per_panel_wh = panel_watt * psh * perf_ratio
17
+ if energy_per_panel_wh == 0:
18
+ return 0
19
+ return int(np.ceil((daily_load_wh / coverage) / energy_per_panel_wh))
20
+
21
+ def tilt_suggestions(latitude):
22
+ return {
23
+ "Rule of Thumb (year-round)": round(latitude, 1),
24
+ "Summer (lat - 15°)": round(latitude - 15, 1),
25
+ "Winter (lat + 15°)": round(latitude + 15, 1)
26
+ }
27
+
28
+ def battery_sizing(daily_load_wh, autonomy_days, dod, inverter_eff, battery_capacity_Wh):
29
+ required_storage_Wh = daily_load_wh * autonomy_days
30
+ usable_per_battery = battery_capacity_Wh * dod * inverter_eff
31
+ if usable_per_battery == 0:
32
+ return 0
33
+ return int(np.ceil(required_storage_Wh / usable_per_battery))
34
+
35
+ def simulate_weekly_monthly(daily_load_wh, daily_prod_wh):
36
+ days = pd.date_range("2025-01-01", periods=365, freq="D")
37
+ df = pd.DataFrame({
38
+ "date": days,
39
+ "load_Wh": daily_load_wh,
40
+ "prod_Wh": daily_prod_wh
41
+ })
42
+ weekly = df.resample("W-MON", on="date").sum()
43
+ monthly = df.resample("M", on="date").sum()
44
+ return weekly, monthly
45
+
46
+ # ----------------------------
47
+ # Streamlit UI
48
+ # ----------------------------
49
+
50
+ st.set_page_config(page_title="☀️ Solar Sizing App", layout="wide")
51
+ st.title("☀️ Solar Energy Consumption & Sizing App")
52
+
53
+ st.sidebar.header("Inputs")
54
+
55
+ # Location & solar input
56
+ latitude = st.sidebar.number_input("Latitude (°)", -90.0, 90.0, 30.0)
57
+ psh = st.sidebar.number_input("Peak Sun Hours (hrs/day)", 0.0, 10.0, 5.0)
58
+
59
+ # Panel specs
60
+ panel_watt = st.sidebar.number_input("Panel STC Wattage (W)", 50, 1000, 400)
61
+ perf_ratio = st.sidebar.slider("Performance Ratio", 0.5, 0.9, 0.75)
62
+ coverage = st.sidebar.slider("System Coverage (%)", 10, 100, 100) / 100
63
+
64
+ # Battery specs
65
+ autonomy_days = st.sidebar.number_input("Battery Autonomy (days)", 0.0, 7.0, 1.0)
66
+ dod = st.sidebar.slider("Battery Depth of Discharge (DoD)", 0.1, 1.0, 0.8)
67
+ inverter_eff = st.sidebar.slider("Inverter Efficiency", 0.5, 1.0, 0.95)
68
+ battery_capacity_Wh = st.sidebar.number_input("Battery Capacity (Wh)", 100, 20000, 5000)
69
+
70
+ # Appliances table
71
+ st.subheader("Appliances")
72
+ appliances = st.data_editor(
73
+ pd.DataFrame(columns=["Appliance", "Power (W)", "Qty", "Hours/day"]),
74
+ num_rows="dynamic"
75
+ )
76
+
77
+ if not appliances.empty:
78
+ daily_load_wh = calculate_daily_load(appliances)
79
+ st.write(f"**Total Daily Load:** {daily_load_wh:,.0f} Wh")
80
+
81
+ # Panel count
82
+ panels_needed = calculate_panels_needed(daily_load_wh, panel_watt, psh, perf_ratio, coverage)
83
+ daily_prod_wh = panels_needed * panel_watt * psh * perf_ratio
84
+
85
+ st.metric("Panels Needed", panels_needed)
86
+ st.metric("Estimated Daily Production (Wh)", f"{daily_prod_wh:,.0f}")
87
+
88
+ # Tilt suggestion
89
+ tilts = tilt_suggestions(latitude)
90
+ st.subheader("Tilt Angle Suggestions (°)")
91
+ st.table(pd.DataFrame(list(tilts.items()), columns=["Case", "Tilt"]))
92
+
93
+ # Battery sizing
94
+ num_batteries = battery_sizing(daily_load_wh, autonomy_days, dod, inverter_eff, battery_capacity_Wh)
95
+ st.metric("Recommended Batteries", num_batteries)
96
+
97
+ # Graphs
98
+ weekly, monthly = simulate_weekly_monthly(daily_load_wh, daily_prod_wh)
99
+
100
+ tab1, tab2 = st.tabs(["📅 Weekly", "📆 Monthly"])
101
+ with tab1:
102
+ fig = px.bar(weekly, x=weekly.index, y=["load_Wh", "prod_Wh"],
103
+ labels={"value":"Energy (Wh)", "date":"Week"},
104
+ barmode="group", title="Weekly Load vs Production")
105
+ st.plotly_chart(fig, use_container_width=True)
106
+ with tab2:
107
+ fig = px.bar(monthly, x=monthly.index, y=["load_Wh", "prod_Wh"],
108
+ labels={"value":"Energy (Wh)", "date":"Month"},
109
+ barmode="group", title="Monthly Load vs Production")
110
+ st.plotly_chart(fig, use_container_width=True)
111
+
112
+ # Export
113
+ st.download_button("Download Monthly Data (CSV)", monthly.to_csv().encode("utf-8"),
114
+ "monthly_results.csv", "text/csv")
115
+ else:
116
+ st.info("➕ Add appliances above to calculate system sizing.")