function [t, x] = readsummary(filename)
% READDLM    - Read a token delimited file. 
%
%    READDLM reads a token delimited fiel containing only ascii numerical data. It
% expects the first row to contain column headers
%
% Use as: [t, x] = readdlm(filename)
%
% Inputs: filename = name of delimited file
%
% Output: t = time of each row in x
%         x = data

% Brian Schlining
% $Id: readsummary.m,v 1.1 2007/05/23 20:28:41 brian Exp $

%===========
% Open File
%===========
if ~nargin || isempty(filename)
   [infile inpath] = uigetfile('*.*','Select delimited data file');
   if infile == 0
      t = [];
      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

data = dlmread(filename, ' ', countHeaderLines(filename), 0); % Skip the first line but read all the data in the file

t = utc2sdn(data(:, 1));
x = data(:, 2:end);


%====================================================
% function countHeaderLines
% count the first block of lines beginning with '#'
%====================================================
function c = countHeaderLines(filename)

c = 0;
fid = fopen(filename);
while ~feof(fid)
   line = fgetl(fid);
   if ~isempty(line) 
       k = strfind(line, '#');
       if isempty(k) || k(1) ~= 1
          break; 
       end
       c = c + 1;
   end
end
fclose(fid);
