function [rangeScanMatrix, timeStampRange] = parseRangeData(dataset, readAgain)
% Copyright: Ashutosh Saxena, Stanford University, 2005.
% http://ai.stanford.edu/~asaxena/rccar/
% http://ai.stanford.edu/~asaxena/learningdepth/data.html
%
% This file parses the raw laser data to form distance matrices.

if nargin < 2
readAgain = false;
end

global maxGraphicsDistance graphicsScaleFactor

if readAgain
switch dataset
case {'laser1', 'laser2'}
switch lower(dataset)
case 'laser1'
fid = fopen('../LaserData/writelog_2005_01_27_11_15.log');
A = zeros(8364, 727);
%-------magic number-----------
timeInitialSynchro = 1106000000;
case 'laser2'
fid = fopen('../LaserDataFeb9/writelog_2005_02_09_13_34.log');
A = zeros(19773, 727);
%-------magic number-----------
timeInitialSynchro = 1107000000;

end

tline = fgets(fid);
tline = fgets(fid);

%reading laser data
scanNumber = 0;
%for scanNumber = 1:8364
while ~feof(fid)
tline = fgets(fid); %sync line
whichLine = sscanf( tline(26:31), '%s', 1);
if strcmp(whichLine, 'laser')
scanNumber = scanNumber + 1;
A(scanNumber, :) = sscanf( tline(35:length(tline)), ' %lf', inf)';
A(scanNumber, 1) = A(scanNumber, 1) - timeInitialSynchro;
end
end
timeStampRange = A(:,1);
rangeScanMatrix = A(:,6:2:size(A,2));

save(['~/scratch/savedMatFiles/Range/RangeData_' dataset '.mat'], 'timeStampRange', 'rangeScanMatrix');

case {'graphics1', 'graphics2', 'graphics3', 'graphics4', 'graphics5', 'graphics6', 'graphics7', 'graphics8', 'graphics9', ...
'graphics10', 'graphics11', 'graphics12'}
fid = fopen(['~jmichels/shared/newSimImages/' num2str(sscanf(dataset(9:10),'%d')) '/depthOutputFile']);
A = zeros(1000, 17);
fgets(fid);
%reading graphics data
scanNumber = 0;
%for scanNumber = 1:1000
while ~feof(fid)
scanNumber = scanNumber + 1;
tline = fgets(fid);
A(scanNumber, 1) = sscanf( tline(42:44), '%d');
position = 1;
while tline(position) ~= ':'
position = position + 1;
end
A(scanNumber, 2:17) = sscanf( tline(position+1:length(tline)), ' %lf,', inf)';
end
timeStampRange = A(:,1);
tmp = exp(A(:,2:size(A,2)));%/graphicsScaleFactor;
%subplot(2,4, str2num(dataset(9)) ); hist( log(tmp(:)), 50);
rangeScanMatrix = (tmp > maxGraphicsDistance) * maxGraphicsDistance + (tmp <= maxGraphicsDistance) .* tmp;
%cat segLog.txt | sed -e "s/img-//g" | sed -e "s/\.jpg://g" | sed -e "s/-/\./g" >
save(['~/scratch/savedMatFiles/Range/RangeData_' dataset '.mat'], 'timeStampRange', 'rangeScanMatrix');
end
else
load(['~/scratch/savedMatFiles/Range/RangeData_' dataset '.mat']);
end

return;