pro plot_vel_trend, sttim, entim, timarr, data, every=every, quiet=quiet, $
    do_slope=do_slope, outfil=outfil, ps=ps, gif=gif, qstop=qstop
;
;SAMPLE CALLING SEQUENCE:
;	plot_vel_trend,'10-may','1-jun',every=10,/do_slope
;	plot_vel_trend,'10-jan','1-jun',every=50
;HISTORY:
;	Written Dec-99 by R.Bush
;V1.0	 2-Dec-99 (RIB) - Based on plot_mtm_shift
;
prog_ver = 'Plot_Vel_Trend  Ver 1.0'

if (n_elements(sttim) eq 0) then sttim = fmt_tim(anytim2ints(ut_time(), off=-48*60.*60))
if (n_elements(entim) eq 0) then entim = ut_time()

dev_status = !d.name

if (keyword_set(ps)) then begin
    if (n_elements(outfil) eq 0) then outfil = 'vel_trend.ps'
    set_plot, 'ps'
    device, filename= outfil, /land
    loadct, 0
end

if (keyword_set(gif)) then begin
    if (n_elements(outfil) eq 0) then outfil = 'vel_trend.gif'
    set_plot, 'z'
    tv2, 600, 800, /init
    loadct, 0
    erase
end

if (n_elements(data) eq 0) then begin
   rd_loi_summary, 'v', sttim, entim, timarr, data, /offset, clean=2, every=every, quiet=quiet
end

if (n_elements(timarr) eq 0) then begin
    print, 'PLOT_MTM_SHIFT: No data in files
    return
end

vel_file = concat_dir(getenv('MDI_CAL_INFO'), 'soho_ephemeris.txt')

if (file_exist(vel_file)) then begin
    mat = rd_tfile('$MDI_CAL_INFO/soho_ephemeris.txt', 8, 1)
    timarr_scv = anytim2ints(mat(0,*))
    scv = float(mat(6,*))
    x1 = int2secarr(timarr_scv, '1-jan-96')
    x2 = int2secarr(timarr, '1-jan-96')
    scv3 = interpol(scv, x1, x2)
    dayarr = int2secarr(timarr)/86400.
end else begin
    print, 'No spacecraft velocity file - returning'
    return
end

!p.multi=[0,1,4]
!p.charsize = 1.7
fmt_timer, timarr, t1, t2
tr = [t1,t2]

tit1 = 'LOI-Proxy Velocity'
ytit1 = 'velocity - m/sec'
utplot, timarr, data, /ynozero, tit=tit1, ytit=ytit1, psym=3, timerange=tr, /xstyle
if (keyword_set(do_slope)) then begin
    coeff1 = poly_fit(dayarr, data, 1, yfit1)
    outplot, timarr, yfit1
    plottimes, .7, .7, 'Slope = ' + strmid(strtrim(coeff1(1),2),0,5) + ' m/sec per day'
end

tit2 = 'Spacecraft Velocity'
ytit2 = 'velocity - m/sec'
utplot, timarr, scv3, /ynozero, tit=tit2, ytit=ytit2, psym=3, timerange=tr, /xstyle
if (keyword_set(do_slope)) then begin
    coeff2 = poly_fit(dayarr, scv3, 1, yfit2)
    outplot, timarr, yfit2
    plottimes, .7, .7, 'Slope = ' + strmid(strtrim(coeff2(1),2),0,5) + ' m/sec per day'
end

tit3 = 'LOI-Proxy Velocity - Spacecraft Velocity'
ytit3 = 'velocity - m/sec'
utplot, timarr, data-scv3, /ynozero, tit=tit3, ytit=ytit3, psym=3, timerange=tr, /xstyle
coeff3 = poly_fit(dayarr, data-scv3, 1, yfit3)
outplot, timarr, yfit3
plottimes, .7, .7, 'Slope = ' + strmid(strtrim(coeff3(1),2),0,5) + ' m/sec per day'

n = n_elements(x2)
dt = median(deriv_arr(x2))
nhrs = 48
if (n lt 2000) then nhrs = 24
block = fix((nhrs*3600./dt)>20)	;take blocks of nhr hours
nout = fix(n/block)	

if (nout ge 2) then begin
    tarr = fltarr(nout)
    sarr = fltarr(nout)
    for i=0L,nout-1 do begin
	xx = x2(i*block:(i+1)*block-1)/86400
	yy = data(i*block:(i+1)*block-1) - scv3(i*block:(i+1)*block-1)
	coeff = poly_fit(xx, yy, 1, yfit)
	sarr(i) = coeff(1)
	tarr(i) = total(xx)/n_elements(xx)*86400.
    end
    tit4 = 'Slope of velocity difference over ' + strtrim(string(nhrs),2) + ' hours'
    ytit4 = 'Slope - m/sec/day'
    utplot, anytim2ints('1-jan-96', off=tarr), sarr, tit=tit4, ytit=ytit4, $
		timerange=tr, psym=-2, yrange=[-6.0, 4.0], /xstyle
end

plottime, 0, 0, prog_ver
plottime, 0.6, 0, 'Plot Made '+!stime 

if (keyword_set(ps)) then begin
    device,/close
end

if (keyword_set(gif)) then begin
    ssw_file_delete, outfil
    zbuff2file, outfil, /gif
end

set_plot, dev_status

if (keyword_set(qstop)) then stop

end
