%function [mission,notransit] = survey(lat0,lon0,rng1,rng2,rng3,rng4,az,depth, ...
%	rpm,nGps,nLegs,transittime)
%  lat0			start lat (degrees)
%  lon0			start lon (degrees)
%  rng1			trackline lead-in length (meters)
%  rng2			trackline length (meters)
%  rng3			trackline spacing 1 (meters)
%  rng4			trackline spacing 2 (meters)
%  az			azimuth (degrees clockwise from North)
%  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
%  nLegs		number of tracklines (integer)
%  transittime  time to transit to first waypoint
function [mission,notransit] = surveyOneway(lat0,lon0,rng1,rng2,rng3,rng4,az,depth, ...
	rpm,nGps,nLegs,transittime)
	global lastWaypoint
	
mission = [];
lastWaypoint = [];

if rng1 <= 0,
	errorbox('Can not use negative track lead-in.')
	return
end

if rng2 <= 0,
	errorbox('Can not use negative track length.')
	return
end

geoid = selectGeoid;
getLegs('set',nLegs);
getGpsLegs('clear');
lat = lat0;
lon = lon0;
gps = nGps;
transitDepth = min(5,depth);
z = 1;
h = waitbar(0,'Calculating Mission, Please wait...');
for i=1:nLegs,
	if nGps < 0,
		if rem(z,abs(nGps)+1) == 0, gps = 1;
		else, gps = 0; end
		z = z+1;
	end
	
    turn = 90;
    spacing = rng3;
    
	[leg,endlat,endlon] = surveyLeg(lat,lon,rng1,rng2,az,depth,rpm,gps,transitDepth,turn,spacing,geoid);
	mission = [mission leg];
	%if rem(i,2),
	%turn = 90;
    %spacing = rng3;
    %else
	%	turn = -90;
	%	spacing = rng4;
    %end
	[lat,lon] = reckon('rh',endlat,endlon,spacing,az+turn,geoid);
	%az = mod(az+180,360);
	getGpsLegs('set',gps);
	waitbar(i/nLegs,h)
end
close(h)

% create transit to start and turn on sonar
transit{1} = getgps;
transit{2} = mission{1};
transit{2}.depth = min(depth,3);
transit{2}.duration = transittime;
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
notransit = mission;
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);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [leg,endlat,endlon] = surveyLeg(lat0,lon0,rng1,rng2,az,depth,rpm,nGps, ...
	transitDepth,turn,spacing,geoid)
	global lastWaypoint

leg = [];
j = 0;
n = 6 + 2*(nGps > 0);
[lat,lon] = reckonTrackline(lat0,lon0,rng1,rng2,az,nGps);
for i=1:4:length(lat),
	if ~isempty(lastWaypoint),
		duration = calcDuration(lat(i),lon(i),lastWaypoint(1),lastWaypoint(2),rpm);
		leg{j*n+1} = waypointgeo(lat(i),lon(i),depth,rpm,duration);
	else
		leg{j*n+1} = waypointgeo(lat(i),lon(i),depth,rpm);
	end
	leg{j*n+2} = tracklinegeo(lat(i),lon(i),lat(i+1),lon(i+1),depth,rpm);
	leg{j*n+3} = controlsonar('on');
	leg{j*n+4} = tracklinegeo(lat(i+1),lon(i+1),lat(i+2),lon(i+2),depth,rpm);
	leg{j*n+5} = controlsonar('off');
	leg{j*n+6} = tracklinegeoP(lat(i+2),lon(i+2),lat(i+3),lon(i+3),0,rpm);
    
    [templat,templon] = reckon('rh',lat(i+3), lon(i+3),spacing,az+turn,geoid);
    
    duration = calcDuration(lat(i+3),lon(i+3),templat,templon,rpm);
    leg{j*n+7} = waypointgeoP(templat,templon,5,rpm,duration);
    
    duration = calcDuration(templat,templon,lat0,lon0,rpm);
    leg{j*n+8} = waypointgeo(lat(1),lon(1),transitDepth,rpm,duration);
    
    lastWaypoint = [lat0 lon0];
    
	if nGps == 0, break, end
	leg{j*n+9} = ascend(az,rpm,depth);
	leg{j*n+10} = getgps;
	j = j+1;
end
endlat = lat0;%lat(end-1);
endlon = lon0;%lon(end-1);