%function mission = qroute(lat,lon,drctn,rng1,rng2,rng3,rng4,depth,rpm, ...
%   nGps,transittime)
%  lat			lat array (degrees)
%  lon			lng array (degrees)
%  drctn		direction (along or cross)
%  rng1			trackline lead-in length (meters)
%  rng2			qroute width (meters)
%  rng3			trackline spacing 1 (meters)
%  rng4			trackline spacing 2 (meters)
%  depth		depth of survey
%  rpm			rpm of tailcone
%  nGps			GPS hits per trackline (-n,...-2,-1,0,1,2,...,n)
%				where negative values skip n legs between fixes
%  transittime  time to transit to first waypoint
function mission = qroute(lat,lon,drctn,rng1,rng2,rng3,rng4,depth,rpm, ...
	nGps,transittime)

mission = [];
numlegs = [];
numgps = [];

if length(lat) ~= length(lon),
	errorbox('Must have equal number of latitude and longitude points')
	return
end

if length(lat) < 2,
	errorbox('Must have at least two latitude and longitude points')
	return
end

geoid = selectGeoid;
stop = 0;
for ii=1:length(lat)-1,
	
	if strcmp(drctn,'along'),
		leglength = distance('rh',lat(ii),lon(ii),lat(ii+1),lon(ii+1),geoid);
		az = azimuth('rh',lat(ii),lon(ii),lat(ii+1),lon(ii+1),geoid);
		[lat0,lon0] = reckon('rh',lat(ii),lon(ii),rng2/2,az-90,geoid);
		nLegs = ceil(rng2/(rng3+rng4)*2);
		if mod(nLegs,2), nLegs = nLegs+2;
		else nLegs = nLegs+1; end
		
		averspacing = (rng3+rng4)/2;
		nLegs = checkNLegs(nLegs,rng1,leglength,averspacing,depth,nGps,180,rpm);
		if isempty(nLegs), stop = 1; end

		if ~stop,
			pause(0.1)
			[tmp,segment] = survey(lat0,lon0,rng1,leglength,rng3,rng4,az,depth, ...
				rpm,nGps,nLegs,0);
			segment{1}.duration = max(calcDuration(rng2/2,rpm),300);
			mission = [mission segment];			
		end
	else
		d = distance('rh',lat(ii),lon(ii),lat(ii+1),lon(ii+1),geoid);
		az = azimuth('rh',lat(ii),lon(ii),lat(ii+1),lon(ii+1),geoid);
		[lat0,lon0] = reckon('rh',lat(ii),lon(ii),rng2/2,az-90,geoid);
		az = az+90;
		nLegs = ceil(d/(rng3+rng4)*2);
		if mod(nLegs,2), nLegs = nLegs+1; end

		averspacing = (rng3+rng4)/2;
		nLegs = checkNLegs(nLegs,rng1,rng2,averspacing,depth,nGps,180,rpm);
		if isempty(nLegs), stop = 1; end
		
		if ~stop,
			pause(0.1)		
			[tmp,segment] = survey(lat0,lon0,rng1,rng2,-rng3,-rng4,az,depth, ...
				rpm,nGps,nLegs,0);						
			segment{1}.duration = max(calcDuration(rng2/2,rpm),300);
			mission = [mission segment];
		end
	end
	numlegs = [numlegs getLegs('get',0)];
	numgps = [numgps getGpsLegs('get',0)];
	if stop, break, end
end

if isempty(mission), return, end

% create transit to start and turn on sonar
transit{1} = getgps;
transit{2} = mission{1};
transit{2}.duration = transittime;
transit{2}.depth = min(depth,3);
descendDistance = depth/tan(20/180*pi);
if rng1 < descendDistance,
	[transit{2}.latitude,transit{2}.longitude] = reckon('rh',mission{1}.latitude, ...
		mission{1}.longitude,descendDistance,az-180,geoid);
end
transit{3} = ascend(0,rpm,depth);
transit{4} = getgps;
transit{5} = controlsonar('normal');

% set descent time for first waypoint
mission{1}.duration = calcDuration(0,depth,15,rpm);

% add transit
mission = [transit mission];

% power down sonar
mission{end+1} = controlsonar('lowpower');

% delete empties
ind = [];
for ii=1:length(mission),
	if isempty(mission{ii}), ind = [ind ii]; end
end
mission(ind) = [];

% reverse order
mission = fliplr(mission);
getLegs('clear');
legsnumber = getLegs('set',numlegs)
getGpsLegs('clear');
gpslegsnumber = getGpsLegs('set',numgps)
