function output = process_files(index_file)
%
% output = process_files(lambda_min, lambda_max, filename_file)
% lambda = desired wavelength vector (in nm units).
% filename_file is name of file containing all spectra to be processed.
% Input spectra files MUST be .MAT files, with variable 'spectrum'
% Must have filenames on top row.

%initialize variables etc.
lambda_min = 400;
lambda_max = 1000;
l = lambda_min:lambda_max;

imin = find(lambda == lambda_min);
imax = find(lambda == lambda_max);

M = [450 500 550 600 650 700 750 800 850 900 950];
W =[50  50  50  50  50  50  50  50  50  50  50];
for i = 1:length(X)
   T(:,i) = feature_template(l, X(i), dX(i))';
end

% start reading and processing files;
fid = fopen(filename_file,'rt');
L = readline(fid);
Err = 0;
i = 1;

while (L > 0 & Err == 0)
   S = parseline(L);
   
   % read next file name
   fname = [S.filename '.mat'];
   ld = ['load ' fname];
   fail = 'Err = 1';
   eval(ld,fail);
   
   if (Err)
      disp(['Cannot load file ' S.filename]);
      break;
   end
   
   % extract features features S1, S2, ....
   for j = 1:length(X)
      C(j)=correlation(T(:,j),spectrum(imin:imax));
   end
   [R,G,B] = rgb(spectrum);
   R = R/1000; G = G/1000; B = B/1000;
   Intensity = R+G+B;
   r = R/Intensity;
   g = G/Intensity;
   b = B/Intensity;
   
   % plot the bunch
   plot(lambda,spectrum, 'm');
   axis([lambda_min lambda_max,-1,1.5]);
   title([S.filename ' - ' S.class])
   hold on
   plot(X,C,'k');
   bar([550 600 650],[R 0 0],'r');
   bar([550 600 650],[0 G 0],'g');
   bar([550 600 650],[0 0 B],'b');
   hold off
   drawnow
   pause
   
   % create output matrix
   output(i).filename = S.filename;
   output(i).class = S.class;
   output(i).id1 = S.id1;
   output(i).type = S.id2;
   output(i).specvalues = [C R G Intensity];
      
   L = readline(fid);
   i = i+1;
end

fclose(fid);