pro chekov,ichn,flag,dp_sync,arr0,arr1,deb
;+
; Procedure to try and correct dp_sync.xxx_cnts for bad telemetry data and  for
; overflow in the 16 bit counters.    NB this routine sets bad data  points and
; their acc_interval to zero and corrects the rest for the acc_interval. Hence
; any counts returned are nominally /sec.  If a data set begins in medium bit
; rate, this section will not be corrected. It is far from guaranteed that this
; routine  will have the desired effect on all data sets!  
;
;  INPUT parameters:
;
;  ichn    - the channel number of counts to be corrected (0:3!)
;  flag    - which set of counts to correct 1=all_cnts, 2=lim_cnts, 3=acc_cnts
;  dp_sync - structure array as read by TEST_RD
;  deb     - if six parameters are present some debugging o/p is given
; 
;  OUTPUT parameters:
;
;  arr0 - long integer counts/second, first count of dp_sync pair
;  arr1 - ............................second ....................
;
;  dp_sync is still changed in that the acc_interval is set to zero for
;          data the routine considers bad. The dp_sync counts are not
;          now affected.
;
;  Use:
;         IDL> chekov,ichn,flag,dp_sync,arr0,arr1,deb
;
;                                                                
;  CDP  Nov 91 
;
;   Version 1     November 91
;
;   Version 2     10/12/91 temporary skip of correcting high rate
;                 regions. It was getting fooled by occasional, not very
;                 clean data drop outs. ie count would fall to zero but
;                 over the course of 1 or 2 data points thus simulating an
;                 overflow. This then triggered correction for subsequent
;                 data. 
;
;   Version 3     January 92
;                 Bug fix to cope with transitions directly from low rate
;                 to high. Transitions to low rate are ignored at present.
;
;           3.1   Bug fix to cope with only one transition to High from med
;                 March 92   CDP
;
;-
; ------------------------------------------------------------------------
;
; get rid of negative numbers and store in long word for ease  
;
if flag eq 1 then begin
   arr0 = long(unsign(dp_sync.all_cnts(ichn,0),16)) 
   arr1 = long(unsign(dp_sync.all_cnts(ichn,1),16))
endif
if flag eq 2 then begin
   arr0 = long(unsign(dp_sync.lim_cnts(ichn,0),16)) 
   arr1 = long(unsign(dp_sync.lim_cnts(ichn,1),16))
endif
if flag eq 3 then begin
   arr0 = long(unsign(dp_sync.acc_cnts(ichn,0),16)) 
   arr1 = long(unsign(dp_sync.acc_cnts(ichn,1),16))
endif
;
;  get rid of rubbish pairs by eliminating those with excessive differences
;  using all_cnts as the guide
;
zd = arr0 - arr1
xx = where(abs(zd) gt 2000)
if xx(0) ne -1 then begin
   dp_sync(xx).acc_interval(*) = 0
   arr0(xx) = 0L
   arr1(xx) = 0L
endif
;
;  find switches of dp_rate from hi->medium and vv
;
high = intarr(500)
med  = intarr(500)
;
;  differentiate rate array (catch switches to high from either med or
;  low in one step (what about the other way round?)
; 
dd   = dp_sync.dp_rate - shift(dp_sync.dp_rate,-1)
med = where(dd eq 64) + 1
high  = where(dd eq 192 or dd eq 160) + 1
;
;   cut out spurious end point
;   and check for single transitions
;
ff = where(med lt n_elements(dp_sync))
if n_elements(ff) eq 1 then begin
   if ff(0) eq -1 then return
endif
med = med(ff)
ff = where(high lt n_elements(dp_sync))
if n_elements(ff) eq 1 then begin
   if ff(0) eq -1 then return
endif
high = high(ff)

if (n_elements(high) eq 1) then begin
   if high(0) eq 0 then begin
;      print,'No rate change all high.'
      high = [0,n_elements(arr0)-1]
;      chekov_nrc,dp_sync,high,arr0,arr1
      return
   endif
endif
if (n_elements(med) eq 1) then begin
   if med(0) eq 0 then begin
;      print,'No rate change all medium.'
      med = [0,n_elements(arr0)-1]
;      chekov_nrc,dp_sync,med,arr0,arr1
      return
   endif
endif
   
;
; add the first element (why originally was it (-2)?)
;
dd = where(dp_sync.dp_rate ne 0)
if med(0) lt high(0) then begin
   high = [1,high(0:n_elements(high)-1)]
   if n_elements(high) eq n_elements(med) then begin
      high = [high,n_elements(dp_sync)-1]
   endif
endif else begin
   med  = [1,med(0:n_elements(med)-1)]
   if n_elements(high) lt n_elements(med) then begin
      high = [high,n_elements(dp_sync)-1]
   endif
endelse
;
;  now start the correction process
;  order of process depends on initial mode
;
if high(0) lt med(0) then begin
   start_jj = 0
   step = 1
endif else begin
   start_jj = 1
   step = 0
endelse


;
;  debug
;
if n_params() eq 6 then begin
   print,'High steps are at:    ',high
   print,'Medium steps are at : ',med
   print,'So will start correcting from ',med(start_jj),' to ',$
          high(start_jj+step)-1
endif
;
;  correct cnts
;
for jj = start_jj,n_elements(med)-1 do begin
   n = 1
   last_high = 0
   while last_high eq 0 and (med(jj)-n) gt 0  do begin
     last_high = arr0(med(jj)-n)  
     n = n + 1
   endwhile

;
;  expected medium bit rate value judged from last high rate one
;
   expect_med = 8L * last_high

   for i=med(jj),high(jj+step)-1 do begin
      if arr0(i) lt 0.75*expect_med then begin
         if dp_sync(i).acc_interval(0) gt 0 then begin
            while (arr0(i) lt 0.75*expect_med) do begin
               arr0(i) = arr0(i) + 65536L
               arr1(i) = arr1(i) + 65536L
            endwhile
            arr0(i) = arr0(i) / float(dp_sync(i).acc_interval(0))
            arr1(i) = arr1(i) / float(dp_sync(i).acc_interval(0))
         endif else begin
            arr0(i) = 0L
            arr1(i) = 0L
         endelse
      endif else begin
         if dp_sync(i).acc_interval(0) gt 0 then begin
            arr0(i) = arr0(i)/float(dp_sync(i).acc_interval(0))
            arr1(i) = arr1(i)/float(dp_sync(i).acc_interval(0))
         endif else begin
            arr0(i) = 0L
            arr1(i) = 0L
         endelse
      endelse
;
; update the expected value for comparison
;
      if arr0(i) gt 0 then begin
         expect_med = arr0(i) * dp_sync(i).acc_interval(0)
      endif
   endfor
endfor

;
;   temporary? skip this bit
;
goto,skipthis
;
;  correct within high mode itself for overflow
;

if med(0) lt high(0) then begin
   nwind = n_elements(med) - 2
   step = 1
endif else begin
   nwind = n_elements(med) - 1
   step = 0
endelse

;
;  debug
;
if n_params() eq 6 then begin
   print,'Number of high windows = ',nwind
endif

for jj = 0,nwind do begin
   n = 0
   last_high = 0
   while last_high eq 0 do begin
     last_high = arr0(high(jj)+n)  
     n = n + 1
   endwhile
   n = n - 1
;
;  expected value judged from last one
;

   expect_high = last_high
   report = 0
   for i = high(jj)+n,med(jj+step) do begin
      if n_params() eq 6 and i eq high(jj) then begin
         print,'Checking data points ',high(jj),' to ',med(jj+step)
         print,'Expected value = ',expect_high
         print,'Which was arr0',(high(jj)+n-1),')',format='(a,i4,a)
         print,'Actual data = ',arr0(i)
      endif
;
;  trap isolated points
;
      if arr0(i-1) eq 0 and arr0(i+1) eq 0 then begin
         arr0(i) = 0
         arr1(i) = 0
      endif

      if (arr0(i) lt 0.75*expect_high) and $
         (arr0(i) gt 0) then begin
         if dp_sync(i).acc_interval(0) gt 0 then begin
            if n_params() eq 6 and report eq 0 then begin
               print,'Correction triggered: expected, actual = ',$
                      expect_high,arr0(i),format='(a,2i7)
               for ii=0,9 do begin
                  print,i-ii,arr0(i-ii),i+ii,arr0(i+ii)
               endfor
               report = 1
            endif
            while (arr0(i) lt 0.75*expect_high) do begin
               arr0(i) = arr0(i) + 65536L
               arr1(i) = arr1(i) + 65536L
            endwhile
         endif 
      endif 
;
; update the expected value for comparison
;
      if arr0(i) gt 0 then begin
         expect_high = arr0(i)
      endif
   endfor
endfor

skipthis:
end  
