; EIT_TIME_CORRECTION
;+
; Name:
;    EIT_TIME_CORRECTION
;
; Purpose:
;    To interpolate or extrapolate the most recent OBE - LOBT time difference
;	from the values in TIME_DIFFERENCE_DB.
;
; Input Parameters:
;    None
;
; Output:
;    None
;
; Keywords:
;    CORRECTION_STRING	-	Used to return an ASCII string with the
;				time difference.
;    INTERP             -       Turn on interpolation
;
; Calling Sequence:
;    dt = EIT_TIME_CORRECTION(obe_time, CORRECTION_STRING = CORRECTION_STRING)
; 
; Restrictions:
;    If large jumps in the difference occur between realtime contacts, this
;	routine could return inaccurate values.
;
; History:
;    1997 April 17 -	D.M. fecit.
;    1997 August 27 -	Added CORRECTION_STRING keyword		D.M. fecit.
;    1999 Feb 7   - Added binary search and indexing to speed finding
;                   the right record, created unix version for Solaris -  DW
;    1999 Feb 8   - Added INTERP keyword - IMPORTANT due to time offset jumps
;                   during out of contact periods, interpolation can produce
;		    totally bogus answers - DW
;
; @(#)eit_time_correction.pro	1.2 02/09/99 NRL IDL LIBRARY
;-

; swap words and bytes 
; VMS data base -> Sun Solaris
; 12345678  -> 7856 3412
function word_swap,long_int
b1 = long_int and 'FF000000'XL
b1 = b1 / '1000000'XL
b1 = b1 and 'FF'XL
b2 = long_int and '00FF0000'XL
b2 = b2 / '100'XL
b3 = long_int and '0000FF00'XL
b3 = b3 * '100'XL
b4 = long_int and '000000FF'XL
b4 = b4 * '1000000'XL
;print,b1,b2,b3,b4,format='(4z10)'
new = b1+b2+b3+b4

return,new
end

pro convert_time,time_unit,rec_num,new_w
obe_time ='1998/6/1 10:58:10'
image_time = anytim2utc(obe_time) 

;
wl = assoc(time_unit, lonarr(4))
wl_0 = wl(0)
if (!VERSION.OS_FAMILY eq 'unix') then rec = word_swap(wl_0(0)) $
 else rec = word_swap(wl_0(0))
;print,'Number of Records',rec,' Finding Rec ',rec_num
;
this_time = [image_time, image_time]
w = assoc(time_unit, this_time)
last_rec = w(rec)

if(rec_num lt 0) then rec_num = rec

if(rec_num le rec) then begin
 w_rec = w(rec_num)
 new_w = w(rec_num)
if (!VERSION.OS_FAMILY eq 'unix') then begin
 new_w(0).mjd = word_swap(w_rec(0).mjd)  
 new_w(0).time = word_swap(w_rec(0).time)  
 new_w(1).mjd = word_swap(w_rec(1).mjd)  
 new_w(1).time = word_swap(w_rec(1).time)  
;print,new_w(0).mjd,new_w(0).time
;print,new_w(1).mjd,new_w(1).time
endif else begin
 new_w(0).mjd = w_rec(0).mjd
 new_w(0).time = w_rec(0).time  
 new_w(1).mjd = w_rec(1).mjd  
 new_w(1).time = w_rec(1).time  
endelse
endif
end

PRO interp_time_corr,image_time,obe_time1,obe_time2,delta_mjd,delta_ms
obe_mjd1 = obe_time1(0).mjd*8.64d4 + 1.e-3*obe_time1(0).time
obe_mjd2 = obe_time2(0).mjd*8.64d4 + 1.e-3*obe_time2(0).time
image_mjd = image_time.mjd*8.64d4 + 1.e-3*image_time.time
lobt_mjd1 =obe_time1(1).mjd*8.64d4 + 1.e-3*obe_time1(1).time
lobt_mjd2 =obe_time2(1).mjd*8.64d4 + 1.e-3*obe_time2(1).time
interp = lobt_mjd1 + (lobt_mjd2 - lobt_mjd1) *(image_mjd - obe_mjd1)/(obe_mjd2 - obe_mjd1) 

delta_mjd =long(image_mjd - interp)/86400L
delta_ms = long(1000. *  (image_mjd - interp - (86400.*delta_mjd)))
end


FUNCTION eit_time_correction, obe_time, correction_string = correction_string,interp=interp

image_time = anytim2utc(obe_time) 
;print,image_time.mjd,image_time.time
;
current_record = image_time.mjd*8.64d4 + 1.e-3*image_time.time
;print, '%EIT_TIME_DIFFERENCE-D-CURRENT, current_record = ' + $
;   strtrim(current_record, 2)
;
status = 0
;
; The TIME_DIFF.DAT is provided by EIT and is a VMS data file
; 1st record contains number of records
; succeeding records are data records with 2 time records of 2 long ints each
; in CDS time format (mjd and time in millisecs)
path = getenv('NRL_LIB')+'/lasco/data/calib/'
file=path+'TIME_DIFF.DAT'
openr, time_unit, file, /get_lun 
;
mjd_index = [50692,50722,50753,50783,50814,50845,50873,50904,50934,50965,50995,51026,51057,51087,51118,51148,51179,51210]
rec_index = [1477,4174,6514,9313,9754,13604,17528,21531,24888,28386,30850,30850,30850,30850,32470,35410,37478,37478]
n_index = n_elements(mjd_index)

last_rec_num = -1
convert_time,time_unit,last_rec_num,last_rec
rec = last_rec_num
prev_rec = 0
if (image_time.mjd le mjd_index(n_index -1)) then BEGIN
 i = 0
 repeat i=i+1 until (mjd_index(i) ge image_time.mjd or (i gt n_index))
 if(i lt n_index) then begin
  rec = rec_index(i)
  prev_rec = rec_index(i-1)
 ; print,'Index ',i
 endif
ENDIF
try_rec = (rec+prev_rec)/2
convert_time,time_unit,try_rec,last_rec

rec_diff = 100
;print,rec,prev_rec,try_rec,rec_diff
while rec_diff gt 1 do begin
; print, '%EIT_TIME_DIFFERENCE-D-CURRENT, current_record = ' + $
;  strtrim(current_record, 2)
   obe_record = last_rec(0).mjd*8.64d4 + 1.e-3*last_rec(0).time
;print, '%EIT_TIME_DIFFERENCE-D-OBE, OBE time = ' + $
;   anytim2utc(last_rec(0),/ecs)
    lobt_record = last_rec(1).mjd*8.64d4 + 1.e-3*last_rec(1).time
;print, '%EIT_TIME_DIFFERENCE-D-OBE, obe_record = ' + $
;   strtrim(obe_record, 2)
;print, '%EIT_TIME_DIFFERENCE-D-LOBT, lobt_record = ' + $
;   strtrim(lobt_record, 2)
;  rec = rec - 1
   if(obe_record gt current_record) then begin
    rec = try_rec
    try_rec = (try_rec + prev_rec)/2 
   endif else begin
    prev_rec = try_rec
    try_rec = (try_rec + rec)/2
   endelse
   rec_diff = rec - prev_rec
if (rec_diff eq 1) then begin
   convert_time,time_unit,rec,last_rec
   record1 = last_rec(0).mjd*8.64d4 + 1.e-3*last_rec(0).time
   diff1 = (current_record - record1)
   convert_time,time_unit,rec,last_rec
   record2 = last_rec(0).mjd*8.64d4 + 1.e-3*last_rec(0).time
   diff2 = abs(current_record - record2)
   if (diff2 gt diff1) then convert_time,time_unit,rec,last_rec
endif else begin
   convert_time,time_unit,try_rec,last_rec
endelse
;print,image_time.mjd,image_time.time,last_rec(0).mjd,last_rec(0).time,last_rec(1).mjd,last_rec(1).time,rec,prev_rec,try_rec,rec_diff
;print, '%EIT_TIME_DIFFERENCE-D-REC, rec = ' + strtrim(rec, 2)
end

;print, '%EIT_TIME_DIFFERENCE-D-REC, using record ' + strtrim(try_rec, 2)

if keyword_set(interp) then begin
 convert_time,time_unit,try_rec-1,last_rec0
 interp_time_corr,image_time,last_rec,last_rec0,delta_mjd,delta_ms 
endif else begin
 delta_mjd = last_rec(0).mjd - last_rec(1).mjd
 delta_ms = last_rec(0).time - last_rec(1).time
endelse
;
delta_time = delta_mjd/86400. + 1.e-3*delta_ms
delta_time_m = fix(delta_time/60) & delta_time_s = delta_time mod 60
delta_time_m = strtrim(delta_time_m, 2)
delta_time_s = string(delta_time_s, format = '$(f7.3)')
delta_time_s = strtrim(delta_time_s, 2)
;
; So far, all deltas have been positive, so treat larger negative deltas
; as bogus data.
;
if delta_time lt (-15) then begin
   delta_time = 0.0
   delta_mjd = 0l & delta_ms = 0.0
end
;
; if delta_time lt (-15) then begin
;    correction_string = 'OBE is running behind LOBT by ' + delta_time_m + $
;       ' m ' + delta_time_s + ' s.'
; endif else
if delta_time gt 15 then begin
   correction_string = 'OBE is running ahead of LOBT by ' + delta_time_m + $
      ' m ' + delta_time_s + ' s.'
endif else begin
   correction_string = 'OBE is within 15 s of LOBT.'
end
;
; print, anytim2utc(last_rec(0), /ecs), '	', anytim2utc(last_rec(1), /ecs)
;
close, time_unit & free_lun, time_unit
;
return, [delta_mjd, delta_ms]
end



