-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmangler.py
executable file
·149 lines (129 loc) · 3.51 KB
/
mangler.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
#!/usr/bin/env python
import os
import sys
import time
import datetime
import math
import json
db = {}
def dbmerge(norad, data):
if norad not in db:
db[norad] = {}
elif "epoch" in data and "epoch" in db[norad] and data["epoch"] < db[norad]["epoch"]:
return
for k in data.keys():
db[norad][k] = data[k]
if len(sys.argv) < 2:
sys.stderr.write("usage: %s <file> [file...]" % sys.argv[0])
sys.exit(1)
def read_lines(path):
with open(path) as f:
return f.readlines()
def is_tle(lines):
total = 0
tles = 0
for line in lines:
line = line.strip()
if len(line) == 0: continue
total += 1
if len(line) == 69:
tles += 1
return tles > total/2
def parse_tle(lines, set):
name = ""
norad = None
classification = None
epoch = None
intn = None
for line in lines:
line = line.strip()
if len(line) == 0: continue
if len(line) == 69:
# CHECKSUM
chksum = 0
for c in line[0:68]:
if c >= "0" and c <= "9":
chksum += int(c)
elif c == "-":
chksum += 1
chksum %= 10
if (chksum) != int(line[68:69]):
raise ValueError("checksum fail (%d) for TLE line: %s" % (chksum, line))
# helper functions
def get_line_number():
if line[0:2] == "1 ":
return 1
elif line[0:2] == "2 ":
return 2
else:
raise ValueError("invalid TLE line: %s" % line)
def get_norad():
return line[2:7]
ln = get_line_number()
# LINE 1
if ln == 1:
norad = get_norad()
classification = line[7:8]
intn = line[9:17]
e = line[18:32]
yy = int(e[0:2])
yyyy = 1900 + yy if yy > 56 else 2000 + yy
days = float(e[2:])
epoch = datetime.datetime(yyyy,1,1) + datetime.timedelta(days)
# LINE 2
elif ln == 2:
if get_norad() != norad:
raise ValueError("norad number mismatch: %s vs %s" % (norad, get_norad()))
inclination = float(line[8:16])
right_ascension_of_ascending_node = float(line[17:25])
eccentricity = float("0." + line[26:33])
argument_of_perigee = float(line[34:42])
mean_anomaly = float(line[43:51])
mean_motion = float(line[52:63])
revolution_number = int(line[63:68])
dbmerge(norad, {
"name": name,
"set": set,
"classification": classification,
"epoch": epoch,
"intn": intn,
"inclination": inclination,
"right_ascension_of_ascending_node": right_ascension_of_ascending_node,
"eccentricity": eccentricity,
"argument_of_perigee": argument_of_perigee,
"mean_anomaly": mean_anomaly,
"mean_motion": mean_motion,
"revolution_number": revolution_number,
})
else:
name = line.strip()
for path in sys.argv[1:]:
lines = read_lines(path)
if is_tle(lines):
print "TLE %s ..." % path
parse_tle(lines, os.path.splitext(os.path.basename(path))[0])
else:
raise ValueError("cannot parse %s: could not guess filetype" % path)
max_epoch = max([v['epoch'] for v in db.values()])
print "writing data0.js ..."
data0 = {"_epoch": long(long(time.mktime(max_epoch.timetuple())) * 1e3 + max_epoch.microsecond / 1e3)}
total = 0
for k,v in db.items():
if v['set'] not in data0:
data0[v['set']] = []
M0 = v['mean_anomaly']
dd = max_epoch - v['epoch']
M = M0 + 360.0 * (dd.total_seconds() / (24.0*60.0*60.0) / v['mean_motion'])
M %= 360.0
data0[v['set']].append([
k,
v['inclination'],
v['right_ascension_of_ascending_node'],
v['eccentricity'],
v['argument_of_perigee'],
M,
v['mean_motion']
])
total += 1
with open("data0.js", "w") as f: f.write("var data0=" + json.dumps(data0, cls = json.JSONEncoder).replace(" ", "") + ";\n")
print "total: %d orbits" % total