function X = readTowFish(filename)
% READTOWFISH   - Read FSI data files
%
% NOTES: The FSI test file had occasional 'weird' characters at the 
% start of some lines. If an error message pops up saying 'Unable to parse string ...'
% its probably becuase of this. To fix the data file go to the line specified in the 
% error message and delete the first character. 

% Brian Schlining
% 24 Jul 2000

%================
% Default values
%================
%token = sprintf('\t');
token = ',';
initMem = 100000;       % This should be set to a number greater than the number of rows in the data file


%===========
% Open File
%===========
if ~nargin
   [infile inpath] = uigetfile('*.*','Select FSI data file',0,0);
   if infile == 0
      X = [];
      return                            % Return if CANCEL is selected
   end
   filename = [inpath infile];
end

fid  = fopen(filename,'rt');
if fid < 0
   error(['Unable to open file ' filename])
end

fprintf(1, 'Reading %s...', filename);

%===================
% Ignore the header
%===================
while ~feof(fid)
   S = fgetl(fid);
   if strmatch('"scans"', lower(S))
      break
   end
end

%====================================
% Get the name of each column header
%====================================
n = 0;
nameBuf = '-';                                 % Placeholder, the value here could be anything
while(~isempty(nameBuf))
   n = n + 1;
   nameBuf = lower(stringtokenizer(S, n, token)); % Parse out each column header and save it
   if strmatch('"',nameBuf)
      nameBuf([1 end]) = [];                      % Chop off the quotes
   end
   if ~isempty(nameBuf)
      Name{n} = stringtokenizer(nameBuf, 1, '*'); % Remove any trailing asterisks
   end
   
end
numFields = length(Name);                      % Count the number of columns

%===================
% Initialize memory
%===================
data     = ones(initMem,numFields)*NaN;
dateBuf = cell(initMem, 1);
timeBuf = cell(initMem, 1);

%===============
% Read the file
%===============
n = 0;
while ~feof(fid)
   S = fgetl(fid);
   n = n + 1;
   if ~isempty(S)
      for i = 1:numFields
         buf = stringtokenizer(S, i, token);
         switch Name{i}
         case 'date'                                   % Store the date string
            if strmatch('"',buf)
               buf([1 end]) = [];                      % Chop off the quotes
            end
            dateBuf{n} = buf;
         case 'time'                                   % Store the time string
            if strmatch('"',buf)             
               buf([1 end]) = [];                      % Chop off the quotes
            end
            timeBuf{n} = buf;
         otherwise
            data(n,i) = sscanf(buf, '%f');             % Store data
         end
      end
   end
   if ~rem(n ,1000)
      fprintf(1, '.');
   end
   
   
end

fprintf(1, '\n');
X.tag = 'FSI';
X.filename = filename;
X.columns = cell(1, numFields - 2);
X.data = ones(n, numFields - 2)*NaN;
m = 0;
for i = 1:numFields
   if strcmp(Name{i}, 'time') | strcmp(Name{i}, 'date')
      % Do nothing
   else
      m = m + 1;
      X.data(:,m) = data(1:n, i);
      X.columns{m} = Name{i};      
   end
end

% Convert string dates to datenumbers
X.time = datenum([char(dateBuf(1:n)) char(timeBuf(1:n))]);

