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
|
#!/usr/bin/env python
import ephem
import time
import os
import sys
import urllib
from MmdDb import Db
from MmdLocation import Location
def dms2degdec (lonlat):
'''
convert a position argument from Deg:Min:Sec.Frac to
Deg.Frac and round the result to 5 decimal points
'''
parts = lonlat.split (':')
if len (parts) != 3:
return 0.0
degrees = float (parts[0])
fraction = (float (parts[1]) * 60.0 + float (parts[2])) / 3600
if parts[0][0] == '-':
return round (degrees - fraction, 5)
return round (degrees + fraction, 5)
class Satellite:
def __init__ (self, location = False):
self.name = 'RADIOSCAF-B'
self.nickname = 'ARISSAT-1'
self.tle_filename = '/usr/local/mmd/tles/current'
self.db = Db ()
self.initSatellite (location)
def getCurrentSSP (self):
ssp = self._loadCurrentSSP ()
return ssp['timestamp'], ssp['longitude'], ssp['latitude']
def getTrajectoryAsJavaArray (self, name, minutes = 180):
astring = 'var {0} = new Array (\n'.format (name)
astring = '{0}\tnew Array ('.format (astring)
astring = '{0}\n\t\tnew OpenLayers.LonLat ({1}, {2}).transform (from, to),'.format (
astring,
self.longitude,
self.latitude
)
last = 'east'
if str (self.longitude)[0] == '-':
last = 'west'
for row in self._loadTrajectory (minutes):
if str (row['longitude'])[0] == '-':
if last == 'east':
astring = astring[:-1]
astring = '{0}\n\t\t),\n\tnew Array ('.format (astring)
last = 'west'
else:
last = 'east'
astring = '{0}\n\t\tnew OpenLayers.LonLat ({1}, {2}).transform (from, to),'.format (
astring, row['longitude'], row['latitude'])
if astring[-1] == ',':
astring = astring[:-1]
astring = '{0}\n\t\t)\n\t);'.format (astring)
return astring
def _loadTrajectory (self, minutes):
return self.db.satelliteLoadTrajectory (time.strftime ('%s'), minutes)
def _loadCurrentSSP (self):
return self.db.satelliteLoadCurrentSSP (time.strftime ('%s'))
def initSatellite (self, location = False):
if not self._loadTLE ():
return False
if location:
loc = location
else:
loc = Location ()
sat = ephem.readtle (self._tle[0], self._tle[1], self._tle[2])
obs = ephem.Observer ()
obs.long, obs.lat, obs.elevation = loc.longitude, loc.latitude, loc.altitude
sat.compute (obs)
self.longitude = dms2degdec (sat.sublong.__str__ ())
self.latitude = dms2degdec (sat.sublat.__str__ ())
self.altitude = round (float (sat.elevation.__str__ ()), 1)
def _computeTrajectory (self):
'''
computes 5 hours of future trajectory data, starting at the
latest timestamp found in data base
'''
if not self._loadTLE ():
return False
sat = ephem.readtle (self._tle[0], self._tle[1], self._tle[2])
graz = ephem.Observer ()
graz.long, graz.lat, graz.elevation = 15.44226, 47.06576, 376
latest = self.db.satelliteGetLatestSSP ()
if latest:
timestamp = int (latest['timestamp']) + 60
else:
timestamp = int (time.strftime ('%s')) + 60
while timestamp % 60 != 0:
timestamp += 1
for i in range (60 * 5):
graz.date = time.strftime ('%Y/%m/%d %H:%M:%S', time.gmtime (timestamp))
sat.compute (graz)
self.db.satelliteInsertNewSSP (timestamp, dms2degdec (sat.sublong.__str__ ()), dms2degdec (sat.sublat.__str__ ()))
timestamp += 60
return True
def _loadTLE (self):
try:
tle = open (self.tle_filename, 'r')
except IOError:
return False
self._tle = []
count = 0
for line in tle.readlines ():
self._tle.append (line)
count += 1
if count != 3:
return False
return True
def cronJob (self):
if self._updateTLEFile ():
self._updateTrajectory ()
return self._computeTrajectory ()
def _updateTLEFile (self):
'''
store new TLE data in file with format
YYYYMMDDhhmmss (Year Month Day hour minute second)
and make symlink to self.tle_filename
'''
if not self._getTLEFileFromNetwork ():
return False
filename = '/usr/local/mmd/tles/{0}'.format (time.strftime ('%Y%m%d%H%M%S'))
for line in self.tle_data:
if line.startswith (self.name):
tle = open (filename, 'w')
tle.write (line)
tle.write (self.tle_data.next ())
tle.write (self.tle_data.next ())
tle.close ()
return self._compareAndLinkNewTLEFile (filename)
return False
def _compareAndLinkNewTLEFile (self, filename):
'''
we should use field 21-32 from first line of TLE to
compare data; then use this date to generate a filename
and update the database;
'''
old_data = open (self.tle_filename).read ()
new_data = open (filename).read ()
if old_data == new_data:
os.unlink (filename)
return False
os.unlink (self.tle_filename)
os.symlink (filename, self.tle_filename)
return True
def _updateTrajectory (self):
self.db.satelliteDeleteObsoleteSSPs (time.strftime ('%s'))
def getLatestSSP (self):
return self.db.satelliteGetLatestSSP ()
def _getTLEFileFromNetwork (self):
'''
retreive TLE update from NORAD
'''
networkfile = 'http://www.celestrak.com/NORAD/elements/amateur.txt'
try:
self.tle_data = urllib.urlopen (networkfile)
except IOError:
return False
return True
if __name__ == "__main__":
satellite = Satellite ()
# print satellite.longitude, satellite.latitude
# print satellite.getTrajectoryAsJavaArray ('test', 100)
try:
assert satellite.cronJob (), 'executing cronjob failed'
except AssertionError as e:
print 'Error: {0}'.format (e)
sys.exit (1)
sys.exit (0)
# vim: tw=0 ts=2
# EOF
|