File: convert-celestrak.py

package info (click to toggle)
gpredict 2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,280 kB
  • sloc: ansic: 40,611; makefile: 486; python: 145; sh: 85; xml: 31
file content (169 lines) | stat: -rwxr-xr-x 4,259 bytes parent folder | download
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
#!/usr/bin/python
#
# This script is used to create the initial satellite data repository by
# converting Celestrak TLE files into a satellites.dat and .cat files.
#
# 1. Copy this script into a temp folder
# 2. Create subdirectories ./in/ ./tmp/ and ./out/
# 3. Run the script
# 4. You should have a .cat file for each category as well as a satellites.dat
#    file in the ./out/ folder
#
# This script is only used during development and is not needed by end users.
#
import os
import string
import time
import urllib.request

# Satellite groups
groups = {
    "amateur" : "Amateur Radio",
    "cubesat" : "Cubesat",
    "galileo" : "Galileo Nav.",
    "glo-ops" : "Glonass Operational",
    "gps-ops" : "GPS Operational",
    "iridium-NEXT" : "Iridium NEXT",
    "molniya" : "Molniya",
    "noaa" : "NOAA",
    "science" : "Space & Earth Science",
    "visual" : "Brightest",
    "weather" : "Weather Satellites"
}

# Satellite nicknames
nicknames = {
    "4321" : "AO-5",
    "6236" : "AO-6",
    "7530" : "AO-7",
    "10703" : "AO-8",
    "14129" : "AO-10",
    "14781" : "UO-11",
    "16909" : "FO-12",
    "20437" : "UO-14",
    "20438" : "UO-15",
    "20439" : "AO-16",
    "20440" : "DO-17",
    "20441" : "WO-18",
    "20442" : "LO-19",
    "20480" : "FO-20",
    "21087" : "RS-14",
    "21089" : "RS-12/13",
    "21575" : "UO-22",
    "22077" : "KO-23",
    "22654" : "AO-24",
    "22825" : "AO-27",
    "22826" : "IO-26",
    "22828" : "KO-25",
    "22829" : "PO-28",
    "23439" : "RS-15",
    "24278" : "FO-29",
    "24305" : "MO-30",
    "25396" : "TO-31",
    "25397" : "GO-32",
    "25509" : "SO-33",
    "25520" : "PO-34",
    "25544" : "ISS",
    "25636" : "SO-35",
    "25693" : "UO-36",
    "26063" : "OO-38",
    "26545" : "SO-41",
    "26548" : "MO-46",
    "26549" : "SO-42",
    "26609" : "AO-40",
    "26931" : "NO-44",
    "26932" : "NO-45",
    "27607" : "SO-50",
    "27844" : "CO-55",
    "27848" : "CO-57",
    "27939" : "RS-22",
    "28375" : "AO-51",
    "28650" : "VO-52",
    "28894" : "XO-53",
    "28895" : "CO-58",
    "28941" : "CO-56",
    "35870" : "SO-67",
    "36122" : "HO-68",
    "39444" : "AO-73",
    "40967" : "AO-85",
    "43017" : "AO-91"
}

urlprefix = "https://celestrak.org/NORAD/elements/gp.php?GROUP="
urlsuffix = "&FORMAT=tle"

for group, name in groups.items():
    webfile = urlprefix + group + urlsuffix
    localfile = "./in/" + group + ".txt"
    print("Fetching " + webfile + " => " + localfile)
    urllib.request.urlretrieve (webfile, localfile)
    time.sleep(30)

### CHK ###

# For each input file
for group, name in groups.items():

    print(name+':')

    # open TLE file for reading
    tlefile = open('./in/'+group+'.txt', 'r')

    # create category file
    category = "./out/"+group+".cat"
    catfile = open(category, 'w')

    # first line is the group name
    catfile.write(name+'\n')

    while 1:
        # read three lines at a time; strip trailing whitespaces
        line1 = tlefile.readline().strip()
        if not line1: break
        line2 = tlefile.readline().strip()
        line3 = tlefile.readline().strip()

        # catalog number; strip leading zeroes
        catnum = line2[2:7].lstrip('0')
        print(" ... " + catnum)

        # add satellite to category
        catfile.write(catnum+'\n')

        # satellite file
        satfile = open('./tmp/'+catnum+'.sat','w')
        
        satfile.write('VERSION=1.1\n')
        satfile.write('NAME='+line1+'\n')
        if catnum in nicknames:
            satfile.write('NICKNAME='+nicknames[catnum]+'\n')
        else:
            satfile.write('NICKNAME='+line1+'\n')
        satfile.write('TLE1='+line2+'\n')
        satfile.write('TLE2='+line3+'\n')

        satfile.close()

    # close TLE and CAT files
    tlefile.close()
    catfile.close()

# now package the .sat files into one .dat file
datfile = open('./out/satellites.dat', 'w')
for file in os.listdir("./tmp/"):

    # open .sat file
    satfile = open("./tmp/"+file, "r")

    # Create fake config group of catnum
    catnum = file.partition(".")[0]
    datfile.write("\n["+catnum+"]\n")

    # read lines from satfile and write them to datfile
    text = satfile.readlines()
    datfile.writelines(text)

    satfile.close();

datfile.close()