pro bcs_clock_fix, daytim, bcs_clock_ref, qdebug=qdebug, test=test
;
;+
;NAME:
;	bcs_clock_fix
;PURPOSE:
;	Use one BCS clock "tie" point which covers many days, even though
;	the BCS clock rolls over every 2.2 hours and there is a 0.2 sec
;	per day drift in the clock.  It was needed because an SEU caused
;	BCS clock value to be unavailable from 16-Jul-92 17:00 to
;	20-Jul-92 14:50.  The output does correct for the 0.2 sec per day 
;	drift.
;INPUT:
;	daytim	- 7 element time of current major frame
;OUTPUT:
;	bcs_clock_ref- A structure with the reference day/time and the
;		  derived BCS clock value for that time
;HISTORY:
;	Written 22-Jul-92 by M.Morrison
;-
;
bcs_clock_ref = {bcs_clock_rec, $
                        ds79: fix(0),   $       ;Reference day from DP clock
                        msod: long(0),  $       ;Reference time from DP clock
                        bcs: long(0)}           ;Reference time from BCS clock
;
bcs_fix_ref = anytim2ints([60728225, 4946])	;16-Jul-92 16:52:08
bcs_fix_clock = 21696
drift = 0.33		;should be 0.2 over the long term (using 3-mar and 13-jul for calculation)
;
expect = 0
if (n_elements(test) eq 0) then test = 0
case test of
    0: tim_in = anytim2ints(daytim)
    1: begin & tim_in = anytim2ints([26362964, 4811]) & expect = 19968 & end		; 3-Mar-92 07:19:12
    2: begin & tim_in = anytim2ints([51688626, 4908]) & expect = 29184 & end		; 8-Jun-92 14:21:28
    3: begin & tim_in = anytim2ints([35560889, 4943]) & expect = 40512 & end		;13-Jul-92 09:52:40
    4: begin & tim_in = anytim2ints([57007089, 4952]) & expect = 10368 & end		;22-Jul-92 15:50:07
    else:
end
;
tdiff = long(int2secarr(tim_in, bcs_fix_ref))			;difference in seconds
error = long(drift/86400*tdiff*8)				;drift correction - in BCS ticks
diff = tdiff*8 + bcs_fix_clock + error				;change in BCS clock
nroll_over = diff/65536
remainder = diff mod 65536
if (remainder lt 0) then begin
    remainder = remainder + 65536
    nroll_over = nroll_over - 1
end
;
if (keyword_set(qdebug) or keyword_set(test)) then begin
    print, '                           Delta      Delta          Drift    Derived  Expected      Error    Error
    print, '                             Sec       BCS           Corr       BCS      BCS          BCS      Sec
           ;22-JUL-92  15:50:07        514678     4139135          15       10367   10368          -1
    print, fmt_tim(tim_in), '  ', tdiff, diff, error, remainder, expect, remainder-expect, (remainder-expect)/8.
end
bcs_clock_ref.ds79 = tim_in.day
bcs_clock_ref.msod = tim_in.time
bcs_clock_ref.bcs = remainder
;
end

