pro plot_mtm_shift, sttim, entim, timarr, data, every=every, do_slope=do_slope, qstop=qstop, hc=hc
;
;SAMPLE CALLING SEQUENCE:
;	plot_mtm_shift,'10-may','1-jun',every=10,/do_slope
;	plot_mtm_shift,'10-jan','1-jun',every=50
;HISTORY:
;	Written 10-Jun-96 by M.Morrison
;V2.0	13-Jun-96 (MDM) - Modified to plot the slope change
;V2.1	28-Jun-96 (MDM) - Changed call to rd_loi_mmad from /clean to clean=2
;			- rd_loi_mmad also has a fudge factor for the velocity now
;V2.2	 5-Aug-96 (MDM) - Added /ynozero to SC-LOI avg plot
;			- Added /hc option
;V2.3	16-Sep-96 (MDM) - Changed fixed scaling of slope trend to not clip at
;			  zero (max)
;V2.4	 6-May-99 (RIB) - Changed line of sight velocity lookup to 
;			  soho_ephemeris.txt instead of soho_orbit.txt
;
progver = 'PLOT_MTM_SHIFT.PRO  Ver 2.4
;
if (keyword_set(hc)) then begin
    save_dev = !d.name
    set_plot, 'ps
    device, /land
end
;
clearplot
;
if (n_elements(data) eq 0) then rd_loi_mmad, 'v', sttim, entim, timarr, data, every=every, clean=2
if (n_elements(timarr) eq 0) then begin
    print, 'PLOT_MTM_SHIFT: No data in files
    return
end
;
if (n_elements(scv) eq 0) then begin
;    mat = rd_tfile('$MDI_CAL_INFO/soho_orbit.txt', 8, 2)
;    timarr_scv = anytim2ints( fid2ex(mat(1,*)) )
;    scv = float( mat(2,*) )
    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)
end
;
tit = 'LOI-V full average
ytit2 = 'S/C Vel (m/sec)
ytit3 = 'Avg Dop Vel - S/C Vel (m/sec)

coeff1 = poly_fit(  int2secarr(timarr)/86400., data,      1, yfit1)
coeff2 = poly_fit(  int2secarr(timarr)/86400., scv3,      1, yfit2)
coeff3 = poly_fit(  int2secarr(timarr)/86400., data-scv3, 1, yfit3)

!p.multi=[0,1,4]
!p.charsize = 1.7
fmt_timer, timarr, t1, t2
utplot, timarr, data, /ynozero, ytit='Average LOI-V (m/s)', psym=3, timerange=[t1,t2], /xstyle
if (keyword_set(do_slope)) then begin
    outplot, timarr, yfit1
    plottimes, .7, .8, 'Slope = ' + strtrim(coeff1(1),2) + ' m/sec per day'
end
utplot, timarr, scv3, /ynozero, tit='S/C Velocity', ytit=ytit2, timerange=[t1,t2], /xstyle
if (keyword_set(do_slope)) then begin
    outplot, timarr, yfit2
    plottimes, .7, .8, 'Slope = ' + strtrim(coeff2(1),2) + ' m/sec per day'
end
utplot, timarr, data-scv3, /ynozero, tit=tit, ytit=ytit3, psym=3, timerange=[t1,t2], /xstyle
if (keyword_set(do_slope)) then begin
    outplot, timarr, yfit3
    plottimes, .7, .8, 'Slope = ' + strtrim(coeff3(1),2) + ' m/sec per day'
end
;
n = n_elements(x2)
dt = median(deriv_arr(x2))
block = fix((24.*60.*60./dt)>20)	;take blocks of 24 hours
nout = fix(n/block )	
if (nout ge 2) then begin
    tarr = fltarr(nout)
    sarr = fltarr(nout)
    for i=0,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
    utplot, anytim2ints('1-jan-96', off=tarr), sarr, tit=tit, ytit='Slope', $
		timerange=[t1,t2], psym=-2, yrange=[min(sarr)>(-25), max(sarr)<25], /xstyle
end

;
plottime & plottime, 0, 0, progver
if (keyword_set(hc)) then begin
    pprint
    set_plot, save_dev
end

clearplot
if (keyword_set(qstop)) then stop
end
