summaryrefslogtreecommitdiff
path: root/map65/libm65/decode0.f90
blob: 1e217501b68937b72b33d5ebab6a36eb922ae985 (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
module decode0_mod
   implicit none
contains

   subroutine decode0(nstandalone) bind(C, name='decode0_')

      use iso_c_binding, only: c_int
      use timer_module, only: timer
      use npar_ptrs_mod
      use datcom_ptrs_mod, only: NFFT, dd_old, dd_use, ss_old, savg_old, ss_use, savg_use, snap
      use debug_log, only: dbg, itoa, rtoa
      use sec_midn_mod, only: sec_midn
      use map65a_mod
      use stdout_channel_mod, only: write_stdout
      use decodes_mod, only: ndecodes, nhsym1, nhsym2
      use sec0_mod, only:sec0

      implicit none

      integer(c_int), intent(in) :: nstandalone
      integer hist(0:32768)
      integer i, j1, j2, j3, j4, m, mcall3b, ndphi, neme0, nsum
      integer :: ndecdone
      real :: tdec, tquick, rmsdd
      integer nz
      integer :: nhsym0
      integer :: newdat0
      integer :: isnap
      character(len=128) :: line
      character mycall0*12, hiscall0*12, hisgrid0*6
      logical :: first_mcall3b_check

      data neme0/-99/, mcall3b/0/, mycall0/'            '/, hiscall0/'            '/, hisgrid0/'      '/
      data first_mcall3b_check/.true./

      save

      ! The decoder owns the claimed parameters until this call returns.
      nhsym0 = nhsym
      newdat0 = newdat

      ! TEMP diagnostic 2026-09-10 for the missing-final-pass / incomplete-
      ! data-set investigation.
      call dbg('decode0: ENTRY at t=' // rtoa(sec_midn()) // &
               ' nhsym0=' // itoa(nhsym0) // ' newdat0=' // itoa(newdat0) // &
               ' manualDecodeFlag=' // itoa(manualDecodeFlag) // &
               ' nagain=' // itoa(nagain) // ' ndiskdat=' // itoa(ndiskdat))

      ! newdat is forced to 1 by decode() for every call (manual or
      ! automatic); it does not by itself mean "the live buffers just
      ! finished a fresh accumulation."
      !
      ! Each automatic pass decodes its own snapshot, taken by dataSink() at
      ! the instant the pass was triggered (snap(1) at nhsym1, snap(2) at
      ! nhsym2). decode0 never copies from the live buffers, so a pass that
      ! starts late (queued behind a still-running pass) cannot see data the
      ! audio thread has already reset for the next cycle. Manual repeats
      ! (nagain=1, Decode button / Find Delta Phi) reuse the most recently
      ! filled snapshot.
      isnap = active_snap_idx             ! slot claimed with this request
      if (isnap < 1) isnap = 2
      call dbg('decode0: using snapshot ' // itoa(isnap) // ' at t=' // rtoa(sec_midn()) // &
               ' nhsym0=' // itoa(nhsym0) // ' nagain=' // itoa(nagain))
      dd_old   => snap(isnap)%dd
      ss_old   => snap(isnap)%ss
      savg_old => snap(isnap)%savg

      nkeep = min(max(ntimeout, 10), 40)

      call sec0(0, tquick)
      call timer('decode0 ', 0)
      
      ! Always decode from the snapshot selected above (dd_old/ss_old/savg_old),
      ! never the live dd/ss/savg. The nhsym2 pass routinely runs past the
      ! minute boundary (several seconds), and symspec_() on the GUI thread
      ! resets/overwrites the live buffers for the next cycle unconditionally
      ! as soon as new UDP data arrives -- with no lock between the two
      ! threads. Without this, a still-running decode reads a mix of this
      ! cycle's and next cycle's data for candidates it hasn't reached yet
      ! in its frequency sweep.
      dd_use   => dd_old
      ss_use   => ss_old
      savg_use => savg_old

      if (newdat0 .ne. 0 .or. manualDecodeFlag /= 0) then
         nz = int( real(nrate_active, kind=real64) * nhsym0 / 5.3833_real64 )
         hist = 0
         do i = 1, nz
            j1 = int(min(abs(dd_use(1, i)), 32768.0))
            hist(j1) = hist(j1) + 1
            j2 = int(min(abs(dd_use(2, i)), 32768.0))
            hist(j2) = hist(j2) + 1
            j3 = int(min(abs(dd_use(3, i)), 32768.0))
            hist(j3) = hist(j3) + 1
            j4 = int(min(abs(dd_use(4, i)), 32768.0))
            hist(j4) = hist(j4) + 1
         enddo
         m = 0
         do i = 0, 32768
            m = m + hist(i)
            if (m .ge. 2*nz) go to 10
         enddo
10       rmsdd = 1.5*i
      endif
      ndphi = 0
      if (iand(nrxlog, 8) .ne. 0) ndphi = 1

      ! Seed mycall0/hiscall0/hisgrid0/neme0 from the real values on the
      ! first call instead of blank sentinels, so the mismatch check below
      ! only fires on a genuine later change, not on startup.
      if (first_mcall3b_check) then
         mycall0 = mycall
         hiscall0 = hiscall
         hisgrid0 = hisgrid
         neme0 = neme
         first_mcall3b_check = .false.
      endif

      if (mycall .ne. mycall0 .or. hiscall .ne. hiscall0 .or. &
          hisgrid .ne. hisgrid0 .or. mcall3 .ne. 0 .or. neme .ne. neme0) mcall3b = 1

      mycall0 = mycall
      hiscall0 = hiscall
      hisgrid0 = hisgrid
      neme0 = neme

      ! Manual decode: force a single nhsym2-style cycle and always emit DecodeFinished
      if (manualDecodeFlag /= 0) then
         nhsym0 = nhsym2
         call announce_decode_pass('manual')

         call timer('map65a  ', 0)
         call map65a(dd_use, newdat0, nutc, fcenter, ntol, idphi, nfa, nfb, &
                     mousedf, mousefqso, nagain, ndecdone, nfshift, ndphi, max_drift, &
                     nfcal, nkeep, mcall3b, nsum, nsave, nxant, mycall, mygrid, &
                     neme, ndepth, nstandalone, hiscall, hisgrid, nhsym0, nfsample, &
                     ndiskdat, nxpol, nmode, ndop00)
         call timer('map65a  ', 1)
         call timer('decode0 ', 1)

         call sec0(1, tdec)

         write (line, '("<DecodeFinished>",3I4,I6,F6.2,I5,1X,I0)') &
            nsum, nsave, nstandalone, nhsym0, tdec, ndecodes, active_request_id
         call write_stdout(trim(line)//new_line('a'))

         call dbg('decode0: MANUAL path RETURNING at t=' // rtoa(sec_midn()))

         return

      else

      ! Normal wideband path: nhsym1 (EarlyFinished) + nhsym2 (DecodeFinished)
      if (ndiskdat /= 0) then
         call announce_decode_pass('disk')
      else if (nhsym0 == nhsym1) then
         call announce_decode_pass('early')
      else if (nhsym0 == nhsym2) then
         call announce_decode_pass('final')
      else
         call announce_decode_pass('unknown')
      endif
      call timer('map65a  ', 0)
      call map65a(dd_use, newdat0, nutc, fcenter, ntol, idphi, nfa, nfb, &
                  mousedf, mousefqso, nagain, ndecdone, nfshift, ndphi, max_drift, &
                  nfcal, nkeep, mcall3b, nsum, nsave, nxant, mycall, mygrid, &
                  neme, ndepth, nstandalone, hiscall, hisgrid, nhsym0, nfsample, &
                  ndiskdat, nxpol, nmode, ndop00)
      call timer('map65a  ', 1)
      call timer('decode0 ', 1)

      call sec0(1, tdec)

      if (nhsym0 == nhsym1) then
         write (line, '("<EarlyFinished>",3I4,I6,F6.2)') &
            nsum, nsave, nstandalone, nhsym0, tdec
         call write_stdout(trim(line)//new_line('a'))
      end if

      ! A manual repeat decode (Decode button / Find Delta Phi) is always a
      ! single, complete request regardless of nhsym, which only reflects
      ! the automatic per-minute cycle and may never equal nhsym2 if no
      ! automatic decode has run yet this session.
      if (nhsym0 == nhsym2 .or. nagain /= 0) then
         write (line, '("<DecodeFinished>",3I4,I6,F6.2,I5,1X,I0)') &
            nsum, nsave, nstandalone, nhsym0, tdec, ndecodes, active_request_id
         call write_stdout(trim(line)//new_line('a'))
      end if

      call dbg('decode0: WIDEBAND path RETURNING at t=' // rtoa(sec_midn()))

      return
   end if
   end subroutine decode0

   subroutine announce_decode_pass(pass_name)
      use stdout_channel_mod, only: write_stdout
      implicit none

      character(len=*), intent(in) :: pass_name

      call write_stdout('<Map65DecodePass> '//trim(pass_name)//new_line('a'))
   end subroutine announce_decode_pass

end module decode0_mod