%function [mission,rng] = surveyAreaEZ(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,rng] = surveyAreaEZ(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);

% 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));

% 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
mission(1) = [];

% set range
rng = [rng1,rng2,rng3];