-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_weather_app.py
More file actions
67 lines (56 loc) · 1.86 KB
/
Copy pathspace_weather_app.py
File metadata and controls
67 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import streamlit as st
import requests
import pandas as pd
from datetime import datetime, timedelta
import plotly.express as px
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
try:
API_KEY = st.secrets["NASA_API_KEY"]
except:
API_KEY = os.getenv("NASA_API_KEY")
# Check if API key is present
if not API_KEY:
st.error("Please set your NASA API Key in a .env file as NASA_API_KEY.")
st.stop()
# Function to fetch NASA Space Weather data
def fetch_event_data(event_type):
base_url = f"https://api.nasa.gov/DONKI/{event_type}"
params = {
"startDate": (datetime.now() - timedelta(days=30)).strftime("%Y-%m-%d"),
"endDate": datetime.now().strftime("%Y-%m-%d"),
"api_key": API_KEY
}
return requests.get(base_url, params=params).json()
# Streamlit App UI
st.title("☀ NASA Space Weather Visualizer")
st.write("Get real-time solar activity and space weather updates from NASA.")
# Event selection
event_type = st.selectbox(
"Select Space Weather Event Type",
["FLR", "GST", "CME"]
)
event_names = {
"FLR": "Solar Flares",
"GST": "Geomagnetic Storms",
"CME": "Coronal Mass Ejections"
}
st.subheader(f"{event_names[event_type]} Over the Last 30 Days")
# Fetch data
data = fetch_event_data(event_type)
if not data:
st.warning("No data available for the selected event type.")
else:
# DataFrame conversion
df = pd.DataFrame(data)
# Format for display
date_col = "beginTime" if "beginTime" in df.columns else "startTime"
df["date"] = pd.to_datetime(df[date_col]).dt.date
# Show table
st.dataframe(df)
# Plotting occurrences over time
chart_data = df.groupby("date").size().reset_index(name="event_count")
fig = px.line(chart_data, x="date", y="event_count", title=f"{event_names[event_type]} Occurrence Trend")
st.plotly_chart(fig)