Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,37 +37,42 @@ data_source = st.sidebar.selectbox(
|
|
| 37 |
openweather_historical_readings = []
|
| 38 |
openweather_forecast_readings = []
|
| 39 |
stormglass_marine_readings = []
|
| 40 |
-
solunar_tide_readings = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- API CALLING FUNCTIONS ---
|
| 43 |
def fetch_openweather_data(lat, lon, datetime_input):
|
| 44 |
"""Fetches historical weather data from OpenWeather's One Call API."""
|
| 45 |
-
# Construct the URL for the API call
|
| 46 |
-
url = f"https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt={int(datetime_input.timestamp())}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
response.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
current_weather_data = data.get('data', [])[0] if data.get('data', []) else {}
|
| 54 |
-
processed_entry = {
|
| 55 |
-
'timestamp': datetime_input,
|
| 56 |
-
'latitude': lat,
|
| 57 |
-
'longitude': lon,
|
| 58 |
-
'temperature': current_weather_data.get('temp'),
|
| 59 |
-
'humidity': current_weather_data.get('humidity'),
|
| 60 |
-
'wind_speed': current_weather_data.get('wind_speed')
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
return data
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
except Exception as err:
|
| 70 |
-
st.error(f"An error occurred: {err}")
|
| 71 |
|
| 72 |
|
| 73 |
def fetch_openweather_forecast(lat, lon):
|
|
@@ -166,9 +171,12 @@ def fetch_solunar_tide_data(lat, lon, datetime_input):
|
|
| 166 |
st.error(f"Error fetching Solunar and Tide data: {e}")
|
| 167 |
return None
|
| 168 |
|
| 169 |
-
def save_openweather_historical_to_csv(
|
| 170 |
"""Save historical OpenWeather data to CSV for download."""
|
| 171 |
try:
|
|
|
|
|
|
|
|
|
|
| 172 |
# Validate input data
|
| 173 |
if not openweather_historical_readings:
|
| 174 |
raise ValueError("No historical data available.")
|
|
@@ -199,10 +207,12 @@ def save_openweather_historical_to_csv(openweather_historical_readings):
|
|
| 199 |
|
| 200 |
except ValueError as ve:
|
| 201 |
logging.error(f"Validation error: {ve}")
|
|
|
|
| 202 |
return None
|
| 203 |
except Exception as e:
|
| 204 |
logging.error(f"Error saving historical data: {str(e)}")
|
| 205 |
-
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
def save_stormglass_marine_to_csv():
|
|
@@ -309,7 +319,7 @@ def main():
|
|
| 309 |
|
| 310 |
# Download button for historical data
|
| 311 |
if st.button("Download Historical Weather Data"):
|
| 312 |
-
csv_data = save_openweather_historical_to_csv(
|
| 313 |
|
| 314 |
if csv_data:
|
| 315 |
try:
|
|
@@ -324,7 +334,7 @@ def main():
|
|
| 324 |
except Exception as e:
|
| 325 |
st.error(f"Error preparing download: {str(e)}")
|
| 326 |
else:
|
| 327 |
-
|
| 328 |
|
| 329 |
|
| 330 |
with col2:
|
|
|
|
| 37 |
openweather_historical_readings = []
|
| 38 |
openweather_forecast_readings = []
|
| 39 |
stormglass_marine_readings = []
|
| 40 |
+
solunar_tide_readings = []
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Initialize session state for historical weather data
|
| 44 |
+
if 'openweather_historical_readings' not in st.session_state:
|
| 45 |
+
st.session_state.openweather_historical_readings = []
|
| 46 |
|
| 47 |
# --- API CALLING FUNCTIONS ---
|
| 48 |
def fetch_openweather_data(lat, lon, datetime_input):
|
| 49 |
"""Fetches historical weather data from OpenWeather's One Call API."""
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
url = f"https://api.openweathermap.org/data/3.0/onecall/timemachine?lat={lat}&lon={lon}&dt={int(datetime_input.timestamp())}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 52 |
+
response = requests.get(url)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
data = response.json()
|
| 55 |
+
|
| 56 |
+
current_weather_data = data.get('data', [])[0] if data.get('data', []) else {}
|
| 57 |
+
processed_entry = {
|
| 58 |
+
'timestamp': datetime_input,
|
| 59 |
+
'latitude': lat,
|
| 60 |
+
'longitude': lon,
|
| 61 |
+
'temperature': current_weather_data.get('temp'),
|
| 62 |
+
'humidity': current_weather_data.get('humidity'),
|
| 63 |
+
'wind_speed': current_weather_data.get('wind_speed')
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# Append the processed entry to the session state list
|
| 67 |
+
st.session_state.openweather_historical_readings.append(processed_entry)
|
| 68 |
+
|
| 69 |
return data
|
| 70 |
+
# After fetching data
|
| 71 |
+
logging.debug(f"Processed Entry: {processed_entry}")
|
| 72 |
+
# When accessing historical data
|
| 73 |
+
logging.debug(f"Historical data count: {len(openweather_historical_readings)}")
|
| 74 |
except Exception as err:
|
| 75 |
+
st.error(f"An error occurred: {err}")
|
| 76 |
|
| 77 |
|
| 78 |
def fetch_openweather_forecast(lat, lon):
|
|
|
|
| 171 |
st.error(f"Error fetching Solunar and Tide data: {e}")
|
| 172 |
return None
|
| 173 |
|
| 174 |
+
def save_openweather_historical_to_csv():
|
| 175 |
"""Save historical OpenWeather data to CSV for download."""
|
| 176 |
try:
|
| 177 |
+
# Access the historical data from session state
|
| 178 |
+
openweather_historical_readings = st.session_state.openweather_historical_readings
|
| 179 |
+
|
| 180 |
# Validate input data
|
| 181 |
if not openweather_historical_readings:
|
| 182 |
raise ValueError("No historical data available.")
|
|
|
|
| 207 |
|
| 208 |
except ValueError as ve:
|
| 209 |
logging.error(f"Validation error: {ve}")
|
| 210 |
+
st.error(f"Validation error: {ve}")
|
| 211 |
return None
|
| 212 |
except Exception as e:
|
| 213 |
logging.error(f"Error saving historical data: {str(e)}")
|
| 214 |
+
st.error(f"Error saving historical data: {str(e)}")
|
| 215 |
+
return None
|
| 216 |
|
| 217 |
|
| 218 |
def save_stormglass_marine_to_csv():
|
|
|
|
| 319 |
|
| 320 |
# Download button for historical data
|
| 321 |
if st.button("Download Historical Weather Data"):
|
| 322 |
+
csv_data = save_openweather_historical_to_csv()
|
| 323 |
|
| 324 |
if csv_data:
|
| 325 |
try:
|
|
|
|
| 334 |
except Exception as e:
|
| 335 |
st.error(f"Error preparing download: {str(e)}")
|
| 336 |
else:
|
| 337 |
+
st.error("No historical data available.")
|
| 338 |
|
| 339 |
|
| 340 |
with col2:
|