forked from filipsPL/autowx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autowx2.py
executable file
·351 lines (269 loc) · 11.5 KB
/
autowx2.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# autowx2
#
import predict
import time
from datetime import datetime
from time import strftime
import subprocess
import os
from _crontab import *
from tendo import singleton # avoid two instancess
from autowx2_conf import * # configuration
me = singleton.SingleInstance()
# will sys.exit(-1) if other instance is running
# import pprint # to remove after debugging
# pp = pprint.PrettyPrinter(indent=4)
satellites = list(satellitesData)
qth = (stationLat, stationLon, stationAlt)
def mkdir_p(outdir):
''' bash "mkdir -p" analog'''
if not os.path.exists(outdir):
os.makedirs(outdir)
class bc():
"""Colors made easy"""
HEADER = '\033[95m'
CYAN = '\033[96m'
YELLOW = '\033[93m'
RED = '\033[91m'
OKBLUE = '\033[94m'
OKGREEN = '\033[97m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
GRAY = '\033[37m'
UNDERLINE = '\033[4m'
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def getTleData(satellite):
'''Open tle file and return a TLE record for the given sat'''
tlefile = open(tleFileName, 'r')
tledata = tlefile.readlines()
tlefile.close()
tleData = []
for i, line in enumerate(tledata):
if satellite in line:
for n in tledata[i:i + 3]:
tleData.append(n.strip('\r\n').rstrip())
break
if len(tleData) > 0:
return tleData
else:
return False
def parseCron(cron):
entry = CronTab(cron).next(default_utc=False)
return entry + time.time() # timestamp of the next occurence
def getFixedRecordingTime(satellite):
'''Reads from the config the fixed recording time'''
try:
fixedTime = satellitesData[satellite]["fixedTime"]
fixedDuration = satellitesData[satellite]["fixedDuration"]
return {"fixedTime": parseCron(fixedTime), "fixedDuration": fixedDuration}
except KeyError:
return False
def genPassTable(howmany=20):
'''generate a table with pass list, sorted'''
passTable = {}
for satellite in satellites:
tleData = getTleData(satellite)
priority = satellitesData[satellite]['priority']
if tleData: # if tle data was there in the file :: SATELLITES
czasStart = time.time()
p = predict.transits(tleData, qth, czasStart)
for i in range(1, howmany):
transit = p.next()
# transitEnd = transit.start + transit.duration() - skipLast
if not time.time() > transit.start + transit.duration() - skipLast - 1: # esttimate the end of the transit, minus last 10 seconds
if int(transit.peak()['elevation']) >= minElev:
passTable[transit.start] = [satellite, int(
transit.start + skipFirst), int(
transit.duration() - skipFirst - skipLast),
int(transit.peak()['elevation']), int(transit.peak()['azimuth']), priority]
# transit.start - unix timestamp
else: # fixed time recording
# cron = getFixedRecordingTime(satellite)["fixedTime"]
cron = satellitesData[satellite]['fixedTime']
duration = getFixedRecordingTime(satellite)["fixedDuration"]
delta = 0
for i in range(0, howmany):
entry = CronTab(
cron).next(now=time.time() + delta,
default_utc=False)
delta += entry
start = delta + time.time()
passTable[start] = [
satellite, int(start), int(duration), '0', '0', priority]
# Sort pass table
passTableSorted = []
for start in sorted(passTable):
passTableSorted.append(passTable[start])
# Clean the pass table according to the priority. If any pass overlaps,
# remove one with less priority (lower priority number).
passTableSortedPrioritized = passTableSorted[:]
passCount = len(passTableSorted)
for i in range(0, passCount - 1): # -1 or -2 :BUG?
satelliteI, startI, durationI, peakI, azimuthI, priorityI = passTableSorted[
i]
satelliteJ, startJ, durationJ, peakJ, azimuthJ, priorityJ = passTableSorted[
i + 1]
endTimeI = startI + durationI
if priorityI != priorityJ:
if (startJ + priorityTimeMargin < endTimeI):
# print "End pass:", satelliteI, t2human(endTimeI), "--- Start
# time:", satelliteJ, t2human(startJ)
if priorityJ < priorityI:
print " 1. discard %s, keep %s" % (satelliteI, satelliteJ)
passTableSortedPrioritized[i] = ''
elif priorityJ > priorityI:
print " 2. discard %s, keep %s" % (satelliteJ, satelliteI)
passTableSortedPrioritized[i + 1] = ''
# let's clean the table and remove empty (removed) records
# and remove the priority record, it will not be useful later -- x[:5]
passTableSortedPrioritized = [x[:5]
for x in passTableSortedPrioritized if x != '']
# pp.pprint(passTableSortedPrioritized)
return passTableSortedPrioritized
def t2human(timestamp):
'''converts unix timestamp to human readable format'''
return strftime('%Y-%m-%d %H:%M', time.localtime(timestamp))
def t2humanMS(seconds):
'''converts unix timestamp to human readable format MM:SS'''
mm = int(seconds / 60)
ss = seconds % 60
return "%02i:%02i" % (mm, ss)
def printPass(satellite, start, duration, peak, azimuth, freq, processWith):
return "● " + bc.OKGREEN + "%10s" % (satellite) + bc.ENDC + " :: " \
+ bc.OKGREEN + t2human(start) + bc.ENDC + " to " + bc.OKGREEN + t2human(start + int(duration)) + bc.ENDC \
+ ", dur: " + t2humanMS(duration) \
+ ", max el. " + str(int(peak)) + "°" + "; azimuth: " + str(int(azimuth)) + \
"° (" + azimuth2dir(azimuth) + ") f=" + str(
freq) + "Hz; Decoding: " + str(processWith)
def listNextPases(passTable, howmany):
i = 1
for satelitePass in passTable[0:howmany]:
satellite, start, duration, peak, azimuth = satelitePass
freq = satellitesData[satellite]['freq']
processWith = satellitesData[satellite]['processWith']
log(printPass(satellite, start, duration,
peak, azimuth, freq, processWith))
i += 1
def runForDuration(cmdline, duration):
cmdline = [str(x) for x in cmdline]
try:
child = subprocess.Popen(cmdline)
time.sleep(duration)
child.terminate()
except OSError as e:
print "OS Error during command: " + " ".join(cmdline)
print "OS Error: " + e.strerror
def justRun(cmdline):
'''Just run the command as long as necesary and return the output'''
cmdline = [str(x) for x in cmdline]
try:
child = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
result = child.communicate()[0]
return result
except OSError as e:
print "OS Error during command: " + " ".join(cmdline)
print "OS Error: " + e.strerror
def getDefaultDongleShift(dongleShift=dongleShift):
log("Reading the default dongle shift")
if os.path.exists(dongleShiftFile):
f = open(dongleShiftFile, "r")
newdongleShift = f.read().strip()
f.close()
if newdongleShift != '' and is_number(newdongleShift): # WARNING and newdongleShift is numeric:
dongleShift = str(float(newdongleShift))
log("Recently used dongle shift is: " + str(dongleShift) + " ppm")
else:
print "else"
log("Using the default dongle shift: " + str(dongleShift) + " ppm")
return dongleShift
def calibrate(dongleShift=dongleShift):
'''calculate the ppm for the device'''
cmdline = [ calibrationTool ]
newdongleShift = justRun(cmdline).strip()
if newdongleShift != '' and is_number(newdongleShift):
log("Recalculated dongle shift is: " + str(newdongleShift) + " ppm")
return str(float(newdongleShift))
else:
log("Using the good old dongle shift: " + str(dongleShift) + " ppm")
return dongleShift
def azimuth2dir(azimuth):
''' convert azimuth in degrees to wind rose directions (16 wings)'''
dirs = ["N↑", "NNE↑↗", "NE↗", "ENE→↗",
"E→", "ESE→↘", "SE↘", "SSE↓↘",
"S↓", "SSW↓↙", "SW↙", "WSW←↙",
"W←", "WNW←↖", "NW↖", "NNW↑↖", ]
part = int(float(azimuth) / 360 * 16)
return dirs[part]
def log(string, style=bc.CYAN):
print bc.BOLD + datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M'), bc.ENDC, style, str(string), bc.ENDC
# ------------------------------------------------------------------------------------------------------ #
if __name__ == "__main__":
dongleShift = getDefaultDongleShift()
while True:
# recalculate table of next passes
passTable = genPassTable()
log("Next five passes:")
listNextPases(passTable, 5)
# get the very next pass
satelitePass = passTable[0]
satellite, start, duration, peak, azimuth = satelitePass
freq = satellitesData[satellite]['freq']
processWith = satellitesData[satellite]['processWith']
fileNameCore = datetime.fromtimestamp(
start).strftime(
'%Y%m%d-%H%M') + "_" + satellite
log("Next pass:")
log(printPass(satellite, start, duration,
peak, azimuth, freq, processWith))
towait = int(start - time.time())
if towait <= 1 and duration > 0:
# here the recording happens
log("!! Recording " + printPass(satellite, start, duration,
peak, azimuth, freq, processWith), style=bc.WARNING)
processCmdline = [
processWith,
fileNameCore,
satellite,
start,
duration + towait,
peak,
azimuth,
freq]
justRun(processCmdline)
time.sleep(10.0)
else:
# recalculating waiting time
if towait > 300:
log("Recalibrating the dongle...")
dongleShift = calibrate() # replace the global value
towait = int(start - time.time())
if scriptToRunInFreeTime:
if towait >= 60: # if we have more than five minutes spare time, let's do something useful
log("We have still %ss free time to the next pass. Let's do something useful!" %
(t2humanMS(towait - 1)))
log("Running: %s for %ss" %
(scriptToRunInFreeTime, t2humanMS(towait - 1)))
runForDuration(
[scriptToRunInFreeTime,
towait - 1,
dongleShift],
towait - 1)
# scrript with runt ime and dongle shift as
# arguments
else:
log("Sleeping for: " + t2humanMS(towait - 1) + "s")
time.sleep(towait - 1)
else:
towait = int(start - time.time())
log("Sleeping for: " + t2humanMS(towait - 1) + "s")
time.sleep(towait - 1)