function X = readoasismicrocat(filename)
% READMICROCAT

%===========
% Open File
%===========
if ~nargin
   [infile inpath] = uigetfile('*.asc','Select Ascii grid file',0,0);
   if infile == 0
      X = [];
      return                            % Return if CANCEL is selected
   end
   filename = [inpath infile];
end

%===========
% Read File
%===========
fid    = fopen(filename,'r');				 % Open File

% Count the number of lines
numLines = 0;
while ~feof(fid)
   S = fgetl(fid);
   if ~strcmp(S(1), '#')
      numLines = numLines + 1;
      if numLines == 1
         numCols = length(findstr(S, ',')) % Number of clumns in data file
      end
      
   end
end

% rewind to the start of the file
fseek(fid,0,-1);

% Initialize memory
X = ones(numLines, 3)*NaN;

% Read the data
currentLine = 0;
while ~feof(fid)
   S = fgetl(fid);
   if ~strcmp(S(1), '#')
      start = min(findstr(S, ' '));
      S = S(start:end);
      currentLine = currentLine + 1;
      try
         X(currentLine, 2) = sscanf(stringtokenizer(S, 1, ','), '%f');
         X(currentLine, 3) = sscanf(stringtokenizer(S, 2, ','), '%f');
         X(currentLine, 1) = datenum([stringtokenizer(S, numCols, ',') ' ' stringtokenizer(S, numCols+1, ',')]);
      catch
         fprintf(1, 'Bad line at line %i\n', currentLine)
      end
   end
end

fclose(fid);
