Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,160 +6,71 @@ import pandas as pd
|
|
| 6 |
import plotly.express as px
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class NasaSsdCneosApi:
|
| 10 |
-
"""
|
| 11 |
-
A class to interact with NASA's SSD/CNEOS API.
|
| 12 |
-
Provides methods to access data on near-Earth objects, close approaches, and more.
|
| 13 |
-
"""
|
| 14 |
-
|
| 15 |
def __init__(self):
|
| 16 |
-
self.
|
| 17 |
-
self.
|
| 18 |
-
|
| 19 |
-
self.ca_url = f"{self.cneos_url}/close_approaches"
|
| 20 |
-
self.nea_url = f"{self.cneos_url}/nea"
|
| 21 |
-
self.scout_url = f"{self.cneos_url}/scout"
|
| 22 |
-
|
| 23 |
def get_fireballs(self, limit=10, date_min=None, energy_min=None):
|
| 24 |
-
"""Get information about recent fireballs"""
|
| 25 |
params = {'limit': limit}
|
| 26 |
if date_min:
|
| 27 |
params['date-min'] = date_min
|
| 28 |
if energy_min:
|
| 29 |
params['energy-min'] = energy_min
|
| 30 |
-
|
| 31 |
response = requests.get(self.fireball_url, params=params)
|
| 32 |
if response.status_code == 200:
|
| 33 |
return response.json()
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
params = {
|
| 44 |
-
|
| 45 |
-
params['dist-max'] = dist_max
|
| 46 |
-
if date_min:
|
| 47 |
-
params['date-min'] = date_min
|
| 48 |
-
if date_max:
|
| 49 |
-
params['date-max'] = date_max
|
| 50 |
-
if h_min:
|
| 51 |
-
params['h-min'] = h_min
|
| 52 |
-
if h_max:
|
| 53 |
-
params['h-max'] = h_max
|
| 54 |
-
if v_inf_min:
|
| 55 |
-
params['v-inf-min'] = v_inf_min
|
| 56 |
-
if v_inf_max:
|
| 57 |
-
params['v-inf-max'] = v_inf_max
|
| 58 |
-
|
| 59 |
response = requests.get(self.ca_url, params=params)
|
| 60 |
if response.status_code == 200:
|
| 61 |
return response.json()
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return None
|
| 65 |
-
|
| 66 |
-
def get_nea_data(self, spk_id=None, des=None, h_max=None):
|
| 67 |
-
"""Get data about specific near-Earth asteroids"""
|
| 68 |
-
params = {}
|
| 69 |
-
if spk_id:
|
| 70 |
-
params['spk-id'] = spk_id
|
| 71 |
-
if des:
|
| 72 |
-
params['des'] = des
|
| 73 |
-
if h_max:
|
| 74 |
-
params['h-max'] = h_max
|
| 75 |
-
|
| 76 |
-
response = requests.get(self.nea_url, params=params)
|
| 77 |
-
if response.status_code == 200:
|
| 78 |
-
return response.json()
|
| 79 |
-
else:
|
| 80 |
-
print(f"Error: {response.status_code}")
|
| 81 |
-
return None
|
| 82 |
-
|
| 83 |
-
def get_scout_data(self, nea_comet='NEA', limit=10):
|
| 84 |
-
"""Get Scout system data for newly discovered objects"""
|
| 85 |
-
params = {'nea-comet': nea_comet, 'limit': limit}
|
| 86 |
-
|
| 87 |
-
response = requests.get(self.scout_url, params=params)
|
| 88 |
-
if response.status_code == 200:
|
| 89 |
-
return response.json()
|
| 90 |
-
else:
|
| 91 |
-
print(f"Error: {response.status_code}")
|
| 92 |
-
return None
|
| 93 |
-
|
| 94 |
def format_response(self, data, format_type):
|
| 95 |
-
"""Format the response data for better readability"""
|
| 96 |
if not data:
|
| 97 |
return None
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
if format_type == 'fireballs':
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
'Longitude': fireball.get('lon'),
|
| 108 |
-
'Altitude (km)': fireball.get('alt'),
|
| 109 |
-
'Velocity (km/s)': fireball.get('vel')
|
| 110 |
-
}
|
| 111 |
-
result.append(entry)
|
| 112 |
-
return pd.DataFrame(result)
|
| 113 |
-
|
| 114 |
elif format_type == 'close_approaches':
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
'Velocity (km/s)': ca.get('v_rel'),
|
| 125 |
-
'H (mag)': ca.get('h')
|
| 126 |
-
}
|
| 127 |
-
result.append(entry)
|
| 128 |
-
return pd.DataFrame(result)
|
| 129 |
-
|
| 130 |
-
elif format_type == 'nea':
|
| 131 |
-
result = []
|
| 132 |
-
for nea in data.get('data', []):
|
| 133 |
-
entry = {
|
| 134 |
-
'Designation': nea.get('des'),
|
| 135 |
-
'H (mag)': nea.get('h'),
|
| 136 |
-
'Diameter (km)': nea.get('diameter'),
|
| 137 |
-
'Orbit Class': nea.get('orbit_class'),
|
| 138 |
-
'Perihelion (au)': nea.get('q'),
|
| 139 |
-
'Aphelion (au)': nea.get('ad'),
|
| 140 |
-
'Inclination (deg)': nea.get('i'),
|
| 141 |
-
}
|
| 142 |
-
result.append(entry)
|
| 143 |
-
return pd.DataFrame(result)
|
| 144 |
-
|
| 145 |
-
elif format_type == 'scout':
|
| 146 |
-
result = []
|
| 147 |
-
for obj in data.get('data', []):
|
| 148 |
-
entry = {
|
| 149 |
-
'Object': obj.get('object'),
|
| 150 |
-
'Rating': obj.get('rating'),
|
| 151 |
-
'Last Observation': obj.get('last_obs'),
|
| 152 |
-
'Arc (days)': obj.get('arc'),
|
| 153 |
-
'Observations': obj.get('n_obs'),
|
| 154 |
-
'H (mag)': obj.get('h'),
|
| 155 |
-
'Diameter (m)': obj.get('diameter'),
|
| 156 |
-
'Close Approach': obj.get('ca_dist_min'),
|
| 157 |
-
'Velocity (km/s)': obj.get('v_inf')
|
| 158 |
-
}
|
| 159 |
-
result.append(entry)
|
| 160 |
-
return pd.DataFrame(result)
|
| 161 |
-
|
| 162 |
-
return None
|
| 163 |
|
| 164 |
|
| 165 |
# Gradio Interface Functions
|
|
|
|
| 6 |
import plotly.express as px
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
|
| 9 |
+
# Updated NASA API client with proper endpoints and data parsing
|
| 10 |
+
import requests
|
| 11 |
+
import pandas as pd
|
| 12 |
+
|
| 13 |
class NasaSsdCneosApi:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
+
self.fireball_url = "https://ssd-api.jpl.nasa.gov/fireball.api"
|
| 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 |
params = {'limit': limit}
|
| 20 |
if date_min:
|
| 21 |
params['date-min'] = date_min
|
| 22 |
if energy_min:
|
| 23 |
params['energy-min'] = energy_min
|
| 24 |
+
|
| 25 |
response = requests.get(self.fireball_url, params=params)
|
| 26 |
if response.status_code == 200:
|
| 27 |
return response.json()
|
| 28 |
+
return None
|
| 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 |
+
params = {'limit': limit, 'dist-max': dist_max, 'date-min': date_min,
|
| 34 |
+
'date-max': date_max, 'h-min': h_min, 'h-max': h_max,
|
| 35 |
+
'v-inf-min': v_inf_min, 'v-inf-max': v_inf_max, 'sort': 'date'}
|
| 36 |
+
# Remove None values
|
| 37 |
+
params = {k: v for k, v in params.items() if v is not None}
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
response = requests.get(self.ca_url, params=params)
|
| 40 |
if response.status_code == 200:
|
| 41 |
return response.json()
|
| 42 |
+
return None
|
| 43 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def format_response(self, data, format_type):
|
|
|
|
| 45 |
if not data:
|
| 46 |
return None
|
| 47 |
+
|
| 48 |
+
fields = data.get('fields')
|
| 49 |
+
rows = data.get('data')
|
| 50 |
+
|
| 51 |
+
if not fields or not rows:
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
df = pd.DataFrame([dict(zip(fields, row)) for row in rows])
|
| 55 |
+
|
| 56 |
if format_type == 'fireballs':
|
| 57 |
+
return df.rename(columns={
|
| 58 |
+
'date': 'Date/Time', 'energy': 'Energy (kt)',
|
| 59 |
+
'impact-e': 'Impact Energy (10^10 J)', 'lat': 'Latitude',
|
| 60 |
+
'lon': 'Longitude', 'alt': 'Altitude (km)',
|
| 61 |
+
'vel': 'Velocity (km/s)'
|
| 62 |
+
})
|
| 63 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
elif format_type == 'close_approaches':
|
| 65 |
+
return df.rename(columns={
|
| 66 |
+
'des': 'Object', 'orbit_id': 'Orbit ID', 'cd': 'Time (TDB)',
|
| 67 |
+
'dist': 'Nominal Distance (au)', 'dist_min': 'Minimum Distance (au)',
|
| 68 |
+
'dist_max': 'Maximum Distance (au)', 'v_rel': 'Velocity (km/s)',
|
| 69 |
+
'h': 'H (mag)'
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
return df
|
| 73 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
# Gradio Interface Functions
|