-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.py
executable file
·785 lines (656 loc) · 29.2 KB
/
Home.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
import base64
import io
import streamlit as st
import pandas as pd
import plotly.express as px
import folium
from PIL import Image
from streamlit_folium import folium_static
from datetime import datetime, timedelta, timezone
from functions import (fetch_apod_data, display_folium_map, fetch_earth_imagery, fetch_eonet_events,
fetch_asteroid_data, fetch_earth_assets, fetch_and_display_photos, get_camera_options,
fetch_epic_data, process_eonet_data, create_ufo_image)
# Set page config
st.set_page_config(page_title="NASA Data Explorer", page_icon="🚀", layout="wide", initial_sidebar_state="expanded")
# Create UFO image
ufo_image = create_ufo_image()
# Add custom CSS
st.markdown(f"""
<style>
@keyframes fly {{
0% {{ left: -150px; top: 10%; }}
25% {{ left: 25%; top: 20%; }}
50% {{ left: 50%; top: 10%; }}
75% {{ left: 75%; top: 20%; }}
100% {{ left: calc(100% + 150px); top: 10%; }}
}}
.flying-ufo {{
position: fixed;
width: 40px;
height: 10px;
background-image: url("data:image/png;base64,{ufo_image}");
background-size: contain;
background-repeat: no-repeat;
z-index: 0;
animation: fly 40s linear infinite;
pointer-events: none;
}}
.stApp {{
background-image: url("https://wallpaperaccess.com/full/3861869.jpg");
background-size: cover;
background-attachment: fixed;
}}
.main-content {{
background-color: rgba(0, 0, 0, 0.7);
border-radius: 10px;
padding: 20px;
margin-top: 20px;
}}
.sidebar .sidebar-content {{
background-color: rgba(0, 0, 0, 0.7);
}}
</style>
<div class="flying-ufo"></div>
""", unsafe_allow_html=True)
# Add JavaScript to handle scrolling issues on iOS
st.markdown("""
<script>
document.addEventListener('DOMContentLoaded', () => {
const preventScroll = (e) => {
e.preventDefault();
e.stopPropagation();
};
const appElement = document.querySelector('.stApp');
const sidebarElement = document.querySelector('[data-testid="stSidebar"]');
if (appElement) {
appElement.addEventListener('touchmove', preventScroll, { passive: false });
}
if (sidebarElement) {
sidebarElement.addEventListener('touchmove', (e) => {
e.stopPropagation();
}, { passive: true });
}
});
</script>
""", unsafe_allow_html=True)
# Custom CSS with space theme and glowing text
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
:root {
--main-bg-color: #0c0c1d;
--text-color: #e0e0ff;
--glow-color: #00ffff;
--sidebar-bg: rgba(255, 0, 0, 0.1);
--button-bg: #4CAF50;
--button-hover: #45a049;
}
body {
background-color: var(--main-bg-color);
color: var(--text-color);
font-family: 'Orbitron', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.stApp {
background-image: url('https://wallpaperaccess.com/full/3861869.jpg');
background-size: cover;
background-attachment: fixed;
}
/* Hide Streamlit components */
#MainMenu, header, footer {
visibility: hidden;
}
/* Title Styles */
.title {
font-size: 48px;
font-weight: bold;
text-align: center;
color: var(--glow-color);
text-shadow: 0 0 10px var(--glow-color), 0 0 20px var(--glow-color), 0 0 30px var(--glow-color);
animation: glow 1.5s ease-in-out infinite alternate;
}
@keyframes glow {
from { text-shadow: 0 0 10px var(--glow-color), 0 0 20px var(--glow-color), 0 0 30px var(--glow-color); }
to { text-shadow: 0 0 20px var(--glow-color), 0 0 30px var(--glow-color), 0 0 40px var(--glow-color); }
}
/* Sidebar Styles */
[data-testid="stSidebar"] > div:first-child {
background-image: linear-gradient(to bottom, rgba(255,0,0,0.15), rgba(255,0,0,0.05));
box-shadow: inset 0 0 30px rgba(255, 0, 0, 0.2);
border-right: 1px solid rgba(255, 0, 0, 0.2);
}
.sidebar-title {
color: var(--glow-color);
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
/* Button Styles */
.stButton > button {
background-color: var(--button-bg);
color: white;
font-weight: bold;
border-radius: 20px;
border: 2px solid var(--button-hover);
transition: all 0.3s;
}
.stButton > button:hover {
background-color: var(--button-hover);
box-shadow: 0 0 10px var(--button-bg);
}
/* Input Styles */
.stTextInput > div > div > input,
.stSelectbox > div > div > select {
background-color: rgba(255, 255, 255, 0.1);
color: var(--text-color);
border: 1px solid var(--glow-color);
border-radius: 10px;
}
/* Section Styles */
.api-section {
background-color: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.api-title {
color: var(--glow-color);
font-size: 28px;
font-weight: bold;
margin-bottom: 10px;
}
/* Sidebar Toggle Button Styles */
[data-testid="collapsedControl"] {
position: relative;
z-index: 1000;
}
[data-testid="collapsedControl"] svg {
fill: var(--glow-color) !important;
filter: drop-shadow(0 0 5px var(--glow-color));
transition: filter 0.3s ease-in-out;
}
[data-testid="collapsedControl"]:hover svg {
filter: drop-shadow(0 0 10px var(--glow-color));
}
/* Tap Text Styles */
[data-testid="collapsedControl"]::after {
content: 'tap';
position: absolute;
left: 100%;
top: 50%;
transform: translateY(-50%);
margin-left: 10px;
font-size: 14px;
color: var(--glow-color);
background-color: rgba(0, 0, 0, 0.7);
padding: 2px 5px;
border-radius: 5px;
white-space: nowrap;
opacity: 1;
transition: opacity 0.3s ease-in-out;
}
.css-1544g2n [data-testid="collapsedControl"]::after {
opacity: 0;
}
/* Image Styles */
img {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto;
}
.responsive-img-container {
width: 100%;
max-width: 800px;
margin: 0 auto;
overflow: hidden;
}
/* Chart Styles */
.plotly-graph-div {
width: 100% !important;
}
/* Map Styles */
.folium-map {
width: 100% !important;
height: 0 !important;
padding-bottom: 75% !important;
position: relative !important;
}
.folium-map iframe {
position: absolute !important;
width: 100% !important;
height: 100% !important;
left: 0 !important;
top: 0 !important;
}
/* Responsive Styles */
@media (max-width: 767px) {
.title {
font-size: 36px;
}
.sidebar-title {
font-size: 20px;
}
}
@media (min-width: 992px) {
[data-testid="collapsedControl"]::after {
display: none;
}
}
/* Ensure proper sizing for columns with iframes */
[data-testid="column"] > div:has(> iframe) {
width: 100%;
height: 100%;
}
[data-testid="column"] > div:has(> iframe) > iframe {
width: 100%;
height: 100%;
}
/* Improve text visibility and contrast */
.title, .sidebar-title, .stButton > button, .stTextInput > div > div > input, .stSelectbox > div > div > select {
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
}
/* Prevent unwanted scrolling behavior */
* {
overflow-anchor: none !important;
}
/* Alien Animation */
@keyframes float {
0% { transform: translateY(0px); }
50% { transform: translateY(-20px); }
100% { transform: translateY(0px); }
}
.alien {
font-size: 80px;
animation: float 3s ease-in-out infinite;
text-align: center;
margin-bottom: -64px;
}
</style>
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown('<p class="title">Mission Control</p>', unsafe_allow_html=True)
api_key = st.text_input("Enter your NASA API key", placeholder="Demo_key",
value="kK3QAv8cS9Lcy00gBb8qRiC2Is076W5P96H9cEax", type="password")
api_choice = st.selectbox("Choose an API",
["APOD", "Mars Rover Photos", "Asteroids NeoWs", "EPIC", "Earth Imagery", "EONET"])
# Main app
st.markdown('<p class="title">Space Explorer 🛸</p>', unsafe_allow_html=True)
if api_choice == "APOD":
st.header("Astronomy Picture of the Day")
apod_mode = st.radio("Select APOD mode:", ["Today's APOD", "Specific Date", "Date Range", "Random Images"])
current_date = datetime.now(tz=timezone.utc) - timedelta(days=1)
if apod_mode == "Today's APOD":
apod_data = fetch_apod_data(api_key)
if isinstance(apod_data, dict):
apod_data = [apod_data] # Convert single dict to list for consistent handling
elif apod_mode == "Specific Date":
date = st.date_input("Select a date", current_date)
apod_data = fetch_apod_data(api_key, date=date.strftime("%Y-%m-%d"))
if isinstance(apod_data, dict):
apod_data = [apod_data]
elif apod_mode == "Date Range":
start_date = st.date_input("Start date", current_date - timedelta(days=7))
end_date = st.date_input("End date", current_date)
if start_date <= end_date:
apod_data = fetch_apod_data(api_key, start_date=start_date.strftime("%Y-%m-%d"),
end_date=end_date.strftime("%Y-%m-%d"))
else:
st.error("End date must be after start date")
apod_data = []
elif apod_mode == "Random Images":
count = st.number_input("Number of random images", min_value=1, max_value=100, value=5)
apod_data = fetch_apod_data(api_key, count=count)
# Display APOD data
if isinstance(apod_data, list):
for item in apod_data:
if "error" in item:
st.error(item["error"]["message"])
else:
st.subheader(item["title"])
if item["media_type"] == "image":
st.markdown('<div class="responsive-img-container">', unsafe_allow_html=True)
st.image(item["url"], caption=item["title"], use_column_width=True)
st.markdown('</div>', unsafe_allow_html=True)
elif item["media_type"] == "video":
st.video(item["url"])
st.markdown(f"**Date:** {item['date']}")
st.markdown(f"**Explanation:** {item['explanation']}")
if "copyright" in item:
st.markdown(f"**Copyright:** {item['copyright']}")
st.markdown("---")
else:
st.error("An error occurred while fetching APOD data.")
elif api_choice == "Mars Rover Photos":
st.header("Mars Rover Photos")
rover = "Curiosity" # We're focusing only on Curiosity
# Add information about the relationship between Sol and Earth date
st.info(
"Note: Sol 0 for Curiosity corresponds to August 6, 2012 (Earth date). Each Sol is approximately 24 hours and 39 minutes long.")
search_type = st.radio("Search by", ["Martian Sol", "Earth Date"])
if search_type == "Martian Sol":
sol = st.number_input("Enter Sol (Martian day)", min_value=0, value=0, step=1)
# Calculate approximate Earth date
earth_date = datetime(2012, 8, 6) + timedelta(days=sol)
st.write(f"Approximate corresponding Earth date: {earth_date.strftime('%Y-%m-%d')}")
date_param = f"sol={sol}"
else:
min_date = datetime(2012, 8, 6)
max_date = datetime.now()
earth_date = st.date_input("Select Earth Date", min_value=min_date, max_value=max_date, value=min_date)
# Calculate approximate Sol
sol = (earth_date - datetime(2012, 8, 6).date()).days
st.write(f"Approximate corresponding Sol: {sol}")
date_param = f"earth_date={earth_date}"
cameras = get_camera_options()
camera = st.selectbox("Select Camera (optional)", ["All"] + list(cameras.keys()))
camera_param = f"&camera={camera.lower()}" if camera != "All" else ""
page = st.number_input("Page", min_value=0, value=0, step=1, key="page_number")
# Fetch photos when any parameter changes
fetch_and_display_photos(api_key, rover, date_param, camera_param, page)
elif api_choice == "Asteroids NeoWs":
st.header("Near Earth Objects")
col1, col2 = st.columns(2)
with col1:
start_date = st.date_input("Start date", datetime.now())
with col2:
end_date = st.date_input("End date", datetime.now() + timedelta(days=7))
if start_date <= end_date:
with st.spinner("Fetching asteroid data..."):
asteroid_data = fetch_asteroid_data(api_key, start_date.strftime("%Y-%m-%d"), end_date.strftime("%Y-%m-%d"))
if "error" in asteroid_data:
st.error(asteroid_data["error"]["message"])
else:
asteroid_counts = []
all_asteroids = []
for date, asteroids in asteroid_data["near_earth_objects"].items():
asteroid_counts.append({"date": date, "count": len(asteroids)})
all_asteroids.extend(asteroids)
df = pd.DataFrame(asteroid_counts)
# Bar chart of asteroid counts
fig = px.bar(df, x="date", y="count", title="Number of Near Earth Objects by Date")
st.plotly_chart(fig, use_container_width=True)
total_asteroids = sum(df["count"])
hazardous_asteroids = sum(1 for asteroid in all_asteroids if asteroid["is_potentially_hazardous_asteroid"])
col1, col2 = st.columns(2)
with col1:
st.metric("Total Near Earth Objects", total_asteroids)
with col2:
st.metric("Potentially Hazardous Asteroids", hazardous_asteroids)
# Asteroid size distribution with names
st.subheader("Asteroid Size Distribution")
size_data = [{"name": asteroid["name"],
"size": asteroid["estimated_diameter"]["meters"]["estimated_diameter_max"],
"hazardous": asteroid["is_potentially_hazardous_asteroid"]}
for asteroid in all_asteroids]
size_df = pd.DataFrame(size_data)
fig_size = px.scatter(size_df, x="size", y="name", color="hazardous",
labels={'size': 'Estimated Max Diameter (meters)', 'name': 'Asteroid Name',
'hazardous': 'Potentially Hazardous'},
title="Asteroid Sizes",
hover_data=["size"])
fig_size.update_layout(height=600)
st.plotly_chart(fig_size, use_container_width=True)
# Closest approaches
st.subheader("Closest Approaches")
closest_approaches = sorted(all_asteroids, key=lambda x: float(
x["close_approach_data"][0]["miss_distance"]["kilometers"]))[:5]
for asteroid in closest_approaches:
st.write(f"Asteroid: {asteroid['name']}")
st.write(f"Close approach date: {asteroid['close_approach_data'][0]['close_approach_date']}")
st.write(
f"Miss distance: {float(asteroid['close_approach_data'][0]['miss_distance']['kilometers']):.2f} km")
st.write("---")
# Size comparison visualization
st.subheader("Asteroid Size Comparison")
selected_asteroids = st.multiselect("Select asteroids to compare",
options=[asteroid['name'] for asteroid in all_asteroids],
default=[asteroid['name'] for asteroid in all_asteroids[:5]])
if selected_asteroids:
comparison_data = [
{
"name": asteroid['name'],
"size": asteroid["estimated_diameter"]["meters"]["estimated_diameter_max"],
"hazardous": asteroid["is_potentially_hazardous_asteroid"]
}
for asteroid in all_asteroids if asteroid['name'] in selected_asteroids
]
comparison_df = pd.DataFrame(comparison_data)
fig_comparison = px.bar(comparison_df, x="name", y="size", color="hazardous",
labels={'size': 'Estimated Max Diameter (meters)', 'name': 'Asteroid Name',
'hazardous': 'Potentially Hazardous'},
title="Asteroid Size Comparison")
fig_comparison.update_layout(xaxis={'categoryorder': 'total descending'})
st.plotly_chart(fig_comparison, use_container_width=True)
# Individual asteroid explorer
st.subheader("Explore Individual Asteroids")
selected_asteroid = st.selectbox("Select an asteroid", [asteroid['name'] for asteroid in all_asteroids])
asteroid_info = next(asteroid for asteroid in all_asteroids if asteroid['name'] == selected_asteroid)
col1, col2 = st.columns([1, 2])
with col1:
# Display a generic asteroid image
st.image(
"https://imgs.search.brave.com/R4nwYmQBrjXwn9eFINYFwNCPWMftnLb8_MdiDwH55GI/rs:fit:500:0:0:0/g:ce/aHR0cHM6Ly93YWxs/cGFwZXJjYXZlLmNv/bS93cC9tS3FqVk82/LmpwZw",
caption="Generic Asteroid Image (NASA)")
with col2:
st.json(asteroid_info)
else:
st.error("End date must be after start date")
elif api_choice == "EPIC":
st.header("Earth Polychromatic Imaging Camera (EPIC)")
date = st.date_input("Select a date", datetime.now() - timedelta(days=2))
epic_data = fetch_epic_data(api_key, date.strftime("%Y-%m-%d"))
if isinstance(epic_data, dict) and "error" in epic_data:
st.error(epic_data["error"]["message"])
elif len(epic_data) == 0:
st.warning(f"No EPIC images available for {date}")
else:
st.success(f"Found {len(epic_data)} EPIC images")
cols = st.columns(3)
for i, image in enumerate(epic_data[:9]):
with cols[i % 3]:
image_date = datetime.strptime(image["date"], "%Y-%m-%d %H:%M:%S")
formatted_date = image_date.strftime("%Y/%m/%d")
image_url = f"https://epic.gsfc.nasa.gov/archive/natural/{formatted_date}/png/{image['image']}.png"
st.image(image_url, caption=f"Date: {image['date']}")
# Create a map of image locations
latitudes = [float(image["centroid_coordinates"]["lat"]) for image in epic_data]
longitudes = [float(image["centroid_coordinates"]["lon"]) for image in epic_data]
df = pd.DataFrame({"lat": latitudes, "lon": longitudes})
fig = px.scatter_geo(df, lat="lat", lon="lon", projection="natural earth")
fig.update_layout(title="EPIC Image Locations")
st.plotly_chart(fig, use_container_width=True)
elif api_choice == "Earth Imagery":
st.header("Earth Imagery")
# Initialize session state variables
if 'earth_image' not in st.session_state:
st.session_state.earth_image = None
if 'earth_image_date' not in st.session_state:
st.session_state.earth_image_date = None
if 'earth_image_assets' not in st.session_state:
st.session_state.earth_image_assets = None
if 'earth_image_params' not in st.session_state:
st.session_state.earth_image_params = None
col1, col2 = st.columns(2)
with col1:
lat = st.number_input("Latitude", value=29.78, step=0.01)
with col2:
lon = st.number_input("Longitude", value=-95.33, step=0.01)
date = st.date_input("Select a date (YYYY-MM-DD)", datetime.now() - timedelta(days=30))
# Add a slider for image resolution
dim = st.slider("Image Resolution (degrees)", min_value=0.01, max_value=0.3, value=0.15, step=0.01,
help="Higher values result in a larger area but lower resolution. Lower values give higher resolution but cover a smaller area.")
col1 = st.columns(1)[0]
with col1:
# Create a map to show the selected location
location_map = folium.Map(location=[lat, lon], zoom_start=4)
folium.Marker([lat, lon], popup="Selected Location").add_to(location_map)
# folium_static for better responsiveness
folium_static(location_map, width=300, height=200)
if st.button("Fetch Earth Imagery"):
with st.spinner("Fetching Earth imagery..."):
image_result, params = fetch_earth_imagery(api_key, lat, lon, date.strftime("%Y-%m-%d"), dim)
assets = fetch_earth_assets(api_key, lat, lon, date.strftime("%Y-%m-%d"))
if isinstance(image_result, dict) and "error" in image_result:
st.error(image_result["error"])
elif isinstance(image_result, Image.Image):
# Store the image, date, assets, and params in session state
st.session_state.earth_image = image_result
st.session_state.earth_image_date = date
st.session_state.earth_image_assets = assets
st.session_state.earth_image_params = params
st.success("Image fetched successfully!")
else:
st.error("Unexpected result when fetching the image. Please try again.")
# Display the image if it exists in session state
if st.session_state.earth_image is not None:
st.image(st.session_state.earth_image, caption=f"Landsat 8 Imagery (Date: {st.session_state.earth_image_date})",
use_column_width=True)
# Display image information
st.subheader("Image Information")
image_width, image_height = st.session_state.earth_image.size
st.write(f"Image dimensions: {image_width}x{image_height} pixels")
st.write(f"Resolution: {st.session_state.earth_image_params['dim']} degrees")
approx_km = st.session_state.earth_image_params['dim'] * 111 # Approximate km per degree
st.write(f"Approximate area covered: {approx_km:.2f}km x {approx_km:.2f}km")
# Display image metadata
if st.session_state.earth_image_assets is not None:
st.subheader("Image Metadata")
st.json(st.session_state.earth_image_assets)
# Convert PIL Image to bytes for download
img_byte_arr = io.BytesIO()
st.session_state.earth_image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Provide a download button for the image
st.download_button(
label="Download Image",
data=img_byte_arr,
file_name="earth_imagery.png",
mime="image/png"
)
# Create a base map for the image overlay
m = folium.Map(location=[lat, lon], zoom_start=10)
# Convert PIL Image to base64 for Folium
buffered = io.BytesIO()
st.session_state.earth_image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
# Add image overlay to the map
img_bounds = [
[lat - st.session_state.earth_image_params['dim'] / 2,
lon - st.session_state.earth_image_params['dim'] / 2],
[lat + st.session_state.earth_image_params['dim'] / 2, lon + st.session_state.earth_image_params['dim'] / 2]
]
folium.raster_layers.ImageOverlay(
image=f"data:image/png;base64,{img_str}",
bounds=img_bounds,
opacity=0.6,
name="Landsat 8 Image"
).add_to(m)
# Display the map with the image overlay
st.subheader("Image Overlay on Map")
display_folium_map(m, height=500)
elif api_choice == "EONET":
st.header("Earth Observatory Natural Event Tracker (EONET)")
# Initialize session state variables
if 'eonet_data' not in st.session_state:
st.session_state.eonet_data = None
if 'selected_asteroid' not in st.session_state:
st.session_state.selected_asteroid = None
col1, col2, col3 = st.columns(3)
with col1:
days = st.slider("Number of days to look back", 1, 365, 30)
with col2:
limit = st.slider("Maximum number of events", 100, 2000, 500)
with col3:
status = st.selectbox("Event status", ["all", "open", "closed"])
if st.button("Fetch EONET Data") or st.session_state.eonet_data is None:
with st.spinner("Fetching EONET data..."):
eonet_data = fetch_eonet_events(limit=limit, days=days, status=status)
st.session_state.eonet_data = eonet_data
if st.session_state.eonet_data:
eonet_data = st.session_state.eonet_data
if "events" in eonet_data:
events_df = process_eonet_data(eonet_data)
if not events_df.empty:
st.success(f"Successfully fetched {len(events_df)} events")
# Event categories pie chart
fig = px.pie(events_df, names="category", title="Event Categories")
st.plotly_chart(fig, use_container_width=True)
# Event timeline
events_df['date'] = pd.to_datetime(events_df['date'])
fig_timeline = px.histogram(events_df, x="date", color="category", title="Event Timeline")
st.plotly_chart(fig_timeline, use_container_width=True)
# Filtering options
st.subheader("Filter Events")
selected_categories = st.multiselect("Select categories",
options=sorted(events_df["category"].unique()))
min_date = events_df["date"].min().date()
max_date = events_df["date"].max().date()
date_range = st.date_input("Select date range", [min_date, max_date])
# Apply filters
filtered_df = events_df
if selected_categories:
filtered_df = filtered_df[filtered_df["category"].isin(selected_categories)]
filtered_df = filtered_df[
(filtered_df["date"].dt.date >= date_range[0]) & (filtered_df["date"].dt.date <= date_range[1])]
# Map of events
st.subheader("Event Map")
m = folium.Map(location=[0, 0], zoom_start=2)
for _, event in filtered_df.iterrows():
folium.Marker(
location=[event["lat"], event["lon"]],
popup=f"<b>{event['title']}</b><br>Date: {event['date']}<br><a href='{event['source']}' target='_blank'>More Info</a>",
tooltip=f"{event['category']}: {event['title']}"
).add_to(m)
folium_static(m, width=700, height=500)
# Events table
st.subheader("Filtered Events")
st.dataframe(filtered_df[["title", "category", "date", "source"]])
# Download CSV
csv = filtered_df.to_csv(index=False)
st.download_button(
label="Download filtered events as CSV",
data=csv,
file_name="eonet_events_filtered.csv",
mime="text/csv",
)
# Explore Individual Events
st.subheader("Explore Individual Events")
event_options = filtered_df['title'].tolist()
selected_event = st.selectbox("Select an event", event_options, key="event_selectbox")
if selected_event:
event_info = filtered_df[filtered_df['title'] == selected_event].iloc[0]
st.json(event_info.to_dict())
else:
st.warning("No events found for the specified criteria.")
else:
st.error("Failed to fetch EONET data or no events found.")
st.sidebar.markdown("---")
st.sidebar.info(
"This app uses NASA's public APIs to explore various space and Earth science data. Enter your API key for full access, or use key provided for limited access.")
st.sidebar.subheader("🌟 Did You Know?")
space_facts = [
"The Sun makes up 99.86% of the mass in our solar system.",
"One day on Venus is longer than one year on Earth.",
"The footprints on the Moon will be there for 100 million years.",
"There is a planet made of diamonds twice the size of Earth.",
"The largest known star, VY Canis Majoris, is 1,400 times larger than our Sun."
]
st.sidebar.info(space_facts[int(datetime.now().timestamp()) % len(space_facts)])
# Easter egg: Konami Code
st.markdown("""
<script>
let keys = [];
const konami = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
document.addEventListener('keydown', (e) => {
keys.push(e.keyCode);
keys = keys.slice(-10);
if (keys.join(',') === konami.join(',')) {
alert('🎉 You found the Easter egg! Enjoy this cosmic joke: Why did the sun go to school? To get brighter!');
}
});
</script>
""", unsafe_allow_html=True)