Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,7 +149,7 @@ def calculate_overall_aqi(row, aqi_breakpoints):
|
|
| 149 |
else:
|
| 150 |
return np.nan
|
| 151 |
|
| 152 |
-
# ---
|
| 153 |
def get_firebase_data(sequence_length: int, latitude: float, longitude: float):
|
| 154 |
"""
|
| 155 |
Retrieve data from Firebase RTDB.
|
|
@@ -178,27 +178,45 @@ def get_firebase_data(sequence_length: int, latitude: float, longitude: float):
|
|
| 178 |
|
| 179 |
# Convert Firebase data to DataFrame
|
| 180 |
data_list = []
|
| 181 |
-
for
|
| 182 |
try:
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
except Exception as e:
|
| 195 |
-
|
|
|
|
| 196 |
continue
|
| 197 |
|
| 198 |
if not data_list:
|
| 199 |
-
print("No valid data points parsed from Firebase")
|
| 200 |
-
return None, "No valid data in Firebase"
|
| 201 |
-
|
| 202 |
df = pd.DataFrame(data_list)
|
| 203 |
df.set_index('time', inplace=True)
|
| 204 |
df.sort_index(inplace=True)
|
|
@@ -248,7 +266,7 @@ def get_firebase_data(sequence_length: int, latitude: float, longitude: float):
|
|
| 248 |
traceback.print_exc()
|
| 249 |
return None, f"Firebase error: {str(e)}"
|
| 250 |
|
| 251 |
-
# ---
|
| 252 |
def get_latest_data_sequence(sequence_length: int, latitude: float, longitude: float):
|
| 253 |
"""
|
| 254 |
Try to get data from Firebase first, fallback to OpenMeteo if insufficient.
|
|
|
|
| 149 |
else:
|
| 150 |
return np.nan
|
| 151 |
|
| 152 |
+
# --- Function to retrieve data from Firebase ---
|
| 153 |
def get_firebase_data(sequence_length: int, latitude: float, longitude: float):
|
| 154 |
"""
|
| 155 |
Retrieve data from Firebase RTDB.
|
|
|
|
| 178 |
|
| 179 |
# Convert Firebase data to DataFrame
|
| 180 |
data_list = []
|
| 181 |
+
for key, sensor_data in firebase_data.items():
|
| 182 |
try:
|
| 183 |
+
# Get the datetime string from inside the sensor_data dictionary
|
| 184 |
+
datetime_str = sensor_data.get('datetime')
|
| 185 |
+
|
| 186 |
+
# Check if datetime_str exists and is a string
|
| 187 |
+
if isinstance(datetime_str, str):
|
| 188 |
+
# Parse the datetime string using the correct format 'YYYY-MM-DDTHH:MM'
|
| 189 |
+
# We assume the datetime in Firebase is already in UTC or is timezone-naive
|
| 190 |
+
# and we treat it as UTC based on the original code's localization attempt.
|
| 191 |
+
timestamp = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M')
|
| 192 |
+
timestamp = pytz.utc.localize(timestamp)
|
| 193 |
|
| 194 |
+
data_point = {
|
| 195 |
+
'time': timestamp,
|
| 196 |
+
'pm25': sensor_data.get('pm25', np.nan),
|
| 197 |
+
'pm10': sensor_data.get('pm10', np.nan),
|
| 198 |
+
'co': sensor_data.get('co', np.nan),
|
| 199 |
+
'temp': sensor_data.get('temperature', np.nan)
|
| 200 |
+
}
|
| 201 |
+
data_list.append(data_point)
|
| 202 |
+
|
| 203 |
+
else:
|
| 204 |
+
# Handle cases where 'datetime' is missing or not a string for a specific item
|
| 205 |
+
print(f"Warning: Data item with key {key} is missing or has invalid 'datetime' field: {datetime_str}")
|
| 206 |
+
|
| 207 |
+
except ValueError as ve:
|
| 208 |
+
# Catch errors specifically related to parsing the datetime string
|
| 209 |
+
print(f"Error parsing datetime string '{datetime_str}' for key {key}: {ve}. Expected format '%Y-%m-%dT%H:%M'")
|
| 210 |
+
continue # Skip this data point if parsing fails
|
| 211 |
except Exception as e:
|
| 212 |
+
# Catch any other unexpected errors during processing of a single item
|
| 213 |
+
print(f"An unexpected error occurred processing item with key {key}: {e}")
|
| 214 |
continue
|
| 215 |
|
| 216 |
if not data_list:
|
| 217 |
+
print("No valid data points parsed from Firebase after attempting to process.")
|
| 218 |
+
return None, "No valid data in Firebase after parsing"
|
| 219 |
+
|
| 220 |
df = pd.DataFrame(data_list)
|
| 221 |
df.set_index('time', inplace=True)
|
| 222 |
df.sort_index(inplace=True)
|
|
|
|
| 266 |
traceback.print_exc()
|
| 267 |
return None, f"Firebase error: {str(e)}"
|
| 268 |
|
| 269 |
+
# --- Data retrieval function ---
|
| 270 |
def get_latest_data_sequence(sequence_length: int, latitude: float, longitude: float):
|
| 271 |
"""
|
| 272 |
Try to get data from Firebase first, fallback to OpenMeteo if insufficient.
|