pro routine_fft_ellip_filter,Date=Date,stats_directory=stats_directory,$
        x_center_file=x_center_file,y_center_file=y_center_file,$
        depth=depth,x_off=x_off,y_off=y_off,exam_date=exam_date


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(exam_date)) then exam_date='1_31'
If (not keyword_set(stats_directory)) then stats_directory='stats_'+exam_date+'/'
If (not keyword_set(x_center_file)) then x_center_file='newest_x_centers.txt'
If (not keyword_set(y_center_file)) then y_center_file='newest_y_centers.txt'
If (not keyword_set(depth)) then depth=1
If (not keyword_set(postscript_dir)) then postscript_dir='distorted_postscripts'
If (not keyword_set(x_off)) then x_off=0
If (not keyword_set(y_off)) then y_off=0

stats_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+stats_directory+'/'
postscript_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/'+exam_date+'_figs/'



nonfiducial=[1,2,3,4,5,6,7,8,9,13,14,17,18,19,20,21,22,23,24,25,$
          26,27,28,29,30,31,32,38,39,45,46,47,48,49,50,$
          51,52,53,54,55,63,64,71,72,73,74,75,$
          76,77,78,79,88,89,98,98,99,100,$
          101,102,103,113,114,123,124,125,$
          126,127,128,138,139,149,150,$
          151,152,163,164,174,175,$
          176,177,188,189,200,$
          201,210,213,214,225,$
          226,238,239,249,$
          251,261,262,263,264,265,266,$
          276,286,287,288,289,290,291,$
          301,302,303,304,305,306,307,308,309,$
          310,311,312,313,314,315,316,$
          317,318,319,320,321,322,323,324,325,$
          326,327,336,337,338,339,340,341,$
          351,361,362,363,364,365,366,$
          376,388,389,$
          401,413,414,425,$
          426,427,438,439,449,450,$
          451,452,454,463,464,473,474,475,$
          476,477,478,488,489,498,499,500,$
          501,502,503,504,513,514,522,523,524,525,$
          526,527,528,529,530,538,539,546,547,548,549,550,$
          551,552,553,554,555,556,563,564,570,571,572,573,574,575,$
          576,577,578,579,580,581,582,588,589,594,595,596,597,598,599,600,$
          601,602,603,604,605,606,607,608,609,610,613,614,617,618,619,620,621,$
          622,623,624,625]-1

;Extra Index for the scale offset
complement,findgen(626),nonfiducial,fiducial
fitted=fiducial

;ARRAYS OF FILES
offset_files=file_search(stats_directory+'*5132*sub_offsets.txt',count=num_stats_files)

;CONSTANTS
num=25
grid_spots=625
index=findgen(625)

;ARRAY REFERENCE 25x25=625 FOR GRID
subx=2*indgen(num)-25           ;The x-reference numbers
suby=indgen(num)-12             ;The y-reference numbers

;HUGE DATA CUBE
x_offset_hor=fltarr(num_stats_files,num,num)
y_offset_hor=fltarr(num_stats_files,num,num)
x_offset_ver=fltarr(num_stats_files,num,num)
y_offset_ver=fltarr(num_stats_files,num,num)

;Correlations
timee1=fltarr(num_stats_files-depth,num,num)
timee2=fltarr(num_stats_files-depth,num,num)

timee1num=fltarr(num_stats_files-depth,num,num)
timee2num=fltarr(num_stats_files-depth,num,num)
timeden=fltarr(num_stats_files-depth,num,num)

timee1_arr=fltarr(num,num)
timee2_arr=fltarr(num,num)

frame_e1=fltarr(num_stats_files)
frame_e2=fltarr(num_stats_files)
;FILL THE DATA CUBES

for file_number=0,num_stats_files-1 do begin

        ;Take the results from the offset.txt files and fill the cube
        readcol,offset_files(file_number),x_offset,y_offset,format='X,X,f,X,f'

        ;Obtain the difference ordered in COLUMN, ROW between centroid and grid         point
	
	x_offset(where(x_offset eq -10))=0
        y_offset(where(y_offset eq -10))=0

        x_offset_hor(file_number,*,*)=x_offset
        y_offset_hor(file_number,*,*)=y_offset

        ;Obtain the difference ordered in ROW, COLUMN
        x_offset_ver(file_number,*,*)=x_offset((index mod 25)*25+index/25)
        y_offset_ver(file_number,*,*)=y_offset((index mod 25)*25+index/25)

endfor

readcol,offset_files(0),xcoord,ycoord,format='x,f,x,f,x'


  for file_number=0,num_stats_files-depth-1 do begin

   timee1num(file_number,*,*)=x_offset_hor(file_number,*,*)*$
			      x_offset_hor(file_number+depth,*,*)-$
         		      y_offset_hor(file_number,*,*)*$
			      y_offset_hor(file_number+depth,*,*)

   timee2num(file_number,*,*)=(x_offset_hor(file_number,*,*)*$
		              y_offset_hor(file_number+depth,*,*)$
			     +x_offset_hor(file_number+depth,*,*)$
			     *y_offset_hor(file_number,*,*))

   timee1temp=timee1num(file_number,*,*)
   timee2temp=timee2num(file_number,*,*)

   frame_e1(file_number)=mean(timee1temp(where(timee1temp ne 0)))
   frame_e2(file_number)=mean(timee2temp(where(timee2temp ne 0)))


endfor

;Grid for partvelvec
x_ref_col=12.5*subx+542
y_ref_col=25.0*suby+460
index=findgen(num*num)
gridx=fltarr(num*num)
gridy=fltarr(num*num)
gridx(index)=x_ref_col(index mod num)
gridy(index)=y_ref_col(index/num)

;Indices and such
no_bins=8
a=findgen(13)+12
b=findgen(12)
subn=[a,b]
subm=subn


e1max=2.4
e1min=-2.4

e1hist=histogram(frame_e1,Nbins=no_bins,locations=loc_e1,$
		 reverse_indices=e1_ind,$
		 max=e1max,min=e1min)

binsize=(e1max-e1min)/(no_bins-1)

set_plot,'ps'
device,filename=Postscript_dir+'Filtered_Offsets_e1_binned_'+Date+'.ps'                  
loadct,0                      ;load color table 39
device,/color                   ;allow color on the postscript
device,ysize=8.5,/inches        ;Height of plot in y
device,xsize=11.0,/inches        ;Width of plot in x
device,yoffset=1.0,/inches      ;Y position of lower left corner

white='FFFFFF'x
black='000000'x
!P.CHARSIZE=1.0
!P.THICK=4.

!p.multi=[0,1,1]

print,e1hist

for i=0, no_bins-1 do begin
	
	;Allocate space for arrays for a given ellipticity bin
	rel_indices=e1_ind(e1_ind(i):e1_ind(i+1)-1)
	xfft_array=complexarr(N_elements(rel_indices),num,num)
	yfft_array=complexarr(N_elements(rel_indices),num,num)
	x_fft=complexarr(num,num)
	y_fft=complexarr(num,num)
	x_fft_re=complexarr(num,num)
	y_fft_re=complexarr(num,num)
	x_low_fft_re=complexarr(num,num)
	x_high_fft_re=complexarr(num,num)
	y_low_fft_re=complexarr(num,num)
	y_high_fft_re=complexarr(num,num)
	x_low_fft=complexarr(num,num)
	y_low_fft=complexarr(num,num)
	x_high_fft=complexarr(num,num)
	y_high_fft=complexarr(num,num)

	;Fill the arrays with the ffts
	for j=0, N_elements(rel_indices)-1 do begin
		xfft_array(j,*,*)=fft(x_offset_hor(rel_indices(j),*,*))	
		yfft_array(j,*,*)=fft(y_offset_hor(rel_indices(j),*,*))
		
	endfor
	
	;Average the fft
	x_fft=avg(xfft_array,0)
	y_fft=avg(yfft_array,0)
	

	;Rearrange indices
	for n=0,num-1 do begin
	    for m=0,num-1 do begin
	    	x_fft_re(subn(n),subm(m))=x_fft(n,m)
		y_fft_re(subn(n),subm(m))=y_fft(n,m)
	    endfor
	endfor

	;Filter the arrays
	x_high_fft_re=x_fft_re
	y_high_fft_re=y_fft_re
        x_high_fft_re(9:15,9:15)=0
        y_high_fft_re(9:15,9:15)=0
	x_low_fft_re(9:15,9:15)=x_fft_re(9:15,9:15)
	y_low_fft_re(9:15,9:15)=y_fft_re(9:15,9:15)

	;Rearrange
	for n=0,num-1 do begin
            for m=0,num-1 do begin
                x_low_fft(n,m)=x_low_fft_re(subn(n),subm(m))
		y_low_fft(n,m)=y_low_fft_re(subn(n),subm(m))
		x_high_fft(n,m)=x_high_fft_re(subn(n),subm(m))
		y_high_fft(n,m)=y_high_fft_re(subn(n),subm(m))
	    endfor
	endfor

	;Inverse fft
	x_offset_high=fft(x_high_fft,/inverse)
	x_offset_low=fft(x_low_fft,/inverse)
	y_offset_high=fft(y_high_fft,/inverse)
	y_offset_low=fft(y_low_fft,/inverse)
	
	x_offset_high_1=fltarr(625)
        x_offset_low_1=fltarr(625)
	y_offset_high_1=fltarr(625)
        y_offset_low_1=fltarr(625)

	x_offset_high_1(*)=x_offset_high
	y_offset_high_1(*)=y_offset_high
	x_offset_low_1(*)=x_offset_low
	y_offset_low_1(*)=y_offset_low

	gridx=[gridx,600]
	gridy=[gridy,810]
	x_offset_high_1=[x_offset_high_1,2]
	y_offset_high_1=[y_offset_high_1,0]
        x_offset_low_1=[x_offset_low_1,2]
        y_offset_low_1=[y_offset_low_1,0]

	;X Low pass frequency
	shade_surf,congrid(abs(x_low_fft_re),600,600),background=white,$
		color=black,xtitle='Kx',ytitle='Ky',$
		position=[.2,.1,.9,.9],/normal
	xyouts,.1,.98,'Low Pass filter in X frequency domain',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal

	;X High Pass Frequency
	shade_surf,congrid(abs(x_high_fft_re),600,600),background=white,$
                color=black,xtitle='Kx',ytitle='Ky',$
                position=[.1,.1,.9,.9],/normal

	xyouts,.1,.98,'High Pass filter in X frequency domain',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal

	;Y High Pass Filter Frequency Domain
	shade_surf,congrid(abs(y_high_fft_re),600,600),background=white,$
                color=black,xtitle='Kx',ytitle='Ky',$
                position=[.1,.1,.9,.9],/normal
	xyouts,.1,.98,'High Pass filter in Y frequency domain',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.6,.93,'Lenght=2',/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal

	;Y Low Pass Filter Frequency Domain
        shade_surf,congrid(abs(y_low_fft_re),600,600),background=white,$
                color=black,xtitle='Kx',ytitle='Ky',$
                position=[.1,.1,.9,.9],/normal
	xyouts,.1,.98,'Low Pass filter in Y frequency domain',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.6,.93,'Lenght=2',/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal

	;Offsets with filter
	partvelvec, x_offset_low_1(fitted),y_offset_low_1(fitted),$
                    gridx(fitted),gridy(fitted),yrange=[150,800],$
                    xrange=[200,850],position=[0.1,0.1,0.9,0.9]

        xyouts,.1,.98,'Offsets with low pass filter applied',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.6,.93,'Lenght=2',/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal

        partvelvec,x_offset_high_1(fitted),y_offset_high_1(fitted),$
                   gridx(fitted),gridy(fitted),yrange=[150,800],$
                   xrange=[200,850],position=[0.1,0.1,0.9,0.9]

        xyouts,.1,.98,'Offsets with high pass filter applied',/normal
        xyouts,.6,.98,'Number of events= '+strtrim(e1hist(i),2),/normal
        xyouts,.6,.93,'Length=2',/normal
        xyouts,.1,.93,strtrim(loc_e1(i),2)+' < e1 < ' +$
                       strtrim(loc_e1(i)+binsize,2),/normal


	
endfor


plot,frame_e1
plot,frame_e2

!p.multi=[0,1,1]
plot,frame_e1,frame_e2,psym=2

device,/close
set_plot,'x'

stop

end

