function y = readoasis2(Filename)
% READOASIS2 - Read files compiled using the java program CoallateSpecprrFiles or coallateMx
%  This version only reports lines with the max number of values on each line. Lines with
%  less than the max values found are ignored
% 
% Use As: x = readoasis2(Filename)
%      or x = readoasis2
% Inputs: Filename = name of coallated data file
% Output: x        = matrix of data. The first column is the year, the second is the 
%                    decimal day of the year. Refer to the source files for the
%                    secifics on each coloumn of data.
%             

% Brian Schlining
% 22 Apr 1999

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

tic;                                   % Start the stopwatch
fid  = fopen(Filename,'rt');           % Open the file for reading
if fid < 0
   error(['Unable to open file ' Filename])
end

trashheader(fid)                       % Get rid of the Header (i.e. lines that start with '#')
tt = toc;         
disp([num2str(tt) ': Trashed the header'])
p    = ftell(fid);                     % Get the position fo the first data point
c    = ones(50000,1)*NaN;              % Initialize memory to speed things up, we're quessing file size here

%========================================================
% 1st pass is to check the number of inputs on each line
%========================================================
n    = 0;
while ~feof(fid)
   s = fgetl(fid);
   v = sscanf(s,'%f');
   n = n + 1;
   c(n,1) = length(v);
end
tt = toc;
disp([num2str(tt) ': 1st pass complete'])

c    = nanmax(c);                       % Find the longest line. This is assumed to be the number of

% points all the lines shoudl have.
fseek(fid,p,'bof');                    % Rewind to the start of the data

%=======================================
% 2nd pass reads the data into a matrix
%=======================================
%t    = ones(n,1)*NaN;
y    = ones(n,c)*NaN;
l    = n;
n    = 0;
while ~feof(fid)
   s = fgetl(fid);
   v = sscanf(s,'%f')';
   %t = v(end);
   if length(v) == c
      n = n + 1;
      if ~rem(n,1000);tt = toc; disp([num2str(tt) ': Read line ' num2str(n) ' of ' num2str(l)]);end
      y(n,:) = v;
   end


   %y(n,1:length(v-1)) = v(1:(end-1));
end
tt = toc;
disp([num2str(tt) ':2nd Pass complete'])
%d = jul2date(y(:,2),t);
%x = [d y(:,3:end)];

y = snip(y);


fclose(fid);
tt = toc;
disp([num2str(tt) ': Finished'])
%=================================
% function SNIP
% Remove rows that start with NaN
%=================================
function x = snip(x)

i = find(~isnan(x(:,1)));
x = x(i,:);

%====================================================
% function TRASHHEADER
% Remove the first block of lines beginning with '#'
%====================================================
function trashheader(fid)

OK = 1;
n = 0;
while OK
   p = ftell(fid);
   s = fgetl(fid);
   n = n + 1
   if strmatch('#',s)
      OK = 1;
   else
      OK = 0;
   end
   
end
fseek(fid,p,'bof');
