-
Notifications
You must be signed in to change notification settings - Fork 1
/
orbitserverwithpost.py
192 lines (144 loc) · 7.85 KB
/
orbitserverwithpost.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
#/usr/bin/env python
'''
Space debris REST API
OrionHack 29/07/2023
curl for testing post:
curl -X POST -d "xvelocityobject=1.23&yvelocityobject=4.56&zvelocityobject=7.89&xpositionobject=10.11&ypositionobject=12.13&zpositionobject=14.15&xvelocitysatellite=16.17&yvelocitysatellite=18.19&zvelocitysatellite=20.21&xpositionsatellite=22.23&ypositionsatellite=24.25&zpositionsatellite=26.27" http://localhost:8888
'''
import tornado.ioloop
from tornado.web import RequestHandler, Application
import json
import pandas as pd
import numpy as np
import sqlite3
def meanmotion_to_semimajoraxis(mm, mu):
return mu**(1/3)/((mm*2*np.pi/84600)**(2/3))
def meananomaly_to_trueanomaly(ma, e):
return 2*np.arctan(np.sqrt((1+e)/(1-e))*np.tan(ma/2))
def find_nodes(orbit0, orbit1,direction=1):
'''
Finds the nodes of two orbits
Not always correct, but good enough for now
'''
i = orbit0["INCLINATION"]
argperi = orbit0["ARG_OF_PERICENTER"]
rx, ry, rz = np.cos(argperi)*np.cos(i), np.sin(argperi)*np.cos(i), np.sin(i)
i = orbit1["INCLINATION"]
argperi = orbit1["ARG_OF_PERICENTER"]
x, y, z = np.cos(argperi)*np.cos(i), np.sin(argperi)*np.cos(i), np.sin(i)
# cross product with reference vector
x, y, z = np.cross([rx, ry, rz], [x, y, z])
x, y, z = x*direction, y*direction, z*direction
# find angle with periapsis vector
px, py, pz = np.cos(argperi), np.sin(argperi), 0
ta = np.arccos(np.dot([x, y, z], [px, py, pz])/(np.linalg.norm([x, y, z])*np.linalg.norm([px, py, pz])))
# get radius from true anomaly
r = orbit1["a"]*(1-orbit1["ECCENTRICITY"]**2)/(1+orbit1["ECCENTRICITY"]*np.cos(ta))
# convert to cartesian
x, y, z = r*np.cos(ta), r*np.sin(ta), 0
return x, y, z
# Set up global constants
Earth_mu = 3.986004418e14
debris_data = pd.read_csv("https://celestrak.org/NORAD/elements/gp.php?GROUP=1982-092&FORMAT=csv")
debris_data["a"] = meanmotion_to_semimajoraxis(debris_data["MEAN_MOTION"], Earth_mu)
class MainHandler(RequestHandler):
def get(self):
global debris_data, Earth_mu
# Get the orbital elements
params = {}
if self.get_argument("mm", None) != None:
paramset = ["mm", "e", "i", "raan", "aop", "ma"]
params["a"] = meanmotion_to_semimajoraxis(float(self.get_argument("mm")), Earth_mu)
else:
paramset = ["a", "e", "i", "raan", "aop", "ta"]
for param in paramset:
argument = self.get_argument(param, None)
if argument == None:
self.set_status(422)
self.write(json.dumps({"error": "Missing orbital elements"}))
return
params[param] = float(argument)
params["INCLINATION"] = params["i"]
params["ECCENTRICITY"] = params["e"]
params["ARG_OF_PERICENTER"] = params["aop"]
dists = []
for n, row in debris_data.iterrows():
nodes = []
for direction in [-1,1]:
x,y,z = find_nodes(row, params, direction)
nodes.append(np.array([x,y,z]))
x,y,z = find_nodes(params, row, direction)
nodes.append(np.array([x,y,z]))
# find the closest nodes
dists.append(np.min([np.linalg.norm(nodes[0]-nodes[1]),
np.linalg.norm(nodes[2]-nodes[3]),
np.linalg.norm(nodes[0]-nodes[3]),
np.linalg.norm(nodes[1]-nodes[2])]))
debris_data["dist"] = dists
sorted_data = debris_data.sort_values(by="dist")
result = {}
for n, row in sorted_data[:5].iterrows():
result[n] = {"semimajoraxis": meanmotion_to_semimajoraxis(row["MEAN_MOTION"], Earth_mu),
"eccentricity": row["ECCENTRICITY"],
"inclination": row["INCLINATION"],
"raan": row["RA_OF_ASC_NODE"],
"aop": row["ARG_OF_PERICENTER"],
"ta": meananomaly_to_trueanomaly(row["MEAN_ANOMALY"], row["ECCENTRICITY"])}
self.set_status(200)
self.write(json.dumps(result))
def post(self):
postdata_xvelocityobject = float(self.get_argument('xvelocityobject'))
postdata_yvelocityobject = float(self.get_argument('yvelocityobject'))
postdata_zvelocityobject = float(self.get_argument('zvelocityobject'))
postdata_xpositionobject = float(self.get_argument('xpositionobject'))
postdata_ypositionobject = float(self.get_argument('ypositionobject'))
postdata_zpositionobject = float(self.get_argument('zpositionobject'))
postdata_xvelocitysatellite = float(self.get_argument('xvelocitysatellite'))
postdata_yvelocitysatellite = float(self.get_argument('yvelocitysatellite'))
postdata_zvelocitysatellite = float(self.get_argument('zvelocitysatellite'))
postdata_xpositionsatellite = float(self.get_argument('xpositionsatellite'))
postdata_ypositionsatellite = float(self.get_argument('ypositionsatellite'))
postdata_zpositionsatellite = float(self.get_argument('zpositionsatellite'))
positionobjectrelativetoearth = np.array([postdata_xpositionobject + postdata_xpositionsatellite ,postdata_ypositionobject + postdata_ypositionsatellite ,postdata_zpositionobject + postdata_zpositionsatellite])
velocityobjectrelativetoearth = np.array([postdata_xvelocityobject + postdata_xvelocitysatellite,postdata_yvelocityobject + postdata_yvelocitysatellite,postdata_zvelocityobject + postdata_zvelocitysatellite])
spacejunkdatabaseconn = sqlite3.connect('spacejunk.db')
spacejunkcursor = spacejunkdatabaseconn.cursor()
createtableq = '''
CREATE TABLE IF NOT EXISTS spacejunk(
id INTEGER PRIMARY KEY AUTOINCREMENT,
a REAL,
i REAL,
e REAL,
raan REAL,
aop REAL,
ta REAL
)
'''
spacejunkcursor.execute(createtableq)
spacejunkdatabaseconn.commit()
posstr = ((positionobjectrelativetoearth[0])*(positionobjectrelativetoearth[0]) + (positionobjectrelativetoearth[1])*(positionobjectrelativetoearth[1]) + (positionobjectrelativetoearth[2])*(positionobjectrelativetoearth[2]))**(1/2)
velocitystr = ((velocityobjectrelativetoearth[0])*(velocityobjectrelativetoearth[0]) + (velocityobjectrelativetoearth[1])*(velocityobjectrelativetoearth[1]) + (velocityobjectrelativetoearth[2])*(velocityobjectrelativetoearth[2]))**(1/2)
#finding specific orbital energy from velocity
h = np.cross(positionobjectrelativetoearth,velocityobjectrelativetoearth)
ske = (velocitystr**(2)/2) - (Earth_mu/posstr)
hnorm = np.linalg.norm(h)
a = -(Earth_mu/(2*ske))
eccentr = np.sqrt((1 + (2*ske*np.square(hnorm))/np.square(Earth_mu)))
inclinationi = np.arccos(h[2]/hnorm)
rightascension = np.arctan2(h[0],-h[1])
evec = (eccentr*h)/np.linalg.norm(h)
argofper = np.arctan2(np.dot(h,evec),(hnorm * eccentr*np.sin(inclinationi)))
insertintoquery = "INSERT INTO spacejunk(a,i,e,raan,aop) VALUES(?,?,?,?,?)"
spacejunkcursor.execute(insertintoquery,(a,inclinationi,eccentr,rightascension,argofper))
spacejunkdatabaseconn.close()
self.set_header("Content-Type","text/plain")
self.write("Succesfully updated database of known space junk!")
self.finish()
def make_app():
return Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()