function X = readeqpac(FileLoc, NumOfDays)
% READEQPAC - Reads most of the ecpaq files 
%
% Use as: X = readeqpac
%         X = readeqpac(FileLocation, NumberOfDays)
%         X = readeqpac(FileLocation)
% Inputs: FileLocation = Path and name of the file to be read
%			  (ex. \oasis\eqpac\ep1\co2)
%         NumberOfDays = Specifes the number of days of data to output.
%           for example, if NumOFDays = 30, only the last 30 days of data 
%           in the fileare outputed
%
% Note: The Decimal dates native to the eqpac files are converted to 
% matlab's serial date number format
%
% Requires: DY2DATE

% Brian Schlining
% 29 Dec 1998

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


fid = fopen(FileLoc,'r');
OK  = 1;
while OK		 							% Trash the header
   XS = fgetl(fid);
   if ~strcmp(XS(1),'#')
      break
   end
end
i = 1;
X = [];
while ~feof(fid)						% Read the data
   X(i,:) = sscanf(XS,'%f')';		% Read the data from the line
   XS     = fgetl(fid);				% Get a line from the file
   i      = i + 1;
end
fclose(fid);

if isempty(X);
   X = [];
   return;
end

[rX cX] = size(X);						% Get it's size
D       = dy2date(X(:,1) + 1900);   % Convert decimal years to serial date numbers
DEnd    = max(D);							% Get the last date
X(:,1)  = D;								% Replace decimal year with Matlab's date format

% if NumOfDays is specified trim the data matrix
if nargin == 2
   i       = find(D >= (DEnd - NumOfDays));	% Find all dates within specified period
   X       = X(i,:);
end

