summaryrefslogtreecommitdiff
path: root/lib/ft8_engine_kernel.f90
blob: 15f3f9547f9d7d9a3b2b7b3cc4cee1849f5bce6b (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
module ft8_engine_kernel
!$ use omp_lib
  use prog_args, only: temp_dir
  use timer_module, only: timer
  use packjt77, only: pack77_state
  use decoder_engine_types, only: ft8_signal_evidence
  use ft8_decode, only: c2fox,g2fox,nsnrfox,nfreqfox,n30fox,n30z,nfox,ft8_a8d
  use ft8_decodevar, only: ltry_a8
  use ft8_decode_ranges, only: max_ft8_decode_ranges, partition_ft8_decode_range
  use ft8_mtd_residual, only: mtd_prepare,mtd_finish,mtd_worker_residual,mtd_worker_spectrum
  use decoder_callbacks, only: decoder_callback_context,counting_ft8_decoder, &
       counting_ft8_decodervar,ft8_decoded,ft8_decodedvar
  use decode_completion_module, only: decode_completion_result,reset_decode_completion, &
       set_decode_completion,write_decode_progress
  use sfrx_engine, only: sfrx_decode
  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,      &
       lastrxmsg,lasthcall
  include 'jt9com.f90'

  private
  public :: ft8_kernel_state,run_ft8_kernel,reset_ft8_kernel,release_ft8_input,params_block

  type :: ft8_kernel_state
     type(counting_ft8_decoder) :: my_ft8
     type(counting_ft8_decodervar) :: my_ft8var
     logical :: first=.true.,firstsd=.true.
     logical :: mtd_residual_ready=.false.
     logical(1) :: lhoundprev=.false.
     integer :: nutc=0,ndelay=0,ntr0=-1,nintcount=0,nwrap=0
     integer :: ndec41=0,ndec46=0,ndec47=0,ndec48=0,ndec49=0
  end type

  interface
     subroutine tone8(lmycallstd,lhiscallstd,knowledge)
       import pack77_state
       logical(1), intent(in) :: lmycallstd,lhiscallstd
       type(pack77_state), target, optional, intent(inout) :: knowledge
     end subroutine
     subroutine tone8myc(knowledge)
       import pack77_state
       type(pack77_state), target, optional, intent(inout) :: knowledge
     end subroutine
     subroutine cwfilter(first,knowledge,render_legacy)
       import pack77_state
       logical, intent(in) :: first
       type(pack77_state), target, optional, intent(inout) :: knowledge
       logical, optional, intent(in) :: render_legacy
     end subroutine
     subroutine partintft8(ndelay,nutc,render_legacy)
       integer :: ndelay,nutc
       logical, optional, intent(in) :: render_legacy
     end subroutine
     subroutine ft8apsetvar(lmycallstd,lhiscallstd,numthreads,knowledge)
       import pack77_state
       logical(1), intent(in) :: lmycallstd,lhiscallstd
       integer :: numthreads
       type(pack77_state), target, optional, intent(inout) :: knowledge
     end subroutine
     subroutine fillhashvar(numthreads,lfill,knowledge)
       import pack77_state
       integer, intent(in) :: numthreads
       logical, intent(in) :: lfill
       type(pack77_state), target, optional, intent(inout) :: knowledge
     end subroutine
  end interface

contains

  subroutine reset_ft8_kernel(state)
    use ft8_mod1, only: reset_ft8_mod1
    use ft8_a7, only: reset_ft8_a7
    type(ft8_kernel_state), intent(inout) :: state
    call state%my_ft8%reset()
    state%my_ft8%decoded=0
    state%my_ft8%first_result=.true.
    state%my_ft8%day_wrap=0
    state%my_ft8var%decodedvar=0
    state%my_ft8var%first_result=.true.
    state%my_ft8var%day_wrap=0
    state%my_ft8var%xdtt=0.
    state%first=.true.
    state%mtd_residual_ready=.false.
    state%firstsd=.true.
    state%lhoundprev=.false.
    state%nutc=0
    state%ndelay=0
    state%ntr0=-1
    state%nintcount=0
    state%nwrap=0
    state%ndec41=0
    state%ndec46=0
    state%ndec47=0
    state%ndec48=0
    state%ndec49=0
    c2fox=''
    g2fox=''
    nsnrfox=-99
    nfreqfox=-99
    n30fox=0
    n30z=0
    nfox=0
    ltry_a8=.true.
    call reset_ft8_mod1()
    call reset_ft8_a7()
    call reset_ft8apsetvar()
  end subroutine

  subroutine release_ft8_input(state)
    type(ft8_kernel_state), intent(inout) :: state
    call state%my_ft8%release_input()
    state%mtd_residual_ready=.false.
    state%ndec41=0
    state%ndec46=0
    state%ndec47=0
    state%ndec48=0
    state%ndec49=0
  end subroutine release_ft8_input

  subroutine run_ft8_kernel(state,knowledge,id2,params,completion,progress_generation,callback_context)
    type(ft8_kernel_state), intent(inout) :: state
    type(pack77_state), target, intent(inout) :: knowledge
    integer(c_short), intent(inout) :: id2(180000)
    type(params_block), intent(inout) :: params
    type(decode_completion_result), intent(out) :: completion
    integer, intent(in) :: progress_generation
    type(decoder_callback_context), intent(in) :: callback_context
    type(decoder_callback_context) :: context
    integer :: ft8_range_low(max_ft8_decode_ranges),ft8_range_high(max_ft8_decode_ranges)
    integer :: ft8_range_count,requested_threads,nthr,active_progress_generation
    integer :: ncontest,nfqso
    logical :: newdat,ex
    character(len=6) :: mygrid
    character(len=60) :: line

    associate(my_ft8=>state%my_ft8,my_ft8var=>state%my_ft8var,first=>state%first, &
         firstsd=>state%firstsd,lhoundprev=>state%lhoundprev,nutc=>state%nutc, &
         ndelay=>state%ndelay,ntr0=>state%ntr0,nintcount=>state%nintcount, &
         nwrap=>state%nwrap,ndec41=>state%ndec41,ndec46=>state%ndec46, &
         ndec47=>state%ndec47,ndec48=>state%ndec48,ndec49=>state%ndec49)
    call reset_decode_completion(completion)
    active_progress_generation=progress_generation
    context=callback_context
    ncontest=iand(params%nexp_decode,7)
    nsynced=0
    navg0=0
    my_ft8%decoded=0
    my_ft8var%decodedvar=0
    my_ft8var%xdtt=0.
    my_ft8%knowledge=>knowledge
    my_ft8var%knowledge=>knowledge
    my_ft8var%callback=>ft8_decodedvar

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

    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)
    ncandallthr=0
    if(params%nzhsym.eq.41 .or. params%lmultift8) ltry_a8=.true.

    if(context%render_legacy) then
       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.'-') exit
                write(*,'(a)') trim(line)
             enddo
5            close(39)
          endif
          go to 800
       endif
       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
       context%ios13=ios13
    endif
    context%nutc=params%nutc
    context%nfqso=params%nfqso
    context%ncontest=ncontest
    context%bVHF=iand(params%nexp_decode,64).ne.0
    context%b_superfox=params%b_superfox
    context%mycall=mycall
    my_ft8%context=context
    my_ft8var%context=context

     if(ncontest.eq.6 .and. context%render_legacy) then
        ! 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_decode(knowledge,context%superfox_sink,context%sink_user, &
             params%yymmdd,params%nutc,params%nfqso,params%ntol,id2)
     else
        call timer('decft8  ',0)
        if(params%nfa.gt.params%nfb) then
           call timer('decft8  ',1)
           go to 800
        endif
        newdat=params%newdat
        if(params%emedelay.ne.0.0.and..not.params%lmultift8) then
           id2(1:156000)=id2(24001:180000)  ! Drop the first 2 seconds of data
           id2(156001:180000)=0
           my_ft8%context%waveform_input_offset_seconds=2.0
        endif

! ft8md below
        if(params%lmultift8 .and. params%nmode.eq.8) then
           ! Disk retries continue subtraction on this input; live attempts start with fresh audio.
           if(.not.(params%ndiskdat.and.params%nagain.and.state%mtd_residual_ready)) dd8=real(id2)
           if(params%lmodechanged) then
              avexdt=0.
              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,context%render_legacy)
              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,knowledge,context%render_legacy)
              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)
           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,knowledge)
                 hiscall12_0=hiscall
                 mycall12_0=mycall
              endif
              lhoundprev=lhound
           endif
           if(params%lmycallstd .and. mycall.ne.'            ' .and.      &
                mycall12_00.ne.mycall) then
              call tone8myc(knowledge)
              mycall12_00=mycall
           endif

           ndecodes=0            !Initialize arrays for multi-threaded decoding
           allmessages=""
           allsnrs=0
           allfreq=0.
           numcores=1
!$         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_max_active_levels(omp_get_supported_active_levels())

           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
                 if(context%render_legacy) 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

           call partition_ft8_decode_range(nfa,nfb,numthreads,ft8_range_low, &
                ft8_range_high,ft8_range_count)
           if(ft8_range_count.eq.0) then
              call timer('decft8  ',1)
              go to 800
           endif
           numthreads=ft8_range_count
           requested_threads=numthreads

           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

           if(hiscall.eq.'') then
              lastrxmsg(1)%lstate=.false.
           else if(lastrxmsg(1)%lstate .and. lasthcall.ne.hiscall .and.        &
                index(lastrxmsg(1)%lastmsg,trim(hiscall)).le.0) then
              lastrxmsg(1)%lstate=.false.
           endif

!$omp parallel num_threads(requested_threads) private(nthr) shared(numthreads)
!$omp single
           numthreads=1
!$         numthreads=omp_get_num_threads()
! Reduced teams cover wider frequency slices, so fixed per-worker candidate limits can lower crowded-band yield.
           call partition_ft8_decode_range(nfa,nfb,numthreads,ft8_range_low, &
                ft8_range_high,ft8_range_count)
           numthreads=ft8_range_count
           call mtd_prepare(dd8,numthreads)
           call fillhashvar(numthreads,.false.,knowledge)
           call ft8apsetvar(params%lmycallstd,params%lhiscallstd,numthreads,knowledge)
!$omp end single

           nthr=1
!$         nthr=omp_get_thread_num()+1
           call my_ft8var%decodevar(params%nQSOProgress,nfqso,                &
                params%nft8rxfsens,params%nftx,ft8_range_low(nthr),           &
                ft8_range_high(nthr),params%ncandthin,params%ndtcenter,nsec,  &
                params%napwid,params%lmycallstd,params%lhiscallstd,           &
                params%nstophint,nthr,numthreads,logical(params%nagainfil),   &
                params%lft8lowth,params%lft8subpass,params%lhideft8dupes,     &
                active_progress_generation,                                  &
                mtd_worker_residual(:,nthr),                                  &
                mtd_worker_spectrum(:,nthr))
!$omp end parallel

           call write_decode_progress(active_progress_generation)
           call mtd_finish(dd8)
           state%mtd_residual_ready=.true.

           call write_decode_progress(active_progress_generation)
           call run_ft8_mtd_a8_decode()
           call write_decode_progress(active_progress_generation)

           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.,knowledge)
           call write_decode_progress(active_progress_generation)
           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

        if(context%render_legacy) rewind 19
        if(nfox.eq.0) then
           if(context%render_legacy) endfile 19
           if(context%render_legacy) 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
                 if(context%render_legacy) 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
           if(context%render_legacy) flush(19)
        endif
     endif


800 ndecoded=my_ft8%decoded+my_ft8var%decodedvar
  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
     call set_decode_completion(completion,nsynced,ndecoded,navg0)
     call write_decode_progress(active_progress_generation)
  endif
  if(context%render_legacy) close(13)
  if(ncontest.eq.6 .and. context%render_legacy) close(19)
  end associate


  contains
  subroutine run_ft8_mtd_a8_decode()
    implicit none

    real f1,xdt,fbest,xsnr,plog,qual
    integer nsnr,iaptype
    character(len=6) dxgrid
    character(len=37) msg37
    type(ft8_signal_evidence) :: evidence

    if(.not.params%lft8apon) return
    if(ncontest.eq.6 .or. ncontest.eq.7) return
    if(len(trim(hiscall)).lt.3 .or. len(trim(hisgrid4)).lt.4) return
    if(.not.ltry_a8) return

    f1=nfqso
    dxgrid=hisgrid4
    call timer('ft8_a8d ',0)
    call ft8_a8d(dd8,mycall,hiscall,dxgrid,f1,xdt,fbest,xsnr,plog,msg37, &
         active_progress_generation,knowledge,evidence)
    call timer('ft8_a8d ',1)

    if(msg37(1:1).ne.' ') then
       if(associated(state%my_ft8var%callback)) then
          nsnr=nint(xsnr)
          iaptype=8
          qual=1.0
          if(plog.lt.-147.0) qual=0.16
          call state%my_ft8var%callback(nsnr,xdt,fbest,msg37,iaptype,qual,evidence)
       endif
    endif
  end subroutine run_ft8_mtd_a8_decode

  end subroutine run_ft8_kernel
end module ft8_engine_kernel