summaryrefslogtreecommitdiff
path: root/lib/iscat.f90
blob: 17d1454f5099ce302796b9da7801f9349513dc8b (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
subroutine iscat(cdat0,npts0,nh,npct,t2,pick,cfile6,minsync,ntol,   &
     mousebutton,mode4,nafc,nmore,psavg,maxlines,nlines,line)

! Decode an ISCAT signal

  parameter (NMAX=30*3101)
  parameter (NSZ=4*1400)
  character cfile6*6                      !File time
  character c42*42
  character msg*29,msg1*29,msgbig*29
  character*80 line(100)
  character csync*1
  complex cdat0(NMAX)
  complex cdat(NMAX)
  real s0(288,NSZ)
  real fs1(0:41,30)
  real psavg(72)                          !Average spectrum of whole file
  integer nsum(30)
  integer ntol
  integer icos(4)
  logical pick,last
  data icos/0,1,3,2/
  data nsync/4/,nlen/2/,ndat/18/
  data c42/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ /.?@-'/
  save cdat,s0

  nlines = 0
  fsample=3100.78125                   !Sample rate after 9/32 downsampling
  nsps=144/mode4

  bigworst=-1.e30                      !Silence compiler warnings ...
  bigxsync=0.
  bigsig=-1.e30
  msglenbig=0
  ndf0big=0
  nfdotbig=0
  bigt2=0.
  bigavg=0.
  bigtana=0.
  if(nmore.eq.-999) bigsig=-1         !... to here

  last=.false.
  do inf=1,6                           !Loop over data-segment sizes
     nframes=2**inf
     if(nframes*24*nsps.gt.npts0) then
        nframes=npts0/(24*nsps)
        last=.true.
     endif
     npts=nframes*24*nsps

     do ia=1,npts0-npts,nsps*24        !Loop over start times stepped by 1 frame
        ib=ia+npts-1
        cdat(1:npts)=cdat0(ia:ib)
        t3=(ia + 0.5*npts)/fsample + 0.9 
        if(pick) t3=t2+t3

! Compute symbol spectra and establish sync:
        call synciscat(cdat,npts,nh,npct,s0,jsym,df,ntol,           &
             mousebutton,mode4,nafc,psavg,xsync,sig,ndf0,msglen,    &
             ipk,jpk,idf,df1)
        nfdot=nint(idf*df1)

        isync=xsync
        if(msglen.eq.0 .or. isync.lt.max(minsync,0)) then
           msglen=0
           worst=1.
           avg=1.
           ndf0=0
           cycle
        endif

        ipk3=0                                  !Silence compiler warning
        nblk=nsync+nlen+ndat
        fs1=0.
        nsum=0
        nfold=jsym/96
        jb=96*nfold
        k=0
        n=0
        do j=jpk,jsym,4                !Fold information symbols into fs1
           k=k+1
           km=mod(k-1,nblk)+1
           if(km.gt.6) then
              n=n+1
              m=mod(n-1,msglen)+1
              ii=nint(idf*float(j-jb/2)/float(jb))
              do i=0,41
                 iii=ii+ipk+2*i
                 if(iii.ge.1 .and. iii.le.288) fs1(i,m)=fs1(i,m) + s0(iii,j)
              enddo
              nsum(m)=nsum(m)+1
           endif
        enddo

        do m=1,msglen
           fs1(0:41,m)=fs1(0:41,m)/nsum(m)
        enddo

! Read out the message contents:
        msg= '                             '
        msg1='                             '
        mpk=0
        worst=9999.
        sum=0.
        do m=1,msglen
           smax=0.
           smax2=0.
           do i=0,41
              if(fs1(i,m).gt.smax) then
                 smax=fs1(i,m)
                 ipk3=i
              endif
           enddo
           do i=0,41
              if(fs1(i,m).gt.smax2 .and. i.ne.ipk3) smax2=fs1(i,m)
           enddo
           rr=0.
           if(smax2.gt.0.0) rr=smax/smax2
           sum=sum + rr
           if(rr.lt.worst) worst=rr
           if(ipk3.eq.40) mpk=m
           msg1(m:m)=c42(ipk3+1:ipk3+1)
        enddo

        avg=sum/msglen
        if(mpk.eq.1) then
           msg=msg1(2:)
        else if(mpk.lt.msglen) then
           msg=msg1(mpk+1:msglen)//msg1(1:mpk-1)
        else
           msg=msg1(1:msglen-1)
        endif

        ttot=npts/3100.78125

        if(worst.gt.bigworst) then
           bigworst=worst
           bigavg=avg
           bigxsync=xsync
           bigsig=sig
           ndf0big=ndf0
           nfdotbig=nfdot
           msgbig=msg
           msglenbig=msglen
           bigt2=t3
           bigtana=nframes*24*nsps/fsample
        endif

        isync = xsync
        if(avg.gt.2.5 .and. xsync.ge.max(float(minsync),1.5) .and. &
             maxlines.ge.2) then
           nsig=nint(sig)
           nworst=10.0*(worst-1.0)
           navg=10.0*(avg-1.0)
           if(nworst.gt.10) nworst=10
           if(navg.gt.10) navg=10
           tana=nframes*24*nsps/fsample
           csync=' '
           if(isync.ge.1) csync='*'
           if(nlines.le.maxlines-1) nlines = nlines + 1
           write(line(nlines),1020) cfile6,isync,nsig,t2,ndf0,nfdot,csync, &
                msg(1:28),msglen,navg,nworst,tana,char(0)
        endif
     enddo
     if(last) exit
  enddo
  
  worst=bigworst
  avg=bigavg
  xsync=bigxsync
  sig=bigsig
  ndf0=ndf0big
  nfdot=nfdotbig
  msg=msgbig
  msglen=msglenbig
  t2=bigt2
  tana=bigtana

  isync=xsync
  nworst=10.0*(worst-1.0)
  navg=10.0*(avg-1.0)
  if(nworst.gt.10) nworst=10
  if(navg.gt.10) navg=10

  if(navg.le.0 .or. isync.lt.max(minsync,0)) then
     msg='                             '
     nworst=0
     navg=0
     ndf0=0
     nfdot=0
     sig=-20
     msglen=0
     tana=0.
     t2=0.
  endif
  csync=' '
  if(isync.ge.1) csync='*'
  nsig=nint(sig)
  
  if(nlines.le.maxlines-1) nlines = nlines + 1
  write(line(nlines),1020) cfile6,isync,nsig,t2,ndf0,nfdot,csync,msg(1:28),  &
       msglen,navg,nworst,tana,char(0)
1020 format(a6,2i4,f5.1,i5,i4,1x,a1,2x,a28,i4,i3,2x,i1,f5.1,a1)

  return
end subroutine iscat