; file /hosts/diapason/usr1/people/tarbell/trace/gti1_closed.pro

Function mean,x
return,total(x)/float(n_elements(x))
end

PRO gti1_closed, summary, xz, abc, gtcal, pztfactor, noffset=noffset, $
  sgcal=sgcal, wedge=wedge
;  before starting IDL, do % source /tsw/setup/setup_trace
;  and % setenv TRACE_J1_DIR /hosts/vestige/archive/sci/trace/trj
;  in tjitter, set new directory to /hosts/vestige/archive/sci/trace/trj/jitter
;  or if you are on vestige already to /archive/sci/trace/trj/jitter
;  read summary files for the gti1 set (2 files only)
;  quit tjitter

pztl = ['A','B','C']
if not keyword_set(noffset) then noffset=0
if not keyword_set(sgcal) then sgcal=[0.163, 0.162, 0.1645]
if not keyword_set(wedge) then wedge=[11.4, 11.4]
xz=fltarr(800*32,2)
abc=fltarr(800*32,3)
for i=0,31 do xz(i*800:i*800+799,0) = float(fft([summary(i).gerr_fft(*,0), $ 
                                   conj(reverse(summary(i).gerr_fft(1:399,0)))],-1))
for i=0,31 do xz(i*800:i*800+799,1) = float(fft([summary(i).gerr_fft(*,1), $ 
                                   conj(reverse(summary(i).gerr_fft(1:399,1)))],-1))				   
for i=0,31 do  abc(i*800:i*800+799,0) = float(fft([summary(i).pzt_fft(*,0), $ 
                                    conj(reverse(summary(i).pzt_fft(1:399,0)))],-1))
for i=0,31 do  abc(i*800:i*800+799,1) = float(fft([summary(i).pzt_fft(*,1), $ 
                                    conj(reverse(summary(i).pzt_fft(1:399,1)))],-1))
for i=0,31 do  abc(i*800:i*800+799,2) = float(fft([summary(i).pzt_fft(*,2), $ 
                                    conj(reverse(summary(i).pzt_fft(1:399,2)))],-1))
!p.multi=[0,0,2]
plot,xz(*,0),/ynoz
plot,xz(*,1),/ynoz
wait,5
!p.multi=[0,0,3]
plot,abc(*,0),/ynoz
plot,abc(*,1),/ynoz
plot,abc(*,2),/ynoz
dxz  = fltarr(2,2)
dabc = fltarr(3,2)
for i=0,1 do begin
	if (i eq 1) then noffset = noffset+12800
	il1 = 2000+noffset  &  ih1 = il1+400
	il2 = 4500+noffset  &  ih2 = il2+400
	il3 = 6900+noffset  &  ih3 = il3+400
	print,mean( xz(il1:ih1,0)),stdev( xz(il1:ih1,0)),mean( xz(il2:ih2,0)),stdev( xz(il2:ih2,0)), $
	      mean( xz(il3:ih3,0)),stdev( xz(il3:ih3,0))
	print,mean( xz(il1:ih1,1)),stdev( xz(il1:ih1,1)),mean( xz(il2:ih2,1)),stdev( xz(il2:ih2,1)), $
	      mean( xz(il3:ih3,1)),stdev( xz(il3:ih3,1))
	print,mean(abc(il1:ih1,0)),stdev(abc(il1:ih1,0)),mean(abc(il2:ih2,0)),stdev(abc(il2:ih2,0)), $
	      mean(abc(il3:ih3,0)),stdev(abc(il3:ih3,0))
	print,mean(abc(il1:ih1,1)),stdev(abc(il1:ih1,1)),mean(abc(il2:ih2,2)),stdev(abc(il2:ih2,2)), $
	      mean(abc(il3:ih3,1)),stdev(abc(il3:ih3,1))
	print,mean(abc(il1:ih1,2)),stdev(abc(il1:ih1,2)),mean(abc(il2:ih2,2)),stdev(abc(il2:ih2,2)), $
	      mean(abc(il3:ih3,2)),stdev(abc(il3:ih3,2))
	dxz(0,i) = 0.5*(mean( xz(il1:ih1,0))+mean( xz(il3:ih3,0)))-mean( xz(il2:ih2,0))
	dxz(1,i) = 0.5*(mean( xz(il1:ih1,1))+mean( xz(il3:ih3,1)))-mean( xz(il2:ih2,1))
	dabc(0,i)= 0.5*(mean(abc(il1:ih1,0))+mean(abc(il3:ih3,0)))-mean(abc(il2:ih2,0))
	dabc(1,i)= 0.5*(mean(abc(il1:ih1,1))+mean(abc(il3:ih3,1)))-mean(abc(il2:ih2,1))
	dabc(2,i)= 0.5*(mean(abc(il1:ih1,2))+mean(abc(il3:ih3,2)))-mean(abc(il2:ih2,2))
endfor
noffset = noffset-12800

; GT Calibration
gtcal = fltarr(2)
for i=0,1 do gtcal(i) = wedge(i)/dxz(i,i)
print,'GT Calibration in arcsec/DN, X & Z:  ',gtcal

; PZT Gain Calibration (neglects small drift in other axis)
pztfactor = fltarr(3)
t = wedge(1)/2.
print,'For Z error, PZT A moved ',dabc(0,1)*sgcal(0),', should have moved ',wedge(1)/2.
print,'Gain is off by factor of ',abs(dabc(0,1)*sgcal(0))/t
pztfactor(0) = abs(dabc(0,1)*sgcal(0))/t
t = wedge(1)/4.*sqrt(1.+(.5/.433)^2)
print,'For Z error, PZT B moved ',dabc(1,1)*sgcal(1),', should have moved ',t
print,'Gain is off by factor of ',abs(dabc(1,1)*sgcal(1))/t
print,'For Z error, PZT C moved ',dabc(2,1)*sgcal(1),', should have moved ',t
print,'Gain is off by factor of ',abs(dabc(2,1)*sgcal(1))/t
t = sqrt((0.5*wedge(0))^2 + (0.433*wedge(0))^2)
print,'For X error, PZT B moved ',dabc(1,0)*sgcal(1),', should have moved ',t
print,'Gain is off by factor of ',abs(dabc(1,0)*sgcal(1))/t
pztfactor(1) = abs(dabc(1,0)*sgcal(1))/t
print,'For X error, PZT C moved ',dabc(2,0)*sgcal(2),', should have moved ',t
print,'Gain is off by factor of ',abs(dabc(2,0)*sgcal(2))/t
pztfactor(2) = abs(dabc(2,0)*sgcal(2))/t
print,'PZT Gain Factors:  ',pztfactor
return
end

	
