forked from rtmrtmrtmrtm/weakmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweakdriver.py
1353 lines (1181 loc) · 45.3 KB
/
weakdriver.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/python
#
# code common between ft8i.py and jt65i.py
#
# Robert Morris, AB1HL
#
import sys
import os
import time
import calendar
import numpy
import threading
import re
import random
import copy
import pyaudio # just for output, not input
import weakcat
import weakaudio
import pskreport
import weakutil
import weakargs
import tty
import fcntl
import termios
import struct
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)
assert len(x) == 2
for e in x:
counts[e] = counts.get(e, 0) + 1
print(counts)
# load lotwreport.adi from LOTW, so we know what's been confirmed
# and thus who it makes sense to contact. returns a dictionary
# with band-call (and True for value).
#
# <APP_LoTW_OWNCALL:5>AB1HL
# <STATION_CALLSIGN:5>AB1HL
# <CALL:6>CE3CBM
# <BAND:3>20M
# <FREQ:8>14.07600
# <MODE:4>FT8
# <APP_LoTW_MODEGROUP:4>DATA
# <QSO_DATE:8>20160903
# <TIME_ON:6>234900
# <QSL_RCVD:1>Y
# <QSLRDATE:8>20160926
# <eor>
#
def read_lotw():
try:
f = open("lotwreport.adi", "r")
except:
return None
d = { }
call = None
band = None
mode = None
qsl_rcvd = None
while True:
line = f.readline()
if line == '':
break
line = line.upper()
line = line.strip()
m = re.match(r'^<([A-Z_]+):*[0-9]*>(.*)$', line)
if m != None:
k = m.group(1)
v = m.group(2)
if k == "CALL":
call = v
if k == "BAND":
band = v
band = re.sub(r'M$', '', band)
if k == "MODE":
mode = v
if k == "QSL_RCVD":
qsl_rcvd = v
if line == "<EOR>":
if call != None and band != None and mode[0:4] == "FT8" and qsl_rcvd == "Y":
#print "+++ %s %s %s %s" % (call, band, mode, qsl_rcvd)
d[band + "-" + call] = True
#else:
# print "--- %s %s %s %s" % (call, band, mode, qsl_rcvd)
call = None
band = None
mode = None
qsl_rcvd = None
f.close()
return d
# returns [ rows, columns ] e.g. [ 24, 80 ]
# works on FreeBSD, Linux, and OSX.
def terminal_size(fd):
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return [ int(cr[0]), int(cr[1]) ]
class Driver:
def __init__(self, progname, recv_class, send_class,
frequencies, auto_bands,
logfileprefix, logmode,
outcard, cards, cats, oneband):
self.recv_class = recv_class # e.g. jt65.JT65
self.send_class = send_class # e.g. jt65.JT65Send
self.frequencies = frequencies # e.g. { "160" : 1.8xx, ... }
self.auto_bands = auto_bands # e.g. [ "160", "80", ... ]
self.logmode = logmode # "FT" or "JT"
self.progname = progname
self.outcard = outcard
self.oneband = oneband
self.mycall = weakutil.cfg(progname, "mycall")
self.mygrid = weakutil.cfg(progname, "mygrid")
self.logname = logfileprefix + "-log.txt"
self.allname = logfileprefix + "-all.txt"
self.cqname = logfileprefix + "-cq.txt"
self.card_args = cards
self.cat_args = cats
self.please_quit = False
# self.bandinfo[band] is an array of [ minute, count ].
# used to pick which band to listen to.
self.bandinfo = { }
# for each minute, for each card index, what band?
self.minute_bands = { }
self.prefixes = load_prefixes()
# contacted call signs, grid counts, entity counts.
# keys are band-call, band-grid, band-entity.
# initialized from ft8-log.txt
self.band_call = { }
self.band_entity = { }
self.all_entity = { }
self.band_grid = { }
self.wrote_cq = { }
self.read_log()
if False and 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.3", False)
else:
#print("not reporting to pskreporter since call/grid not in weak.cfg")
self.pskr = None
# all messages decoded.
self.log = [ ] # log of all msgs received
self.log_already = { } # to suppress duplicates in dlog, band-minute-txt
# for display()
self.dthis = [ ] # log of this conversation
self.dhiscall = None # who we're trying to respond to
self.dsending = False # TX vs RX
self.dwanted = None # set to a CQ Decode if user wants to respond
self.dmessages = [ ] # messages to display to the user for a few seconds
def decode_loop(self):
while True:
self.decode_new()
time.sleep(0.1)
# read latest msgs from all cards,
# append to self.log[].
def decode_new(self):
minute = self.minute(time.time())
oindex = len(self.log)
# for each card, new msgs
for ci in range(0, len(self.r)):
msgs = self.r[ci].get_msgs()
# each msg is a ft8.Decode.
for dec in msgs:
dec.card = ci
dec.band = self.get_band(dec.minute, ci)
f = open("xlog", "a")
f.write("%s %d %.2f -- %d %s %s\n" % (self.ts(time.time()), self.minute(), self.second(), dec.minute, dec.band, dec.msg))
f.close()
if self.r[ci].enabled:
if dec.band == None:
# ???
continue
ak = dec.band + "-" + str(dec.minute) + "-" + dec.msg
if not ak in self.log_already:
self.log_already[ak] = True
self.log.append(dec)
self.record_stat(dec)
# append each msg to ft8-all.txt.
# incorrectly omits some lines if a call transmits the same message
# on multiple bands simultaneously, e.g. W5KDJ.
for dec in self.log[oindex:]:
f = open(self.allname, "a")
ci = dec.card
f.write("%s %s rcv %d %2d %3.0f %6.1f %s\n" % (self.ts(self.minute2time(dec.minute)),
dec.band,
dec.card,
0, # dec.nerrs,
dec.snr,
dec.hz(),
dec.msg))
f.close()
self.write_cq(dec.msg, dec.band, dec.snr, self.r[ci].ts(dec.decode_time))
# 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 dec.nerrs >= 0 and dec.nerrs < 30:
if True:
txt = dec.msg
hz = dec.hz() + int(self.frequencies[dec.band] * 1000000.0)
tm = dec.decode_time
self.maybe_pskr(txt, hz, tm)
# if we've received a newish CQ, record it in ft8-cq.txt.
def write_cq(self, txt, band, snr, ts):
otxt = txt
txt = txt.strip()
txt = re.sub(r' *', ' ', txt)
txt = re.sub(r'CQ DX ', 'CQ ', txt)
txt = re.sub(r'CQDX ', 'CQ ', txt)
txt = re.sub(r'CQ NA ', 'CQ ', txt)
txt = re.sub(r'CQ USA ', 'CQ ', txt)
txt = re.sub(r'/QRP ', ' ', txt)
call = None
grid = None
mm = re.search(r'^CQ ([0-9A-Z/]+) ([A-R][A-R][0-9][0-9])$', txt)
if mm != None:
call = mm.group(1)
grid = mm.group(2)
else:
mm = re.search(r'^CQ ([0-9A-Z/]+) *([0-9A-Z/]*)', txt)
if mm != None:
if len(mm.group(1)) >= len(mm.group(2)):
call = mm.group(1)
else:
call = mm.group(2)
if call == None or not self.iscall(call):
return
k = band + "-" + call
now = time.time()
if k in self.wrote_cq and now - self.wrote_cq[k] < 6*3600:
return
self.wrote_cq[k] = now
ng = 0
if grid != None:
ng = self.band_grid.get(self.gridkey(grid, band), 0)
entity = look_prefix(call, self.prefixes)
xentity = None
ne = 0
ae = 0
if entity != None:
ne = self.band_entity.get(self.entitykey(entity, band), 0)
ae = self.all_entity.get(entity, 0)
xentity = entity.replace(" ", "-")
call_any = "N"
for b in list(self.frequencies.keys()):
if (b + "-" + call) in self.band_call:
call_any = "Y"
if (band + "-" + call) in self.band_call:
call_this = "Y"
else:
call_this = "N"
f = open(self.cqname, "a")
f.write("%s %s %4d %4d %3d %s%s %s %s\n" % (ts,
band,
ae,
ne,
ng,
call_any,
call_this,
xentity,
otxt))
f.close()
# report to pskreporter if we can figure out the originating
# call and grid.
def maybe_pskr(self, txt, hz, tm):
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)):
if self.pskr != None:
self.pskr.got(mm.group(1), hz, "FT8", 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)):
if self.pskr != None:
self.pskr.got(mm.group(2), hz, "FT8", 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
if self.sender.packcall(call) == -1:
# don't know how to encode this call, e.g. R870T
return False
return True
# wait until the very end of the cycle indicated by minute.
def wait_end(self, minute):
while True:
now = time.time()
if self.minute(now) > minute:
# oops
return
if self.minute(now) == minute:
if self.seconds_left(now) <= 0.3:
return
time.sleep(0.2)
# is txt a CQ?
# if yes, return [ call, grid ]
# grid may be ""
# otherwise return None
def is_cq(self, txt):
txt = txt.strip()
txt = re.sub(r'CQ DX ', 'CQDX ', txt)
txt = re.sub(r'CQDXDX ', 'CQDX ', txt)
txt = re.sub(r'CQ NA ', 'CQ ', txt)
txt = re.sub(r'CQ USA ', 'CQ ', txt)
txt = re.sub(r'/QRP ', ' ', txt)
txt = re.sub(r' DXDX$', ' DX', txt)
txt = re.sub(r' DX DX$', ' DX', txt)
# change CQ UR4UT DX to CQDX UR4UT
mm = re.search(r'^CQ +([A-Z0-9]+) +DX$', txt)
if mm != None:
txt = "CQDX %s" % (mm.group(1))
mm = re.search(r'^CQ +([A-Z0-9]+) *([A-Z0-9]*)$', txt)
if mm != None and self.iscall(mm.group(1)):
xc = mm.group(1) # his call
xg = mm.group(2) # his grid, maybe ""
return [ xc, xg ]
mm = re.search(r'^CQDX +([A-Z0-9]+) *([A-Z0-9]*)$', txt)
if mm != None and self.iscall(mm.group(1)):
xc = mm.group(1) # his call
xg = mm.group(2) # his grid, maybe ""
return [ xc, xg ]
return None
# is the call in the our log, on any band?
def call_in_log(self, call):
for band in list(self.frequencies.keys()):
k = self.callkey(call, band)
if k in self.band_call:
return True
return False
# is call+band in our log?
def band_call_in_log(self, call, band):
k = self.callkey(call, band)
return k in self.band_call
# set up frequencies for a QSO minute during which we transmit.
def qso_sending(self, band):
minute = self.minute(time.time())
for i in range(0, len(self.r)):
self.set_band(minute, i, band)
if i == 0:
self.r[i].enabled = True
thisband = band
else:
self.r[i].enabled = False
# not band, to avoid receiver overload
if band == "10":
thisband = "40"
else:
thisband = "10"
if self.cats[i] != None:
self.cats[i].setf(i, int(self.frequencies[thisband] * 1000000.0))
if self.cats[0] != None:
self.cats[0].sync()
# set up frequencies for a QSO minute during which we receive.
def qso_receiving(self, band):
minute = self.minute(time.time())
for i in range(0, len(self.r)):
self.set_band(minute, i, band)
self.r[i].enabled = (i == 0)
if self.cats[i] != None:
self.cats[i].setf(i, int(self.frequencies[band] * 1000000.0))
if self.cats[0] != None:
self.cats[0].sync()
# what band was card ci tuned to during a particular minute?
# returns a string, e.g. "20", or None.
# returns info for most recent minute for which
# any bands were set.
def get_band(self, minute, ci):
if minute in self.minute_bands and ci in self.minute_bands[minute]:
return self.minute_bands[minute][ci]
if len(self.minute_bands) == 0:
return None
for m in range(minute, minute-60, -1):
if m in self.minute_bands:
return self.minute_bands[m].get(ci, None)
return None
def set_band(self, minute, ci, band):
if not minute in self.minute_bands:
self.minute_bands[minute] = { }
self.minute_bands[minute][ci] = band
# incorporate a reception into per-band counts.
def record_stat(self, dec):
#if dec.nerrs >= 25:
# return
# self.bandinfo[band] is an array of [ minute, count ]
if not dec.band in self.bandinfo:
self.bandinfo[dec.band] = [ ]
if len(self.bandinfo[dec.band]) == 0 or self.bandinfo[dec.band][-1][0] != dec.minute:
self.bandinfo[dec.band].append( [ dec.minute, 0 ] )
self.bandinfo[dec.band][-1][1] += 1
# return a list of bands to listen to.
def rankbands(self):
# for each band, count of recent receptions.
counts = { }
for band in self.bandinfo:
if len(self.bandinfo[band]) > 1:
counts[band] = (self.bandinfo[band][-1][1] + self.bandinfo[band][-2][1]) / 2.0
else:
counts[band] = self.bandinfo[band][-1][1]
# are we missing bandinfo stats for any bands?
missing = [ ]
for band in self.auto_bands:
if counts.get(band) == None:
missing.append(band)
# most profitable bands, highest count first.
best = sorted(self.auto_bands, key = lambda band : -counts.get(band, -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:len(self.r)-len(missing)] + missing
ret = [ ]
# choose weighted by activity, but
# give a little weight even to dead bands.
c = copy.copy(counts)
for band in c:
if c[band] > 5.0:
# no band too high a weight
c[band] = 5.0
wsum = numpy.sum([ c[band] for band in c ])
wsum = max(wsum, 1.0)
minweight = wsum * 0.25 / len(c) # spend about 1/4 of the time on inactive bands
wa = [ [ band, max(c[band], minweight*self.idleweight(band)) ] for band in c ]
ret = wchoice(wa, len(self.r))
return ret
def idleweight(self, band):
return 1.0
def choose_bands(self, minute):
if self.oneband != None:
bands = [ self.oneband ]
else:
bands = self.rankbands()
bands = bands[0:len(self.r)]
while len(bands) < len(self.r):
bands.append(bands[0])
for band in bands:
# trick rankbands() into counting each
# band as missing just once.
if not band in self.bandinfo:
self.bandinfo[band] = [ [ 0, 0 ] ]
for ci in range(0, len(self.r)):
self.set_band(minute, ci, bands[ci])
for ci in range(0, len(self.r)):
band = bands[ci]
if self.cats[ci] != None:
self.cats[ci].setf(ci, int(self.frequencies[band] * 1000000.0))
self.cats[ci].sync()
# if the user has asked to respond to a CQ, do it.
def one(self):
self.dhiscall = None
for i in range(0, len(self.r)):
self.r[i].enabled = True
do_switch = False
# if the user doesn't select a CQ, wait for last
# second of the cycle, then break to switch bands.
# if the user selects a CQ, even after cycle begins,
# break and answer that CQ.
while True:
now = time.time()
if self.seconds_left(now) <= 0.3:
do_switch = True
break
if self.dwanted != None:
break
time.sleep(0.2)
if do_switch:
# user did not ask to respond to a CQ.
# switch radio to new band.
while self.second(time.time()) > 0.5:
time.sleep(0.2)
self.choose_bands(self.minute(time.time()))
return
cq = self.dwanted
self.dwanted = None
now = time.time()
minute = self.minute(now)
second = self.second(now)
if cq.minute != minute and not (cq.minute == minute - 1 and second < 5):
self.show_message("Too late to reply to %s." % (cq.hiscall))
return
self.show_message("Replying to %s." % (cq.hiscall))
entity = look_prefix(cq.hiscall, self.prefixes)
# reply off-frequency in case others reply too.
myhz = cq.hz() + (random.random() * 300) - 150
if myhz < 211:
myhz = 211
if myhz > 2200:
myhz = 2200
self.dhiscall = cq.hiscall
self.dthis = [ ]
self.dthis.append("<<< %s" % (cq.msg))
# listen for his signal report.
# if we hear nothing from him (including no msg to other call),
# then sleep a minute and listen again. this situation comes
# up fairly frequently.
hissig = None
olen = len(self.log)
why = None
for tries in range(0, 2):
if tries > 0:
if why == None:
self.show_message("No reply from %s, trying once more." % (cq.hiscall))
else:
self.show_message(why)
why = None
self.qso_sending(cq.band) # disable receivers
self.send(cq.band, myhz, "%s %s %s" % (cq.hiscall,
self.mycall,
self.mygrid))
minute = self.minute(time.time())
self.wait_end(minute)
minute += 1
self.qso_receiving(cq.band) # enable receivers
assert (minute == cq.minute + 2 or minute == cq.minute + 4)
assert self.minute() <= minute
# wait for AB1HL HISCALL -YY until just after the end of his cycle.
while self.minute() <= minute or (self.minute() == minute+1 and self.second() < 0.6):
if self.dwanted != None:
# user wants to switch to another CQ.
self.show_message("Terminating contact with %s." % (self.dhiscall))
return
while olen < len(self.log):
m = self.log[olen]
olen += 1
if m.minute != minute:
continue
txt = m.msg.strip()
txt = re.sub(r' *', ' ', txt)
txt = re.sub(r'CQ DX ', 'CQ ', txt)
txt = re.sub(r'CQDX ', 'CQ ', txt)
txt = re.sub(r'CQ NA ', 'CQ ', txt)
txt = re.sub(r'CQ USA ', 'CQ ', txt)
txt = re.sub(r'/QRP ', ' ', txt)
a = txt.split(' ')
if len(a) == 3 and a[0] == self.mycall and a[1] == cq.hiscall:
# AB1HL HISCALL -YY
if hissig == None:
self.dthis.append("<<< %s" % (m.msg))
self.show_message("%s replied." % (cq.hiscall))
hissig = a[2]
elif len(a) == 2 and a[0] == self.mycall and ("-" in a[1]):
# AB1HL -YY
if hissig == None:
self.dthis.append("<<< %s" % (m.msg))
self.show_message("%s replied." % (cq.hiscall))
hissig = a[1]
if hissig == None and len(a) >= 2 and a[0] != self.mycall and a[1] == cq.hiscall:
# he responded to someone else, or he CQ'd again.
if a[0] == "CQ":
# he CQd again.
why = "%s CQ'd again." % (cq.hiscall)
else:
self.show_message("%s responded to %s." % (cq.hiscall, a[0]))
return
time.sleep(0.1)
if hissig != None:
break
if hissig == None:
if why == None:
self.show_message("%s did not respond." % (cq.hiscall))
else:
self.show_message(why)
time.sleep(2)
return
snr = int(cq.snr)
if snr > -1:
snr = -1
for tries in range(0, 3):
self.qso_sending(cq.band) # disable receivers
self.send(cq.band, myhz, "%s %s R-%02d" % (cq.hiscall, self.mycall, -snr))
minute = self.minute(time.time())
self.wait_end(minute)
minute += 1
self.qso_receiving(cq.band) # enable receivers
# wait for AB1HL HISCALL RRR or 73.
# or AB1HL RRR 73
# but try once more if we don't see RRR/73,
# e.g. if he (re)sends AB1HL XXX -04.
found = False
while self.minute() <= minute or (self.minute() == minute+1 and self.second() < 0.5):
if self.dwanted != None:
# user wants to switch to another CQ.
self.show_message("Terminating contact with %s." % (self.dhiscall))
return
got = None
while olen < len(self.log):
m = self.log[olen]
olen += 1
if m.minute != minute:
continue
txt = m.msg.strip()
txt = re.sub(r' *', ' ', txt)
has73 = ("73" in txt or "RRR" in txt or "TU" in txt)
if has73 and (self.mycall in txt):
got = m.msg
if has73 and (abs(m.hz() - myhz) < 10 or abs(m.hz() - cq.hz()) < 10):
got = m.msg
if got != None:
self.dthis.append("<<< %s" % (got))
found = True
break
time.sleep(0.2)
if found:
break
self.show_message("Logging %s on %s meters." % (cq.hiscall, cq.band))
self.write_log(cq.band, cq.hz(), cq.hiscall, hissig, cq.hisgrid, snr)
self.qso_sending(cq.band) # disable receivers
self.send(cq.band, myhz, "%s %s 73" % (cq.hiscall, self.mycall))
minute = self.minute(time.time())
self.wait_end(minute)
self.qso_receiving(cq.band) # enable receivers
return
# generate audio signal for sending.
def audio(self, hz, msg):
twelve = self.sender.pack(msg)
x1 = self.sender.tones(twelve, hz, self.snd_rate)
# keep levels well below max sound card output.
# but RigBlaster needs this to be at least 0.3 to
# trigger VOX, maybe 0.4.
# actually what the RigBlaster needs depends on audio
# frequency; to work down to 200 hz you need 0.9 here.
x1 *= 0.3
# sound card expects 16-bit signed ints.
x1 *= 32767.0
x1 = x1.astype(numpy.int16)
return x1
# write audio samples to sound card for output.
def tocard(self, x1):
# write in smallish chunks so control-C works.
blocksize = self.snd_rate
i = 0
while i < len(x1):
x2 = x1[i:i+blocksize]
x2 = x2.tostring()
self.snd.write(x2)
i += blocksize
# msg is e.g. "CQ AB1HL FN42".
# send out the sound card.
# returns after 49 seconds (or just the time to send
# the samples, not the whole 60 seconds).
def send(self, band, hz, msg):
self.dthis.append(">>> %s" % (msg))
# this stuff takes a noticeable amount of CPU, so do it
# before waiting until one second after the minute.
x1 = self.audio(hz, msg)
# wait for 0.5 second after the minute
while True:
s = self.second(time.time())
if s >= 0.5 and s < 10:
break
lf = self.seconds_left(time.time())
if lf > 1 and s > 10:
self.show_message("too late %.2f" % (s))
return
time.sleep(0.1)
f = open(self.allname, "a")
f.write("%s %s snd %6.1f %s\n" % (self.ts(time.time()), band, hz, msg))
f.close()
# if we're starting late, drop initial samples.
dt = self.second(time.time()) - 0.5
ds = int(dt * self.snd_rate)
if ds > 0:
x1 = x1[ds:]
self.dsending = True
self.tocard(x1)
self.dsending = False
def write_log(self, band, hz, call, sig, grid, snr):
key = self.callkey(call, band)
self.band_call[key] = time.time()
entity = look_prefix(call, self.prefixes)
if entity != None:
ek = self.entitykey(entity, band)
self.band_entity[ek] = self.band_entity.get(ek, 0) + 1
self.all_entity[entity] = self.all_entity.get(entity, 0) + 1
if grid != "":
gk = self.gridkey(grid, band)
self.band_grid[gk] = self.band_grid.get(gk, 0) + 1
freq = self.frequencies[band]
ts = self.ts(time.time())
ts = re.sub(r':[0-9][0-9]$', '', ts) # delete seconds
f = open(self.logname, "a")
f.write("%-9s %s %6.3f 599 FT X %s, %s, %s, %.0f, %s\n" % (call,
ts,
freq,
entity,
grid,
sig,
snr,
self.logmode))
f.close()
# turn a frequency in MHz into a band like "40".
def f2b(self, freq):
try:
mhz = int(float(freq))
except:
return None
if mhz == 1:
return "160"
if mhz == 3:
return "80"
if mhz == 5:
return "60"
if mhz == 7:
return "40"
if mhz == 10:
return "30"
if mhz == 14:
return "20"
if mhz == 18:
return "17"
if mhz == 21:
return "15"
if mhz == 24:
return "12"
if mhz == 28:
return "10"
if mhz == 50:
return "6"
sys.stderr.write("ft8i f2b(%s) no band\n" % (freq))
return None
# derive key for self.band_call[].
# band-call or just call.
def callkey(self, call, band):
if band != None:
return band + "-" + call
else:
return call
def gridkey(self, grid, band):
if band != None:
return band + "-" + grid
else:
return grid
def entitykey(self, entity, band):
if band != None:
return band + "-" + entity
else:
return entity
# populate self.band_call[] &c from log file.
# only increment grid/entity counts for
# lotw-confirmed contacts.
def read_log(self):
# either None or dict of band-call
lotw = None
#lotw = read_lotw()
#if lotw == None:
# print("could not read lotwreport.adi")
inlotw = 0
notinlotw = 0
try:
f = open(self.logname, "r")
except:
f = None
if f != None:
while True:
l = f.readline()
if l == "":
break
l = re.sub(r' *', ' ', l)
a = l.split(" ")
band = self.f2b(a[3])
# parse the time, 15/02/17 17:51
da = a[1].split("/")
ta = a[2].split(":")
if len(da) == 3 and len(ta) >= 2:
tm = time.struct_time([
int(da[2]) + 2000, # year
int(da[1]), # mon
int(da[0]), # mday
int(ta[0]), # hour
int(ta[1]), # min
0, # sec
0, # wday
0, # yday
-1]) # isdst
secs = calendar.timegm(tm)
else:
secs = time.time()
key = self.callkey(a[0], band)
self.band_call[key] = secs
if lotw == None or key in lotw:
inlotw += 1
entity = look_prefix(a[0], self.prefixes)
if entity != None:
ek = self.entitykey(entity, band)
self.band_entity[ek] = self.band_entity.get(ek, 0) + 1
self.all_entity[entity] = self.all_entity.get(entity, 0) + 1
m = re.search(r', ([A-Z][A-Z][0-9][0-9]), ', l)
if m != None:
grid = m.group(1)
gk = self.gridkey(grid, band)
self.band_grid[gk] = self.band_grid.get(gk, 0) + 1
else:
notinlotw += 1
sys.stdout.flush()
f.close()
if lotw != None:
print("%d in lotw, %d not in lotw" % (inlotw, notinlotw))
# open sound card, create ft8 instance.
def soundsetup(self):
if self.outcard != None:
# do this early because the Mac sound system gets
# upset if threads are already running.
# we want 12000, but some cards don't support it.
outcard = int(self.outcard)
self.snd_rate = weakaudio.pya_output_rate(outcard, 12000)
# receive card(s) and cat(s)
self.r = [ ] # FT8 instance per sound card
self.cats = [ ] # CAT receiver (or transceiver) per sound card
self.rth = [ ] # thread that runs the FT8
for ci in range(len(self.card_args)):
desc = self.card_args[ci]
r = self.recv_class() # ft8.FT8 or jt65.JT65
self.r.append(r)
r.opencard(desc)
th = threading.Thread(target=lambda r=r: r.gocard())
th.daemon = True
th.start()
self.rth.append(th)
if ci > 0 and self.cat_args[ci][0] == "k3" and self.cat_args[ci][1] == "-":
# -catX k3 -
# this is for the sub-receiver, and card zero must be the main K3.
if self.cat_args[0][0] == "k3":
self.cats.append(self.cats[0])
else:
sys.stderr.write("-cat k3 - can only be used if it follows -cat k3 /dev/...\n")
sys.exit(1)
elif self.cat_args[ci] != None: