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
|
#!/usr/bin/env python
import ephem
import time
import os
from MmdDb import Db
def createJavaArray (point_list, name):
astring = '''var {0} = new Array (\n'''.format (name)
astring = '{0}\tnew Array ('.format (astring)
last = 'west'
for point in point_list:
if point[0][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, point[0], point[1])
if astring[-1] == ',':
astring = astring[:-1]
astring = '{0}\n\t\t)\n\t);'.format (astring)
return astring
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):
self.name = 'RADIOSCAF-B/ARISSAT-1'
self.tle_filename = '/usr/local/mmd/tles/current'
self.db = Db ()
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)
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 computeTrajectory (self):
'''
computes 5 hours of future trajectory data, starting at the
latest timestamp found in data base
'''
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
if __name__ == "__main__":
satellite = Satellite ()
minutes = 30
try:
assert satellite._loadTLE (), 'loading TLE failed'
print satellite._tle
assert satellite.computeTrajectory (), 'computing trajectory failed'
trajectory = satellite.getTrajectoryAsJavaArray ('curves', minutes)
assert trajectory, 'got not trajectory'
print trajectory
t, lon, lat = satellite.getCurrentSSP ()
assert t, 'loading current SSP failed'
print lon, lat, time.strftime ('%c', time.gmtime (t))
except AssertionError as e:
print 'Error: {0}'.format (e)
# vim: tw=0 ts=2
# EOF
|