forked from rtmrtmrtmrtm/weakmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jt65mon.py
executable file
·360 lines (305 loc) · 10.7 KB
/
jt65mon.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
352
353
354
355
356
357
358
359
360
#!/usr/local/bin/python
#
# receive JT65.
#
# switches among bands if weakcat.py understands the radio.
# reports to pskreporter.info if mycall/mygrid defined in weak.ini.
#
# Robert Morris, AB1HL
#
import jt65
import sys
import os
import time
import numpy
import threading
import re
import random
import copy
import weakcat
import weakaudio
import weakutil
import weakargs
import pskreport
# look only at these bands.
plausible = [ "40", "30", "20", "17", "15" ]
b2f = { "160" : 1.838, "80" : 3.576, "60" : 5.357, "40" : 7.076,
"30" : 10.138, "20" : 14.076,
"17" : 18.102, "15" : 21.076, "12" : 24.917,
"10" : 28.076, "6" : 50.276 }
def load_prefixes():
d = { }
f = open("jt65prefixes.dat")
for ln in f:
ln = re.sub(r'\t', ' ', ln)
ln = re.sub(r' *', ' ', ln)
ln.strip()
ln = re.sub(r' *\(.*\) *', '', ln)
ln.strip()
m = re.search(r'^([A-Z0-9]+) +(.*)', ln)
if m != None:
d[m.group(1)] = m.group(2)
f.close()
return d
def look_prefix(call, d):
if len(call) == 5 and call[0:3] == "KG4":
# KG4xx is Guantanamo, KG4x and KG4xxx are not.
return "Guantanamo Bay"
while len(call) > 0:
if call in d:
return d[call]
call = call[0:-1]
return None
# weighted choice (to pick bands).
# a[i] = [ value, weight ]
def wchoice(a, n):
total = 0.0
for e in a:
total += e[1]
ret = [ ]
while len(ret) < n:
x = random.random() * total
for ai in range(0, len(a)):
e = a[ai]
if x <= e[1]:
ret.append(e[0])
total -= e[1]
a = a[0:ai] + a[ai+1:]
break
x -= e[1]
return ret
def wchoice_test():
a = [ [ "a", .1 ], [ "b", .1 ], [ "c", .4 ], [ "d", .3 ], [ "e", .1 ] ]
counts = { }
for iter in range(0, 500):
x = wchoice(a, 2)
for e in x:
counts[e] = counts.get(e, 0) + 1
print(counts)
# listen for CQ, answer.
class JT65Mon:
def __init__(self, desc1, desc2, cat, oneband):
self.mycall = weakutil.cfg("jt65mon", "mycall")
self.mygrid = weakutil.cfg("jt65mon", "mygrid")
self.oneband = oneband
self.verbose = False
self.rate = 11025
self.allname = "jt65-all.txt"
self.bandname = "jt65-band.txt"
self.jtname = "jt65"
self.incards = [ ]
self.incards.append(desc1)
if desc2 != None:
self.incards.append(desc2)
if cat != None:
self.cat = weakcat.open(cat)
self.cat.sync()
self.cat.set_usb_data()
else:
self.cat = None
# for each band, count of received signals last time we
# looked at it, to guess most profitable band.
self.bandinfo = { }
self.prefixes = load_prefixes()
if self.mycall != None and self.mygrid != None:
# talk to pskreporter.
print("reporting to pskreporter as %s at %s" % (self.mycall, self.mygrid))
self.pskr = pskreport.T(self.mycall, self.mygrid, "weakmon 0.2", False)
else:
print("not reporting to pskreporter since call/grid not in weak.cfg")
self.pskr = None
# read latest msgs from all cards.
def readall(self, now, bands):
minute = self.r[0].minute(now)
# for each card, new msgs
all = [ ] # all messages, with duplicates
d = { } # for each text, [ card_index, msg ]
for ci in range(0, len(self.r)):
bandcount = 0 # all msgs
bandcount1 = 0 # msgs with lowist reed-solomon error counts
msgs = self.r[ci].get_msgs()
# each msg is a jt65.Decode
for m in msgs:
m.card = ci
if m.minute == minute:
bandcount += 1
if m.nerrs < 25:
bandcount1 += 1
all.append(m)
z = d.get(m.msg, [])
z.append([ ci, m ])
d[m.msg] = z
else:
print("LATE: %s %.1f %s" % (self.r[ci].ts(m.decode_time), m.hz(), m.msg))
x = self.bandinfo.get(bands[ci], 0)
self.bandinfo[bands[ci]] = 0.5 * x + 0.5 * bandcount1
f = open(self.bandname, "a")
f.write("%s %s %d %2d %2d\n" % (self.r[ci].ts(now),
bands[ci],
ci,
bandcount,
bandcount1))
f.close()
# append each msg to jt65-all.txt.
for txt in d:
band = None
got = ""
hz = None
snr = None
nerrs = [ -1, -1 ] # for each antenna
for [ ci, m ] in d[txt]:
band = bands[ci]
if not (str(ci) in got):
got += str(ci)
hz = m.hz()
nerrs[ci] = m.nerrs
if snr == None or m.snr > snr:
snr = m.snr
info = "%s %s rcv %2s %2d %2d %3.0f %6.1f %s" % (self.r[ci].ts(m.decode_time),
band,
got,
nerrs[0],
nerrs[1],
snr,
m.hz(),
m.msg)
print(info)
f = open(self.allname, "a")
f.write(info + "\n")
f.close()
# send msgs that don't have too many errors to pskreporter.
# the 30 here suppresses some good CQ receptions, but
# perhaps better that than reporting erroneous decodes.
if (nerrs[0] >= 0 and nerrs[0] < 30) or (nerrs[1] >= 0 and nerrs[1] < 30):
txt = m.msg
hz = m.hz() + int(b2f[band] * 1000000.0)
tm = m.decode_time
self.maybe_pskr(txt, hz, tm)
return all
# report to pskreporter if we can figure out the originating
# call and grid.
def maybe_pskr(self, txt, hz, tm):
if self.pskr == None:
return
txt = txt.strip()
txt = re.sub(r' *', ' ', txt)
txt = re.sub(r'CQ DX ', 'CQ ', txt)
txt = re.sub(r'CQDX ', 'CQ ', txt)
mm = re.search(r'^CQ ([0-9A-Z/]+) ([A-R][A-R][0-9][0-9])$', txt)
if mm != None and self.iscall(mm.group(1)):
self.pskr.got(mm.group(1), hz, "JT65", mm.group(2), tm)
return
mm = re.search(r'^([0-9A-Z/]+) ([0-9A-Z/]+) ([A-R][A-R][0-9][0-9])$', txt)
if mm != None and self.iscall(mm.group(1)) and self.iscall(mm.group(2)):
self.pskr.got(mm.group(2), hz, "JT65", mm.group(3), tm)
return
# does call look syntactically correct?
def iscall(self, call):
if len(call) < 3:
return False
if re.search(r'[0-9][A-Z]', call) == None:
# no digit
return False
return True
# return two bands to listen for CQs on.
# specialized to two receivers. doesn't work for
# just one receiver.
def rankbands(self):
global plausible
if len(plausible) == 1:
return [ plausible[0] ]
# are we missing bandinfo for any bands?
missing = [ ]
for b in plausible:
if self.bandinfo.get(b) == None:
missing.append(b)
# most profitable bands, best first.
best = sorted(plausible, key = lambda b : -self.bandinfo.get(b, -1))
# always explore missing bands first.
if len(missing) >= len(self.r):
return missing[0:len(self.r)]
if len(missing) > 0:
return [ best[0], missing[0] ]
ret = [ ]
if len(self.r) > 1:
# two receivers.
# always look at best band.
ret.append(best[0])
best = best[1:]
if len(best) == 0:
pass
elif random.random() < 0.3 or self.bandinfo[best[0]] <= 0.1:
band = random.choice(best)
ret.append(band)
else:
wa = [ [ b, self.bandinfo[b] ] for b in best ]
band = wchoice(wa, 1)[0]
ret.append(band)
return ret
def one(self):
if self.oneband != None:
bands = [ self.oneband ] * len(self.r)
else:
# choose a band per receiver.
bands = self.rankbands()
bands = bands[0:len(self.r)]
while len(bands) < len(self.r):
bands.append(bands[0])
# highest frequency on main receiver, so that low-pass ATU
# will let us use main antenna on sub-receiver too.
bands = sorted(bands, key = lambda b : int(b))
if self.verbose:
sys.stdout.write("band ")
for b in bands:
sys.stdout.write("%s " % (b))
sys.stdout.write("; ")
for b in self.bandinfo:
sys.stdout.write("%s %.1f, " % (b, self.bandinfo[b]))
sys.stdout.write("\n")
sys.stdout.flush()
if self.cat != None:
for i in range(0, len(self.r)):
self.cat.setf(i, int(b2f[bands[i]] * 1000000.0))
time.sleep(55)
# wait for the 59th second.
while True:
now = time.time()
second = self.r[0].second(now)
if second >= 59.5:
break
time.sleep(0.2)
self.readall(now, bands)
def go(self):
# receive card(s)
self.r = [ ]
self.rth = [ ]
for c in self.incards:
r = jt65.JT65()
self.r.append(r)
r.cardrate = self.rate
r.opencard(c)
th = threading.Thread(target=lambda : r.gocard())
th.daemon = True
th.start()
self.rth.append(th)
while True:
self.one()
def close(self):
for r in self.r:
r.close()
for th in self.rth:
th.join()
def main():
parser = weakargs.stdparse('Decode JT65A.')
parser.add_argument("-band")
args = weakargs.parse_args(parser)
if args.card == None:
parser.error("jt65mon requires -card")
if args.cat == None and args.band == None:
parser.error("jt65mon needs either -cat or -band")
jt65mon = JT65Mon(args.card, args.card2, args.cat, args.band)
jt65mon.verbose = args.v
jt65mon.go()
jt65mon.close()
sys.exit(0)
main()