-
Notifications
You must be signed in to change notification settings - Fork 0
/
satFunctions.py
235 lines (161 loc) · 6.12 KB
/
satFunctions.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
# satFunctions.py
#
# Functions for computing satellite related things with Skyfield
#
# Harry Krantz
# Steward Observatory
# University of Arizona
# Copyright May 2020
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime as dt
import numpy as np
import skyfield.api
# Compute the ephemeris and other parameters for a given TLE, location, and singular time
# Args: tle = string, loc = skyfield topos, time = Skyfield Time or datetime
# Returns: dict
def computeEphemeris(tle, loc, time):
#Split the tle
name, line1, line2 = tle
noradID = parseTLEID(tle)
#Initialization of things
sat = skyfield.sgp4lib.EarthSatellite(line1, line2, name)
ts = skyfield.api.load.timescale()
planets = skyfield.api.load('de421.bsp')
earth = planets['earth']
moon = planets['moon']
sun = planets['sun']
#Convert time if needed
if type(time) == dt.datetime:
time = ts.utc(time)
#Compute satellite position
geocentric = sat.at(time)
subpoint = geocentric.subpoint()
lat = subpoint.latitude
lon = subpoint.longitude
ele = subpoint.elevation
difference = sat - loc
topocentric = difference.at(time)
alt, az, distance = topocentric.altaz()
ra, dec, temp = topocentric.radec()
#Angular velocity per second
#Step time forward one second and get the difference in pointing
velocity = topocentric.separation_from( difference.at(ts.tt_jd(time.tt + 1/86400)) )
newTopocentric = difference.at(ts.tt_jd(time.tt + 1/86400))
newRa, newDec, temp = newTopocentric.radec()
raRate = newRa._degrees - ra._degrees
decRate = newDec.degrees - dec.degrees
#Skyfield does not have a built-in eclipsed function like PyEphem does :(
#This is a crude way of doing it but should be fine for this purpose
geocentricElong = geocentric.separation_from( earth.at(time).observe(sun) )
geocentricDist = geocentric.distance()
sunVectorSep = np.cos(geocentricElong.radians - np.pi/2) * geocentricDist.km
earthRadius = 6378 #km
umbraWidth = earthRadius - max(0, np.tan(np.radians(0.25)) * (np.sin(geocentricElong.radians - np.pi/2) * geocentricDist.km))
eclipsed = sunVectorSep < umbraWidth
#Determine if sun or moon is up and corresponging elongations
l = (earth + loc).at(time)
m = l.observe(moon).apparent()
s = l.observe(sun).apparent()
mAlt = m.altaz()[0]
sAlt = s.altaz()[0]
sunUp = sAlt.degrees > 0
sunElong = topocentric.separation_from(s)
moonUp = mAlt.degrees > 0
moonElong = topocentric.separation_from(m)
#Format output into dictionary
passs = {
"name" : name.strip(),
"id" : noradID,
"time" : time.utc_datetime(),
"range" : distance.km,
"height" : ele.km,
"altitude" : alt.degrees,
"azimuth" : az.degrees,
"ra" : ra._degrees,
"dec" : dec.degrees,
"raRate" : raRate,
"decRate" : decRate,
"lat" : lat.degrees,
"lon" : lon.degrees,
"velocity" : velocity.degrees,
"sunElong" : sunElong.degrees,
"moonElong" : moonElong.degrees,
"eclipsed" : eclipsed,
"sunUp" : sunUp,
"moonUp" : moonUp
}
return passs
# def printEphemeris(ephemeris):
# print("{: <24} {: <8} {: <21} {: <14.7s} {: <21} {: <10.7s} {: <14.7s} {: <21} {: <13.7s} {: <13.7s}".format(*map(str, [ephemeris["name"], ephemeris["id"], ephemeris["time"].strftime('%Y-%m-%d %H:%M:%S'), ephemeris["azimuth"], ephemeris["altitude"], ephemeris["ra"], ephemeris["dec"], ephemeris["lat"], ephemeris["lon"], ephemeris["velocity"], ephemeris["range"], ephemeris["height"], ephemeris["ra"] ],)))
# def printPassList(passes):
# #Print the header and list of passes
# headers = ["Name", "ID", "Rise Time", "Rise Azimuth", "Peak Time", "Peak Alt", "Peak Azimuth", "Set Time", "Set Azimuth", "Duration"]
# print("{: <24} {: <8} {: <21} {: <14} {: <21} {: <10} {: <14} {: <21} {: <13} {: <13}".format(*headers))
# print("----------------------------------------------------------------------------------------------------------------------------------------------------------------------")
# for p in passes:
# printPass(p)
# Extract the epoch date from a TLE
# Args: tle = array of string
# Return: datetime
def parseTLEdate(tle):
year = int("20" + tle[1][18:20])
day = float(tle[1][20:33])
return dt.datetime(year-1,12,31,0,0,0) + dt.timedelta(day)
# Extract the NORAD ID from a TLE
# Args: tle = array pf string
# Returns: string
def parseTLEID(tle):
return tle[1][2:8]
# Split TLE into the two/three lines
# Args: tle = array of string
# Returns: three strings
def splitTLE(tle):
#Split on newline characters
lines = tle.split("\n")
#Actual TLE data should always be the last two lines
line1 = lines[-2]
line2 = lines[-1]
#Check if there are three lines
if len(lines) > 2:
name = lines[0]
else:
name = "NONAME"
return name, line1, line2
# Print the lines of a TLE
# Args: tle = array of string
# Returns: nothing
def printTLE(tle):
for line in tle:
print(line)
# Compute the checksum for a single TLE line
# Args: line = string
# Returns: num
def checksum(line):
sum = 0
for ch in line[:-1]:
if ch.isdigit():
sum += int(ch)
if ch == "-":
sum += 1
return sum%10
# Compute and replace the checksum for a single TLE line
# Args: line = string
# Returns: string
def fixChecksum(line):
return line[:-1] + str(checksum(line))
#Compute the elongation between two (ra,dec), must be in radians
def elongation(ra1, dec1, ra2, dec2):
return np.arccos(np.sin(dec1)*np.sin(dec2) + np.cos(dec1)*np.cos(dec2)*np.cos(ra1 - ra2))
# 53.46721664899972 | 50.147600526245235 | 118.3210811540933 | -48.177141875391875
# temp = np.degrees(elongation(53.46721664899972,50.147600526245235,118.3210811540933,-48.177141875391875))
# print(temp)