summaryrefslogtreecommitdiff
path: root/lib/decoder.f90
blob: 6d8e2fe05e483ee2d96cbf2b7b7a36aea0189d73 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
subroutine multimode_decoder(ss,id2,params,nfsample)

!$ use omp_lib
  use prog_args
  use timer_module, only: timer
  use jt4_decode
  use jt65_decode
  use jt9_decode
  use ft8_decode
  use ft8_decodevar
  use ft4_decode
  use fst4_decode
  use q65_decode

!ft8md added 3 uses below
  use ft8_mod1, only : ndecodes,allmessages,allsnrs,allfreq,mycall12_0,         &
       mycall12_00,hiscall12_0,nmsg,odd,even,oddcopy,evencopy,nlasttx,          &
       lqsomsgdcd,mycalllen1,msgroot,msgrootlen,lapmyc,sumxdtt,avexdt,          &
       nfawide,nfbwide,mycall,hiscall,lhound,mybcall,hisbcall,lenabledxcsearch, &
       lwidedxcsearch,hisgrid4,lmultinst,dd8,nft8cycles,lskiptx1,ncandallthr,   &
       nincallthr,incall,msgincall,xdtincall,maskincallthr,ltxing,hisgrid

  use packjt77, only : lcommonft8b,ihash22var,calls12var,calls22var

  include 'jt9com.f90'
  include 'timer_common.inc'

  type, extends(jt4_decoder) :: counting_jt4_decoder
     integer :: decoded
  end type counting_jt4_decoder

  type, extends(jt65_decoder) :: counting_jt65_decoder
     integer :: decoded
  end type counting_jt65_decoder

  type, extends(jt9_decoder) :: counting_jt9_decoder
     integer :: decoded
  end type counting_jt9_decoder

  type, extends(ft8_decoder) :: counting_ft8_decoder
     integer :: decoded
  end type counting_ft8_decoder

  type, extends(ft8_decodervar) :: counting_ft8_decodervar
     integer :: decodedvar
     real :: xdtt(200)
  end type counting_ft8_decodervar

  type, extends(ft4_decoder) :: counting_ft4_decoder
     integer :: decoded
  end type counting_ft4_decoder

  type, extends(fst4_decoder) :: counting_fst4_decoder
     integer :: decoded
  end type counting_fst4_decoder

  type, extends(q65_decoder) :: counting_q65_decoder
     integer :: decoded
  end type counting_q65_decoder

  logical first,firstsd !ft8md
  logical(1) lhoundprev !ft8md
  integer nutc,ndelay
  type(params_block) :: params
  data ndelay/0/
  data first/.true./ !ft8md
  data firstsd/.true./ !ft8md
  data lhoundprev/.false./ !ft8md
  real ss(184,NSMAX)
  logical baddata,newdat65,newdat9,single_decode,bVHF,bad0,newdat,ex
  logical lprinthash22
  integer*2 id2(NTMAX*12000)
  integer nqf(20)
  real*4 dd(NTMAX*12000)
  character(len=20) :: datetime
  !character(len=12) :: mycall, hiscall  !ft8md
  character(len=6) :: mygrid!, hisgrid   !ft8md
  character*60 line
  character*37 msg37
  data ndec8/0/,ntr0/-1/
  save
  type(counting_jt4_decoder) :: my_jt4
  type(counting_jt65_decoder) :: my_jt65
  type(counting_jt9_decoder) :: my_jt9
  type(counting_ft8_decoder) :: my_ft8
  type(counting_ft8_decodervar) :: my_ft8var
  type(counting_ft4_decoder) :: my_ft4
  type(counting_fst4_decoder) :: my_fst4
  type(counting_q65_decoder) :: my_q65  

  if(.not.params%newdat .and. params%ntr.gt.ntr0) go to 800
  ntr0=params%ntr
  rms=sqrt(dot_product(float(id2(1:180000)),float(id2(1:180000)))/180000.0)
  if(rms.lt.0.5) go to 800

! Cast C character arrays to Fortran character strings
  datetime=transfer(params%datetime, datetime)
  mycall=transfer(params%mycall,mycall)
  mybcall=transfer(params%mybcall,mybcall)
  hiscall=transfer(params%hiscall,hiscall)
  hisbcall=transfer(params%hisbcall,hisbcall)
  mygrid=transfer(params%mygrid,mygrid)
  hisgrid=transfer(params%hisgrid,hisgrid)
  hisgrid4=hisgrid(1:4)

! Initialize decode counts
  my_jt4%decoded = 0
  my_jt65%decoded = 0
  my_jt9%decoded = 0
  my_ft8%decoded = 0
  my_ft8var%decodedvar = 0
  my_ft4%decoded = 0
  my_fst4%decoded = 0
  my_q65%decoded = 0
  my_ft8var%xdtt=0.

  ncandall=0           !ft8md
  ncandallthr=0        !ft8md
  
! FT8 defaults at start to enable a8 decoding
  if(params%nzhsym.eq.41 .or. params%lmultift8) ltry_a8=.true.

! For testing only: return Rx messages stored in a file as decodes
  inquire(file='rx_messages.txt',exist=ex)
  if(ex) then
     if(params%nzhsym.eq.41) then
        open(39,file='rx_messages.txt',status='old')
        do i=1,9999
           read(39,'(a60)',end=5) line
           if(line(1:1).eq.' ' .or. line(1:1).eq.'-') go to 800
           write(*,'(a)') trim(line)
        enddo
5       close(39)
     endif
     go to 800
  endif

  ncontest=iand(params%nexp_decode,7)
  single_decode=iand(params%nexp_decode,32).ne.0
  bVHF=iand(params%nexp_decode,64).ne.0
  if(mod(params%nranera,2).eq.0) ntrials=10**(params%nranera/2)
  if(mod(params%nranera,2).eq.1) ntrials=3*10**(params%nranera/2)
  if(params%nranera.eq.0) ntrials=0

  nfail=0
10 if (params%nagain) then
     open(13,file=trim(temp_dir)//'/decoded.txt',status='unknown',            &
          position='append',iostat=ios13)
  else
     open(13,file=trim(temp_dir)//'/decoded.txt',status='unknown',iostat=ios13)
  endif
  if(ios13.ne.0) then
     nfail=nfail+1
     if(nfail.le.3) then
        call sleep_msec(10)
        go to 10
     endif
  endif

  if(params%nmode.eq.8) then
    ! We're in FT8 mode
     if(ncontest.eq.6) then            !Fox=6, Hound=7
        ! Fox mode: initialize and open houndcallers.txt     
        inquire(file=trim(temp_dir)//'/houndcallers.txt',exist=ex)
        if(.not.ex) then
           c2fox='            '
           g2fox='    '
           nsnrfox=-99
           nfreqfox=-99
           n30z=0
           nwrap=0
           nfox=0
        endif
        open(19,file=trim(temp_dir)//'/houndcallers.txt',status='unknown')
     endif

     if(ncontest.eq.7 .and. params%b_superfox .and. params%b_even_seq) then
        if(params%nzhsym.lt.50) go to 800
        ! Call the superFox decoder
        call sfrx_sub(params%yymmdd,params%nutc,params%nfqso,params%ntol,id2)
     else
        call timer('decft8  ',0)
        newdat=params%newdat
        if(params%emedelay.ne.0.0) then
           id2(1:156000)=id2(24001:180000)  ! Drop the first 2 seconds of data
           id2(156001:180000)=0
        endif

! ft8md below
        if(params%lmultift8 .and. params%nmode.eq.8) then
           if(params%lmodechanged) then
              avexdt=0.
              ihash22var=-1
              calls22var=''
              calls12var=''
              nintcount=3
           endif ! avexdt fast track in FT8 after mode change

           if(.not.params%nagain) ndelay=params%ndelay
           lqsomsgdcd=.false.
           if(ndelay.gt.0) then ! received incomplete interval
              call partintft8(ndelay,params%nutc)
              lqsomsgdcd=.true.
           endif
           ntrials=params%nranera
           if(params%nsecbandchanged.gt.0) then
              nsamplesdel=params%nsecbandchanged*12000
              if(params%nsecbandchanged.gt.14) then
                 dd8=0. ! protection
              else
                 dd8(1:nsamplesdel)=0.
              endif
           endif
  
           if(.not.params%nagain) nutc=params%nutc

           if(first) then
              call cwfilter(first) 
              first=.false. 
           endif ! + ALLCALL to memory
           lenabledxcsearch=params%lenabledxcsearch
           lwidedxcsearch=params%lwidedxcsearch

           lmultinst=params%lmultinst
           lskiptx1=params%lskiptx1
           ltxing=params%ltxing

           mycalllen1=len_trim(mycall)+1
           msgroot=''
           msgroot=trim(mycall)//' '//trim(hiscall)//' '
           msgrootlen=len_trim(msgroot)
           lcommonft8b=params%lcommonft8b
           lhound=params%lhound
           nft8cycles=params%nft8cycles
           forcedt=0.
        
           if((hiscall.ne.hiscall12_0 .and. hiscall.ne.'            ')          &
                .or. (mycall.ne.mycall12_0 .and. mycall.ne.'            ') .or. &
                (lhound.neqv.lhoundprev)) then
              if(hiscall.ne.'            ') then
                 call tone8(params%lmycallstd,params%lhiscallstd)
                 hiscall12_0=hiscall
                 mycall12_0=mycall
              endif
              lhoundprev=lhound
           endif
           if(params%lmycallstd .and. mycall.ne.'            ' .and.      &
                mycall12_00.ne.mycall) then
              call tone8myc()
              mycall12_00=mycall
           endif

           ndecodes=0            !Initialize arrays for multi-threaded decoding
           allmessages=""
           allsnrs=0
           allfreq=0.
           numcores=omp_get_num_procs()
           nuserthr=params%nmt

           numthreads=1                                         ! fallback
           if(nuserthr.eq.0) then                               ! auto
              if(numcores.eq.1) then
                 numthreads=1
              else if(numcores.gt.1 .and. numcores.lt.5) then
                 numthreads=numcores-1
              else if(numcores.gt.4 .and. numcores.lt.9) then
                 numthreads=numcores-2
              else if(numcores.gt.8 .and. numcores.lt.16) then
                 numthreads=numcores-3
              else
                 numthreads=12
              endif
           else if(nuserthr.gt.0 .and. nuserthr.lt.13) then
              ! number of threads shall not exceed number of logical cores
              if(numcores.ge.nuserthr) then
                 numthreads=nuserthr
              else
                 numthreads=numcores
              endif
           endif

           call omp_set_dynamic(.false.)
           call omp_set_nested(.true.)

           nfa=params%nfa
           nfb=params%nfb
           nfqso=params%nfqso
           nfawide=params%nfa
           nfbwide=params%nfb

           if(params%nagainfil) then
              if(nfqso.lt.nfa .or. nfqso.gt.nfb) then
                 write(*,64) nutc,'nfqso is out of bandwidth','d'
64               format(i6.6,2x,a25,16x,a1)
                 go to 800
              endif
              nfa=max(nfa,nfqso-25) ! 50Hz bandwidth for decode via double click
              nfb=min(nfb,nfqso+25)
              numthreads=min(4,numthreads)
! To do: withdraw limitation when threads are in sync at main passes?
           endif

           nsec=mod(nutc,100)
           nmsg=0
           if(nsec.ne.0 .and. nsec.ne.15 .and. nsec.ne.30 .and. nsec.ne.45) then
! Reading simulated wav file
              odd%lstate=.false.
              even%lstate=.false.
              oddcopy%lstate=.false.
              evencopy%lstate=.false.  
           endif

           if(firstsd) then
              odd%lstate=.false.
              even%lstate=.false.
              firstsd=.false.
           endif

           if(nsec.eq.0 .or. nsec.eq.30) then
              evencopy%msg=even%msg
              evencopy%freq=even%freq
              evencopy%dt=even%dt
              evencopy%lstate=even%lstate
              even%lstate=.false.
           endif

           if(nsec.eq.15 .or. nsec.eq.45) then
              oddcopy%msg=odd%msg
              oddcopy%freq=odd%freq
              oddcopy%dt=odd%dt
              oddcopy%lstate=odd%lstate
              odd%lstate=.false.
           endif

           nlasttx=params%nlasttx
           lapmyc=params%lapmyc
           nFT8decd=0
           sumxdt=0.0
           if(params%nmode.eq.4) sumxdtt=0.0
           call fillhashvar(numthreads,.false.)
           call ft8apsetvar(params%lmycallstd,params%lhiscallstd,numthreads)

           if(numthreads.eq.1) then
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,  &
                   nfqso,params%nft8rxfsens,params%nftx,nutc,nfa,nfb,       &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,    &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,   &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth, &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
           endif

           if(numthreads.eq.2) then
              nfmid=nfa+nint(abs(nfb-nfa)/2.)
              if((nfmid+1).gt.nfb) nfmid=nfb-1

!$omp parallel sections num_threads(2) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid,                &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid+1,nfb,              &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.3) then
              nfdelta=nint(abs(nfb-nfa)/3.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              if((nfmid2+1).gt.nfb) nfmid2=nfb-1
!$omp parallel sections num_threads(3) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac
              
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.4) then 
              nfdelta=nint(abs(nfb-nfa)/4.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              if((nfmid3+1).gt.nfb) nfmid3=nfb-1

!$omp parallel sections num_threads(4) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth, &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth, &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.5) then
              nfdelta=nint(abs(nfb-nfa)/5.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              if((nfmid4+1).gt.nfb) nfmid4=nfb-1

!$omp parallel sections num_threads(5) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.6) then
              nfdelta=nint(abs(nfb-nfa)/6.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              if((nfmid5+1).gt.nfb) nfmid5=nfb-1

!$omp parallel sections num_threads(6) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.7) then
              nfdelta=nint(abs(nfb-nfa)/7.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              if((nfmid6+1).gt.nfb) nfmid6=nfb-1

!$omp parallel sections num_threads(7) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.8) then
              nfdelta=nint(abs(nfb-nfa)/8.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              nfmid7=nfmid6+nfdelta
              if((nfmid7+1).gt.nfb) nfmid7=nfb-1

!$omp parallel sections num_threads(8) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfmid7,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid7+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   8,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.9) then
              nfdelta=nint(abs(nfb-nfa)/9.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              nfmid7=nfmid6+nfdelta
              nfmid8=nfmid7+nfdelta
              if((nfmid8+1).gt.nfb) nfmid8=nfb-1

!$omp parallel sections num_threads(9) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfmid7,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid7+1,nfmid8,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   8,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid8+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   9,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.10) then
              nfdelta=nint(abs(nfb-nfa)/10.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              nfmid7=nfmid6+nfdelta
              nfmid8=nfmid7+nfdelta
              nfmid9=nfmid8+nfdelta
              if((nfmid9+1).gt.nfb) nfmid9=nfb-1

!$omp parallel sections num_threads(10) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac
              
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfmid7,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid7+1,nfmid8,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   8,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid8+1,nfmid9,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   9,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                  params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest) 
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid9+1,nfb,             &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   10,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.11) then
              nfdelta=nint(abs(nfb-nfa)/11.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              nfmid7=nfmid6+nfdelta
              nfmid8=nfmid7+nfdelta
              nfmid9=nfmid8+nfdelta
              nfmid10=nfmid9+nfdelt
              if((nfmid10+1).gt.nfb) nfmid10=nfb-1
!$omp parallel sections num_threads(11) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac
              
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfmid7,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid7+1,nfmid8,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   8,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid8+1,nfmid9,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   9,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid9+1,nfmid10,         &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   10,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid10+1,nfb,            &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   11,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif

           if(numthreads.eq.12) then
              nfdelta=nint(abs(nfb-nfa)/12.)
              nfmid1=nfa+nfdelta
              nfmid2=nfmid1+nfdelta
              nfmid3=nfmid2+nfdelta
              nfmid4=nfmid3+nfdelta
              nfmid5=nfmid4+nfdelta
              nfmid6=nfmid5+nfdelta
              nfmid7=nfmid6+nfdelta
              nfmid8=nfmid7+nfdelta
              nfmid9=nfmid8+nfdelta
              nfmid10=nfmid9+nfdelta
              nfmid11=nfmid10+nfdelt
              if((nfmid11+1).gt.nfb) nfmid11=nfb-1
!$omp parallel sections num_threads(12) shared(dd8,nmsg,even,odd,lqsomsgdcd), &
!$omp& shared(first_osd,gen,ndecodes,allmessages,allsnrs,allfreq) if(.true.) !iif() needed on Mac

!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid5+1,nfmid6,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   6,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid6+1,nfmid7,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   7,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid4+1,nfmid5,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   5,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid7+1,nfmid8,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   8,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid3+1,nfmid4,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   4,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid8+1,nfmid9,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   9,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid2+1,nfmid3,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   3,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid9+1,nfmid10,         &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   10,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid1+1,nfmid2,          &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   2,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid10+1,nfmid11,        &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   11,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfa,nfmid1,               &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   1,numthreads,logical(params%nagainfil),params%lft8lowth,      &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp section
              call my_ft8var%decodevar(ft8_decodedvar,params%nQSOProgress,nfqso, &
                   params%nft8rxfsens,params%nftx,nutc,nfmid11+1,nfb,            &
                   params%ncandthin,params%ndtcenter,nsec,params%napwid,         &
                   params%lmycallstd,params%lhiscallstd,params%nstophint,        &
                   12,numthreads,logical(params%nagainfil),params%lft8lowth,     &
                   params%lft8subpass,params%lhideft8dupes,params%lft8apon,ncontest)
!$omp end parallel sections
           endif
           
           do i=1,numthreads
              do m=1,nincallthr(i)
                 nindex=maskincallthr(i)+m
                 incall(30:2:-1)=incall(30-1:1:-1)
                 incall(1)%msg=msgincall(nindex)
                 incall(1)%xdt=xdtincall(nindex)
              enddo
           enddo

           if(nsec.eq.0 .or. nsec.eq.30) even(nmsg+1:130)%lstate=.false.
           if(nsec.eq.15 .or. nsec.eq.45) odd(nmsg+1:130)%lstate=.false.

           if(params%ndelay.eq.0) then
              nFT8decd=my_ft8var%decodedvar
              dtmed=0.            
!            if(params%lforcesync) then; nintcount=3 ! fast track after Sync
!            elseif(nintcount.gt.0) then; nintcount=nintcount-1
              if(nintcount.gt.0) then
                 nintcount=nintcount-1
              endif
!              if(params%lforcesync .and. nFT8decd.eq.0) then
              if(nFT8decd.eq.0) then
                 avexdt=forcedt
              else
                 if(nFT8decd.gt.2) then
                    do i=1,nFT8decd
                       if(i.lt.nFT8decd-1) then
                          if((my_ft8var%xdtt(i).gt.my_ft8var%xdtt(i+1) .and.   &
                               my_ft8var%xdtt(i).lt.my_ft8var%xdtt(i+2)) .or.  &
                               (my_ft8var%xdtt(i).lt.my_ft8var%xdtt(i+1) .and. &
                               my_ft8var%xdtt(i).gt.my_ft8var%xdtt(i+2))) then
                             dtmed=my_ft8var%xdtt(i)
                          else if((my_ft8var%xdtt(i+1).gt.my_ft8var%xdtt(i) .and. &
                               my_ft8var%xdtt(i+1).lt.my_ft8var%xdtt(i+2)) .or.   &
                               (my_ft8var%xdtt(i+1).lt.my_ft8var%xdtt(i) .and.    &
                               my_ft8var%xdtt(i+1).gt.my_ft8var%xdtt(i+2))) then
                             dtmed=my_ft8var%xdtt(i+1)
                          else if((my_ft8var%xdtt(i+2).gt.my_ft8var%xdtt(i) .and. &
                               my_ft8var%xdtt(i+2).lt.my_ft8var%xdtt(i+1)) .or.   &
                               (my_ft8var%xdtt(i+2).lt.my_ft8var%xdtt(i) .and.    &
                               my_ft8var%xdtt(i+2).gt.my_ft8var%xdtt(i+1))) then
                             dtmed=my_ft8var%xdtt(i+2)
                          else
                             dtmed=my_ft8var%xdtt(i)
                          endif
                          sumxdt=sumxdt+dtmed
                       else
                          sumxdt=sumxdt+dtmed ! use last median value
                       endif
                    enddo
                    if(nFT8decd.gt.5) then
                       avexdt=(avexdt+sumxdt/nFT8decd)/2
                    else if(nFT8decd.eq.5) then
                       avexdt=(1.1*avexdt+0.9*sumxdt/nFT8decd)/2
                    else if(nFT8decd.eq.4) then
                       avexdt=(1.25*avexdt+0.75*sumxdt/nFT8decd)/2
                    else if(nFT8decd.eq.3) then
                       avexdt=(1.35*avexdt+0.65*sumxdt/nFT8decd)/2
                    endif
                 else if(nFT8decd.gt.0) then
                    sumxdt=sum(my_ft8var%xdtt(1:nFT8decd))
                    if(nFT8decd.eq.2) then
                       avexdt=(1.5*avexdt+0.5*sumxdt/nFT8decd)/2
                    else if(nFT8decd.eq.1) then
                       avexdt=(1.75*avexdt+0.25*sumxdt)/2
                    endif
                 endif
              endif
           endif
           
           if(nFT8decd.gt.10 .and. nintcount.eq.1) avexdt=sumxdt/nFT8decd ! fast track after Sync or mode change on crowded bands
           call fillhashvar(numthreads,.true.)
           ncandall=sum(ncandallthr(1:numthreads))
           if(nFT8decd.eq.0) avexdt=0. ! reset to let correct sliding in decoder
           call timer('decft8  ',1)
        else        ! still FT8 but not ft8md
           call my_ft8%decode(ft8_decoded,id2,params%nQSOProgress,params%nfqso,  &
                params%nftx,newdat,params%nutc,params%nfa,params%nfb,            &
                params%nzhsym,params%ndepth,params%emedelay,ncontest,            &
                logical(params%nagain),logical(params%lft8apon),ltry_a8,         &
                logical(params%lapcqonly),params%napwid,mycall,hiscall,hisgrid,  &
                params%ndiskdat)
           call timer('decft8  ',1)
        endif       ! end of 'still FT8 but not ft8md'
     endif         ! end of 'if not in SuperFox mode'
     
     j=0
     if(ncontest.eq.6) then
        ! Fox mode: save decoded Hound calls for possible selection by FoxOp
        n=params%nutc
        n30=(3600*(n/10000) + 60*mod((n/100),100) + mod(n,100))/30
        if(n30.lt.n30z) nwrap=nwrap+2880    !New UTC day, handle the wrap
        n30z=n30
        n30=n30+nwrap

        rewind 19
        if(nfox.eq.0) then
           endfile 19
           rewind 19
        else
           do i=1,nfox
              n=n30fox(i)
              nage=min(99,mod(n30-n+288000,2880))
              if(nage.le.4) then
                 j=j+1
                 c2fox(j)=c2fox(i)
                 g2fox(j)=g2fox(i)
                 nsnrfox(j)=nsnrfox(i)
                 nfreqfox(j)=nfreqfox(i)
                 n30fox(j)=n
                 ! nage=min(99,mod(n30-n+288000,2880))
                 if(len(trim(g2fox(j))).eq.4) then
                    call azdist(mygrid,g2fox(j)//'  ',0.d0,nAz,nEl,nDmiles, &
                         nDkm,nHotAz,nHotABetter)
                 else
                    nDkm=9999
                 endif
                 write(19,1004) c2fox(j),g2fox(j),nsnrfox(j),nfreqfox(j), &
                      nDkm,nage
1004             format(a12,1x,a4,i5,i6,i7,i3)
              endif
           enddo
           nfox=j
           flush(19)
        endif
     endif
     go to 800
  endif    ! end of code for FT8 mode

  if(params%nmode.eq.5) then
     call timer('decft4  ',0)
     call my_ft4%decode(ft4_decoded,id2,params%nQSOProgress,params%nfqso,    &
          params%nfa,params%nfb,params%ndepth,                               &
          logical(params%lapcqonly),ncontest,mycall,hiscall)
     call timer('decft4  ',1)
     go to 800
  endif

  if(params%nmode.eq.66) then        !NB: JT65 = 65, Q65 = 66.
     ! We're in Q65 mode
     open(17,file=trim(temp_dir)//'/red.dat',status='unknown')
     open(14,file=trim(temp_dir)//'/avemsg.txt',status='unknown')
     call timer('dec_q65 ',0)
     nqd=1
     call my_q65%decode(q65_decoded,id2,nqd,params%nutc,params%ntr,      &
          params%nsubmode,params%nfqso,params%ntol,params%ndepth,        &
          params%nfa,params%nfb,logical(params%nclearave),               &
          single_decode,logical(params%nagain),params%max_drift,         &
          logical(params%newdat),params%emedelay,mycall,hiscall,hisgrid, &
          params%nQSOProgress,ncontest,logical(params%lapcqonly),navg0,nqf)
     params%nclearave=.false.

     if(.not.params%nagain) then
                ! Go through identified candidates again, treating each as if it had been
                ! double-clicked on the waterfall.
        do k=1,20
           if(nqf(k).eq.0) exit
           if(params%nagain .and. abs(nqf(k)-params%nfqso).gt.params%ntol) cycle
           nqd=1
           navg0=0
           ntol=5
           call my_q65%decode(q65_decoded,id2,nqd,params%nutc,params%ntr,    &
                params%nsubmode,nqf(k),ntol,params%ndepth,                   &
                params%nfa,params%nfb,logical(params%nclearave),             &
                .true.,.true.,params%max_drift,                              &
                .false.,params%emedelay,mycall,hiscall,hisgrid,              &
                params%nQSOProgress,ncontest,logical(params%lapcqonly),      &
                navg0,nqf)
        enddo
     endif

     call timer('dec_q65 ',1)
     close(17)
     go to 800
  endif

  if(params%nmode.eq.240) then
     ! We're in FST4 mode
     ndepth=iand(params%ndepth,3)
     iwspr=0
     lprinthash22=.false.
     params%nsubmode=0
     call timer('dec_fst4',0)
     call my_fst4%decode(fst4_decoded,id2,params%nutc,                &
          params%nQSOProgress,params%nfa,params%nfb,                  &
          params%nfqso,ndepth,params%ntr,params%nexp_decode,          &
          params%ntol,params%emedelay,logical(params%nagain),         &
          logical(params%lapcqonly),mycall,hiscall,iwspr,lprinthash22)
     call timer('dec_fst4',1)
     go to 800
  endif

  if(params%nmode.eq.241 .or. params%nmode.eq.242) then
     ! We're in FST4W mode
     ndepth=iand(params%ndepth,3)
     iwspr=1
     lprinthash22=.false.
     if(params%nmode.eq.242) lprinthash22=.true. 
     call timer('dec_fst4',0)
     call my_fst4%decode(fst4_decoded,id2,params%nutc,                &
          params%nQSOProgress,params%nfa,params%nfb,                  &
          params%nfqso,ndepth,params%ntr,params%nexp_decode,          &
          params%ntol,params%emedelay,logical(params%nagain),         &
          logical(params%lapcqonly),mycall,hiscall,iwspr,lprinthash22)
     call timer('dec_fst4',1)
     go to 800
  endif

  ! Zap data at start that might come from T/R switching transient?
  nadd=100
  k=0
  bad0=.false.
  do i=1,240
     sq=0.
     do n=1,nadd
        k=k+1
        sq=sq + float(id2(k))**2
     enddo
     rms=sqrt(sq/nadd)
     if(rms.gt.10000.0) then
        bad0=.true.
        kbad=k
        rmsbad=rms
     endif
  enddo
  if(bad0) then
     nz=min(NTMAX*12000,kbad+100)
              !     id2(1:nz)=0                ! temporarily disabled as it can breaak the JT9 decoder, maybe others
  endif

  if(params%nmode.eq.4 .or. params%nmode.eq.65) open(14,file=trim(temp_dir)// &
       '/avemsg.txt',status='unknown')

  if(params%nmode.eq.4) then
     jz=52*nfsample
     if(params%newdat) then
        if(nfsample.eq.12000) call wav11(id2,jz,dd)
        if(nfsample.eq.11025) dd(1:jz)=id2(1:jz)
     else
        jz=52*11025
     endif
     call my_jt4%decode(jt4_decoded,dd,jz,params%nutc,params%nfqso,         &
          params%ntol,params%emedelay,params%dttol,logical(params%nagain),  &
          params%ndepth,logical(params%nclearave),params%minsync,           &
          params%minw,params%nsubmode,mycall,hiscall,         &
          hisgrid,params%nlist,params%listutc,jt4_average)
     go to 800
  endif

  npts65=52*12000
  if(baddata(id2,npts65)) then
     nsynced=0
     ndecoded=0
     go to 800
  endif
  
  ntol65=params%ntol              !### is this OK? ###
  newdat65=params%newdat
  newdat9=params%newdat

  call omp_set_dynamic(.true.)

!$omp parallel sections num_threads(2) shared(ndecoded) if(.true.) !iif() needed on Mac

!$omp section
  if(params%nmode.eq.65) then                       ! We're in JT65 mode     
     if(newdat65) dd(1:npts65)=id2(1:npts65)
     nf1=params%nfa
     nf2=params%nfb
     call timer('jt65a   ',0)
     call my_jt65%decode(jt65_decoded,dd,npts65,newdat65,params%nutc,      &
          nf1,nf2,params%nfqso,ntol65,params%nsubmode,params%minsync,      &
          logical(params%nagain),params%n2pass,logical(params%nrobust),    &
          ntrials,params%naggressive,params%ndepth,params%emedelay,        &
          logical(params%nclearave),mycall,hiscall,                        &
          hisgrid,params%nexp_decode,params%nQSOProgress,                  &
          logical(params%ljt65apon))
     call timer('jt65a   ',1)

  else if(params%nmode.eq.9 .or. (params%nmode.eq.(65+9) .and.             &
       params%ntxmode.eq.9)) then
              ! We're in JT9 mode, or should do JT9 first
     call timer('decjt9  ',0)
     call my_jt9%decode(jt9_decoded,ss,id2,params%nfqso,                   &
          newdat9,params%npts8,params%nfa,params%nfsplit,params%nfb,       &
          params%ntol,params%nzhsym,logical(params%nagain),params%ndepth,  &
          params%nmode,params%nsubmode,params%nexp_decode)
     call timer('decjt9  ',1)
  endif

!$omp section
  if(params%nmode.eq.(65+9)) then       !Do the other mode (we're in dual mode)
     if (params%ntxmode.eq.9) then
        if(newdat65) dd(1:npts65)=id2(1:npts65)
        nf1=params%nfa
        nf2=params%nfb
        call timer('jt65a   ',0)
        call my_jt65%decode(jt65_decoded,dd,npts65,newdat65,params%nutc,   &
             nf1,nf2,params%nfqso,ntol65,params%nsubmode,params%minsync,   &
             logical(params%nagain),params%n2pass,logical(params%nrobust), &
             ntrials,params%naggressive,params%ndepth,params%emedelay,     &
             logical(params%nclearave),mycall,hiscall,       &
             hisgrid,params%nexp_decode,params%nQSOProgress,        &
             logical(params%ljt65apon))
        call timer('jt65a   ',1)
     else
        call timer('decjt9  ',0)
        call my_jt9%decode(jt9_decoded,ss,id2,params%nfqso,                &
             newdat9,params%npts8,params%nfa,params%nfsplit,params%nfb,    &
             params%ntol,params%nzhsym,logical(params%nagain),             &
             params%ndepth,params%nmode,params%nsubmode,params%nexp_decode)
        call timer('decjt9  ',1)
     end if
  endif

!$omp end parallel sections


! JT65 is not yet producing info for nsynced, ndecoded.
800 ndecoded = my_jt4%decoded + my_jt65%decoded + my_jt9%decoded +       &
         my_ft8%decoded + my_ft8var%decodedvar + my_ft4%decoded +        &
         my_fst4%decoded + my_q65%decoded
  if(params%lmultift8 .and. params%nmode.eq.8) then
     if(params%nzhsym.eq.41) ndec41=0
     if(params%nzhsym.eq.46) ndec46=ndecoded
     if(params%nzhsym.eq.47) ndec47=ndecoded
     if(params%nzhsym.eq.48) ndec48=ndecoded
     if(params%nzhsym.eq.49) ndec49=ndecoded
     if(params%nzhsym.eq.50) then
        ndecoded=ndec41+ndec46+ndec47+ndec48+ndec49+ndecoded
     endif
  elseif(params%nmode.eq.8 .and. params%nzhsym.eq.41) then 
     ndec41=ndecoded
  elseif(params%nmode.eq.8 .and. params%nzhsym.eq.47) then 
     ndec47=ndecoded
  elseif(params%nmode.eq.8 .and. params%nzhsym.eq.50) then
     ndecoded=ndec41+ndec47+ndecoded
  endif
  if(params%nmode.ne.8 .or. params%nzhsym.eq.50 .or. &
       (params%lmultift8 .and. params%nmode.eq.8 .and. params%nzhsym.gt.45) .or. &
       .not.params%ndiskdat) then !ft8md
     if(.not.lquiet) write(*,1010) nsynced,ndecoded,navg0
1010 format('<DecodeFinished>',2i4,i9)
     call flush(6)
  endif
  close(13)
  if(ncontest.eq.6) close(19)
  if(params%nmode.eq.4 .or. params%nmode.eq.65 .or. params%nmode.eq.66) close(14)
  return

contains

  subroutine jt4_decoded(this,snr,dt,freq,have_sync,sync,is_deep,    &
       decoded0,qual,ich,is_average,ave)
    implicit none
    class(jt4_decoder), intent(inout) :: this
    integer, intent(in) :: snr
    real, intent(in) :: dt
    integer, intent(in) :: freq
    logical, intent(in) :: have_sync
    logical, intent(in) :: is_deep
    character(len=1), intent(in) :: sync
    character(len=22), intent(in) :: decoded0
    real, intent(in) :: qual
    integer, intent(in) :: ich
    logical, intent(in) :: is_average
    integer, intent(in) :: ave

    character*22 decoded
    character*3 cflags

    if(ich.eq.-99) stop                         !Silence compiler warning
    if (have_sync) then
       decoded=decoded0
       cflags='   '
       if(decoded.ne.'                      ') then
          cflags='f  '
          if(is_deep) then
             cflags='d  '
             write(cflags(2:2),'(i1)') min(int(qual),9)
             if(qual.ge.10.0) cflags(2:2)='*'
             if(qual.lt.3.0) decoded(22:22)='?'
          endif
          if(is_average) then
             write(cflags(3:3),'(i1)') min(ave,9)
             if(ave.ge.10) cflags(3:3)='*'
             if(cflags(1:1).eq.'f') cflags=cflags(1:1)//cflags(3:3)//' '
          endif
       endif
       write(*,1000) params%nutc,snr,dt,freq,sync,decoded,cflags
1000   format(i4.4,i4,f5.1,i5,1x,'$',a1,1x,a22,1x,a3)
    else
       write(*,1000) params%nutc,snr,dt,freq
    end if

    select type(this)
    type is (counting_jt4_decoder)
       this%decoded = this%decoded + 1
    end select
  end subroutine jt4_decoded

  subroutine jt4_average (this, used, utc, sync, dt, freq, flip)
    implicit none
    class(jt4_decoder), intent(inout) :: this
    logical, intent(in) :: used
    integer, intent(in) :: utc
    real, intent(in) :: sync
    real, intent(in) :: dt
    integer, intent(in) :: freq
    logical, intent(in) :: flip
    character(len=1) :: cused, csync

    cused = '.'
    csync = '*'
    if (used) cused = '$'
    if (flip) csync = '$'
    write(14,1000) cused,utc,sync,dt,freq,csync
1000 format(a1,i5.4,f6.1,f6.2,i6,1x,a1)
  end subroutine jt4_average

  subroutine jt65_decoded(this,sync,snr,dt,freq,drift,nflip,width,     &
       decoded0,ft,qual,nsmo,nsum,minsync)

    use jt65_decode
    implicit none

    class(jt65_decoder), intent(inout) :: this
    real, intent(in) :: sync
    integer, intent(in) :: snr
    real, intent(in) :: dt
    integer, intent(in) :: freq
    integer, intent(in) :: drift
    integer, intent(in) :: nflip
    real, intent(in) :: width
    character(len=22), intent(in) :: decoded0
    integer, intent(in) :: ft
    integer, intent(in) :: qual
    integer, intent(in) :: nsmo
    integer, intent(in) :: nsum
    integer, intent(in) :: minsync

    integer i,nap
    logical is_deep,is_average
    character decoded*22,csync*2,cflags*3

    !$omp critical(decode_results)
    decoded=decoded0
    cflags='   '
    is_deep=ft.eq.2

    if(ft.eq.0 .and. minsync.ge.0 .and. int(sync).lt.minsync) then
       write(*,1010) params%nutc,snr,dt,freq
    else
       is_average=nsum.ge.2
       if(bVHF .and. ft.gt.0) then
          cflags='f  '
          if(is_deep) then
             cflags='d  '
             write(cflags(2:2),'(i1)') min(qual,9)
             if(qual.ge.10) cflags(2:2)='*'
             if(qual.lt.3) decoded(22:22)='?'
          endif
          if(is_average) then
             write(cflags(3:3),'(i1)') min(nsum,9)
             if(nsum.ge.10) cflags(3:3)='*'
          endif
          nap=ishft(ft,-2)
          if(nap.ne.0) then
             if(nsum.lt.2) write(cflags(1:3),'(a1,i1," ")') 'a',nap
             if(nsum.ge.2) write(cflags(1:3),'(a1,2i1)') 'a',nap,min(nsum,9)
          endif
       endif
       csync='# '
       i=0
       if(bVHF .and. nflip.ne.0 .and.                         &
            sync.ge.max(0.0,float(minsync))) then
          csync='#*'
          if(nflip.eq.-1) then
             csync='##'
             if(decoded.ne.'                      ') then
                do i=22,1,-1
                   if(decoded(i:i).ne.' ') exit
                enddo
                if(i.gt.18) i=18
                decoded(i+2:i+4)='OOO'
             endif
          endif
       endif
       n=len(trim(decoded))
       if(n.eq.2 .or. n.eq.3) csync='# '
       if(cflags(1:1).eq.'f') then
          cflags(2:2)=cflags(3:3)
          cflags(3:3)=' '
       endif
       write(*,1010) params%nutc,snr,dt,freq,csync,decoded,cflags
1010   format(i4.4,i4,f5.1,i5,1x,a2,1x,a22,1x,a3)
    endif
    if(ios13.eq.0) write(13,1012) params%nutc,nint(sync),snr,dt,    &
         float(freq),drift,decoded,ft,nsum,nsmo
1012 format(i4.4,i4,i5,f6.2,f8.0,i4,3x,a22,' JT65',3i3)
    call flush(6)

    !$omp end critical(decode_results)
    select type(this)
    type is (counting_jt65_decoder)
       this%decoded = this%decoded + 1
    end select
  end subroutine jt65_decoded
  
  subroutine jt9_decoded (this, sync, snr, dt, freq, drift, decoded)
    use jt9_decode
    implicit none

    class(jt9_decoder), intent(inout) :: this
    real, intent(in) :: sync
    integer, intent(in) :: snr
    real, intent(in) :: dt
    real, intent(in) :: freq
    integer, intent(in) :: drift
    character(len=22), intent(in) :: decoded

    !$omp critical(decode_results)

    write(*,1000) params%nutc,snr,dt,nint(freq),decoded
1000 format(i4.4,i4,f5.1,i5,1x,'@ ',1x,a22)
    if(ios13.eq.0) write(13,1002) params%nutc,nint(sync),snr,dt,freq,  &
         drift,decoded
1002 format(i4.4,i4,i5,f6.1,f8.0,i4,3x,a22,' JT9')
    call flush(6)
    !$omp end critical(decode_results)
    select type(this)
    type is (counting_jt9_decoder)
       this%decoded = this%decoded + 1
    end select
  end subroutine jt9_decoded

  subroutine ft8_decodedvar  (this,snr,dt,freq,decodedvar,nap,qual) !ft8md was(this,snr,dt,freq,decodedvar)
    use ft8_decodevar
    implicit none

    class(ft8_decodervar), intent(inout) :: this
    integer, intent(in) :: snr
    real, intent(in) :: dt
    real, intent(in) :: freq
    real, intent(in) :: qual
    real fdiff
    character(len=37), intent(in) :: decodedvar !ft8md was 26
    character*2 annot
    character c1*12,c2*12,g2*4,w*4
    integer i1,i2,i3,i4,i5,n30,nwrap
    integer, intent(in) :: nap  !ft8md
    integer msglen
    character*37 decoded0
    logical isgrid4,first,b0,b1,b2
    data first/.true./
    save

    isgrid4(w)=(len_trim(w).eq.4 .and.                                        &
         ichar(w(1:1)).ge.ichar('A') .and. ichar(w(1:1)).le.ichar('R') .and.  &
         ichar(w(2:2)).ge.ichar('A') .and. ichar(w(2:2)).le.ichar('R') .and.  &
         ichar(w(3:3)).ge.ichar('0') .and. ichar(w(3:3)).le.ichar('9') .and.  &
         ichar(w(4:4)).ge.ichar('0') .and. ichar(w(4:4)).le.ichar('9'))
    
    if(first) then
       c2fox='            '
       g2fox='    '
       nsnrfox=-99
       nfreqfox=-99
       n30z=0
       nwrap=0
       nfox=0
       first=.false.
    endif

    fdiff=freq-params%nfqso
    if(abs(fdiff).lt.3.0) ltry_a8=.false.

    decoded0=decodedvar

    annot='  ' 
    if(nap.ne.0) then
       if(nap.ge.9) then 
          write(annot,'(a1,i1)') 'a',9
       else
          write(annot,'(a1,i1)') 'a',nap
       endif
       if(qual.lt.0.17) decoded0(37:37)='?'
    endif

    write(*,1000) params%nutc,snr,dt,nint(freq),decoded0,annot
1000 format(i6.6,i4,f5.1,i5,' ~ ',1x,a37,1x,a2)
    
    if(ncontest.eq.6) then
       i1=index(decoded0,' ')
       i2=i1 + index(decoded0(i1+1:),' ')
       i3=i2 + index(decoded0(i2+1:),' ')
       if(i1.ge.3 .and. i2.ge.7 .and. i3.ge.10) then
          c1=decoded0(1:i1-1)//'            '
          c2=decoded0(i1+1:i2-1)
          g2=decoded0(i2+1:i3-1)
          b0=c1.eq.mycall
          if(c1(1:3).eq.'DE ' .and. index(c2,'/').ge.2) b0=.true.
          if(len(trim(c1)).ne.len(trim(mycall))) then
             i4=index(trim(c1),trim(mycall))
             i5=index(trim(mycall),trim(c1))
             if(i4.ge.1 .or. i5.ge.1) b0=.true.
          endif
          b1=i3-i2.eq.5 .and. isgrid4(g2)
          b2=i3-i2.eq.1
          if(b0 .and. (b1.or.b2) .and. (nint(freq).ge.1000 .or.             &
               params%b_superfox)) then
             n=params%nutc
             n30=(3600*(n/10000) + 60*mod((n/100),100) + mod(n,100))/30
             if(n30.lt.n30z) nwrap=nwrap+2880    !New UTC day, handle the wrap
             n30z=n30
             n30=n30+nwrap
             if(nfox.lt.MAXFOX) nfox=nfox+1
             c2fox(nfox)=c2
             g2fox(nfox)=g2
             nsnrfox(nfox)=snr
             nfreqfox(nfox)=nint(freq)
             n30fox(nfox)=n30
          endif
       endif
    endif
    call flush(6)
    if(ios13.eq.0) call flush(13)

    select type(this)
    type is (counting_ft8_decodervar)
       this%decodedvar = this%decodedvar + 1
       this%xdtt(this%decodedvar)=dt
    end select
    
    return
  end subroutine ft8_decodedvar
  
  subroutine ft8_decoded (this,sync,snr,dt,freq,decoded,nap,qual)
    use ft8_decode
    implicit none

    class(ft8_decoder), intent(inout) :: this
    real, intent(in) :: sync
    integer, intent(in) :: snr
    real, intent(in) :: dt
    real, intent(in) :: freq
    character(len=37), intent(in) :: decoded
    character c1*12,c2*12,g2*4,w*4
    integer i0,i1,i2,i3,i4,i5,n30,nwrap
    integer, intent(in) :: nap 
    real, intent(in) :: qual
    real fdiff
    character*2 annot
    character*37 decoded0
    logical isgrid4,first,b0,b1,b2
    data first/.true./
    save

    isgrid4(w)=(len_trim(w).eq.4 .and.                                        &
         ichar(w(1:1)).ge.ichar('A') .and. ichar(w(1:1)).le.ichar('R') .and.  &
         ichar(w(2:2)).ge.ichar('A') .and. ichar(w(2:2)).le.ichar('R') .and.  &
         ichar(w(3:3)).ge.ichar('0') .and. ichar(w(3:3)).le.ichar('9') .and.  &
         ichar(w(4:4)).ge.ichar('0') .and. ichar(w(4:4)).le.ichar('9'))

    if(first) then
       c2fox='            '
       g2fox='    '
       nsnrfox=-99
       nfreqfox=-99
       n30z=0
       nwrap=0
       nfox=0
       first=.false.
    endif

    decoded0=decoded
    fdiff=freq-params%nfqso
    if(abs(fdiff).lt.3.0) ltry_a8=.false.

    annot='  '
    if(nap.ne.0) then
       write(annot,'(a1,i1)') 'a',nap
       if(qual.lt.0.17) decoded0(37:37)='?'
    endif

  !    i0=index(decoded0,';')
  ! Always print 37 characters? Or, send i3,n3 up to here from ft8b_2 and use them
  ! to decide how many chars to print?
  !TEMP
    i0=1
    if(i0.le.0) write(*,1000) params%nutc,snr,dt,nint(freq),decoded0(1:22),annot
1000 format(i6.6,i4,f5.1,i5,' ~ ',1x,a22,1x,a2)
    if(i0.gt.0) write(*,1001) params%nutc,snr,dt,nint(freq),decoded0,annot
1001 format(i6.6,i4,f5.1,i5,' ~ ',1x,a37,1x,a2)
    if(ios13.eq.0) write(13,1002) params%nutc,nint(sync),snr,dt,freq,0,decoded0
1002 format(i6.6,i4,i5,f6.1,f8.0,i4,3x,a37,' FT8')

    if(ncontest.eq.6) then
       i1=index(decoded0,' ')
       i2=i1 + index(decoded0(i1+1:),' ')
       i3=i2 + index(decoded0(i2+1:),' ')
       if(i1.ge.3 .and. i2.ge.7 .and. i3.ge.10) then
          c1=decoded0(1:i1-1)//'            '
          c2=decoded0(i1+1:i2-1)
          g2=decoded0(i2+1:i3-1)
          b0=c1.eq.mycall
          if(c1(1:3).eq.'DE ' .and. index(c2,'/').ge.2) b0=.true.
          if(len(trim(c1)).ne.len(trim(mycall))) then
             i4=index(trim(c1),trim(mycall))
             i5=index(trim(mycall),trim(c1))
             if(i4.ge.1 .or. i5.ge.1) b0=.true.
          endif
          b1=i3-i2.eq.5 .and. isgrid4(g2)
          b2=i3-i2.eq.1
          if(b0 .and. (b1.or.b2) .and. (nint(freq).ge.1000 .or.          &
               params%b_superfox)) then
             n=params%nutc
             n30=(3600*(n/10000) + 60*mod((n/100),100) + mod(n,100))/30
             if(n30.lt.n30z) nwrap=nwrap+2880    !New UTC day, handle the wrap
             n30z=n30
             n30=n30+nwrap
             if(nfox.lt.MAXFOX) nfox=nfox+1
             c2fox(nfox)=c2
             g2fox(nfox)=g2
             nsnrfox(nfox)=snr
             nfreqfox(nfox)=nint(freq)
             n30fox(nfox)=n30
          endif
       endif
    endif

    call flush(6)
    if(ios13.eq.0) call flush(13)

    select type(this)
    type is (counting_ft8_decoder)
       this%decoded = this%decoded + 1
    end select
    
    return
  end subroutine ft8_decoded

  subroutine ft4_decoded (this,sync,snr,dt,freq,decoded,nap,qual)
    use ft4_decode
    implicit none

    class(ft4_decoder), intent(inout) :: this
    real, intent(in) :: sync
    integer, intent(in) :: snr
    real, intent(in) :: dt
    real, intent(in) :: freq
    character(len=37), intent(in) :: decoded
    integer, intent(in) :: nap
    real, intent(in) :: qual 
    character*2 annot
    character*37 decoded0

    decoded0=decoded

    annot='  ' 
    if(nap.ne.0) then
       write(annot,'(a1,i1)') 'a',nap
       if(qual.lt.0.17) decoded0(37:37)='?'
    endif

    write(*,1001) params%nutc,snr,dt,nint(freq),decoded0,annot
1001 format(i6.6,i4,f5.1,i5,' + ',1x,a37,1x,a2)

    if(ios13.eq.0) then
       write(13,1002,err=10) params%nutc,nint(sync),snr,dt,freq,0,decoded0
1002   format(i6.6,i4,i5,f6.1,f8.0,i4,3x,a37,' FT4')
       flush(13)
    endif

10  call flush(6)

    select type(this)
    type is (counting_ft4_decoder)
       this%decoded = this%decoded + 1
    end select

    return
  end subroutine ft4_decoded

  subroutine fst4_decoded (this,nutc,sync,nsnr,dt,freq,decoded,nap,   &
       qual,ntrperiod,fmid,w50)

    use fst4_decode
    implicit none

    class(fst4_decoder), intent(inout) :: this
    integer, intent(in) :: nutc
    real, intent(in) :: sync
    integer, intent(in) :: nsnr
    real, intent(in) :: dt
    real, intent(in) :: freq
    character(len=37), intent(in) :: decoded
    integer, intent(in) :: nap
    real, intent(in) :: qual
    integer, intent(in) :: ntrperiod
    real, intent(in) :: fmid
    real, intent(in) :: w50

    character*2 annot
    character*37 decoded0
    character*70 line

    decoded0=decoded
    annot='  '
    if(nap.ne.0) then
       write(annot,'(a1,i1)') 'a',nap
       if(qual.lt.0.17) decoded0(37:37)='?'
    endif

    if(ntrperiod.lt.60) then
       write(line,1001) nutc,nsnr,dt,nint(freq),decoded0,annot
1001   format(i6.6,i4,f5.1,i5,' ` ',1x,a37,1x,a2)
       if(ios13.eq.0) write(13,1002) nutc,nint(sync),nsnr,dt,freq,0,decoded0
1002   format(i6.6,i4,i5,f6.1,f8.0,i4,3x,a37,' FST4')
    else
       write(line,1003) nutc,nsnr,dt,nint(freq),decoded0,annot
1003   format(i4.4,i4,f5.1,i5,' ` ',1x,a37,1x,a2,2f7.3)
       if(ios13.eq.0) write(13,1004) nutc,nint(sync),nsnr,dt,freq,0,decoded0
1004   format(i4.4,i4,i5,f6.1,f8.0,i4,3x,a37,' FST4')
    endif

    if(fmid.ne.-999.0) then
       if(w50.lt.0.95) write(line(65:70),'(f6.3)') w50
       if(w50.ge.0.95) write(line(65:70),'(f6.2)') w50
    endif

    write(*,1005) line
1005 format(a70)

    call flush(6)
    if(ios13.eq.0) call flush(13)

    select type(this)
    type is (counting_fst4_decoder)
       this%decoded = this%decoded + 1
    end select

    return
  end subroutine fst4_decoded

  subroutine q65_decoded (this,nutc,snr1,nsnr,dt,freq,decoded,idec,   &
       nused,ntrperiod)

    use q65_decode
    implicit none

    class(q65_decoder), intent(inout) :: this
    integer, intent(in) :: nutc
    real, intent(in) :: snr1
    integer, intent(in) :: nsnr
    real, intent(in) :: dt
    real, intent(in) :: freq
    character(len=37), intent(in) :: decoded
    integer, intent(in) :: idec
    integer, intent(in) :: nused
    integer, intent(in) :: ntrperiod
    character*3 cflags

    cflags='   '
    if(idec.ge.0) then
       cflags='q  '
       write(cflags(2:2),'(i1)') idec
       if(nused.ge.2) write(cflags(3:3),'(i1)') nused
    endif

    if(ntrperiod.lt.60) then
       write(*,1001) nutc,nsnr,dt,nint(freq),decoded,cflags
1001   format(i6.6,i4,f5.1,i5,' : ',1x,a37,1x,a3)
       if(ios13.eq.0) write(13,1002) nutc,nint(snr1),nsnr,dt,freq,0,decoded
1002   format(i6.6,i4,i5,f6.1,f8.0,i4,3x,a37,' Q65')
    else
       write(*,1003) nutc,nsnr,dt,nint(freq),decoded,cflags
1003   format(i4.4,i4,f5.1,i5,' : ',1x,a37,1x,a3)
       if(ios13.eq.0) write(13,1004) nutc,nint(snr1),nsnr,dt,freq,0,decoded
1004   format(i4.4,i4,i5,f6.1,f8.0,i4,3x,a37,' Q65')

    endif
    call flush(6)
    if(ios13.eq.0) call flush(13)

    select type(this)
    type is (counting_q65_decoder)
       if(idec.ge.0) this%decoded = this%decoded + 1
    end select

    return
  end subroutine q65_decoded

end subroutine multimode_decoder