YashMK89 commited on
Commit
ee13934
·
verified ·
1 Parent(s): ab5b14b

update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -60
app.py CHANGED
@@ -108,6 +108,16 @@ end_date = st.date_input("End Date", value=pd.to_datetime('2020-12-31'))
108
  start_date_str = start_date.strftime('%Y-%m-%d')
109
  end_date_str = end_date.strftime('%Y-%m-%d')
110
 
 
 
 
 
 
 
 
 
 
 
111
  # Initialize session state for storing results if not already done
112
  if 'results' not in st.session_state:
113
  st.session_state.results = []
@@ -173,47 +183,6 @@ def calculate_custom_formula(image, geometry, formula):
173
  )
174
  return result.get('Custom Index')
175
 
176
- # Function to calculate the frequency-specific aggregation
177
- def aggregate_by_frequency(results, frequency):
178
- # Create an empty list to store the aggregated results
179
- aggregated_results = []
180
-
181
- # Aggregate based on the frequency selected
182
- if frequency == 'Daily':
183
- for result in results:
184
- # Add daily aggregation logic
185
- date = result['Date'].date()
186
- aggregated_results.append({**result, 'Aggregated Date': date})
187
-
188
- elif frequency == 'Weekly':
189
- for result in results:
190
- # Aggregate by week: calculate the start of the week (Monday)
191
- date = result['Date']
192
- week_start = date - timedelta(days=date.weekday()) # Get the start of the week
193
- aggregated_results.append({**result, 'Aggregated Date': week_start})
194
-
195
- elif frequency == 'Monthly':
196
- for result in results:
197
- # Aggregate by month
198
- date = result['Date']
199
- month_start = date.replace(day=1) # Set the date to the first of the month
200
- aggregated_results.append({**result, 'Aggregated Date': month_start})
201
-
202
- elif frequency == 'Yearly':
203
- for result in results:
204
- # Aggregate by year
205
- date = result['Date']
206
- year_start = date.replace(month=1, day=1) # Set the date to the first of the year
207
- aggregated_results.append({**result, 'Aggregated Date': year_start})
208
-
209
- return aggregated_results
210
-
211
- # Select frequency for aggregation (Daily, Weekly, Monthly, Yearly)
212
- frequency = st.selectbox(
213
- "Select Frequency for Aggregation",
214
- ['Daily', 'Weekly', 'Monthly', 'Yearly']
215
- )
216
-
217
  # Process each point for index calculation
218
  if file_upload:
219
  locations_df = None # Initialize locations_df to None
@@ -265,6 +234,11 @@ if file_upload:
265
  # Store the map in session_state
266
  st.session_state.map_data = m
267
 
 
 
 
 
 
268
  # Store results with dates for aggregation
269
  results_with_dates = []
270
  for idx, row in locations_df.iterrows():
@@ -307,31 +281,47 @@ if file_upload:
307
  result = calculate_custom_formula(image, roi, custom_formula)
308
 
309
  if result is not None:
310
- # Store the result along with the date (use the image acquisition date)
311
- acquisition_date = image.get('system:time_start').getInfo()
312
- date = datetime.utcfromtimestamp(acquisition_date / 1000) # Convert to datetime
313
  calculated_value = result.getInfo() # Get the numeric value
314
 
315
  # Add the date and calculated value to the results list
316
- results_with_dates.append({
317
  'Location Name': location_name,
318
  'Latitude': latitude,
319
  'Longitude': longitude,
320
  'Date': date,
321
  'Calculated Value': calculated_value
322
  })
323
-
324
- # Aggregate the results based on the selected frequency
325
- aggregated_results = aggregate_by_frequency(results_with_dates, frequency)
326
-
327
- # Convert the aggregated results to a DataFrame
328
- agg_df = pd.DataFrame(aggregated_results)
329
-
330
- # Optionally, allow user to download the results as a CSV file
331
- filename = f"{main_selection}_{sub_selection}_{start_date.strftime('%Y/%m/%d')}_{end_date.strftime('%Y/%m/%d')}_{shape_type}_{frequency}.csv"
332
- st.download_button(
333
- label="Download aggregated results as CSV",
334
- data=agg_df.to_csv(index=False).encode('utf-8'),
335
- file_name=filename,
336
- mime='text/csv'
337
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  start_date_str = start_date.strftime('%Y-%m-%d')
109
  end_date_str = end_date.strftime('%Y-%m-%d')
110
 
111
+ # Frequency selection
112
+ frequency = st.selectbox("Select Frequency for Date Incrementation",
113
+ ["Daily", "Weekly", "Monthly", "Yearly", "Custom Number of Days"])
114
+
115
+ # Custom days input if "Custom Number of Days" is selected
116
+ if frequency == "Custom Number of Days":
117
+ custom_days = st.number_input("Enter number of days", min_value=1, value=1)
118
+ else:
119
+ custom_days = None # Default to None when custom days aren't required
120
+
121
  # Initialize session state for storing results if not already done
122
  if 'results' not in st.session_state:
123
  st.session_state.results = []
 
183
  )
184
  return result.get('Custom Index')
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  # Process each point for index calculation
187
  if file_upload:
188
  locations_df = None # Initialize locations_df to None
 
234
  # Store the map in session_state
235
  st.session_state.map_data = m
236
 
237
+
238
+ # Loop through the selected frequency and increment the date
239
+ while current_date <= end_date:
240
+ current_date_str = current_date.strftime('%Y-%m-%d')
241
+
242
  # Store results with dates for aggregation
243
  results_with_dates = []
244
  for idx, row in locations_df.iterrows():
 
281
  result = calculate_custom_formula(image, roi, custom_formula)
282
 
283
  if result is not None:
 
 
 
284
  calculated_value = result.getInfo() # Get the numeric value
285
 
286
  # Add the date and calculated value to the results list
287
+ st.session_state.results.append({
288
  'Location Name': location_name,
289
  'Latitude': latitude,
290
  'Longitude': longitude,
291
  'Date': date,
292
  'Calculated Value': calculated_value
293
  })
294
+
295
+ # Increment the current date based on the selected frequency
296
+ if frequency == "Daily":
297
+ current_date += timedelta(days=1)
298
+ elif frequency == "Weekly":
299
+ current_date += timedelta(weeks=1)
300
+ elif frequency == "Monthly":
301
+ current_date += timedelta(days=30) # Approximation
302
+ elif frequency == "Yearly":
303
+ current_date += timedelta(days=365) # Approximation
304
+ elif frequency == "Custom Number of Days" and custom_days is not None:
305
+ current_date += timedelta(days=custom_days)
306
+
307
+ # After processing, show the results
308
+ if st.session_state.results:
309
+ # Convert the results to a DataFrame for better visualization
310
+ result_df = pd.DataFrame(st.session_state.results)
311
+
312
+ # Show the results in a table format
313
+ st.write("Processed Results Table:")
314
+ st.dataframe(result_df[['Location Name', 'Latitude', 'Longitude', 'Date', 'Calculated Value']])
315
+
316
+ # Generate the dynamic filename
317
+ filename = f"{main_selection}_{sub_selection}_{start_date.strftime('%Y/%m/%d')}_{end_date.strftime('%Y/%m/%d')}_{shape_type}.csv"
318
+
319
+ # Convert results to DataFrame for download
320
+ if st.session_state.results:
321
+ result_df = pd.DataFrame(st.session_state.results)
322
+ st.download_button(
323
+ label="Download results as CSV",
324
+ data=result_df.to_csv(index=False).encode('utf-8'),
325
+ file_name=filename,
326
+ mime='text/csv'
327
+ )