;(1)  Read in semi-reduced TRACE data
;(2)  Run trace_prep on all TRACE data, including wavelength correction, flatfielding and despike and POSSIBLY deripple
;(3)  Apply 3x3 PSF convolution to MDI image and all TRACE passband datacubes --- trim 1 pixel border
;(4)  Self-align the individual TRACE datacubes to middle image
;(5)  Align individual TRACE passbands; 1550 ref and 1660 ref to the 1700 ref
;(6)  Apply offsets to entire 1550 & 1600 datacubes
;(7)  Trim based on pointings then rebin MDI to TRACE spatial sampling		(context.bat)
;(8)  Align MDI to 1700 ref

;*** ALL DATA ALIGNED TO MID-1700 IMAGE ***

;(8b) check the time cadence stability

;(9)  Run automated wavelet analysis on every pixel for each of the TRACE passbands --- add in randomisation testing ???
;(10) Compare oscillation parameters against underlying MDI field
;(11) Write paper
;(12) Go to pub

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;(1+2)
;trace_prep2,['trb20040703_155029.mxf','trb20040703_160003.mxf'],-1,ind_1550,dat_1550,$
;		/wave2point_correct,despike=2,/deripple,/no_darksub
;trace_prep2,['trb20040703_155025.mxf','trb20040703_160015.mxf'],-1,ind_1600,dat_1600,$
;		/wave2point_correct,despike=2,/deripple,/no_darksub
;trace_prep2,['trb20040703_155033.mxf','trb20040703_160008.mxf'],-1,ind_1700,dat_1700,$
;		/wave2point_correct,despike=2,/deripple,/no_darksub

;*************
;****1550*****
;*************

;read in, dark subtract and flat field all images
trace_prep2,['20040703/trb20040703_155029.mxf','20040703/trb20040703_160003.mxf'],-1,ind_1550_temp,dat_1550_temp

;replace dropped-pixel blocks
dat_1550_temp=trace_pixel_drop_replace(TEMPORARY(dat_1550_temp),index=ind_1550_temp)

;replace outlying pixel minima intensities
dat_1550_temp = trace_pixel_fix(TEMPORARY(dat_1550_temp),ind_1550_temp,/update_hist,/loud)

;non-consistent minima check
mins=fltarr(128)
for i=0,127 do mins[i]=MIN(dat_1550_temp[*,*,i])
plot,mins,psym=7

;determine largest negative value in entire cube possibly caused by dark subtraction
;and set value of correction required to all images AS LONG AS the minima are consistent
;there is a chance that one or two rouge images could be substantially different.
;check manually that the minimum values are consistent
ped_1550 = ABS((MIN(dat_1550_temp)-1) < 0)

;apply various cleaning routines
trace_prep2,ind_1550_temp,dat_1550_temp,ind_1550,dat_1550,$
    /no_ff_dk,/wave2point_correct,/cleanjpg,add_pedestal=ped_1550;,/deripple

;deripple data cube in time & space trace_deripple_time.pro
dat_1550 = trace_deripple_time(ind_1550,dat_1550,/update_hist,/loud,nsigma=6,mask_size=10)

;save 0th image for future alignment with preceeding MDI data
;NOTE***THIS IMAGE iS NOT DESPIKED
ref_dat_1550 = dat_1550[*,*,0]
ref_ind_1550 = ind_1550[0]
	
;create new cube with middle image at beginning 
;for use as reference in alignment
dat_1550_temp = FLTARR(512,512,130)
dat_1550_temp[*,*,0] = dat_1550[*,*,64]
FOR i=1,129 DO dat_1550_temp[*,*,i] = dat_1550[*,*,i-1]
;*** NOW index pointing values DO NOT relate to the image pointings ***
;index pointing values are for pointing of 0th image BUT all images 
;are aligned to the 64th image

;align
ccshifts = TR_GET_DISP(dat_1550_temp,/shift)

;lose reference image at beginning of cube & clip
dat_1550_temp = REFORM(dat_1550_temp[6:505,2:509,1:*])		

;despike data cube in time & space using Marcus' code
dat_1550_temp = trace_unspike_time(ind_1550_temp,dat_1550_temp,/update_hist)

;hot pixels found manually at:
;       122.96541       480.91303
;       458.25746       460.52364
;in reference image coordinates
;
;3x3 problem 1st spans (110:122,478:482) throughout aligned cube, so set to 0
dat_1550_temp(110:122,478:482,*) = 0.
;3x3 problem 2nd spans (446:457,457:461) throughout aligned cube, so set to 0
dat_1550_temp(446:457,457:461,*) = 0.
;some other spurious effects at
;image 99,98 - pixels(134:138,76:83)
;image 123 - streak in bottom left

dat_1550=dat_1550_temp
ind_1550=ind_1550_temp

save,file='aligned_1550.sav',dat_1550,ind_1550,ref_dat_1550,ref_ind_1550

;*************
;****1600*****
;*************

trace_prep2,['20040703/trb20040703_155025.mxf','20040703/trb20040703_160015.mxf'],-1,ind_1600_temp,dat_1600_temp

;replace dropped-pixel blocks
dat_1600_temp=trace_pixel_drop_replace(TEMPORARY(dat_1600_temp),index=ind_1600_temp)

;replace outlying pixel minima intensities
dat_1600_temp = trace_pixel_fix((dat_1600_temp),ind_1600_temp,/update_hist,/loud)

;non-consistent minima check
mins=fltarr(128)
for i=0,127 do mins[i]=MIN(dat_1600_temp[*,*,i])
plot,mins,psym=7

;determine largest negative value in entire cube caused by dark subtraction
;and set value of correction required to all images
ped_1600 = ABS((MIN(dat_1600_temp)-1) < 0)
;apply various cleaning & correction routines
TRACE_PREP2,ind_1600_temp,dat_1600_temp,ind_1600,dat_1600,$
    /no_ff_dk,/wave2point_correct,cleanjpg=0.08,add_pedestal=ped_1600;,/deripple

;deripple data cube in time & space 
dat_1600 = trace_deripple_time(ind_1600,dat_1600,/update_hist,/loud,nsigma=6,mask_size=10)

;save 0th image for future alignment with preceeding MDI data
ref_dat_1600 = dat_1600[*,*,0]
ref_ind_1600 = ind_1600[0]
	
;check the 1600 middle image is the same time as the 1550 middle image
;print,ind_1550[64].img_time
;print,ind_1600[63:65].img_time

;create new cube with middle image at beginning 
;for use as reference in alignment
ind_1600_temp = ind_1600
dat_1600_temp = FLTARR(512,512,129)
dat_1600_temp[*,*,0] = dat_1600[*,*,63]
FOR i=1,128 DO dat_1600_temp[*,*,i] = dat_1600[*,*,i-1]
;*** index pointing values DO NOT relate to the image pointings ***
;index pointing values are for pointing of 0th image BUT all images 
;are aligned to the 64th image

;align
ccshifts = TR_GET_DISP(dat_1600_temp,/shift)

;lose reference image at beginning of cube & chop edges
dat_1600_temp = REFORM(dat_1600_temp[6:505,2:509,1:*])		

;despike in time and/or space using Marcus' code
dat_1600_temp = trace_unspike_time(ind_1600_temp,dat_1600_temp,/update_hist)

trace_image_interp,dat_1600_temp,ind_1600_temp,7,dat_1600,ind_1600

save,file='aligned_1600.sav',dat_1600,ind_1600,ref_dat_1600,ref_ind_1600

;*************
;****1700****
;*************

trace_prep2,['20040703/trb20040703_155033.mxf','20040703/trb20040703_160008.mxf'],-1,ind_1700_temp,dat_1700_temp

;replace dropped-pixel blocks
dat_1700_temp=trace_pixel_drop_replace(TEMPORARY(dat_1700_temp),index=ind_1700_temp)

;replace outlying pixel minima intensities
dat_1700_temp = trace_pixel_fix((dat_1700_temp),ind_1700_temp,/update_hist,/loud)

;non-consistent minima check
mins=fltarr(128)
for i=0,127 do mins[i]=MIN(dat_1700_temp[*,*,i])
plot,mins,psym=7

;manual replace the two bad images, 94, 95

temp=fltarr(512,512,3)
temp(*,*,0)=dat_1700_temp(*,*,93)
temp(*,*,1)=dat_1700_temp(*,*,94)
temp(*,*,2)=dat_1700_temp(*,*,93)+((2./3.)*(dat_1700_temp(*,*,96)-dat_1700_temp(*,*,93)))

temp=trace_pixel_drop_replace(temp,415,424,327,336)
temp=trace_pixel_drop_replace(temp,415,424,383,448)

temp2=fltarr(512,512,3)
temp2(*,*,0)=dat_1700_temp(*,*,93)+((1./3.)*(dat_1700_temp(*,*,96)-dat_1700_temp(*,*,93)))
temp2(*,*,1)=dat_1700_temp(*,*,95)
temp2(*,*,2)=dat_1700_temp(*,*,96)

temp2=trace_pixel_drop_replace(temp2,415,424,327,336)
temp2=trace_pixel_drop_replace(temp2,415,424,383,448)

dat_1700_temp(*,*,94)=temp(*,*,1)
dat_1700_temp(*,*,95)=temp2(*,*,1)

;determine largest negative value in entire cube caused by dark subtraction
;and set value of correction required to all images
ped_1700 = ABS((MIN(dat_1700_temp)-1) < 0)
;apply various cleaning & correction routines
TRACE_PREP2,ind_1700_temp,dat_1700_temp,ind_1700,dat_1700,$
    /no_ff_dk,/wave2point_correct,cleanjpg=0.08,add_pedestal=ped_1700;,/deripple

;deripple data cube in time & space 
dat_1700 = trace_deripple_time(ind_1700,dat_1700,/update_hist,/loud,nsigma=6,mask_size=10)

;save 0th image for future alignment with preceeding MDI data
ref_dat_1700 = dat_1700[*,*,0]
ref_ind_1700 = ind_1700[0]
	
;check the 1700 middle image is the same time as the 1550 middle image
;print,ind_1550[64].img_time
;print,ind_1700[63:65].img_time

;create new cube with middle image at beginning 
;for use as reference in alignment
ind_1700_temp = ind_1700
dat_1700_temp = FLTARR(512,512,130)
dat_1700_temp[*,*,0] = dat_1700[*,*,64]
FOR i=1,129 DO dat_1700_temp[*,*,i] = dat_1700[*,*,i-1]
;*** index pointing values DO NOT relate to the image pointings ***
;index pointing values are for pointing of 0th image BUT all images 
;are aligned to the 64th image

;align
ccshifts = TR_GET_DISP(dat_1700_temp,/shift)

;lose reference image at beginning of cube & chop edges
dat_1700_temp = REFORM(dat_1700_temp[6:505,2:509,1:*])		

;despike in time and/or space using Marcus' code
dat_1700_temp = trace_unspike_time(ind_1700_temp,dat_1700_temp,/update_hist)

dat_1700=dat_1700_temp
ind_1700=ind_1700_temp

save,file='aligned_1700.sav',dat_1700,ind_1700,ref_dat_1700,ref_ind_1700

trace_prep2,['trb20040703_155033.mxf','trb20040703_160008.mxf'],-1,ind_1700,dat_1700,$
		/wave2point_correct,/cleanjpg

dat_1600 = trace_unspike_time(ind_1600,dat_1600,/update_hist)
dat_1700 = trace_unspike_time(ind_1700,dat_1700,/update_hist)

;(3)
psf=[[1./16.,1./8,1./16.],[1./8.,1./4.,1./8.],[1./16.,1./8.,1./16.]]

for i=0,127 do dat_1600(*,*,i) = convolve(dat_1600(*,*,i),psf)
for i=0,128 do dat_1700(*,*,i) = convolve(dat_1700(*,*,i),psf)
for i=0,128 do dat_1550(*,*,i) = convolve(dat_1550(*,*,i),psf)
;need to do same for MDI

;(4)
ref_1600=REFORM(dat_1600(*,*,63))
align_cube2,dat_1600,aligned_1600,ref=ref_1600
aligned_1600=reform(aligned_1600(6:505,2:509,*))
ref_1600=REFORM(ref_1600(6:505,2:509))
align_cube2,aligned_1600,dat_1600,ref=ref_1600

ref_1700=REFORM(dat_1700(*,*,64))
align_cube2,dat_1700,aligned_1700,ref=ref_1700
aligned_1700=reform(aligned_1700(6:505,2:509,*))
ref_1700=REFORM(ref_1700(6:505,2:509))
align_cube2,aligned_1700,dat_1700,ref=ref_1700

ref_1550=REFORM(dat_1550(*,*,64))
align_cube2,dat_1550,aligned_1550,ref=ref_1550
aligned_1550=reform(aligned_1550(6:505,2:509,*))
ref_1550=REFORM(ref_1550(6:505,2:509))
align_cube2,aligned_1550,dat_1550,ref=ref_1550

offset1600 = alignoffset( ref_1600, ref_1700 )

offset1600 = alignoffset( ref_1600, ref_1700 )

tst=dat_1550
ftst=fft(tst)

tst=fltarr(512,512,129)
FOR i=0,6 do tst(*,*,i)=dat_1600(*,*,i)
FOR i=8,128 do tst(*,*,i)=dat_1600(*,*,i-1)
tst(*,*,7)=(dat_1600(*,*,6)+dat_1600(*,*,8))/2.
dat_1600=tst
delvarx,tst