% OMA Dedefect:
%    1.Remove the defects on  spectrum measured by OMA/LS2000. 
%    2.Output 6 plots and files for you to choose one optimized from.
%  Syntax: Dedefect(SourceFilename) ex. Dedefect('0929-2.txt'):
%    SourceFilename:
%      spectrum file(ex. '0929-2.txt')
%      double-column ASCII file 
%      column 1: wavelength(nm)
%      column 2: spectrum(count)
%    output files (ex. '0929-2-Dedefect3.txt'):
%        three-column ASCII file                                   
%        column 1: wavelength(nm)
%        column 2: background corrected spectrum (count)
%        column 3: normalized spectrum (A.U.)
%    Auther: Hai-N Tsai (2002/9/24)

function result = Dedefect(SourceFilename)
%load the spectrum and wavelength
temp=load(SourceFilename);
spectrum=temp(:,2);
wavelength=temp(:,1);
%load background spectrum and norlmalizing factor
temp=load('NormalizedFactor_BackgroundSpectrum.txt');
Normalized=temp(:,1);
NormalizingFactor=Normalized(4:878);
BackgroundSpectrum=temp(:,2);
%Spectrum-BackgroundSpectrum
a=spectrum-BackgroundSpectrum;
%shift spectrum -2 to 3 pixels
a1=a(2:876);
a2=a(3:877);
a3=a(4:878);
a4=a(5:879);
a5=a(6:880);
a6=a(7:881);
%shift background -2 to 3 pixels
bg1=BackgroundSpectrum(2:876);
bg2=BackgroundSpectrum(3:877);
bg3=BackgroundSpectrum(4:878);
bg4=BackgroundSpectrum(5:879);
bg5=BackgroundSpectrum(6:880);
bg6=BackgroundSpectrum(7:881);
%shift wavelength -2 to 3 pixels
wavelength1=wavelength(2:876);
wavelength2=wavelength(3:877);
wavelength3=wavelength(4:878);
wavelength4=wavelength(5:879);
wavelength5=wavelength(6:880);
wavelength6=wavelength(7:881);
%compensate the defect
Y1=(a1-bg1).*NormalizingFactor;
Y2=(a2-bg2).*NormalizingFactor;
Y3=(a3-bg3).*NormalizingFactor;
Y4=(a4-bg4).*NormalizingFactor;
Y5=(a5-bg5).*NormalizingFactor;
Y6=(a6-bg6).*NormalizingFactor;
%plot 
subplot(2,3,1),plot(wavelength1, Y1),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 1');
subplot(2,3,2),plot(wavelength2, Y2),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 2');
subplot(2,3,3),plot(wavelength3, Y3),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 3');
subplot(2,3,4),plot(wavelength4, Y4),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 4');
subplot(2,3,5),plot(wavelength5, Y5),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 5');
subplot(2,3,6),plot(wavelength6, Y6),xlabel(' wavelength (nm)'), ylabel('count'),title('plot 6');
figure;
plot(wavelength3,a3) , xlabel('wavelength (nm)'), ylabel('count'),title('original spectrum');

%find the maximum of Y
M1 = max(Y1);
M2 = max(Y2);    
M3 = max(Y3);
M4 = max(Y4);
M5 = max(Y5);
M6 = max(Y6);
%normalize Y
Z1 = Y1./M1;
Z2 = Y2./M2;
Z3 = Y3./M3;
Z4 = Y4./M4;
Z5 = Y5./M5;
Z6 = Y6./M6;
%ask the user which file he prefers
k=menu('choose a plot in figure No.1:','plot1','plot2','plot3','plot4','plot5','plot6' )
%output
if k==1
output1     = wavelength1;
output1(:,2)= Y1;
output1(:,3)= Z1;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect1.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output1');
fclose(fid);
elseif k==2
output2     = wavelength2;
output2(:,2)= Y2;
output2(:,3)= Z2;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect2.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output2');
fclose(fid);
elseif k==3
output3     = wavelength3;
output3(:,2)= Y3;
output3(:,3)= Z3;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect3.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output3');
fclose(fid);
elseif k==4
output4     = wavelength4;
output4(:,2)= Y4;
output4(:,3)= Z4;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect4.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output4');
fclose(fid);
elseif k==5
output5     = wavelength5;
output5(:,2)= Y5;
output5(:,3)= Z5;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect5.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output5');
fclose(fid);
elseif k==6
output6     = wavelength6;
output6(:,2)= Y6;
output6(:,3)= Y6;
temp = strtok(SourceFilename,'.');
OutputFilename = strcat(temp,'-Dedefect6.txt');
fid = fopen(OutputFilename,'w');
fprintf(fid, '%g  %g  %g\n' ,output6');
fclose(fid);
end