-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathupdate_tle.py
executable file
·121 lines (99 loc) · 4.24 KB
/
update_tle.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
#!/usr/bin/env python3
import re
import os
import datetime
import argparse
import configparser
from io import BytesIO
from shutil import copyfile
from zipfile import ZipFile
from urllib.request import urlopen
from spacetrack import SpaceTrackClient
if __name__ == '__main__':
# Read commandline options
conf_parser = argparse.ArgumentParser(description="Update TLEs from" +
" online sources")
conf_parser.add_argument("-c", "--conf_file",
help="Specify configuration file. If no file" +
" is specified 'configuration.ini' is used.",
metavar="FILE")
args = conf_parser.parse_args()
# Process commandline options and parse configuration
cfg = configparser.ConfigParser(inline_comment_prefixes=("#", ";"))
if args.conf_file:
cfg.read([args.conf_file])
else:
cfg.read("configuration.ini")
# Create TLE locatio
tle_path = cfg.get("Elements", "tlepath")
if not os.path.exists(tle_path):
os.makedirs(tle_path)
now = datetime.datetime.utcnow()
time = now.strftime("%Y%m%d_%H%M%S")
print("Get Space Track TLEs")
catalog_tle = os.path.join(tle_path, "catalog.tle")
st = SpaceTrackClient(identity=cfg.get("Credentials", "st-username"),
password=cfg.get("Credentials", "st-password"))
data = st.tle_latest(iter_lines=True, epoch=">now-30",
ordinal=1, format="3le")
with open(catalog_tle, "w") as fp:
for line in data:
# Fix missing leading zeros
line = re.sub("^1 ", "1 0000", line)
line = re.sub("^2 ", "2 0000", line)
line = re.sub("^1 ", "1 000", line)
line = re.sub("^2 ", "2 000", line)
line = re.sub("^1 ", "1 00", line)
line = re.sub("^2 ", "2 00", line)
line = re.sub("^1 ", "1 0", line)
line = re.sub("^2 ", "2 0", line)
fp.write(line + "\n")
copyfile(catalog_tle, os.path.join(tle_path, time + "_catalog.txt"))
print("Get classified TLEs")
resp = urlopen("https://mmccants.org/tles/classfd.zip")
zipfile = ZipFile(BytesIO(resp.read()))
zipfile.extractall(path=tle_path)
classfd_tle = os.path.join(tle_path, "classfd.tle")
content = ""
outsize = 0
with open(classfd_tle, "rb") as infile:
content = infile.read()
with open(classfd_tle, "wb") as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + b"\n")
copyfile(classfd_tle, os.path.join(tle_path, time + "_classfd.txt"))
print("Get integrated TLEs")
resp = urlopen("https://mmccants.org/tles/inttles.zip")
zipfile = ZipFile(BytesIO(resp.read()))
zipfile.extractall(path=tle_path)
int_tle = os.path.join(tle_path, "inttles.tle")
content = ""
outsize = 0
with open(int_tle, "rb") as infile:
content = infile.read()
with open(int_tle, "wb") as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + b"\n")
copyfile(int_tle, os.path.join(tle_path, time + "_inttles.txt"))
print("Get supplemental Starlink TLEs")
resp = urlopen("https://celestrak.org/NORAD/elements/supplemental/sup-gp.php?FILE=starlink&FORMAT=tle")
starlink_tle = os.path.join(tle_path, "starlink.tle")
lines = resp.read().decode("utf-8")
with open(starlink_tle, "w") as fp:
fp.write(lines)
copyfile(starlink_tle, os.path.join(tle_path, time + "_starlink.txt"))
print("Get supplemental OneWeb TLEs")
resp = urlopen("https://celestrak.org/NORAD/elements/supplemental/sup-gp.php?FILE=oneweb&FORMAT=tle")
oneweb_tle = os.path.join(tle_path, "oneweb.tle")
lines = resp.read().decode("utf-8")
with open(oneweb_tle, "w") as fp:
fp.write(lines)
copyfile(oneweb_tle, os.path.join(tle_path, time + "_oneweb.txt"))
print("Create bulk catalog")
catalogs = [catalog_tle, classfd_tle]
with open(os.path.join(tle_path, "bulk.tle"), "w") as outfile:
for fname in catalogs:
with open(fname) as infile:
outfile.write(infile.read())