function X = readPtSurUnderway(filename);
% readPtSureUnderway - Read an underway data file from the R/V Pt. Sur
%
% Use as: X = readPtSurUnderway
%         X = readPtSurUnderway(filename)
%
% Inputs: filename = the name of the data file to read. If none is
%           supplied a file selection dialog will appear.
%
% Output: X = structure contianing the following fields:
%            filename: A string of the canononical filename
%            column: cell array containing the headers for each column of data
%            time: Time of each sample (row) in Matlab's date format (see DATENUM)
%            lat: Latitude of each sample (row) of data (decimal degrees +N/-S)
%            lon: Longitude of each sample (row) of data (decimal degrees +E/-W)
%            data: matrix of data. Each row represnts a sample time each column 
%               is a different variable. See the column field to see the headers
%               for each column.

% Brian Schlining
% 11 Aug 2000

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

X.filename = filename;

fprintf(1, 'Reading %s...', filename);

% Get header row
S = fgetl(fid);
c = length(findstr(S, ',')) + 1;
for i = 1:c
   Column{i} = stringtokenizer(S, i, ',');   
end

X.column = Column;

% Locate the important columns
dateC = strmatch('Date',Column);
timeC = strmatch(' GMT time', Column);
latD  = strmatch(' Lat (furuno) degree', Column);
latM  = strmatch(' Lat (furuno) min/sec', Column);
lonD  = strmatch(' Lon (furuno) degree', Column);
lonM  = strmatch(' Lon (furuno) min/sec', Column);

lonA  = strmatch(' Lon (ashtech)', Column);
latA  = strmatch(' Lat (ashtech)', Column);


% Count the number of rows
r = 0;
while ~feof(fid);
   S = fgetl(fid);
   r = r + 1;
end

% Initialize memory
X.time = ones(r,1)*NaN;
X.lat  = ones(r,1)*NaN;
X.lon  = ones(r,1)*NaN;
X.data = ones(r,c)*NaN;

% Rewind to the beginning of the file
fseek(fid,0,-1);
S = fgetl(fid); % skip the header line
rx = 0;
while ~feof(fid)
   S = fgetl(fid);
   rx = rx + 1;
   if ~rem(rx, 1000)
      fprintf(1, '.');
   end
   
   try 
      for cx = 1:c
         %[buf, S]  = strtok(S, ',');
         buf = stringtokenizer(S, cx, ',');
         switch cx
         case dateC
            dateBuf = buf;
            X.data(rx, cx) = NaN;
         case timeC
            X.time(rx) = datenum([dateBuf ' ' buf]);
            X.data(rx, cx) = NaN;
         case latA
            DD = sscanf(stringtokenizer(buf, 1), '%f');
            MM = sscanf(stringtokenizer(buf, 2), '%f');
            X.data(rx, cx) = DD + MM./60;
         case lonA
            DD = sscanf(stringtokenizer(buf, 1), '%f');
            MM = sscanf(stringtokenizer(buf, 2), '%f');
            X.data(rx, cx) = DD + MM./60;
         otherwise
            X.data(rx, cx) = sscanf(buf, '%f');
         end
      end
   catch
      warning(sprintf('Unable to read line %i \n%s\n', rx+1, S));
   end
   
end

fclose(fid);

X.lat = (X.data(:, latD) + X.data(:, latM)./60);
X.lon = (X.data(:, lonD) + X.data(:, lonM)./60)*-1;


fprintf(1, '\n');

