;+
; NAME:
;  plot_att_cmd
; PURPOSE:
;  Plot the difference between att and cmd pointings.
; CALLING SEQUENCE:
;  .run plot_att_cmd
; HISTORY:
;  	24-apr-95, J. R. Lemen, Written
;V2.0	 2-May-95 (MDM) - Modified to use the times of the SFM images
;			  rather than equally spaced times
;			- Added options to overplot the correction
;			  function
;			- Added option to plot the residuals
;			- Restore file /ys/gen/status/plot_att_cmd.idl
;			  for a quick run
;V2.1	 2-May-95 (MDM) - Corrected sign error
;V3.0	 5-May-95 (MDM) - Added option to make the SHOW_PIX plot page
;V3.1    6-jun-95 (SLF) - move restore file to site / dont allow restore if 
;			     not available
;-
progver = 'PLOT_ATT_CMD.PRO  Ver 3.1

print, 'qplot_cor1=1	  ;will allow Morrison correction to be overplotted
print, 'qplot_cor2=1      ;will allow Katsev modified correction to be overplotted

if n_elements(att_pnt) eq 0 then begin
    savfile='/ys/site/share_env/plot_att_cmd.idl'
    ans=file_exist(savfile)
    if ans then begin
       print, 'There are two options'
       print, '   1. Restore from an IDL save file'
       print, '   2. Regenerate the information (takes approx 45 min)'
       yesnox, 'Do you want to restore from the IDL save file', ans, 'Yes'
    endif

    if (ans) then begin
	restore, savfile
    end else begin
	t0 = systime(1)

	;tgrid = timegrid('1-oct-91',!stime,days=.5)   ; Get a time every 12 hours
	;tim2orbit,tgrid,tim2night=tim2night           ; Get day time cases only
	;tgrid = tgrid(where(tim2night gt 0))

	get_sfm,timegrid('1-oct-91',!stime,days=1),index,/nodata

	print,'Will get pointing information for '+strtrim(n_elements(index),2)+' times'
	cmd_pnt = sxt_cen(index,/rawcmd)      ; Cmd pointings
	att_pnt = sxt_cen(index)              ; Att pointings
	print,'Calls to tim2orbit and sxt_cen took: ',(systime(1)-t0)/60.,' mins'
	;
	;   save, index, cmd_pnt, att_pnt, file='plot_att_cmd.idl'
    end
endif

!p.multi = [0,1,2]
;
if !d.name eq 'PS' then begin
   xsize = 9. & ysize = 6.
   device,/land,/inches,xsize=xsize,ysize=ysize,xoffs=(xsize-ysize)/2.,yoffs=10.6-(10.6-xsize)/2.
endif

;----------- Original Correction
    ref_time = '1-jan-92'
    x = int2secarr(index, ref_time)
    x_yr = x/(365*86400.)
    x_day = x/86400.

    param1 = [2.2123401, 0.74076641, 0.17411420]
    param2 = [2.3265929,  5.6742266, 0.24390289, 9.7029480, -4.6606623]
    attcmd_funct1, x_yr, param1, cor1x
    attcmd_funct2, x_yr, param2, cor1y

;----------- Updated Correction

    p=[1.12e-3, 4.256, 0.609375, 4.80, 28.15, 1.20, 14.15, 0.42, 2.0]
    cor2y=(p(0)*x_day+p(1))*(p(2)*cos((x_day-p(3))/p(4))+1)*exp(-(cos((x_day-p(5))/p(6))/p(7))^2)-p(8)

;----------- Plotting

utplot,index,att_pnt(0,*)-cmd_pnt(0,*),/ynoz,psym=3,ytit='E/W ATT-CMD (SXT Full Res Pix.)', yr=[-10,10]
if (keyword_set(qplot_cor1)) then outplot, index, cor1x
;
utplot,index,att_pnt(1,*)-cmd_pnt(1,*),/ynoz,psym=3,ytit='N/S ATT-CMD (SXT Full Res Pix.)', yr=[-50,max(att_pnt(1,*)-cmd_pnt(1,*))]
if (keyword_set(qplot_cor1)) then outplot, index, cor1y
if (keyword_set(qplot_cor2)) then outplot, index, cor1y-cor2y

plottime, 0, 0, progver
plottime

if (keyword_set(qplot_cor1) or keyword_set(qplot_cor2)) then begin
    resid_x = att_pnt(0,*)-cmd_pnt(0,*)  - cor1x
    resid_y = att_pnt(1,*)-cmd_pnt(1,*)  - cor1y
    tit = 'Morrison '
    if (keyword_set(qplot_cor2)) then begin
	resid_y = resid_y + cor2y
	tit = tit + ' and Katsev'
    end
    tit = tit + ' Correction'

    pause
    utplot,index,resid_x,/ynoz,psym=3,ytit='E/W ATT-CMD Residual (SXT FR Pix)',yr=[-10,10], tit=tit
    utplot,index,resid_y,/ynoz,psym=3,ytit='N/S ATT-CMD Residual (SXT FR Pix)',yr=[-10,10]

    plottime, 0, 0, progver
    plottime
end

pprint

if (keyword_set(qshow_pix)) then begin
    pause
    ;
    wdef, 1, 600, 800, /already
    !p.charsize = 1.8
    !p.multi = [0,1,4]
    utplot,index,att_pnt(0,*)-cmd_pnt(0,*),/ynoz,psym=3,ytit='E/W ATT-CMD !c(SXT Full Res Pix.)', yr=[-10,10]
    outplot, index, cor1x
    ;
    utplot,index,att_pnt(1,*)-cmd_pnt(1,*),/ynoz,psym=3,ytit='N/S ATT-CMD !c(SXT Full Res Pix.)',yr=[-20,max(att_pnt(1,*)-cmd_pnt(1,*))]
    outplot, index, cor1y-cor2y

    utplot,index,resid_x,/ynoz,psym=3,ytit='E/W ATT-CMD Residual !c(SXT FR Pix)',yr=[-10,10], tit='Morrison Correction
    utplot,index,resid_y,/ynoz,psym=3,ytit='N/S ATT-CMD Residual !c(SXT FR Pix)',yr=[-10,10], tit=tit

    plottime, 0, 0, progver
    plottime
end

end
