pro rd_loi_summary, which, sttim, entim, timarr, data, p30p60, $
	every=every, clean=clean, offset=offset, quiet=quiet, mat=mat, datamat=datamat
;+
;NAME:
;	rd_loi_summary
;PURPOSE:
;	Read the LOI -C and -V summary files, optionally plot
;SAMPLE CALLING SEQUENCE:
;	rd_loi_summary, 'c', '15-apr', '15-jun', timarr, data, every=120, /clean
;	rd_loi_summary, 'v',  '1-may',  '1-jul', timarr, data, every=120, /clean, /offset
;HISTORY:
;	Written 1-Dec-99 by R. Bush based on rd_loi_mmad
;

filebase = '/mdisw/health/loi_summary/loi_' + which + '_'

tmp_fil = '$MDI_TEMP_DIR/rd_loi_summary.temp_' + strtrim(long(systime(1)),2)
file_delete, tmp_fil

for iday=gt_day(sttim),gt_day(entim) do begin
    fid = ex2fid(anytim2ex(fmt_tim(iday)))
    infil = filebase+strmid(fid,0,6)+'.summary'
    if (file_exist(infil)) then begin
        if (not keyword_set(quiet)) then print, 'Reading: ' + infil
        tmat = rd_tfile(infil)
        n = n_elements(tmat)
        if (keyword_set(every)) then tmat = tmat( indgen((n/every)>1)*every )
        file_append, tmp_fil, tmat
    end else begin
        if (not keyword_set(quiet)) then print, 'File missing: '+ infil
    end
end

if (not file_exist(tmp_fil)) then begin
    print, 'No data available for this time period'
    return
end

mat = rd_tfile(tmp_fil, 6)
file_delete, tmp_fil
ss33 = uniq(mat(0,*))
mat = mat(*,ss33)
mat = double(mat)
timarr = anytim2ints('1-jan-70', off=reform(mat(0,*)))
data = reform(mat(3,*))
datamat = reform(mat(1:5,*))
p30p60 = reform(mat(5,*))

tuning_file = concat_dir(getenv('MDI_CAL_INFO'),'mdi_tuning.txt')
tuning = rd_tfile(tuning_file,3,1)
tune_times = reform(tuning(0,*) + ' ' + tuning(1,*))
deltav = reform(float(tuning(2,*)))
n_tuning = n_elements(tune_times)

if (which eq 'v') then begin
    ;--------- Bin List Changes
    ss = sel_timrange(timarr, ' 1-Jan-1996 00:00', ' 5-Apr-1996 19:31', /between)	;7003
    if (ss(0) ne -1) then data(ss) = (data(ss) * 100.0 + 478.0)*0.7783
    
    ss = sel_timrange(timarr, '5-Apr-1996 19:31', '31-Dec-2005', /between)		;7005
    if (ss(0) ne -1) then data(ss) = data(ss) * 0.08522 		;convert to m/sec

    ;--------- Tuning Changes
    if (keyword_set(offset)) then begin
        voffset = 0.0
        for i = 0, n_tuning - 2 do begin
            voffset = voffset + deltav(i)
            ss = sel_timrange(timarr, tune_times(i), tune_times(i+1), /between)
            nss = n_elements(ss)
            print, i, voffset, nss, '  ', tune_times(i), '  ',  tune_times(i+1)
            if (ss(0) ne -1) then begin
              data(ss) = data(ss) + voffset
            end   
        end
        data = data - voffset
    end
end

if (which eq 'c') then begin

    ;--------- Bin List Changes
    ss = sel_timrange(timarr, '1-Jan-1996 00:00', '23-Apr-1996 21:43', /between)	;7004?
    if (ss(0) ne -1) then data(ss) = data(ss) * 42.4	;arbitrary scaling

    ss = sel_timrange(timarr, '23-Apr-1996 21:43', '23-May-1996 22:03', /between)	;7006
    if (ss(0) ne -1) then begin
	data(ss) = unsign(long(data(ss)),16)
	data(ss) = data(ss)/8.811	;arbitrary scaling
    end

    ss = sel_timrange(timarr, '23-May-1996 22:03', '31-Dec-2005', /between)	;7007
    if (ss(0) ne -1) then data(ss) = data(ss) * (2.^16/1896075.)

end

if (keyword_set(clean)) then begin
    for iclean=0,clean do begin
        ss = avoid_spikes(data, nss)
        timarr = timarr(ss)
        data = data(ss)
        datamat = datamat(*,ss)
        p30p60 = p30p60(ss)
    end
end

ss = sel_timrange(timarr, sttim, entim, /between)
if (ss(0) ne -1) then begin
    timarr = timarr(ss)
    data = data(ss)
    p30p60 = p30p60(ss)
end

end
