%function [out,mission,routeFile] = writeMaptechFile(mission,routeFile)
%  mission must be in geo not utm
function [out,mission,routeFile] = writeMaptechFile(mission,routeFile)
	global numWaypoint, global fid
	
numWaypoint = 0;

out = checkMissionFile(mission,1);
if ~out, return, end
out = 0;

if nargin == 1,
	routeFile = [];
	[fname,pname] = uiputfile(['routes32.txt'],'Save route to:');
	if fname == 0, return, end
	routeFile = [pname fname];
else
	fname = fliplr(strtok(fliplr(routeFile),'\/'));
end

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

firstLeg = 1;
writeRouteHeader(mission)
for i=length(mission):-1:1,
	switch lower(mission{i}.name)
	case 'waypoint'
		lat = mission{i}.latitude;
		lon = mission{i}.longitude;
		if ischar(lat), lat = str2num(lat); end
		if ischar(lon), lon = str2num(lon); end
		writeWaypoint(lat,lon,mission{i},i)
		firstLeg = 1;
	case 'trackline'
		if firstLeg,
			ilat = mission{i}.initiallat;
			ilon = mission{i}.initiallong;
			if ischar(ilat), ilat = str2num(ilat); end
			if ischar(ilon), ilon = str2num(ilon); end	
			writeWaypoint(ilat,ilon,mission{i},i)
			firstLeg = 0;
		end
		lat = mission{i}.latitude;
		lon = mission{i}.longitude;
		if ischar(lat), lat = str2num(lat); end
		if ischar(lon), lon = str2num(lon); end	
		writeWaypoint(lat,lon,mission{i},i)		
	end
end

fclose(fid);
uiwait(msgbox(['Saved route to ' routeFile],'modal'))
out = 1;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function n = countWaypoints(mission)
n = 0;
firstLeg = 1;
for i=1:length(mission),
	switch lower(mission{i}.name)
	case 'waypoint'
		n = n+1;
		firstLeg = 1;
	case 'trackline'
		if firstLeg,
			n = n+1;
			firstLeg = 0;
		end
		n = n+1;
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function writeRouteHeader(mission)
	global fid
c1 = '4003';										%?	
c2 = '0';											%?
c3 = '1';											%?
c4 = '0';											%?
c5 = ['48sgw' mission{1}.name ' ' datestr(now,0)];	%Long name
if isfield(mission{1},'description'), c6 = mission{1}.description;
else c6 = ' '; end									%Note
c7 = int2str(countWaypoints(mission));				%Num waypoints
c8 = '500';											%?
c9 = '0';											%?
c10 = '0';											%Open (0) or closed (1)
c11 = ['RTE' upper(strtok(mission{1}.name,'.'))];	%Short name
fprintf(fid,[c1 '\t' c2 '\t' c3 '\t' c4 '\t' c5 '\t' c6 '\t' c7 ...
	'\t' c8 '\t' c9 '\t' c10 '\t' c11(1:6) '\n']);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function writeWaypoint(lat,lon,bstruct,n)
	global fid, global numWaypoint
numWaypoint = numWaypoint + 1;
if ischar(lat), lat = str2num(lat); end
if ischar(lon), lon = str2num(lon); end
c1 = '4003';										%?
c2 = int2str(numWaypoint);							%Waypoint number
c3 = '0';											%?
c4 = mynum2str(abs(lat*60));						%Latitude*60
c5 = mynum2str(abs(lon*60));						%Longitude*60
c6 = bstruct.name;									%Long name						
if isfield(bstruct,'description'), c7 = bstruct.description;
else c7 = ' '; end									%Note			
c8 = '0';											%?
c9 = '0';											%?
c10 = '0';											%?
c11 = '0';											%?
c12 = '0';											%?
c13 = '0';											%?
c14 = ['WP' int2str(n) upper(bstruct.name)];		%Short name
fprintf(fid,[c1 '\t' c2 '\t' c3 '\t' c4 '\t' c5 '\t' c6 '\t' c7 ...
	'\t' c8 '\t' c9 '\t' c10 '\t' c11 '\t' c12 '\t' c13 '\t' c14(1:6) '\n']);