summaryrefslogtreecommitdiff
path: root/lib/ft8var/ft8sdvar.f90
blob: 8eaf7e9a64391f0fdc69a5159ab82ec9dd31ce1e (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
subroutine ft8sdvar(s8,srr,itone,msgd,msg37,lft8sd,lcq,knowledge)

  use packjt77, only: pack77_state
  use ft8_codec_context, only: get_ft8_codec_state
  use ft8var_codec_context, only: genft8sdvar_for_state

  use ft8_mod1, only : mycall
  real, intent(in) :: s8(0:7,79)
  real s8_1(0:7,79)
  character msgd*37,msg37*37,msg372*37,msgsent37*37,c1*12,c2*12
  character*37 msg4(4)
  integer idtone4(4,58),itone4(4,79)
  integer itone(79),itonedem(58),idtone(58),ip1(1)
  integer*1 msgbits(77)
  logical(1), intent(in) :: lcq
  logical(1) lft8sd,lmatched(58),valid4(4)
  type(pack77_state), target, optional, intent(inout) :: knowledge
  type(pack77_state), pointer :: codec

  codec => get_ft8_codec_state()
  if(present(knowledge)) codec => knowledge

  if(index(msgd,' RR73').gt.0 .or. index(msgd,' 73').gt.0) return ! do not process 73 messages
! do not process messages with grid
  if(.not.(msgd(1:3).eq.'CQ ' .or. index(msgd,'-').gt.0 .or. index(msgd,'+').gt.0 .or. index(msgd,' RRR').gt.0)) return

  lmatched=.false.; s8_1=s8; itonedem=11

  if(.not.lcq) then
    c1='            '; c2='            '
    ispc1=index(msgd,' '); ispc2=index(msgd((ispc1+1):),' ')+ispc1
    if(len(msgd(1:ispc1-1)).le.12 .and. len(msgd(ispc1+1:ispc2-1)).le.12) then
      c1=msgd(1:ispc1-1); c2=msgd(ispc1+1:ispc2-1)
    else
      return
    endif
    if(len_trim(c1).lt.3 .or. len_trim(c2).lt.3 .or. c2.eq.trim(mycall)) return
    do i=1,4
      msg4(i)='                                     '
      if(i.eq.1) msg4(i)=msgd
      if(i.eq.2) msg4(i)=trim(c1)//' '//trim(c2)//' RRR'
      if(i.eq.3) msg4(i)=trim(c1)//' '//trim(c2)//' RR73'
      if(i.eq.4) msg4(i)=trim(c1)//' '//trim(c2)//' 73'
    enddo
  endif

  if(lcq) then
    i3=-1; n3=-1
    msg372=msgd
    call genft8sdvar_for_state(codec,msg372,i3,n3,msgsent37,msgbits,itone)
    if(i3.lt.0) return
    idtone(1:29)=itone(8:36)
    idtone(30:58)=itone(44:72)
  else
    valid4=.false.
    do i=1,4
      msg372=msg4(i)
      i3=-1; n3=-1
      call genft8sdvar_for_state(codec,msg372,i3,n3,msgsent37,msgbits,itone)
      if(i3.lt.0) cycle
      valid4(i)=.true.
      idtone4(i,1:29)=itone(8:36)
      idtone4(i,30:58)=itone(44:72)
      itone4(i,1:79)=itone(1:79)
    enddo
  endif

  do i=1,58
    if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
    else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
  enddo

  imax=0
  if(lcq) then
    nmatch1=0; ncrcpaty1=0
    do k=1,58
      if(idtone(k).eq.itonedem(k)) then; nmatch1=nmatch1+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty1=ncrcpaty1+1; endif
    enddo
    if(nmatch1.gt.26) then; lft8sd=.true.; msg37=msgd; return; endif
  else
    nmatchditer1=0; nbaseiter1=0; ncrcpatyiter1=0;
    do i=1,4
      if(.not.valid4(i)) cycle
      nmatch1=0; nbase1=0; ncrcpaty1=0
      do k=1,58
        if(idtone4(i,k).eq.itonedem(k)) then
          nmatch1=nmatch1+1; if(k.lt.23) nbase1=nbase1+1; if(k.gt.25) ncrcpaty1=ncrcpaty1+1 ! CRC+parity
        endif
      enddo
      if(nmatch1.gt.nmatchditer1) then
        imax=i; nmatchditer1=nmatch1; nbaseiter1=nbase1; ncrcpatyiter1=ncrcpaty1
      endif
    enddo
    if(imax.eq.0) return
    if(srr.gt.3.0 .and. nbaseiter1.lt.12) return ! prevent false decodes from the strong signals
    nmatch1=nmatchditer1; ncrcpaty1=ncrcpatyiter1!; nbase1=nbaseiter1
    idtone(1:58)=idtone4(imax,1:58)
    if(nmatch1.gt.26 .and. ncrcpaty1.gt.10) then
      lft8sd=.true.; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); return
    endif
    do k=1,58
      if(idtone(k).eq.itonedem(k)) lmatched(k)=.true. 
    enddo
  endif

  if(nmatch1.ge.16) then
    do i=1,58
      if(lmatched(i)) cycle
      if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
      else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
    enddo
    nmatch2=nmatch1; ncrcpaty2=ncrcpaty1
    do k=1,58
      if(lmatched(k)) cycle
      if(idtone(k).eq.itonedem(k)) then; nmatch2=nmatch2+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty2=ncrcpaty2+1; endif
    enddo
    if(nmatch2.gt.38 .and. ncrcpaty2.gt.19) then
      lft8sd=.true.
      if(lcq) then; msg37=msgd; else; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); endif
      return
    endif

    do i=1,58
      if(lmatched(i)) cycle
      if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
      else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
    enddo
    nmatch3=nmatch2; ncrcpaty3=ncrcpaty2
    do k=1,58
      if(lmatched(k)) cycle
      if(idtone(k).eq.itonedem(k)) then; nmatch3=nmatch3+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty3=ncrcpaty3+1; endif
    enddo
    if(nmatch3.gt.44 .and. ncrcpaty3.gt.21) then
      lft8sd=.true.
      if(lcq) then; msg37=msgd; else; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); endif
      return
    endif

    do i=1,58
      if(lmatched(i)) cycle
      if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
      else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
    enddo
    nmatch4=nmatch3; ncrcpaty4=ncrcpaty3
    do k=1,58
      if(lmatched(k)) cycle
      if(idtone(k).eq.itonedem(k)) then; nmatch4=nmatch4+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty4=ncrcpaty4+1; endif
    enddo
    if(nmatch4.gt.47 .and. ncrcpaty4.gt.23) then
      lft8sd=.true.
      if(lcq) then; msg37=msgd; else; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); endif
      return
    endif

    do i=1,58
      if(lmatched(i)) cycle
      if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
      else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
    enddo
    nmatch5=nmatch4; ncrcpaty5=ncrcpaty4
    do k=1,58
      if(lmatched(k)) cycle
      if(idtone(k).eq.itonedem(k)) then; nmatch5=nmatch5+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty5=ncrcpaty5+1; endif
    enddo
    if(nmatch5.gt.50 .and. (nmatch1.gt.21 .or. nmatch2.gt.31 .or. nmatch3.gt.38 .or. nmatch4.gt.46) .and. &
       ncrcpaty5.gt.25) then
      lft8sd=.true.
      if(lcq) then; msg37=msgd; else; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); endif
      return
    endif

    do i=1,58
      if(lmatched(i)) cycle
      if(i.le.29) then; ip1=maxloc(s8_1(:,i+7))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+7)=0.0
      else; ip1=maxloc(s8_1(:,i+14))-1; itonedem(i)=ip1(1); s8_1(ip1(1),i+14)=0.0; endif
    enddo
    nmatch6=nmatch5; ncrcpaty6=ncrcpaty5
    do k=1,58
      if(lmatched(k)) cycle
      if(idtone(k).eq.itonedem(k)) then; nmatch6=nmatch6+1; lmatched(k)=.true.; if(k.gt.25) ncrcpaty6=ncrcpaty6+1; endif
    enddo
    if(nmatch6.gt.54 .and. (nmatch1.gt.22 .or. nmatch2.gt.27 .or. nmatch3.gt.35 .or. &
       (nmatch2-nmatch1).gt.9 .or. (nmatch3-nmatch2).gt.10) .and. ncrcpaty6.gt.29) then
      lft8sd=.true.
      if(lcq) then; msg37=msgd; else; msg37=msg4(imax); itone(1:79)=itone4(imax,1:79); endif
      return
    endif

  endif

  return
end subroutine ft8sdvar