-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
349 lines (282 loc) · 12.4 KB
/
test.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
import unittest
import math
import datetime as dt
import numpy as np
import initialize_track
def assert_within_bounds(testCase,test_num,num,tol):
## Purpose: Assert that the test number is within a tolerance of the true number
##
## Inputs:
## test_num: test number from script
## num: true number
## tol: tolerance test number must be within
testCase.assertGreaterEqual(test_num,num-tol)
testCase.assertLessEqual(test_num,num+tol)
def assert_matrix_almost_equal(testCase,true,test):
## Purpose: Assert that all elements of input matrices are equal
##
## Inputs:
## True array
## Test array
r,c = true.shape
r_test,c_test = test.shape
testCase.assertEqual(r,r_test)
testCase.assertEqual(c,c_test)
for i in range(0,r):
for j in range(0,c):
testCase.assertAlmostEqual(test[i][j],true[i][j],2)
class Test_parse_rcfile(unittest.TestCase):
def test_function(self):
line = '53-46-5, -269-52-32'
coords = np.array([math.radians(53.7181),math.radians(-269.88)])
coords_out = np.array(initialize_track.parse_rcfile(line))
self.assertAlmostEqual(coords[0],coords_out[0],2)
self.assertAlmostEqual(coords[1],coords_out[1],2)
class Test_calc_time_since_perigee(unittest.TestCase):
e = 0.37255
w = math.radians(120)
a = 15300e3
epoch = '2018-06-07 23:54:00'
i = 0
tp = 4077
n = math.sqrt(initialize_track.Mew/math.pow(a,3))
E = 2 * math.atan2(math.sqrt((1 - e) / (1 + e)) * math.tan(w / 2), 1)
M = E - e*math.sin(E)
def test_ic(self):
tp_out, h, P, T = initialize_track.ic_calc_time_since_perigee(Test_calc_time_since_perigee.e,
Test_calc_time_since_perigee.w,
Test_calc_time_since_perigee.a,
Test_calc_time_since_perigee.epoch)
tp_out = (dt.datetime.strptime(Test_calc_time_since_perigee.epoch,'%Y-%m-%d %H:%M:%S')
- tp_out).total_seconds()
self.assertAlmostEqual(Test_calc_time_since_perigee.tp,tp_out,0)
def test_tle(self):
epoch = '2018-180 23:54:00.56'
tp_out, h, P, dO, dwp, T = initialize_track.tle_calc_time_since_perigee(Test_calc_time_since_perigee.M,
Test_calc_time_since_perigee.e,
Test_calc_time_since_perigee.n,
Test_calc_time_since_perigee.i,
epoch)
tp_out = (dt.datetime.strptime(epoch,'%Y-%j %H:%M:%S.%f')
- tp_out).total_seconds()
self.assertAlmostEqual(Test_calc_time_since_perigee.tp, tp_out, 0)
class Test_parse_tle(unittest.TestCase):
def test_function(self):
line1 = '1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927'
line2 = '2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537'
sat_num_out, epoch_out, i_out, O_out, e_out, wp_out, M_out, n_out = initialize_track.parse_tle(line1,line2)
sat_num = '25544'
epoch = '2008-264 12:25:40.1041'
i = math.radians(51.6416)
O = math.radians(247.4627)
e = 0.0006703
wp = math.radians(130.5360)
M = math.radians(325.0288)
n = 15.72125391*2*math.pi/86400
self.assertEqual(sat_num,sat_num_out)
self.assertEqual(epoch,epoch_out)
self.assertEqual(i,i_out)
self.assertEqual(O,O_out)
self.assertEqual(e,e_out)
self.assertEqual(wp,wp_out)
self.assertEqual(M,M_out)
self.assertEqual(n,n_out)
class Test_solve_kepler(unittest.TestCase):
def test_function(self):
E = math.radians(85)
e = 0.8
M = 0.68657
E_out = initialize_track.solve_kepler(M,e)
self.assertAlmostEqual(E,E_out,2)
class Test_calc_w(unittest.TestCase):
def test_function(self):
w = math.radians(85)
e = 0.8
E = 2*math.atan2(math.sqrt((1-e)/(1+e))*math.tan(w/2),1)
w_out = initialize_track.calc_w(E,e)
self.assertAlmostEqual(w,w_out,2)
class Test_peri2geo(unittest.TestCase):
def test_function(self):
i = math.radians(30)
O = math.radians(40)
wp = math.radians(60)
Q = np.array([[-0.0991,0.89593,0.43301],[-0.94175,-0.22496,0.25],[0.32139,-0.38302,0.86603]])
Q = np.transpose(Q)
Q_out = initialize_track.peri2geo(O,i,wp)
assert_matrix_almost_equal(self,Q,Q_out)
class Test_calc_greenwich_sidereal_time(unittest.TestCase):
def test_function(self):
time = dt.datetime(year=2004,month=3,day=3,hour=4,minute=30)
lon = math.radians(139.8)
theta = initialize_track.calc_greenwich_sidereal(time)
self.assertAlmostEqual(theta,math.radians(228.79354),2)
class Test_geo2ecef(unittest.TestCase):
def test_function(self):
time = dt.datetime(year=2018, month=6, day=15, hour=19, minute=39, second=26)
Q = np.array([[-0.9474,-0.3200,0.0017],[0.3200,-0.9474,-0.0006],[0,0,1.0000]])
Q_out = initialize_track.geo2ecef(time)
assert_matrix_almost_equal(self,Q,Q_out)
class Test_get_lat_lon(unittest.TestCase):
w = math.radians(30)
O = math.radians(40)
i = math.radians(30)
wp = math.radians(60)
h = 80e3*1e6
e = 1.4
P = math.pow(h, 2) / initialize_track.Mew
output_rgeo = initialize_track.find_rgeo(P, e, w, i, wp, O)
r_geo = [-4040e3,4815e3,3629e3]
def test_comparison(self):
r_eci = Test_get_lat_lon.r_geo
l_time = dt.datetime(year=2018, month=6, day=15, hour=19, minute=39, second=26)
qg_ecef = initialize_track.geo2ecef(l_time)
qg_ecef = np.asarray(qg_ecef)
r_ecef = np.matmul(qg_ecef, r_eci)
lat, lon = initialize_track.get_latlon(r_ecef)
lat2 = math.atan2(r_eci[2],math.sqrt(math.pow(r_eci[0],2)+math.pow(r_eci[1],2)))
theta_g = initialize_track.calc_greenwich_sidereal(l_time)
lon2 = math.atan2(r_eci[1],r_eci[0]) - theta_g
while lon2 < math.radians(0):
lon2 += 2*math.pi
while lon2 > math.radians(360):
lon2 -= 2*math.pi
while lat2 < math.radians(-90):
lat2 += 2*math.pi
while lat2 > math.radians(90):
lat2 -= 2*math.pi
self.assertAlmostEqual(lat,lat2,2)
self.assertAlmostEqual(lon,lon2,2)
def test_function_find_r_geo(self):
r_geo = Test_get_lat_lon.r_geo
output_rgeo = Test_get_lat_lon.output_rgeo
tol = 1
assert_within_bounds(self,output_rgeo[0][0]/1e3,r_geo[0]/1e3,tol)
assert_within_bounds(self,output_rgeo[1][0]/1e3,r_geo[1]/1e3,tol)
assert_within_bounds(self,output_rgeo[2][0]/1e3,r_geo[2]/1e3,tol)
def test_function_get_latlon(self):
r_eci = [-5368e3,-1784e3,3691e3]
lat, lon = initialize_track.get_latlon(r_eci)
self.assertAlmostEqual(lat,math.radians(33.12),2)
self.assertAlmostEqual(lon,math.radians(198.4),2)
class Test_get_dO_dwp(unittest.TestCase):
def test_function(self):
e = 0.008931
a = 6718e3
i = math.radians(51.43)
dO = -1.0465e-6
dwp = 7.9193e-7
dO_out, dwp_out = initialize_track.get_dO_dwp(a,e,i)
self.assertAlmostEqual(dO,dO_out,2)
self.assertAlmostEqual(dwp,dwp_out,2)
# class Test_haversine(unittest.TestCase):
# def test_function(self):
# ref_coords = [50.76,-83.4]
# coords = [-60.2,89]
#
# dist = 18860e3
# dist_out = initialize_track.haversine(ref_coords,coords)
#
# self.assertAlmostEqual(dist,dist_out,0)
class Test_lla2geo(unittest.TestCase):
def test_function(self):
lat = math.radians(40)
lon = math.radians(-75)
time = dt.datetime(year=1995,month=10,day=1,hour=9)
r_geo = initialize_track.lla2geo([lat,lon],time)
x = r_geo[0][0]
y = r_geo[1][0]
z = r_geo[2][0]
true_x = int(1703.295e3)
true_y = int(4586.650e3)
true_z = int(4077.984e3)
tol = 1.6
# self.assertLessEqual(x,true_x+tol) # Check that values align within 2 m
# self.assertGreaterEqual(x,true_x-tol)
# self.assertLessEqual(y,true_y+tol)
# self.assertGreaterEqual(y,true_y-tol)
# self.assertLessEqual(z,true_z+tol)
# self.assertGreaterEqual(z,true_z-tol)
assert_within_bounds(self,x,true_x,tol)
assert_within_bounds(self,y,true_y,tol)
assert_within_bounds(self,z,true_z,tol)
class Test_geo2topo(unittest.TestCase):
def test_function(self):
theta = math.radians(60)
lat = math.radians(30)
Q = np.array([[0.25,0.43301,-0.866],[-0.866,0.5,0],[0.43301,0.75,0.5]])
Q_out = initialize_track.geo2topo(theta,lat)
assert_matrix_almost_equal(self,Q,Q_out)
class Test_calc_sat_subpoint(unittest.TestCase):
def test_function(self):
lat = math.radians(45)
lon = math.radians(-93)
r_geo = [-4400.594e3, 1932.87e3, 4760.712e3]
r_lla_out = initialize_track.calc_sat_subpoint(lat, lon, r_geo)
r_lla = [math.radians(44.91),math.radians(-92.31),397.507e3]
tol = 2
self.assertAlmostEqual(r_lla[0],r_lla_out[0],1)
self.assertAlmostEqual(r_lla[1],r_lla_out[1],1)
self.assertGreaterEqual(r_lla_out[2],r_lla[2]-tol)
self.assertLessEqual(r_lla_out[2],r_lla[2]+tol)
class Test_get_look_angles(unittest.TestCase):
def test_function(self):
ref_coords = [math.radians(45),math.radians(-93)]
time = dt.datetime(year=1995, month=11, day=18, hour=12, minute=46)
sat_geo = np.array([[-4400.594e3], [1932.87e3], [4760.712e3]])
elev_out, az_out = initialize_track.get_look_angles(ref_coords, time, sat_geo)
elev = 81.52
az = 100.36
self.assertAlmostEqual(elev,math.degrees(elev_out),2)
self.assertAlmostEqual(az,math.degrees(az_out),2)
# class Test_calc_sun_pos(unittest.TestCase):
# def test_comparison(self):
# time = dt.datetime.now()
#
# sun_eci = np.array(initialize_track.calc_sun_pos(time))
# j0 = initialize_track.calc_j0(time)
# UT = time.hour + time.minute/60.0 + time.second/3600.0
# JD = j0 + UT/24.0
#
# T = (JD - 2451545.0)/36525.0
# lamb = 280.4606184 + 36000.77005361*T #deg
# lamb = initialize_track.conv_ang(lamb, 'deg')
# M = 357.5277233 + 35999.05034*T #deg
# M = initialize_track.conv_ang(M, 'deg')
# eclip_lon = lamb + 1.914666471*math.sin(math.radians(M)) + 0.918994643*math.sin(math.radians(2*M)) #deg
# eclip_lon = initialize_track.conv_ang(eclip_lon, 'deg')
# e = 23.439291 - 0.0130042*T #deg
# e = initialize_track.conv_ang(e, 'deg')
#
# u_sun_eci_x = math.cos(math.radians(eclip_lon))
# u_sun_eci_y = math.cos(math.radians(e))*math.sin(math.radians(eclip_lon))
# u_sun_eci_z = math.sin(math.radians(e))*math.sin(math.radians(eclip_lon))
#
# sun_eci_x2 = sun_eci[0]/np.linalg.norm(sun_eci)
# sun_eci_y2 = sun_eci[1]/np.linalg.norm(sun_eci)
# sun_eci_z2 = sun_eci[2]/np.linalg.norm(sun_eci)
#
# self.assertAlmostEqual(u_sun_eci_x, sun_eci_x2, 2)
# self.assertAlmostEqual(u_sun_eci_y, sun_eci_y2, 2)
# self.assertAlmostEqual(u_sun_eci_z, sun_eci_z2, 2)
class Test_conv_ang(unittest.TestCase):
def test_function(self):
ang = -15
res = initialize_track.conv_ang(ang, 'deg')
self.assertEqual(res, 345)
ang = 400
res = initialize_track.conv_ang(ang, 'deg')
self.assertEqual(res, 40)
ang = 900
res = initialize_track.conv_ang(ang, 'deg')
self.assertEqual(res, 180)
ang = math.radians(-100)
res = initialize_track.conv_ang(ang, 'rad')
self.assertEqual(res, math.radians(260))
ang = math.radians(361)
res = initialize_track.conv_ang(ang, 'rad')
self.assertAlmostEqual(res, math.radians(1), 2)
ang = math.radians(950)
res = initialize_track.conv_ang(ang, 'rad')
self.assertAlmostEqual(res, math.radians(230), 2)
if __name__ == '__main__':
unittest.main()