about summary refs log tree commit diff
path: root/main2.py
blob: 83b512be6a678141e26a98b926089e540806be00 (plain)
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
# import modules
import bpy
import matplotlib.pyplot as plt
import urllib
import math
import numpy as np
from timeit import default_timer as timer

# import helper-modules
# convert polar to cartesian koordinates:
# import poltocart as ptc

class TLE:
    def get(value, category, satNr):
        with open('TLE/' + category + '.txt') as data:
            content = data.readlines()

            if value == "name":
                return content[(satNr)*3][0:15]
            elif value == "LineNumber":
                return content[((satNr)*3)+1][0:1]
            elif value == "Classification":
                return content[((satNr)*3)+1][7:8]
            elif value == "LaunchYear":
                return content[((satNr)*3)+1][9:11]
            elif value == "LaunchNumber":
                return content[((satNr)*3)+1][11:14]
            elif value == "LaunchPiece":
                return content[((satNr)*3)+1][14:17]
            elif value == "EpochYear":
                return content[((satNr)*3)+1][18:20]
            elif value == "EpochDayFraction":
                return content[((satNr)*3)+1][20:32]
            elif value == "EpochDay":
                return content[((satNr)*3)+1][20:23]
            elif value == "EpochTime":
                return content[((satNr)*3)+1][24:32]
            elif value == "FirstTimeDerivative":
                return content[((satNr)*3)+1][33:43]
            elif value == "SecondTimeDerivative":
                return content[((satNr)*3)+1][44:52]
            elif value == "BSTARDragTerm":
                return content[((satNr)*3)+1][53:61]
            elif value == "Num0":
                return content[((satNr)*3)+1][62:63]
            elif value == "ElementSetNumber":
                return content[((satNr)*3)+1][64:68]
            elif value == "Checksum1":
                return content[((satNr)*3)+1][68:69]

            elif value == "LineNumber2":
                return content[((satNr)*3)+2][0:1]
            elif value == "Number2":
                return content[((satNr)*3)+2][2:7]
            elif value == "Inclination":
                return content[((satNr)*3)+2][8:16]
            elif value == "RAAN":
                return content[((satNr)*3)+2][17:25]
            elif value == "Eccentricity":
                return content[((satNr)*3)+2][26:33]
            elif value == "ArgumentOfPerigee":
                return content[((satNr)*3)+2][34:42]
            elif value == "MeanAnomaly":
                return content[((satNr)*3)+2][43:51]
            elif value == "MeanMotion":
                return content[((satNr)*3)+2][52:63]
            elif value == "Revloution":
                return content[((satNr)*3)+2][63:68]
            elif value == "Checksum2":
                return content[((satNr)*3)+2][68:69]

    # downlaod specific categeory
    def download(category):
        webx_loc = 'http://celestrak.com/NORAD/elements/' + category + '.txt'
        disk_loc = 'TLE/' + category + '.txt'
        urllib.request.urlretrieve(webx_loc, disk_loc)

    # get number of satellites in specific ategory
    def numOfSat(category):
        with open('TLE/' + category + '.txt') as data:
            content = data.readlines()
            return int(len(content) / 3)

    # print specified category
    def printTLE(category, satNr):
        with open('TLE/' + category + '.txt') as data:
            content = data.readlines()
            print(content[((satNr)*3)+1], end="")
            print(content[((satNr)*3)+2], end="")

run_x_times = 10000
timer_list = list()

start_all = timer()

for value_mult in range(0, run_x_times, 1):

    start = timer()

    # controll values
    category = "dummy"
    globalScale = 1
    satSize = 0.5
    orbitSubDivs = 256
    resolution = 5000      # get position of sat each x frames
    threshold = 0.1

    # if internet connection available:
    # TLE.download(category)

    # define
    sce = bpy.context.scene
    n = TLE.numOfSat(category)
    numOfSat = TLE.numOfSat(category)
    rotate = bpy.ops.transform.rotate

    # static
    earthRadius = 6371
    daylengthsec = 86400

    # create list
    xyz = list( [[], [], []] for _ in range(0, numOfSat) )

    # select all -> delete
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete(use_global=False)

    # add earth model (sphere)
    bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(0, 0, 0))
    bpy.ops.object.subdivision_set(level=4)

    # cycle through every satellite in one category
    for i in range(0, numOfSat):

        # define
        satNr = i
        name = TLE.get("name", category, satNr).rstrip("\n")

        # get inclination and convert
        inc_deg = float(TLE.get("Inclination", category, satNr))
        inc_rad = inc_deg / 180 * math.pi

        # get RAAN and convert
        RAAN_deg = float(TLE.get("RAAN", category, satNr))
        RAAN_rad = RAAN_deg * math.pi / 180

        # get AoP and convert
        AoP_deg = float(TLE.get("ArgumentOfPerigee", category, satNr))
        AoP_rad = AoP_deg * math.pi / 180

        # get Mean Motion
        n0 = float(TLE.get("MeanMotion", category, satNr))

        # define duration (time for one rotation around earth)
        duration = int(daylengthsec / n0)

        # calculate apogee / perigee
        semimajoraxis = ((6.6228 / pow(n0, 2/3)) * earthRadius)
        orbitheight = semimajoraxis - earthRadius

        # calculate Eccentricity and convert ("decimal point assumed")
        e0_a = str(TLE.get("Eccentricity", category, satNr))
        e0 = float("0." + e0_a)

        # define apogee and perigee
        apogee = abs(semimajoraxis * (1 + e0) - earthRadius)
        perigee = abs(semimajoraxis * (1 - e0) - earthRadius)

        # print important values

        # print("")
        #
        # print("{:<10}{:<80}".format("name:", name))
        # print("{:<4}/{:<4}".format(i, numOfSat))
        print((value_mult / run_x_times)*100)

        # TLE.printTLE(category, satNr)

        # print("")

        # define names
        orbitname = name
        satname = name + "sat"

        # add orbit, rename orbit
        bpy.ops.mesh.primitive_circle_add(radius=1, vertices=orbitSubDivs)
        bpy.context.object.name = orbitname

        # add sat, rename sat
        bpy.ops.mesh.primitive_cube_add(radius=satSize)
        bpy.context.object.name = satname

        # define object names
        orbit = bpy.context.scene.objects[name]
        sat = bpy.context.scene.objects[name + "sat"]

        # convert orbit to curve and attach sat
        sat.select = False
        orbit.select = True
        sce.objects.active = orbit
        bpy.ops.object.convert(target='CURVE')

        # set orbit duration
        bpy.data.curves[name].path_duration = duration

        # resize orbit
        orbit.scale[0] = apogee
        orbit.scale[1] = perigee

        # move sat to perigee
        sat.location[1] = perigee

        # make sat follow orbit
        sat.select = True
        sce.objects.active = orbit
        bpy.ops.object.parent_set(type='FOLLOW')

        # set duration for 1 revolution
        bpy.data.curves[orbitname].path_duration = duration

        # rotate orbit
        orbit.select = True

        rotate(value=RAAN_rad, axis=(0, 0, 1))
        rotate(value=inc_rad, axis=(1, 0, 0))
        rotate(value=AoP_rad, axis=(0, 0, 1))

        # Getting the position of a satellite:
        # 1. jump to specific frame
        # 2. clear parent (keep transform)
        # 3. get position value and append it to a list
        # 4. reset parent

        for t in range(0, duration, resolution):
            # jump to frame
            sce.frame_set(t)

            # clear parent
            bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')

            # append sat location to list xyz
            xyz[satNr][0].append(sat.location[0])
            xyz[satNr][1].append(sat.location[1])
            xyz[satNr][2].append(sat.location[2])

            # reset parent
            bpy.ops.object.parent_set(type="FOLLOW")

    # end timer -> print runtime
    end = timer()

    # plot x, y and z values ove every satellite for every moment
    # for i in range(0, numOfSat, 1):
    #     plt.plot(xyz[i][0], '-ro')
    #     plt.plot(xyz[i][1], '-go')
    #     plt.plot(xyz[i][2], '-bo')
    #
    # plt.show()

    # Collision Detect
    # https://hanemile.github.io/docs/master.pdf
    # p. 8 - 11

    # create an array filled with 0
    # array[t, y, x]

    # print("")
    # print("{:<10}{:<10}".format("Total duration (sek.): ", end - start))

    timer_list.append(end - start)

# print all timer values
print(timer_list)

# plot the timer_list and show it
plt.plot(timer_list, '-ro')
plt.show()

end_all = timer()
duration_all = start_all - end_all
print(duration_all)

plt.savefig('x10000_res100.png')