Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
from datetime import datetime, timedelta
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
|
|
6 |
import plotly.express as px
|
7 |
import plotly.graph_objects as go
|
8 |
|
@@ -16,60 +17,72 @@ class NasaSsdCneosApi:
|
|
16 |
self.ca_url = "https://ssd-api.jpl.nasa.gov/cad.api"
|
17 |
|
18 |
def get_fireballs(self, limit=10, date_min=None, energy_min=None):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
return response.json()
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
def get_close_approaches(self, dist_max=None, date_min=None, date_max=None,
|
31 |
h_min=None, h_max=None, v_inf_min=None, v_inf_max=None,
|
32 |
limit=10):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
return response.json()
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
def format_response(self, data, format_type):
|
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 |
|
|
|
3 |
from datetime import datetime, timedelta
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
6 |
+
import traceback
|
7 |
import plotly.express as px
|
8 |
import plotly.graph_objects as go
|
9 |
|
|
|
17 |
self.ca_url = "https://ssd-api.jpl.nasa.gov/cad.api"
|
18 |
|
19 |
def get_fireballs(self, limit=10, date_min=None, energy_min=None):
|
20 |
+
try:
|
21 |
+
params = {'limit': limit}
|
22 |
+
if date_min:
|
23 |
+
params['date-min'] = date_min
|
24 |
+
if energy_min:
|
25 |
+
params['energy-min'] = energy_min
|
26 |
|
27 |
+
response = requests.get(self.fireball_url, params=params)
|
28 |
+
response.raise_for_status()
|
29 |
return response.json()
|
30 |
+
except Exception as e:
|
31 |
+
print("Fireball API Error:", e)
|
32 |
+
traceback.print_exc()
|
33 |
+
return None
|
34 |
|
35 |
def get_close_approaches(self, dist_max=None, date_min=None, date_max=None,
|
36 |
h_min=None, h_max=None, v_inf_min=None, v_inf_max=None,
|
37 |
limit=10):
|
38 |
+
try:
|
39 |
+
params = {'limit': limit, 'dist-max': dist_max, 'date-min': date_min,
|
40 |
+
'date-max': date_max, 'h-min': h_min, 'h-max': h_max,
|
41 |
+
'v-inf-min': v_inf_min, 'v-inf-max': v_inf_max, 'sort': 'date'}
|
42 |
+
params = {k: v for k, v in params.items() if v is not None}
|
43 |
|
44 |
+
response = requests.get(self.ca_url, params=params)
|
45 |
+
response.raise_for_status()
|
46 |
return response.json()
|
47 |
+
except Exception as e:
|
48 |
+
print("Close Approaches API Error:", e)
|
49 |
+
traceback.print_exc()
|
50 |
+
return None
|
51 |
|
52 |
def format_response(self, data, format_type):
|
53 |
+
try:
|
54 |
+
if not data:
|
55 |
+
return None
|
56 |
|
57 |
+
fields = data.get('fields')
|
58 |
+
rows = data.get('data')
|
59 |
|
60 |
+
if not fields or not rows:
|
61 |
+
return None
|
62 |
|
63 |
+
df = pd.DataFrame([dict(zip(fields, row)) for row in rows])
|
64 |
|
65 |
+
if format_type == 'fireballs':
|
66 |
+
return df.rename(columns={
|
67 |
+
'date': 'Date/Time', 'energy': 'Energy (kt)',
|
68 |
+
'impact-e': 'Impact Energy (10^10 J)', 'lat': 'Latitude',
|
69 |
+
'lon': 'Longitude', 'alt': 'Altitude (km)',
|
70 |
+
'vel': 'Velocity (km/s)'
|
71 |
+
})
|
72 |
|
73 |
+
elif format_type == 'close_approaches':
|
74 |
+
return df.rename(columns={
|
75 |
+
'des': 'Object', 'orbit_id': 'Orbit ID', 'cd': 'Time (TDB)',
|
76 |
+
'dist': 'Nominal Distance (au)', 'dist_min': 'Minimum Distance (au)',
|
77 |
+
'dist_max': 'Maximum Distance (au)', 'v_rel': 'Velocity (km/s)',
|
78 |
+
'h': 'H (mag)'
|
79 |
+
})
|
80 |
|
81 |
+
return df
|
82 |
+
except Exception as e:
|
83 |
+
print("Data formatting error:", e)
|
84 |
+
traceback.print_exc()
|
85 |
+
return None
|
86 |
|
87 |
|
88 |
|