%function [mission,surveyFile] = readWinfrogFile(surveyFile)
function [mission,surveyFile] = readWinfrogFile(surveyFile)

mission = [];
if nargin == 0,
	[fname,pname] = uigetfile(['surline.pts'],'Open survey file:');
	if fname == 0, return, end
	surveyFile = [pname fname];
else
	fname = fliplr(strtok(fliplr(surveyFile),'\/'));
end

fid = fopen(surveyFile,'rt');
if fid < 0, disp(['Could not open file ' surveyFile]), return, end

routes = [];
headers = [];
headerNames = [];
waypoints = [];
j = 1;
while ~feof(fid),
	line = line2cell(fgetl(fid));
	if str2num(line{1}) == 0,
		if ~isempty(waypoints),
			routes{j} = waypoints;
			j = j+1;
		end
		headers{j} = readRouteHeader(line);
		headerNames{j} = headers{j}.name;
		waypoints = [];
	else
		waypoints = [waypoints readRouteWaypoint(line)];
	end
end
fclose(fid);

if length(headers) == 0, disp('No valid routes read'), return, end
waypoints = fliplr(waypoints);
routes{j} = waypoints;
headerNames = cellstr(headerNames');

% check for multiple routes in the file
if length(headers) > 1, 
	[s,v] = listdlg('PromptString','Select a route:',...
		'SelectionMode','single','ListString',headerNames);
	if isempty(v), return, end
	header = headers{s};
	waypoints = routes{s};
elseif length(headers) == 1,
	header = headers{1};
	waypoints = routes{1};
end

mission = createMission(header,waypoints);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mission = createMission(header,waypoints)
bdefault = behaviorDefaults;
mission{1} = bdefault{findin(bdefault,'name','worksite')};
mission{2} = bdefault{findin(bdefault,'name','missiontimer')};
mission{3} = bdefault{findin(bdefault,'name','depthenvelope')};
if isempty(findstr(header.name,'.mis')), mission{1}.name = [header.name '.mis'];
else mission{1}.name = header.name; end
mission{1}.author = '*';

if header.numWaypoints == 1,
	mission{4} = newwaypoint(waypoints);
else
	j = 4;
	for i=1:header.numWaypoints-1,
		mission{j} = newtrackline(waypoints(i:i+1));
		j =j+1;
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bstruct = newwaypoint(waypoints)
[bdefault,bnames] = behaviorDefaults;
bstruct = bdefault{indexof(bnames,'waypointgeo')};
bstruct = setfield(bstruct,'latitude',waypoints(2).latitude);
bstruct = setfield(bstruct,'longitude',waypoints(2).longitude);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bstruct = newtrackline(waypoints)
[bdefault,bnames] = behaviorDefaults;
bstruct = bdefault{indexof(bnames,'tracklinegeo')};
bstruct = setfield(bstruct,'initiallat',waypoints(1).latitude);
bstruct = setfield(bstruct,'initiallong',waypoints(1).longitude);
bstruct = setfield(bstruct,'latitude',waypoints(2).latitude);
bstruct = setfield(bstruct,'longitude',waypoints(2).longitude);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = readRouteHeader(line)
out.name = line{2};
out.numWaypoints = str2num(line{7});


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = readRouteWaypoint(line)
out.latitude = str2num(line{2});
out.longitude = str2num(line{3});


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c = line2cell(line)
c = [];
str = [];
while ~isempty(line)
	[t,line] = strtok(line,',');
	if t == -1, return, end
	str = strvcat(str,t);
end
c = cellstr(str);
