function D = readSpmrLwn(fileIn)
% readSpmrLwn   - Read seaBASS formatted files that do not contain time series
%
% Use as: s = readSpmrLwn(filename)
%
% Inputs: filename = name of the file to read
% Output: s = structure containing the header info and data from filename
%             s.data(:,1) contaings the date (as matlab date numbers), while
%             s.data(:,2:end) contains the data values

% Brian Schlining
% 24 Jan 2001

%===========
% 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
j = 0;
comments = [];
fieldBuf   = [];                             % For storing column headers; only works if last '#' comment contains them
n     = 0;                                % Line counter
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(stringtokenizer(S,1), 'SATHDR')                   % Read the header
      n = n + 1;
      ID = deblank(stringtokenizer(S, 2));
      data = deblank(S(9+length(ID):end));
      if ~isempty(data)
         D = setfield(D,ID, data);
      end

   elseif strcmp(S(1),'#')          % Catch special comments
      start = min(find(abs(S) >= 65 & abs(S) <= 122));
      fieldBuf   = S(start:end);    % the last comment line contains the column headers so hang on to this to parse later
      comments = [comments sprintf('%s\n', S)];

   else                             % Read Data

      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

fclose(fid);

% Parse the column header out of the last comment, which is stored in fieldBuf
delim = sprintf('\t');
n = 1;
buf = stringtokenizer(fieldBuf, n, delim);
while(~isempty(buf))
   field{n} = buf;
   n = n + 1;
   buf = stringtokenizer(fieldBuf, n, delim);
end


D.comments_ = comments;
D.fields = field;
D.data = dat;


