%function mission = surveyCrissCross(lat0,lon0,lat1,lon1,lat2,lon2, ...
%	rng1,rng2,rng3,depth,rpm,nGps,transittime)
%  lat0			first corner latitude
%  lon0			first corner longitude
%  lat1			second corner latitude
%  lon1			second corner longitude
%  lat2			third corner latitude
%  lon2			third corner longitude
%  sonarrange	sonar range from vehicle (meters)
%  naverr		navigation error as function of distance travelled (percent)
%  depth		depth of survey (meters)
%  rpm			rpm of tailcone (revolutions per minute)
%  nGps			GPS hits per trackline (-n,...-2,-1,0,1,2,...,n)
%				where negative values skip n legs between fixes
%  transitdist  distance to transit to first waypoint
function mission = surveyCrissCross(lat0,lon0,lat1,lon1,lat2,lon2, ...
	sonarrange,naverr,depth,rpm,nGps,transitdistance)

mission = [];

if naverr < 0 | naverr > 100,
	errorbox('Navigation error must be a percent (from 0 to 100).')
	return
end

if sonarrange <= 0 | sonarrange > 1000,
	errorbox('Sonar range is not valid.  Must be between 0 and 1000 meters')
	return
end

% calculate trackline lead-in length
pitch = 20; %degrees
minLeadIn = 50; %meters
rng1 = max(approximate(depth/tan(pitch/180*pi)),minLeadIn);

% choose calculation method for track spacing
%str = 'Use Nav Error or % Sonar Coverage for Calculating Track Spacing?';
%button = questdlg(str,'Choose Method:','Nav Error','% Coverage','Nav Error');
%tsMethod = button;
%if strcmp(button,'% Coverage'),

	%disp('use percent coverage');
	
%else
	% calculate length over which navigation error matters
	geoid = selectGeoid;
	tracklength = distance('rh',lat0,lon0,lat1,lon1,geoid);
	navlength = tracklength + rng1;

	% calculate narrow track-spacing
	altitude = sonarrange/10;
	rng2 = ceil(sonarrange - altitude - 2*naverr/100*navlength);

	% calculate wide track-spacing
	rng3 = ceil(2*(sonarrange - naverr/100*navlength));
%end
% confirm these values with the operator
str = 'Calculate area survey with these inputs?';
str = strvcat(str,' ');
str = strvcat(str,['  Track lead-in length = ' num2str(rng1) ' meters']);
str = strvcat(str,['  Narrow track spacing = ' num2str(rng2) ' meters']);
str = strvcat(str,['  Wide track spacing = ' num2str(rng3) ' meters']);
str = strvcat(str,' ');
button = questdlg(str,'Confirm the track spacing:');
if strcmp(button,'Cancel'),
	return
elseif strcmp(button,'No'),
	prompt{1} = 'Track lead-in length (meters)';
	prompt{2} = 'Narrow track spacing (meters)';
	prompt{3} = 'Wide track spacing (meters)';
	defans{1} = num2str(rng1);
	defans{2} = num2str(rng2);
	defans{3} = num2str(rng3);
	answer = inputdlg(prompt,'Edit input parameters',1,defans);
	if isempty(answer), return, end
	for ii=1:length(answer), if isempty(answer{ii}), return, end, end
	rng1 = str2num(answer{1});
	rng2 = str2num(answer{2});
	rng3 = str2num(answer{3});
end

% calculate transit time
transittime = calcDuration(transitdistance,rpm);

% calculate survey mission
mission = surveyArea(lat0,lon0,lat1,lon1,lat2,lon2, ...
	rng1,rng2,rng3,depth,rpm,nGps,transittime);

% delete controlsonaredgetech, sonarsystempoweroff behavior and
% delete extra getgps behavior
mission(1) = [];

numlegs = getLegs('get',0);
numgps = getGpsLegs('get',0);

if ~mod(numlegs,2),    % even number of legs
	lat4 = lat0;
	lon4 = lon0;
	lat5 = lat1;
	lon5 = lon1;
else
	lat4 = lat1;
	lon4 = lon1;
	lat5 = lat0;
	lon5 = lon0;
end

% need to get initlat and initlon of final trackline from first mission
% initialn,initiale,initlat,initlon
for index=1:length(mission),
	name = lower(mission{index}.name);
	
	if strcmp(name,'trackline'),
		bstruct = mission{index};
		if isfield(bstruct,'initiallat') & isfield(bstruct,'initiallong'),
			lon3 = bstruct.initiallong;
			lat3 = bstruct.initiallat;
		elseif isfield(bstruct,'initialn') & isfield(bstruct,'initiale'),
			lon3 = bstruct.initiale;
			lat3 = bstruct.initialn;
		end
		break;
	end
end

% confirm these values with the operator
str = 'Calculate 2nd area survey with these inputs?';
str = strvcat(str,' ');
str = strvcat(str,['  Track lead-in length = ' num2str(rng1) ' meters']);
str = strvcat(str,['  Narrow track spacing = ' num2str(rng2) ' meters']);
str = strvcat(str,['  Wide track spacing = ' num2str(rng3) ' meters']);
str = strvcat(str,' ');
button = questdlg(str,'Confirm the track spacing:');
if strcmp(button,'Cancel'),
	return
elseif strcmp(button,'No'),
	prompt{1} = 'Track lead-in length (meters)';
	prompt{2} = 'Narrow track spacing (meters)';
	prompt{3} = 'Wide track spacing (meters)';
	defans{1} = num2str(rng1);
	defans{2} = num2str(rng2);
	defans{3} = num2str(rng3);
	answer = inputdlg(prompt,'Edit input parameters',1,defans);
	if isempty(answer), return, end
	for ii=1:length(answer), if isempty(answer{ii}), return, end, end
	rng1 = str2num(answer{1});
	rng2 = str2num(answer{2});
	rng3 = str2num(answer{3});
end

mission2 = surveyArea(lat3,lon3,lat4,lon4,lat5,lon5, ...
	rng1,rng2,rng3,depth,rpm,nGps,transittime);
% delete controlsonaredgetech, sonarsystempoweroff behavior
mission2(length(mission2)-4:length(mission2)) = [];
mission = [mission2 mission];

numlegs2 = getLegs('get',0);
numgps2 = getGpsLegs('get',0);
getLegs('set',[numlegs numlegs2]);
getGpsLegs('clear');
getGpsLegs('set',[numgps numgps2]);

