Esmaeilkiani commited on
Commit
041538c
·
verified ·
1 Parent(s): 33d8db1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -105
app.py CHANGED
@@ -1,110 +1,39 @@
1
  import streamlit as st
2
- import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import plotly.graph_objects as go
5
- import json
6
- from mpl_toolkits.mplot3d import Axes3D
7
- import base64
8
- from io import BytesIO
9
-
10
- # بارگذاری داده‌ها
11
- @st.cache_data
12
- def load_data():
13
- # فایل CSV یا JSON با اطلاعات مزارع
14
- data = pd.read_csv('dashboard18.csv')
15
- return data
16
-
17
- # فیلتر کردن داده‌ها بر اساس نام مزرعه
18
- def filter_data(df, مزرعه):
19
- return df[df['مزرعه'].str.contains(farm_name, case=False)]
20
-
21
- # نمایش جدول اطلاعات مزرعه
22
- def show_farm_table(df):
23
- st.dataframe(df)
24
-
25
- # اضافه کردن ارتفاع هفتگی
26
- def add_weekly_growth(df):
27
- df[' 1 ارتفاع هفته'] = df['ارتفاع'] + (df['سن'] * 0.5)
28
- return df
29
-
30
- # ایجاد نمودار 3D
31
- def create_3d_plot(df):
32
- fig = plt.figure(figsize=(10, 8))
33
- ax = fig.add_subplot(111, projection='3d')
34
- ax.scatter(df['x'], df['y'], df['height'])
35
- ax.set_xlabel('X')
36
- ax.set_ylabel('Y')
37
- ax.set_zlabel('Height')
38
- ax.set_title('3D Farm Plot')
39
- return fig
40
-
41
- # ایجاد نمودار انیمیشنی Plotly
42
- def create_animated_plot(df):
43
- frames = []
44
- for week in range(5):
45
- df['animated_height'] = df['ارتفاع هفته '] + (week * 0.5)
46
- frame = go.Frame(data=[go.Scatter3d(x=df['x'], y=df['y'], z=df['animated_height'],
47
- mode='markers', marker=dict(size=5))])
48
- frames.append(frame)
49
-
50
- fig = go.Figure(
51
- data=[go.Scatter3d(x=df['x'], y=df['y'], z=df['height'], mode='markers', marker=dict(size=5))],
52
- layout=go.Layout(
53
- scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Height'),
54
- updatemenus=[dict(type='buttons', showactive=False, buttons=[dict(label='Play',
55
- method='animate', args=[None, dict(frame=dict(duration=500, redraw=True), fromcurrent=True)])])]
56
- ),
57
- frames=frames
58
  )
 
59
 
60
- return fig
61
-
62
- # تابع اصلی Streamlit
63
- def main():
64
- st.title("داشبورد هوشمند مزرعه")
65
-
66
- # بارگذاری داده‌ها
67
- df = load_data()
68
-
69
- # جستجوی نام مزرعه
70
- farm_name = st.text_input("نام مزرعه را جستجو کنید:")
71
- if farm_name:
72
- df = filter_data(df, farm_name)
73
-
74
- # نمایش جدول اطلاعات مزرعه
75
- show_farm_table(df)
76
-
77
- # فیلد رشد هفتگی
78
- weekly_growth = st.checkbox("نمایش رشد هفتگی")
79
- if weekly_growth:
80
- df = add_weekly_growth(df)
81
- show_farm_table(df)
82
-
83
- # نمایش نمودارها
84
- if st.button("نمایش نمودارها"):
85
- plot_type = st.selectbox("نوع نمودار را انتخاب کنید:", ["نمودار 3D ثابت", "نمودار 3D انیمیشنی"])
86
-
87
- if plot_type == "نمودار 3D ثابت":
88
- fig = create_3d_plot(df)
89
- st.pyplot(fig)
90
- else:
91
- fig = create_animated_plot(df)
92
- st.plotly_chart(fig)
93
-
94
- # دانلود داده و نمودار
95
- if st.button("دانلود داده‌ها (CSV)"):
96
- csv = df.to_csv(index=False)
97
- b64 = base64.b64encode(csv.encode()).decode()
98
- href = f'<a href="data:file/csv;base64,{b64}" download="farm_data.csv">دانلود فایل CSV</a>'
99
- st.markdown(href, unsafe_allow_html=True)
100
 
101
- if st.button("دانلود نمودار (PNG)"):
102
- fig = create_3d_plot(df)
103
- buf = BytesIO()
104
- fig.savefig(buf, format="png")
105
- b64 = base64.b64encode(buf.getvalue()).decode()
106
- href = f'<a href="data:image/png;base64,{b64}" download="farm_plot.png">دانلود تصویر PNG</a>'
107
- st.markdown(href, unsafe_allow_html=True)
108
 
109
- if __name__ == "__main__":
110
- main()
 
 
1
  import streamlit as st
2
+ import ee
3
+ from datetime import datetime
4
+
5
+ # Earth Engine Authentication
6
+ service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
7
+ credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
8
+ ee.Initialize(credentials)
9
+
10
+ # Define a function to fetch historical temperature data
11
+ def get_historical_temperature(aoi, start_date, end_date):
12
+ dataset = ee.ImageCollection("ECMWF/ERA5/DAILY") \
13
+ .filterBounds(aoi) \
14
+ .filterDate(start_date, end_date) \
15
+ .select('mean_2m_air_temperature')
16
+
17
+ # Calculate daily mean temperature (in Celsius)
18
+ temp_collection = dataset.map(lambda image: image.subtract(273.15).rename('daily_mean_temp'))
19
+
20
+ # Reduce the collection to mean values over the area
21
+ mean_temp = temp_collection.mean().reduceRegion(
22
+ reducer=ee.Reducer.mean(),
23
+ geometry=aoi,
24
+ scale=1000,
25
+ bestEffort=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  )
27
+ return mean_temp.getInfo()
28
 
29
+ # Define the area of interest (AOI)
30
+ # Example: Assume aoi is defined based on user's GeoJSON or coordinates
31
+ aoi = ee.Geometry.Point([48.73168141056203, 31.53180450320103]) # Example coordinates for a specific farm location
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ # Set start and end dates
34
+ start_date = "2023-01-01"
35
+ end_date = "2023-12-31"
 
 
 
 
36
 
37
+ # Fetch and display temperature data
38
+ temp_data = get_historical_temperature(aoi, start_date, end_date)
39
+ st.write("Historical Temperature Data (°C):", temp_data['daily_mean_temp'])