function D = readseabass2(fileIn)
% READSEABASS2 - Read seaBASS formatted files
%
% Use as: s = readseabass2(filename)
%
% Inputs: filename = name of the file to read
% Output: s = A structure containing the header info and data contained int
%             the file specified in 'filename'.
%
% Refer to the SeaBASS web site for more information on the seaBASS data format

% Copyright (c) 1999 by Monterey Bay Aquarium Research Institute
% Brian Schlining
% 12 May 1999
% 19 Aug 1999; First working version
% 27 Sep 2000; Bug fix; Pointer doesn't always rewind to correct spot. Added try/catch

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

%===========
% Read File
%===========

% Several different index pointers to keep track of
n     = 0;                                % Line counter
firstPass = 1;
D.filename = fileIn;                      % Store the filename
fid = fopen(fileIn,'rt');                 % Open it for reading
if fid < 0
   warning(['Invalid file name: ' fileIn])
   D = [];
   return
end


while ~feof(fid)                          % Loop through the file
   %p = ftell(fid);                       % Get the start of each line
   S = fgetl(fid);                        % Get the line

   if strcmp(S(1),'/') & ~firstPass                   % Read the header
      % Do nothing
   elseif strcmp(S(1),'/') & firstPass                   % Read the header
      n = n + 1;
      ID = deblank(stringtokenizer(S, 1, '='));
      ID = ID(2:end);
      warning('off')
      Data = deblank(stringtokenizer(S, 2, '='));
      warning('on')
      if ~isempty(Data)
         switch ID                        % Catch special attributes

         case 'fields'                    % parse the fields apart

            columns = length(findstr(Data, ',')) + 1;

            for j = 1:columns
               T = stringtokenizer(Data, j, ',');
               fields{j} = T;
            end

            D = setfield(D,ID, fields);

         case 'units'
            columns = length(findstr(Data, ',')) + 1;
            for j = 1:columns
               T = stringtokenizer(Data, j, ',');
               units{j} = T;
            end
            D = setfield(D,ID, units);
         otherwise
            D = setfield(D,ID, Data);
         end
      end

   elseif strcmp(S(1),'!')          % Ignore comments

   else                             % Read Data
      if firstPass
         j = 0;
         d = fread(fid,'int8');             % Scan in the entire file as ascii
         rows = length(find(d == 10))+1;    % Count the return characters
         dat = ones(rows,columns)*NaN;         % Initialize memory
         firstPass = 0;
         ind = 1:columns;%<-------added 12/15
         fseek(fid, 0, -1);                 % rewind the file to the start of the file
      else
         try
            j = j + 1;
            dat(j,:) = sscanf(S, '%f')';             % parse each line of data
         catch
            warning(['Unable to read: ' S])
            j = j - 1;
         end

      end


   end


end
fclose(fid);

n = 0;
while(1)
   if isfield(D, 'missing')
      n = n + 1;
      warning('off')
      bad = stringtokenizer(D.missing, n, ',');
      warning('on')
      if isempty(bad)
         break;
      end
      bad = sscanf(bad, '%f');
      i = find(dat == bad);            % Set missing calues to NaN
      dat(i) = NaN;
   else
      break;
   end
end
D.data = dat;

