-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectonomicon_GUI.py
1165 lines (919 loc) · 44.5 KB
/
Connectonomicon_GUI.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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import spiceypy as spice
from datetime import datetime, timedelta
import re
import sys
import matplotlib.pyplot as plt
import subprocess
import os
import mplcursors
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttk
title = "== == == Connectonomicon == == =="
version = "v20241221.00"
authors = "Authors: H. A. Guler\n\nConnectonomicon uses find_orb for orbit determination, developed by Bill Gray. (https://www.projectpluto.com/)"
license_text = "Connectonomicon is licensed under GNU General Public License v2.\nfind_orb is licensed under GNU General Public License v2, see https://www.projectpluto.com/find_orb.htm#License.\n" +\
"SPICE kernels are not shared alongside this tool, but their use may be credited as described in https://naif.jpl.nasa.gov/naif/credit.html"
help_text =\
"""
Connectonomicon is a tool that helps with the identification of separate astrometric measurements belonging to the same object. Below is a short version of README.txt, read that file for more info.
Connectonomicon makes use of find_orb for orbit determination. It then uses its gravitationally perturbed orbit propagator to predict the future trajectory of the object. """ +\
"""There is also a built-in (native) orbit determination algorithm based on the Laplace and Herget methods, but it is not as robust as find_orb and is quite a lot slower. """ +\
"""For short arcs, it also attempts to figure out the possible area where other sightings at a given date may be, accounting for possible measurement errors. """ +\
"""Lastly, it classifies potential related astrometric observations from a given list of observations.
Connectonomicon requires the console version of find_orb (fo64.exe) to be in the same directory as itself. You can get fo64.exe from the following URL on Project Pluto website:
https://www.projectpluto.com/fo_usage.htm
You need to define observations in Obs80 (MPC 80-column format) in two files - a list of known observations which will be used for the orbit fit and a list of undetermined observations to search within.
You will also need some SPICE kernels from NAIF, and a list of obscodes from MPC. See README.txt for more info.
"""
# Constants
km_per_AU = 149597870.7
AU = 149597870.7
km_per_mAU = 149597870.7 * 1e-3
s_per_d = 86400
day = 86400
arcsecond = 0.00027778 # deg
observatories = {}
## CLASS DEFINITIONS
class MainBody: # for the Sun and planets
def __init__(self, name, pos, GM):
self.name = name
self.pos = pos
self.GM = GM # a.k.a. mu, standard gravitational parameter
class MP: # minor planets, comets
def __init__(self, des, pos, vel):
self.des = des
self.pos = pos
self.vel = vel
class Obs: # MPC 80-column observations
def __init__(self, obs_str, debug=False):
if type(obs_str) == str:
self.obs_str = obs_str
packed_perm = obs_str[0:5]
packed_prov = obs_str[5:12]
date_str = obs_str[15:31]
RA_str = obs_str[32:43]
DEC_str = obs_str[44:55]
mag_str = obs_str[65:69]
obscode_str = obs_str[77:80]
# date handling
obs_date_parts = date_str.split()
year = int(obs_date_parts[0])
month = int(obs_date_parts[1])
day = int(float(obs_date_parts[2]))
decimal_day = float(obs_date_parts[2]) - day
day_seconds = 86400
decimal_secs = day_seconds * decimal_day
obs_datetime = datetime(year, month, day)
obs_datetime = obs_datetime + timedelta(seconds=decimal_secs)
# RA - DEC handling
hour2deg = 360/24
minute2deg = 360/(24*60)
second2deg = 360/(24*60*60)
RA_parts = RA_str.split()
RA_deg = float(RA_parts[0]) * hour2deg + float(RA_parts[1]) * minute2deg + float(RA_parts[2]) * second2deg
if obs_str[44] == "-":
DEC_sign = -1
else:
DEC_sign = 1
DEC_parts = DEC_str.split()
DEC_deg = (abs(float(DEC_parts[0])) + float(DEC_parts[1]) / 60 + float(DEC_parts[2]) / 3600) * DEC_sign
# mag handling
try:
mag = float(mag_str)
except ValueError:
mag = 0
self.perm = packed_perm
self.prov = packed_prov
self.date = obs_datetime
self.RA = RA_deg
self.DEC = DEC_deg
self.mag = mag
self.obs_code = obscode_str
elif type(obs_str) == list:
self.RA = obs_str[0]
self.DEC = obs_str[1]
self.date = obs_str[2]
def __str__(self):
return self.obs_str
def __repr__(self):
return str(self)
class ObsPair: # A pair is any 2 astrometric observations of the same object
def __init__(self, o1, o2, pix_error=1, deg_per_pixel=9.793873680970562e-05):
self.o1 = o1
self.o2 = o2
self.delta_t = (o2.date - o1.date).total_seconds()
self.delta_RA = (o2.RA - o1.RA)
self.delta_DEC = (o2.DEC - o1.DEC)
self.RA_vel = self.delta_RA / self.delta_t
self.DEC_vel = self.delta_DEC / self.delta_t
self.main_dir = [self.delta_RA, self.delta_DEC]
# some "numerical" error finding
self.o1_p = [[self.o1.RA + deg_per_pixel * pix_error, self.o1.DEC],
[self.o1.RA - deg_per_pixel * pix_error, self.o1.DEC],
[self.o1.RA, self.o1.DEC + deg_per_pixel * pix_error],
[self.o1.RA, self.o1.DEC - deg_per_pixel * pix_error],
[self.o1.RA + deg_per_pixel * pix_error, self.o1.DEC + deg_per_pixel * pix_error],
[self.o1.RA + deg_per_pixel * pix_error, self.o1.DEC - deg_per_pixel * pix_error],
[self.o1.RA - deg_per_pixel * pix_error, self.o1.DEC + deg_per_pixel * pix_error],
[self.o1.RA - deg_per_pixel * pix_error, self.o1.DEC - deg_per_pixel * pix_error]]
self.o2_p = [[self.o2.RA + deg_per_pixel * pix_error, self.o2.DEC],
[self.o2.RA - deg_per_pixel * pix_error, self.o2.DEC],
[self.o2.RA, self.o2.DEC + deg_per_pixel * pix_error],
[self.o2.RA, self.o2.DEC - deg_per_pixel * pix_error],
[self.o2.RA + deg_per_pixel * pix_error, self.o2.DEC + deg_per_pixel * pix_error],
[self.o2.RA + deg_per_pixel * pix_error, self.o2.DEC - deg_per_pixel * pix_error],
[self.o2.RA - deg_per_pixel * pix_error, self.o2.DEC + deg_per_pixel * pix_error],
[self.o2.RA - deg_per_pixel * pix_error, self.o2.DEC - deg_per_pixel * pix_error]]
all_lines = []
all_dists = []
for i in range(8):
for j in range(8):
c_line = [self.o2_p[j][0] - self.o1_p[i][0], self.o2_p[j][1] - self.o1_p[i][1]]
all_lines.append(c_line)
all_dists.append([abs(self.o2_p[j][0] - self.o1_p[i][0]), abs(self.o2_p[j][1] - self.o1_p[i][1])])
# find max difference in slope
max_diff = 0
if self.delta_RA != 0:
main_slope = self.delta_DEC / self.delta_RA
else:
main_slope = 1e10 # very large finite number
for i in range(len(all_lines)):
if all_lines[i][0] != 0:
slope = all_lines[i][1] / all_lines[i][0]
else:
slope = 1e10 # very large finite number
slope_diff = abs(slope - main_slope)
if slope_diff > max_diff:
max_diff = slope_diff
min_RA_vel = float("Inf")
max_RA_vel = -float("Inf")
min_DEC_vel = float("Inf")
max_DEC_vel = -float("Inf")
for i in range(len(all_dists)):
if all_dists[i][0] / self.delta_t < min_RA_vel:
min_RA_vel = all_dists[i][0] / self.delta_t
if all_dists[i][0] / self.delta_t > max_RA_vel:
max_RA_vel = all_dists[i][0] / self.delta_t
if all_dists[i][1] / self.delta_t < min_DEC_vel:
min_DEC_vel = all_dists[i][1] / self.delta_t
if all_dists[i][1] / self.delta_t > max_DEC_vel:
max_DEC_vel = all_dists[i][1] / self.delta_t
self.max_vel = (max_RA_vel**2 + max_DEC_vel**2)**0.5
self.min_vel = (min_RA_vel**2 + min_DEC_vel**2)**0.5
self.slope_diff = max_diff
self.main_slope = main_slope
def __str__(self):
output = str(o1) + "\n" + str(o2) + "\n\n"
output += "Max. slope diff: " + str(self.slope_diff) + "\n"
output += "Max. vel: " + str(self.max_vel) + " deg s-1,\tMin. vel: " + str(self.min_vel) + " deg s-1\n"
return output
def __repr__(self):
return str(self)
def guessPositions(self, new_dates, plot=True):
# sanitize dates
new_delta_ts = []
for new_date in new_dates:
obs_date_parts = new_date.split()
year = int(obs_date_parts[0])
month = int(obs_date_parts[1])
day = int(float(obs_date_parts[2]))
decimal_day = float(obs_date_parts[2]) - day
day_seconds = 86400
decimal_secs = day_seconds * decimal_day
obs_datetime = datetime(year, month, day)
obs_datetime = obs_datetime + timedelta(seconds=decimal_secs)
new_delta_t = (obs_datetime - self.o1.date).total_seconds()
new_delta_ts.append(new_delta_t)
position_sets = []
for i in range(len(new_delta_ts)):
dt = new_delta_ts[i]
p_center = [self.o1.RA + self.RA_vel * dt, self.o1.DEC + self.DEC_vel * dt]
max_slope = self.main_slope + self.slope_diff
min_slope = self.main_slope - self.slope_diff
max_normalized_RA_rate = 1 / (1 + max_slope**2)**0.5
max_normalized_DEC_rate = max_slope / (1 + max_slope**2)**0.5
min_normalized_RA_rate = 1 / (1 + min_slope**2)**0.5
min_normalized_DEC_rate = min_slope / (1 + min_slope**2)**0.5
if max_normalized_RA_rate * self.RA_vel < 0:
max_normalized_RA_rate = -max_normalized_RA_rate
min_normalized_RA_rate = -min_normalized_RA_rate
max_normalized_DEC_rate = -max_normalized_DEC_rate
min_normalized_DEC_rate = -min_normalized_DEC_rate
p1 = [self.o1.RA + max_normalized_RA_rate * self.max_vel * dt, self.o1.DEC + max_normalized_DEC_rate * self.max_vel * dt]
p2 = [self.o1.RA + max_normalized_RA_rate * self.min_vel * dt, self.o1.DEC + max_normalized_DEC_rate * self.min_vel * dt]
p3 = [self.o1.RA + min_normalized_RA_rate * self.max_vel * dt, self.o1.DEC + min_normalized_DEC_rate * self.max_vel * dt]
p4 = [self.o1.RA + min_normalized_RA_rate * self.min_vel * dt, self.o1.DEC + min_normalized_DEC_rate * self.min_vel * dt]
position_sets.append([p_center, p1, p2, p3, p4])
plt.scatter([o1.RA, o2.RA], [o1.DEC, o2.DEC], color="b", label="Previous Observations")
# plt.plot([o1.RA, o2.RA], [o1.DEC, o2.DEC], "--")
for i in range(len(new_delta_ts)):
p_center = position_sets[i][0]
p1 = position_sets[i][1]
p2 = position_sets[i][2]
p3 = position_sets[i][3]
p4 = position_sets[i][4]
plt.scatter([p_center[0]], [[p_center[1]]], color="r", label="Perfect Observation")
plt.plot([p1[0], p2[0], p4[0], p3[0], p1[0]], [p1[1], p2[1], p4[1], p3[1], p1[1]], color="yellow", label="Prediction Range")
plt.plot([o1.RA, p_center[0]], [o1.DEC, p_center[1]], "--")
plt.grid()
plt.show()
return position_sets
def checkDateObs(self, new_date, plot=True, checked_obs=None):
dt = (new_date - self.o1.date).total_seconds()
p_center = [self.o1.RA + self.RA_vel * dt, self.o1.DEC + self.DEC_vel * dt]
max_slope = self.main_slope + self.slope_diff
min_slope = self.main_slope - self.slope_diff
max_normalized_RA_rate = 1 / (1 + max_slope**2)**0.5
max_normalized_DEC_rate = max_slope / (1 + max_slope**2)**0.5
min_normalized_RA_rate = 1 / (1 + min_slope**2)**0.5
min_normalized_DEC_rate = min_slope / (1 + min_slope**2)**0.5
if max_normalized_RA_rate * self.RA_vel < 0:
max_normalized_RA_rate = -max_normalized_RA_rate
min_normalized_RA_rate = -min_normalized_RA_rate
max_normalized_DEC_rate = -max_normalized_DEC_rate
min_normalized_DEC_rate = -min_normalized_DEC_rate
p1 = [self.o1.RA + max_normalized_RA_rate * self.max_vel * dt, self.o1.DEC + max_normalized_DEC_rate * self.max_vel * dt]
p2 = [self.o1.RA + max_normalized_RA_rate * self.min_vel * dt, self.o1.DEC + max_normalized_DEC_rate * self.min_vel * dt]
p3 = [self.o1.RA + min_normalized_RA_rate * self.max_vel * dt, self.o1.DEC + min_normalized_DEC_rate * self.max_vel * dt]
p4 = [self.o1.RA + min_normalized_RA_rate * self.min_vel * dt, self.o1.DEC + min_normalized_DEC_rate * self.min_vel * dt]
if not plot:
return p_center, p1, p2, p3, p4
plt.scatter([self.o1.RA, self.o2.RA], [self.o1.DEC, self.o2.DEC], color="b", label="Previous Observations")
plt.scatter([p_center[0]], [[p_center[1]]], color="r", label="Perfect Observation")
plt.plot([p1[0], p2[0], p4[0], p3[0], p1[0]], [p1[1], p2[1], p4[1], p3[1], p1[1]], color="yellow", label="Prediction Range")
plt.plot([self.o1.RA, p_center[0]], [self.o1.DEC, p_center[1]], "--")
if checked_obs:
plt.scatter([new_obs.RA], [new_obs.DEC], color="g", label="Checked Observation")
plt.title("Observation Prediction")
plt.xlabel("RA (deg)")
plt.ylabel("DEC (deg)")
plt.legend(loc='upper left')
plt.grid()
plt.show()
class Observatory:
def __init__(self, code, lon, cos_phi, sin_phi, name):
self.code = code
self.lon = lon
self.name = name
# convert MPC's phi and rho to latitude and altitude
self.lat = np.degrees(np.arcsin(sin_phi))
self.rho = 6371.0088 * (cos_phi**2 + sin_phi**2)**0.5
def __repr__(self):
return (f"Observatory(code={self.code}, name='{self.name}', "
f"lon={self.lon:.4f}, lat={self.lat:.4f}, rho={self.rho:.4f})")
def performPairAnalysis(knwon_pair, oc, pix, res):
known_obses = text1.split("\n")
o1 = Obs(known_pair[0])
o2 = Obs(known_pair[1])
pair = ObsPair(o1, o2, pix, res)
check = pair.check_obs(oc)
def spherical2cartezian(d, RA, DEC):
x = d * np.cos(DEC) * np.cos(RA)
y = d * np.cos(DEC) * np.sin(RA)
z = d * np.sin(DEC)
return np.array([x, y, z])
def cartezian2spherical(vec):
d = (vec[0]**2 + vec[1]**2 + vec[2]**2)**0.5
RA = np.arctan2(vec[1], vec[0])
DEC = np.arcsin(vec[2] / d)
RA_deg = np.rad2deg(RA)
if RA_deg < 0:
RA_deg += 360
return d, RA_deg, np.rad2deg(DEC)
# return d, np.rad2deg(RA), np.rad2deg(DEC)
def sign(x):
if x >= 0:
return 1
return -1
def gravAccel(mp, bodies):
accel = np.array([0, 0, 0])
for body in bodies:
dist = np.linalg.norm(body.pos - mp.pos)
grav_dir = (body.pos - mp.pos) / dist
grav_mag = body.GM / dist**2
accel = accel + grav_mag * grav_dir
return accel
def stepYoshida8(mp, bodies, dt):
# - - - CONSTANTS - - -
w1 = 0.311790812418427e0
w2 = -0.155946803821447e1
w3 = -0.167896928259640e1
w4 = 0.166335809963315e1
w5 = -0.106458714789183e1
w6 = 0.136934946416871e1
w7 = 0.629030650210433e0
w0 = 1.65899088454396
ds = [w7, w6, w5, w4, w3, w2, w1, w0, w1, w2, w3, w4, w5, w6, w7]
cs = [0.3145153251052165, 0.9991900571895715, 0.15238115813844, 0.29938547587066, -0.007805591481624963,
-1.619218660405435, -0.6238386128980216, 0.9853908484811935, 0.9853908484811935, -0.6238386128980216,
-1.619218660405435, -0.007805591481624963, 0.29938547587066, 0.15238115813844, 0.9991900571895715,
0.3145153251052165]
# - - - - - - - - -
for i in range(15):
mp.pos = mp.pos + mp.vel * cs[i] * dt
accel = gravAccel(mp, bodies)
mp.vel = mp.vel + accel * ds[i] * dt
mp.pos = mp.pos + mp.vel * cs[15] * dt
def computeKepler(r, v, mu=1.3271244004193938e11):
global km_per_AU
r_mag = np.linalg.norm(r)
v_mag = np.linalg.norm(v)
h = np.cross(r, v)
h_mag = np.linalg.norm(h)
inclination = np.degrees(np.arccos(h[2] / h_mag))
k = np.array([0, 0, 1])
n = np.cross(k, h)
n_mag = np.linalg.norm(n)
if n_mag != 0:
omega = np.degrees(np.arccos(n[0] / n_mag))
if n[1] < 0:
omega = 360 - omega
else:
omega = 0
e_vec = (1 / mu) * (np.cross(v, h) - mu * r / r_mag)
eccentricity = np.linalg.norm(e_vec)
if n_mag != 0:
if eccentricity != 0:
arg_periapsis = np.degrees(np.arccos(np.dot(n, e_vec) / (n_mag * eccentricity)))
if e_vec[2] < 0:
arg_periapsis = 360 - arg_periapsis
else:
arg_periapsis = 0
else:
arg_periapsis = 0
if eccentricity != 0:
true_anomaly = np.degrees(np.arccos(np.dot(e_vec, r) / (eccentricity * r_mag)))
if np.dot(r, v) < 0:
true_anomaly = 360 - true_anomaly
else:
true_anomaly = np.degrees(np.arccos(np.dot(r / r_mag, v / v_mag)))
specific_energy = v_mag**2 / 2 - mu / r_mag
if abs(eccentricity - 1) > 1e-8:
semi_major_axis = -mu / (2 * specific_energy)
else:
semi_major_axis = np.inf
if eccentricity < 1:
E = 2 * np.arctan(np.tan(np.radians(true_anomaly) / 2) * np.sqrt((1 - eccentricity) / (1 + eccentricity)))
if E < 0:
E += 2 * np.pi
mean_anomaly = np.degrees(E - eccentricity * np.sin(E))
elif eccentricity > 1:
F = 2 * np.arctanh(np.tan(np.radians(true_anomaly) / 2) * np.sqrt((eccentricity - 1) / (eccentricity + 1)))
mean_anomaly = np.degrees(eccentricity * np.sinh(F) - F)
else:
mean_anomaly = None
return {
"a": semi_major_axis / AU,
"e": eccentricity,
"i": inclination,
"lon_asc": omega,
"arg_peri": arg_periapsis,
"true_anomaly": true_anomaly,
"mean anomaly": mean_anomaly
}
def propagate(p0, v0, date_init, date_final, obs_code="Geocentric", mark_date=None, dt=None):
global AU, day
# generate bodies
mp = MP("", p0, v0)
bodies = []
body_names = ["MERCURY BARYCENTER",
"VENUS BARYCENTER",
"EARTH BARYCENTER",
"MARS BARYCENTER",
"JUPITER BARYCENTER",
"SATURN BARYCENTER",
"URANUS BARYCENTER",
"NEPTUNE BARYCENTER",
"SUN"]
body_GMs = [2.2031780000000021E+04,
3.2485859200000006E+05,
4.0350323550225981E+05,
4.2828375214000022E+04,
1.2671276480000021E+08,
3.7940585200000003E+07,
5.7945486000000080E+06,
6.8365271005800236E+06,
1.3271244004193938e11]
for i in range(9):
new_body = MainBody(body_names[i], np.array([0, 0, 0]), body_GMs[i])
bodies.append(new_body)
# set time parameters
time_interval = (date_final - date_init).total_seconds()
if mark_date:
md_time_interval = (mark_date - date_init).total_seconds()
if not dt:
if mark_date:
dt = min([md_time_interval / 200, 10 * day])
else:
dt = min([time_interval / 200, 10 * day])
N_cycles = int(time_interval // dt) + 1
date_final_actual = date_init + timedelta(seconds=N_cycles * dt)
rhos = []
RAs = []
DECs = []
mark_RA = None
mark_DEC = None
# numerically propagate orbit
for cycle in range(N_cycles):
cycle_date = date_init + timedelta(seconds=cycle * dt)
cycle_date_str = cycle_date.strftime('%Y-%m-%dT%H:%M:%S')
t = spice.str2et(cycle_date_str)
for ib, body in enumerate(bodies):
state, _ = spice.spkezr(body.name, t, 'ECLIPJ2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
body.pos = state[:3]
stepYoshida8(mp, bodies, dt)
if N_cycles > 5000 and cycle % 1000 == 0:
percent_done = round(cycle / N_cycles * 100, 2)
print(f"Propagating: {percent_done}%")
if obs_code == "Geocentric":
rho, RA, DEC = getRADEC(cycle_date, mp.pos)
rhos.append(rho)
RAs.append(RA)
DECs.append(DEC)
else:
rho, RA, DEC = getRADECObsCode(cycle_date, obs_code, mp.pos)
rhos.append(rho)
RAs.append(RA)
DECs.append(DEC)
if mark_date and abs((mark_date - cycle_date).total_seconds()) < 2 * dt:
mark_RA = RA
mark_DEC = DEC
return mp.pos, mp.vel, date_final_actual, rhos, RAs, DECs, mark_RA, mark_DEC
def getEarthPos(obs_date, coord='ecliptic'):
t = spice.str2et(obs_date.strftime('%Y-%m-%dT%H:%M:%S'))
if coord == 'ecliptic':
earth_state, _ = spice.spkezr("EARTH", t, 'ECLIPJ2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
else:
earth_state, _ = spice.spkezr("EARTH", t, 'J2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
earth_pos = earth_state[:3]
return earth_pos
def readObservatoryData(file_path):
global observatories
print("Reading observatory data...")
with open(file_path, 'r') as file:
for line in file:
if line.strip() and not line.startswith("Code"):
try:
code = line[0:3].strip()
longitude = float(line[3:13])
cos_phi = float(line[13:21])
sin_phi = float(line[21:30])
name = ''.join(line[30:len(line)-1])
observatories[code] = Observatory(code, longitude, cos_phi, sin_phi, name)
except ValueError as e: # space telescopes etc. obviously don't have these
pass
return observatories
def getObsCodePos(obs_date, obs_code, coord='ecliptic'):
lat = observatories[obs_code].lat
lon = observatories[obs_code].lon
rho = observatories[obs_code].rho
t = spice.str2et(obs_date.strftime('%Y-%m-%dT%H:%M:%S'))
if coord == 'ecliptic':
earth_pos, _ = spice.spkpos('EARTH', t, 'ECLIPJ2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
else:
earth_pos, _ = spice.spkpos('EARTH', t, 'J2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
lat_rad = np.deg2rad(lat)
lon_rad = np.deg2rad(lon)
R_EARTH = 6371.0088
x_geo = rho * np.cos(lat_rad) * np.cos(lon_rad)
y_geo = rho * np.cos(lat_rad) * np.sin(lon_rad)
z_geo = rho * np.sin(lat_rad)
geo_pos = np.array([x_geo, y_geo, z_geo])
geo_to_eci = spice.pxform('ITRF93', 'J2000', t)
eci_pos = geo_to_eci @ geo_pos
if coord == 'ecliptic':
eci_to_ecliptic = spice.pxform('J2000', 'ECLIPJ2000', t)
ecliptic_pos = eci_to_ecliptic @ eci_pos
obscode_pos = np.array(earth_pos) + ecliptic_pos
else:
obscode_pos = np.array(earth_pos) + eci_pos
return obscode_pos
def getRADEC(obs_date, pos):
earth_pos = getEarthPos(obs_date, 'equatorial')
return cartezian2spherical(ecliptic2equatorial(pos) - earth_pos)
def getRADECObsCode(obs_date, obs_code, pos):
obscode_pos = getObsCodePos(obs_date, obs_code, 'equatorial')
return cartezian2spherical(ecliptic2equatorial(pos) - obscode_pos)
def equatorial2ecliptic(eq_pos):
epsilon = np.deg2rad(23.439281)
rotation_matrix = np.array([
[1, 0, 0],
[0, np.cos(epsilon), np.sin(epsilon)],
[0, -np.sin(epsilon), np.cos(epsilon)]
])
ecliptic_coords = np.dot(rotation_matrix, eq_pos)
return ecliptic_coords
def ecliptic2equatorial(ec_pos):
epsilon = np.deg2rad(23.439281)
rotation_matrix = np.array([
[1, 0, 0],
[0, np.cos(epsilon), -np.sin(epsilon)],
[0, np.sin(epsilon), np.cos(epsilon)]
])
equatorial_coords = np.dot(rotation_matrix, ec_pos)
return equatorial_coords
# === === === ORBIT DETERMINATION FUNCTIONS === === ===
def placeRelToEarth(obs_date, R, RA, DEC, coord='ecliptic'):
t = spice.str2et(obs_date.strftime('%Y-%m-%dT%H:%M:%S'))
if coord == 'ecliptic':
earth_state, _ = spice.spkezr("EARTH", t, 'ECLIPJ2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
else:
earth_state, _ = spice.spkezr("EARTH", t, 'J2000', 'NONE', 'SOLAR SYSTEM BARYCENTER')
earth_pos = earth_state[:3]
p = earth_pos + spherical2cartezian(R, np.deg2rad(RA), np.deg2rad(DEC))
return p
def placeRelToObsCode(obs_date, obs_code, R, RA, DEC, coord='ecliptic'):
obscode_pos = getObsCodePos(obs_date, obs_code, coord)
p = obscode_pos + spherical2cartezian(R, np.deg2rad(RA), np.deg2rad(DEC))
return p
def constructUnitVector(RA, DEC):
x = np.cos(np.deg2rad(DEC)) * np.cos(np.deg2rad(RA))
y = np.cos(np.deg2rad(DEC)) * np.sin(np.deg2rad(RA))
z = np.sin(np.deg2rad(DEC))
return np.array([x, y, z]) / np.linalg.norm(np.array([x, y, z]))
def get_rho2s(o1, o2, o3):
mu = 1.3271244004193938e11
rhat_1 = constructUnitVector(o1.RA, o1.DEC)
rhat_2 = constructUnitVector(o2.RA, o2.DEC)
rhat_3 = constructUnitVector(o3.RA, o3.DEC)
R_1 = getEarthPos(o1.date, 'equatorial')
R_2 = getEarthPos(o2.date, 'equatorial')
R_3 = getEarthPos(o3.date, 'equatorial')
t3mt1 = (o3.date - o1.date).total_seconds()
rhatprime_2 = (rhat_3 - rhat_1) / t3mt1
rhatprimeprime_2 = (rhat_3 - 2 * rhat_2 + rhat_1) / (t3mt1**2)
tau_1 = (o1.date - o2.date).total_seconds()
tau_3 = (o3.date - o2.date).total_seconds()
tau = (o3.date - o1.date).total_seconds()
p_1 = np.cross(rhat_2, rhat_3)
p_2 = np.cross(rhat_1, rhat_3)
p_3 = np.cross(rhat_1, rhat_2)
D_0 = np.dot(rhat_1, p_1)
D_11 = np.dot(R_1, p_1)
D_12 = np.dot(R_1, p_2)
D_13 = np.dot(R_1, p_3)
D_21 = np.dot(R_2, p_1)
D_22 = np.dot(R_2, p_2)
D_23 = np.dot(R_2, p_3)
D_31 = np.dot(R_3, p_1)
D_32 = np.dot(R_3, p_2)
D_33 = np.dot(R_3, p_3)
A = 1/D_0 * (-D_12 * tau_3 / tau + D_22 + D_32 * tau_1 / tau)
B = 1 / (6 * D_0) * (D_12 * (tau_3**2 - tau**2) * tau_3 / tau + D_32 * (tau**2 - tau_1**2) * tau_1 / tau)
E = np.dot(R_2, rhat_2)
R_2sq = np.dot(R_2, R_2)
a = -(A**2 + 2*A*E + R_2sq)
b = -2*mu*B*(A+E)
c = -mu**2 * B**2
rho_2s = np.roots([1, 0, a, 0, 0, b, 0, 0, c]).real
return rho_2s
def perfectV0(R1, deltaR, o1, o2):
R2 = R1 + deltaR
# p0 = equatorial2ecliptic(placeRelToEarth(o1.date, R1, o1.RA, o1.DEC, 'equatorial'))
# pf = equatorial2ecliptic(placeRelToEarth(o2.date, R2, o2.RA, o2.DEC, 'equatorial'))
p0 = equatorial2ecliptic(placeRelToObsCode(o1.date, o1.obs_code, R1, o1.RA, o1.DEC, 'equatorial'))
pf = equatorial2ecliptic(placeRelToObsCode(o2.date, o2.obs_code, R2, o2.RA, o2.DEC, 'equatorial'))
date_final = o2.date
date_init = o1.date
delta_time = (date_final - date_init).total_seconds()
v0 = (pf - p0) / delta_time # km s-1
error = float('Inf')
tol = 1e-4
while error > tol:
p_final, v_final, date_final_actual, _, _, _, _, _ = propagate(p0, v0, date_init, date_final)
R_final, RA_final, DEC_final = getRADECObsCode(o2.date, o2.obs_code, pf)
p_err = pf - p_final
v0 = v0 + p_err / delta_time
error = np.linalg.norm(p_err)
# print(f"Perfected v0 with {error} km of error.")
return v0
def determineOrbit(obs_all):
mu = 1.3271244004193938e11
o1 = obs_all[0]
o3 = obs_all[len(obs_all) - 1]
o2 = obs_all[int(len(obs_all) / 2)]
date_init = o1.date
date_final = o2.date
date_check = o3.date
rho_2s = abs(get_rho2s(o1, o2, o3))
# --- initial observation guess ---
# initially assuming perfect observation with no errors
R1 = max(rho_2s) # max. is usually the closest one
deltaR = 0 * AU
R2 = R1 + deltaR
# p0 = equatorial2ecliptic(placeRelToEarth(o1.date, R1, o1.RA, o1.DEC, 'equatorial'))
# pf = equatorial2ecliptic(placeRelToEarth(o2.date, R2, o2.RA, o2.DEC, 'equatorial'))
p0 = equatorial2ecliptic(placeRelToObsCode(o1.date, o1.obs_code, R1, o1.RA, o1.DEC, 'equatorial'))
pf = equatorial2ecliptic(placeRelToObsCode(o2.date, o2.obs_code, R2, o2.RA, o2.DEC, 'equatorial'))
delta_time = (date_final - date_init).total_seconds()
# v0 = perfectV0(R1, deltaR, o1, o2)
v0 = (mu / np.linalg.norm(p0))**0.5 * np.array([-p0[1] / np.linalg.norm(p0), p0[0] / np.linalg.norm(p0), 0])
orbital_elems = computeKepler(p0, v0)
print("Initial guess:")
print(orbital_elems)
# --- --- --- --- --- --- --- --- ---
adjust_factor = 1
adjust_pfactor = 1
good_fit = False
retry_count = 0
max_retry = 100
while (not good_fit) and retry_count <= max_retry:
err_val = 0
for idx_o, o in enumerate(obs_all):
if o.date != date_init:
p_check, v_check, date_check_actual, rhos, RAs, DECs, _, _ = propagate(p0, v0, date_init, o.date)
d_prop, RA_prop, DEC_prop = getRADECObsCode(o.date, o.obs_code, p_check)
RA_err = o.RA - RA_prop
DEC_err = o.DEC - DEC_prop
err_val += RA_err**2 + DEC_err**2
print(f"Iter: {retry_count}, errRA: {RA_err}, errDEC: {DEC_err}")
orbital_elems = computeKepler(p0, v0)
# print(orbital_elems)
if abs(RA_err) < 1 * arcsecond and abs(DEC_err) < 1 * arcsecond:
good_fit = True
else:
adjust_vals = [0, 0, 0, 0, 0, 0]
adjust_vecs = [np.array([0.01, 0, 0]),
np.array([-0.01, 0, 0]),
np.array([0, 0.01, 0]),
np.array([0, -0.01, 0]),
np.array([0, 0, 0.01]),
np.array([0, 0, -0.01])]
for i in range(6):
adjust_vecs[i] *= adjust_factor
# adjust vel
for i in range(6):
v0_1 = v0 + adjust_vecs[i]
adjust_vals[i] = 0
for idx_o, o in enumerate(obs_all):
if o.date != date_init:
p_check_1, v_check_1, _, _, _, _, _, _ = propagate(p0, v0_1, date_init, o.date)
_, RA_prop_1, DEC_prop_1 = getRADECObsCode(o.date, o.obs_code, p_check_1)
RA_err_1 = o.RA - RA_prop_1
DEC_err_1 = o.DEC - DEC_prop_1
adjust_vals[i] += RA_err_1**2 + DEC_err_1**2
idx_min = np.argmin(adjust_vals)
if adjust_vals[idx_min] < err_val:
v0 = v0 + adjust_vecs[idx_min]
adjust_factor *= 2
else:
adjust_factor *= 0.1
# adjust pos
adjust_pvals = [0, 0, 0, 0, 0, 0]
adjust_pvecs = [np.array([0.01, 0, 0]),
np.array([-0.01, 0, 0]),
np.array([0, 0.01, 0]),
np.array([0, -0.01, 0]),
np.array([0, 0, 0.01]),
np.array([0, 0, -0.01])]
for i in range(6):
adjust_pvecs[i] *= adjust_pfactor
for i in range(6):
p0_1 = p0 + adjust_pvecs[i]
adjust_pvals[i] = 0
for idx_o, o in enumerate(obs_all):
if o.date != date_init:
p_check_1, v_check_1, _, _, _, _, _, _ = propagate(p0_1, v0, date_init, o.date)
_, RA_prop_1, DEC_prop_1 = getRADECObsCode(o.date, o.obs_code, p_check_1)
RA_err_1 = o.RA - RA_prop_1
DEC_err_1 = o.DEC - DEC_prop_1
adjust_pvals[i] += RA_err_1**2 + DEC_err_1**2
idx_min = np.argmin(adjust_pvals)
if adjust_pvals[idx_min] < err_val:
p0 = p0 + adjust_pvecs[idx_min]
adjust_pfactor *= 2
else:
adjust_pfactor *= 0.1
retry_count += 1
pf, vf, date_check_actual, rhos, RAs, DECs, _, _ = propagate(p0, v0, date_init, obs_all[-1].date)
orbital_elems = computeKepler(p0, v0)
print("Final fit:")
print(p0, v0)
print(orbital_elems)
return p0, v0, date_init
# === === === === === === === === === === === === === === === ===
def extractStateVector(filename="elements.txt"):
with open(filename, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if line.startswith("# State vector"):
pos_line = lines[i+1][3:53]
vel_line = lines[i+2][3:53]
position = np.array([float(x) for x in pos_line.split()])
velocity = np.array([float(x) for x in vel_line.split()])
if line.startswith("Epoch"):
epoch_str = line[6:20].strip()
epoch_date = datetime.strptime(epoch_str, '%Y %b %d.%f')
return position, velocity, epoch_date
def classifyObsNearCurve(X, Y, obs_list, tolerance, known_obs, final_date, downsample_factor=None):
curve = np.column_stack((X, Y))
close_obs = []
far_obs = []
known_mag = known_obs.mag
if downsample_factor is not None:
curve = curve[::downsample_factor]
for obs in obs_list:
point = np.array([obs.RA, obs.DEC])
# Find the closest point on the curve
distances = np.linalg.norm(curve - point, axis=1)
min_distance = np.min(distances)
if (min_distance < tolerance and ((not obs.mag) or (obs.mag and abs(obs.mag - known_mag) < 2)) # distance and magnitude
and known_obs.date < obs.date < final_date + timedelta(seconds=(final_date - known_obs.date).total_seconds() * 0.2)): # obs dates
close_obs.append(obs)
else:
far_obs.append(obs)
return close_obs, far_obs
def readObsFile(filename='primary.obs'):
obses = []
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
new_o = Obs(line)
obses.append(new_o)
return obses
class AstrometryApp:
def __init__(self, root):
self.root = root
self.root.title("Connectonomicon " + str(version))
current_row = 0
icon_path = "connectonomicon.png"
if os.path.exists(icon_path):
self.ico_img = tk.PhotoImage(file=icon_path).subsample(6, 6)
self.root.iconphoto(False, self.ico_img)
header_label = tk.Label(root, image=self.ico_img, bg="#212331")
header_label.grid(row=current_row, column=0, columnspan=2, sticky="n", pady=10)
current_row += 1
self.root.configure(bg="#212331")
# Input fields
tk.Label(root, text="Known Observations File:", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.primary_file_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.primary_file_entry.insert(0, "primary.obs")
self.primary_file_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
tk.Label(root, text="Potential Observations File:", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.secondary_file_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.secondary_file_entry.insert(0, "secondary.obs")
self.secondary_file_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
tk.Label(root, text="Propagation Time (days):", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.prop_time_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.prop_time_entry.insert(0, '5')
self.prop_time_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
tk.Label(root, text="Max. Measurement Error (pixel):", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.pix_error_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.pix_error_entry.insert(0, '1')
self.pix_error_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
tk.Label(root, text="Pixel Resolution (deg/pixel):", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.resolution_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.resolution_entry.insert(0, '9.793873680970562e-05')
self.resolution_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
tk.Label(root, text="Reference Obscode:", bg="#212331", fg="#eeeeee").grid(row=current_row, column=0, padx=10, pady=5, sticky="w")
self.obscode_entry = tk.Entry(root, width=40, bg="#212350", fg="#eeeeee")
self.obscode_entry.insert(0, 'Geocentric')
self.obscode_entry.grid(row=current_row, column=1, padx=10, pady=5)
current_row += 1
s = ttk.Style() # silly ttk needs styles to style widgets
s.configure('Connect.TRadiobutton',