function [p, t, s, o] = read_asc(file, ncol, icol);
% function [p, t, s, o] = read_asc(file, ncol, icol);
%
%
% This is for reading flat ascii files that may begin with headers.
% It also can handle comment lines in the middle of the file.
% Any line starting with a non-numeric character is a comment line.
%
% Input:
%     file     name of ascii file.
%     ncol     optional: number of columns in the file
%     icol     optional: vector listing column numbers to output
% Output:
%     a matrix or set of up to 4 vectors read from "file".
%
% Examples:
%     p = read_asc('file.ctd'); reads all columns into matrix p
%     p = read_asc('file.ctd', 13); as above, but requires exactly 13 cols
%     [p, t] = read_asc('file.ctd', 13, [1 3]); reads p from column 1
%                     and t from column 3
%     pt = read_asc('file.ctd', 13, [1 3]); returns columns 1 and 3 in
%                     the matrix, pt.
%
%--------------------------------------------------------------------
%                         Eric Firing
%
% 96/01/14 original version, derived from readflat.m
% 96/06/05 Modified to handle comment lines and to make the
%          second argument (ncol) optional.
% 97/09/17 Modified to handle comma-delimited files.
%


FID = fopen(file, 'rt');
if FID == -1, error(['Can''t open file: ' file]); end

if nargin < 1,
   eval('help read_asc');
   error('read_asc requires either 2 or 3 input arguments');
elseif nargin == 1,
   ncol = 0;
end

first_time = 1;
xxx = [];
while ~feof(FID),

   % Dump header lines and comments.
   finished = 0;
   while ~finished & ~feof(FID),
      start = ftell(FID);
      line = fgets(FID);
      if length(line) > 1,
         icomma = find(line == ',');
         ncomma = length(icomma);
         if ncomma > 0,
            line(icomma) = ' ' * ones(size(line(icomma)));
         end
         [xx, count] = sscanf(setstr(line), '%f', inf);
         % setstr above is needed only for Matlab 4.  Same
         % goes for "* ones(size(line(icomma)))"
         if ncol,
            if count == ncol,
               finished = 1;
            end
         elseif count >= 1,
            ncol = count;
            finished = 1;
         end
         if finished == 1,
            if ncomma == 0,
               fmt_string = '%f';
            elseif ncomma == ncol,
               fmt_string = '%f,';
            elseif ncomma == ncol -1,
               fmt_string = '%f,'.';
               fmt_string = fmt_string(:,ones(1,ncol));
               fmt_string = fmt_string(1:(3*ncol-1));
            else
               finished = 0;
            end
         end
      end
   end

   if ~finished, break, end;  % This exits the loop on
                              %   reaching end of file while
                              %   searching for a valid data
                              %   line.

   % The last line read was not a header or comment.

   if first_time == 1,
      if nargin < 3,
         if nargout == 0 | nargout == 1,
            icol = 1:ncol;
         else
            icol = 1:nargout;
         end
      end

      if max(icol) > ncol,
         error('An index in icol exceeds the number of columns, ncol');
      end
      nc = length(icol);
      first_time = 0;
   end

   % Now back up one line and read the whole file, or until a comment.
   if ftell(FID) ~= start,
      fseek(FID, start, 'bof');
      xx = fscanf(FID, fmt_string, [ncol inf]);
      xxx = [xxx xx];
   end
end      % "while ~feof(FID)"

fclose(FID);

if isempty(xxx),
   p = [];
   disp(['Warning: read_asc found no data in ' file]);
   disp(['   Does this file actually have ' int2str(ncol) ' columns?']);
   return
end

if nargout == 0 | nargout == 1,
   p = xxx(icol,:).';
   return;
end

p = xxx(icol(1),:).';
if nargout > 1,
   if nc > 1,
      t = xxx(icol(2),:).';
   else
      t = NaN * p;
   end
end
if nargout > 2,
   if nc > 2,
      s = xxx(icol(3),:).';
   else
      s = NaN * p;
   end
end
if nargout > 3,
   if nc > 3,
      o = xxx(icol(4),:).';
   else
      o = NaN * p;
   end
end

