pro plot_pzt_corr, sttim0, entim0, daytim, xoff, yoff, $
	clean=clean, every=every, $
	closed_loop=closed_loop, qstop=qstop, thresh=thresh, info=info, $
	window_clean=window_clean, prime30=prime30, qdebug=qdebug, hc=hc
;+
;NAME:
;	plot_pzt_corr
;PURPOSE:
;	To plot the pointing correction applied by the ISS PZT mirrors
;SAMPLE CALLING SEQUENCE:
;	plot_pzt_corr, '1-jun', '2-jun'
;	plot_pzt_corr, '1-may', '6-jun', every=500
;	plot_pzt_corr, sttim, entim, /clean
;	plot_pzt_corr, sttim, entim, /clean, thresh=1
;	plot_pzt_corr, sttim, entim, window_clean=2
;INPUTS:
;	sttim	- The starting time/date
;	entim	- The ending time/date
;OPTIONAL KEYWORD INPUT:
;	clean	- De-spike the data (clean it up somewhat)
;	every	- Only read every N data values
;	window_clean - Remove +/- "N" data points around the detected
;			spikes where N is "window_clean"
;	prime30	- If set, only take data points which were sampled
;		  during PRIME30 structure
;	qdebug	- If set, then plot the scatter plot for PRIME30 option
;HISTORY:
;	Written 6-Jun-96 by M.Morrison
;V1.1	12-Jun-96 (MDM) - Modified the labels
;			- Added closed_loop, thresh, info and window_clean options
;V1.2	28-Aug-96 (MDM) - Added PRIME30 option
;V1.3	30-Aug-96 (MDM) - Added /HC option
;	11-Sep-96 (MDM) - Added passing daytim,xoff,yoff out
;V1.4	16-Dec-96 (MDM) - Added 
;-
;
if (keyword_set(hc)) then setps,/land
;
if (n_elements(sttim0) eq 0) then sttim = anytim2ints(ut_time(), off=-24*60.*60.) else sttim = anytim2ints(sttim0)
if (n_elements(entim0) eq 0) then entim = anytim2ints(ut_time()) else entim = anytim2ints(entim0)
;
info = get_hk_info([sttim, entim], ['multpzt'+['1','2','3'], 'mkltloop'], /nostring, every=every)
if (data_type(info) ne 8) then begin
    print, 'PLOT_PZT_CORR:  Read of HK failed.  Returning...
    return
end
pzt1 = info.mdi.value(*,0)
pzt2 = info.mdi.value(*,1)
pzt3 = info.mdi.value(*,2)
qopen= info.mdi.value(*,3)
;
xoff = -5.6 * pzt1 + 2.68*(pzt2+pzt3)
yoff = 0.16 * pzt1 + 6.68*(pzt2-pzt3)
;
n = n_elements(xoff)
qok = bytarr(n)
ss = avoid_spikes(xoff, thresh=thresh)		& qok(ss) = 1
ss = avoid_spikes(yoff, thresh=thresh)		& qok(ss) = qok(ss) + 1
ss = where(qok eq 2)

if (keyword_set(prime30)) then begin
    rel_secs = (info.mdi.daytime.time mod 60000)/1000.
    ss = where((rel_secs lt 20) or (rel_secs gt 55))
    if (keyword_set(qdebug)) then begin
	!p.multi=[0,2,2]
	plot, rel_secs, xoff, psym=1, ytit='Xoff (unfiltered)'
	plot, rel_secs, yoff, psym=1, ytit='Yoff (unfiltered)'
	plot, rel_secs(ss), xoff(ss), psym=1, ytit='Xoff (filtered)'
	plot, rel_secs(ss), yoff(ss), psym=1, ytit='Yoff (filtered)'
	pause
    end
end

ytit1 = 'X Correction (E/W) (Arcsec)
ytit2 = 'Y Correction (N/S) (Arcsec)
!p.multi=[0,1,2]
qss = 1
case 1 of
    (keyword_set(clean)): begin
	    utplot, info.mdi.daytime(ss), xoff(ss), ytit=ytit1, /ynozero
	    utplot, info.mdi.daytime(ss), yoff(ss), ytit=ytit2, /ynozero
	end
    (keyword_set(window_clean)): begin
	    qok = bytarr(n) + 1
	    qspike = bytarr(n) + 1
	    qspike(ss) = 0
	    ss2 = where(qspike, nss2)
	    for ii=-1*window_clean,window_clean do qok( (ss2+ii)>0<(n-1) ) = 0
	    ss = where(qok)
	    utplot, info.mdi.daytime(ss), xoff(ss), ytit=ytit1, /ynozero
	    utplot, info.mdi.daytime(ss), yoff(ss), ytit=ytit2, /ynozero
	end
    (keyword_set(closed_loop)): begin
	    qok = bytarr(n) + 1
	    ss2 = where(not qopen, nss2)
	    for ii=-4,4 do qok( (ss2+ii)>0<(n-1) ) = 0
	    ss = where(qok)
	    utplot, info.mdi.daytime(ss), xoff(ss), ytit=ytit1, /ynozero
	    utplot, info.mdi.daytime(ss), yoff(ss), ytit=ytit2, /ynozero
	end
    else: begin
	    qss = 0
	    utplot, info.mdi.daytime, xoff, ytit=ytit1, /ynozero, yr=[min(xoff(ss)), max(xoff(ss))]
	    utplot, info.mdi.daytime, yoff, ytit=ytit2, /ynozero, yr=[min(yoff(ss)), max(yoff(ss))]
	  end
end
;
if (qss) then begin
    daytim = info.mdi.daytime(ss)
    xoff = xoff(ss)
    yoff = yoff(ss)
end else begin
    daytim = info.mdi.daytime
    xoff = xoff
    yoff = yoff
end
;
plottime, 0, 0, 'PLOT_PZT_CORR  Ver 1.4'
plottime
pprint, reset=(keyword_set(hc))
end