function MetalDetectorCasefile(index_file, casefilename)

% MetalDetectorCasefile(index_file, casefilename)
%
% Process data in index_file to generate a Netica casefile to train metal detector
% cpt's.  Index file is a text file,
% each column representing a field, each row a new observation.

% Liam Pedersen, NASA ARC/CMU 1998

%**************************
%initialize variables etc.
%**************************

%************************************************************
% spectral feature extraction global data

%*****************************************************************
%*****************************************************************

% casefile header labels
CaseFileLabels = 'IDnum sample_id  basic_class  classification  formation  metal  NumCases  ';

% open CASE file for output.
casefile = OpenCaseFile(casefilename, CaseFileLabels);

% Open INDEX file and process each line
Err = 0; i = 1;
fid = OpenDataFile(index_file, 3);
L = readline(fid);

while (L > 0)
   S = ParseDataFileLine(L);
   
   
   %********************************
   % Process METAL DETECTOR (if possible).
   
   MetalErr = 1;
   if (S.metalfile(1) ~= '*')
      % load spectra and extract feature vector: (local function)
      X = ReadMetal(S.metalfile);
      if (X > -10)  % data read successfully....
         MetalErr = 0;
         
         % compute pdf from this data set
         [x,pdf] = MetalDetectorPDF(X, 40);

         % write first fields of S
         for i = 1:length(x)
            fprintf(casefile,'%d\t', S.idnum);
            fprintf(casefile,'%s\t', S.sample_id,S.basic_class,S.classification, S.formation);
            freq = round(100*pdf(i));
            fprintf(casefile,'%g  %g  \n', x(i), freq);
         end
         
      else
         MetalErr = 1;
         disp('Unable to read metal detector data from file');
         S.specfile
      end
   end
   
   drawnow
   L = readline(fid);
end

% close I/O files
fclose(fid);
fclose(casefile);


