YashMK89 commited on
Commit
91458a3
·
verified ·
1 Parent(s): db652ad

update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -90,9 +90,8 @@ if main_selection:
90
 
91
  # Earth Engine Index Calculator Section
92
  st.header("Earth Engine Index Calculator")
93
-
94
  # Load band information based on selected dataset
95
- if main_selection and sub_selection: # Now safe because sub_selection is initialized
96
  dataset_bands = data[main_selection]["bands"].get(sub_selection, [])
97
  st.write(f"Available Bands for {sub_options[sub_selection]}: {', '.join(dataset_bands)}")
98
 
@@ -114,21 +113,20 @@ if main_selection and sub_selection: # Now safe because sub_selection is initia
114
 
115
  # Show custom formula input if bands are selected
116
  if selected_bands:
117
- # Provide a default formula based on the number of selected bands
118
  if len(selected_bands) == 1:
119
  default_formula = f"{selected_bands[0]}"
120
  example = f"'{selected_bands[0]} * 2' or '{selected_bands[0]} + 1'"
121
- else: # len(selected_bands) == 2
122
  default_formula = f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
123
  example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
124
 
125
  custom_formula = st.text_input(
126
  "Enter Custom Formula",
127
  value=default_formula,
128
- help=f"Use only these bands: {', '.join(selected_bands)}. Examples: {example}"
129
  )
130
 
131
- # Validate the formula
132
  def validate_formula(formula, selected_bands):
133
  allowed_chars = set(" +-*/()0123456789.")
134
  terms = re.findall(r'[a-zA-Z][a-zA-Z0-9_]*', formula)
@@ -368,9 +366,13 @@ def calculate_custom_formula(image, geometry, selected_bands, custom_formula, re
368
  band_values = {}
369
  band_names = image.bandNames().getInfo()
370
 
 
 
 
 
 
 
371
  for band in selected_bands:
372
- if band not in band_names:
373
- raise ValueError(f"Band '{band}' not found in the dataset.")
374
  band_values[band] = image.select(band)
375
 
376
  reducer = get_reducer(reducer_choice)
@@ -405,7 +407,7 @@ def calculate_custom_formula(image, geometry, selected_bands, custom_formula, re
405
  except Exception as e:
406
  st.error(f"Unexpected error: {e}")
407
  return ee.Image(0).rename('custom_result').set('error', str(e))
408
-
409
  # Function to calculate index for a period
410
  def calculate_index_for_period(image, roi, selected_bands, custom_formula, reducer_choice):
411
  return calculate_custom_formula(image, roi, selected_bands, custom_formula, reducer_choice)
 
90
 
91
  # Earth Engine Index Calculator Section
92
  st.header("Earth Engine Index Calculator")
 
93
  # Load band information based on selected dataset
94
+ if main_selection and sub_selection:
95
  dataset_bands = data[main_selection]["bands"].get(sub_selection, [])
96
  st.write(f"Available Bands for {sub_options[sub_selection]}: {', '.join(dataset_bands)}")
97
 
 
113
 
114
  # Show custom formula input if bands are selected
115
  if selected_bands:
 
116
  if len(selected_bands) == 1:
117
  default_formula = f"{selected_bands[0]}"
118
  example = f"'{selected_bands[0]} * 2' or '{selected_bands[0]} + 1'"
119
+ else:
120
  default_formula = f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
121
  example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
122
 
123
  custom_formula = st.text_input(
124
  "Enter Custom Formula",
125
  value=default_formula,
126
+ help=f"Use only these bands: {', '.join(selected_bands)}. Examples: {example}. Note: Actual band availability depends on the data."
127
  )
128
 
129
+ # Validate the formula syntax
130
  def validate_formula(formula, selected_bands):
131
  allowed_chars = set(" +-*/()0123456789.")
132
  terms = re.findall(r'[a-zA-Z][a-zA-Z0-9_]*', formula)
 
366
  band_values = {}
367
  band_names = image.bandNames().getInfo()
368
 
369
+ # Check if all selected bands are available in the image
370
+ missing_bands = [band for band in selected_bands if band not in band_names]
371
+ if missing_bands:
372
+ st.warning(f"The following bands are not available in the dataset for this image: {', '.join(missing_bands)}. Available bands: {', '.join(band_names)}. Please adjust your formula or data selection.")
373
+ return ee.Image(0).rename('custom_result').set('error', f"Missing bands: {', '.join(missing_bands)}")
374
+
375
  for band in selected_bands:
 
 
376
  band_values[band] = image.select(band)
377
 
378
  reducer = get_reducer(reducer_choice)
 
407
  except Exception as e:
408
  st.error(f"Unexpected error: {e}")
409
  return ee.Image(0).rename('custom_result').set('error', str(e))
410
+
411
  # Function to calculate index for a period
412
  def calculate_index_for_period(image, roi, selected_bands, custom_formula, reducer_choice):
413
  return calculate_custom_formula(image, roi, selected_bands, custom_formula, reducer_choice)