function lonLatAlt = transKml(kmlFileName)
%
% PURPOSE: Extract waypoints from a .kml file and generate the outline of
%          a mission file.
%
 if( ~isstr(kmlFileName) )
   fprintf(' Please input the name of the kml file in a string.\n');
   return
 end
 fid = fopen(kmlFileName);
 if( fid == -1 )   
   fprintf(' File %s not found!\n',kmlFileName);
   return
 end
 lineNo=0;
 record = fgetl(fid);
 while( record ~= -1 )
   lastRecord = record;
   record = fgetl(fid);
   if( strfind(lastRecord,'<coordinates>') )
     [lonLatAlt eleRead errorStr]= sscanf(record,'%f,%f,%f',[3,inf]);
     if( errorStr )
       fprintf('Error: %s while reading record %s.\n', errorStr, record);
       return
     end
     fprintf('Found %d coordinate pairs.\n',eleRead/3);
     lonLatAlt = lonLatAlt';
     lineNo = lineNo + 1;
     break;
   end
   lineNo = lineNo + 1;
   %fprintf('lineNo = %d.\n',lineNo);
 end
 if( record == -1 )
   fprintf('Didn''t find coordinates in file %s.\n',kmlFileName);
   lonLatAlt=0;
 end
 fprintf('Read %d lines from file %s.\n',lineNo, kmlFileName);
 fclose(fid);
 %
 % Remove the .kml extension:
 extPtr = strfind(kmlFileName,'.kml');
 missionFileName = kmlFileName(1:extPtr-1);
 %
 % Add the .cfg extension:
 missionFileName = strcat(missionFileName,'.cfg')
 %
 % Open the file:
 fid = fopen(missionFileName,'w')

 transitDist = 500;
 depthAlt = [ 60 8;
	      60 5;
	      60 4;
	      ];
 
 
 

 initSeg(fid);
%fseek(fid,0,'bof')
 transitTime = transit(fid,transitDist);  
 totalTime = totalTime + transTimeOut;

 timeDown = spindown(fid, depthAlt(1,:));
 totalTime = totalTime + timeDown;
 
 %walk the array with waypoint_bottoms
 %totalTime = totalTime + time
 

 spinUp(fid)

%depth envelope 
%mission timer 
 
 
 fclose(fid)
 return;









 function [] = spinUp(fid)
 spinUpTxt = {
     'behavior getgps',
     '{',
     '  duration = 600;',
     '  minHits = 10;',
     '  abortOnTimeout = True;',
     '}',
     'behavior ascend',
     '{',
     '  duration = 300;',
     '  horizontalMode = rudder;',
     '  horizontal = 3.000000;',
     '  pitch = 45.000000;',
     '  speed = 1.500000;',
     '  endDepth = 2.000000;',
     '}',
     'behavior acousticUpdate',
     '{',
     '  duration = 2;',
     '  dummy = 1;',
     '}',
     };
 for( i=1:length(spinUpTxt) )
   fprintf(fid,'%s\n',char(spinUpTxt(i)));
 end
 return
 
 
 function [] = initSeg(fid)
 initSegTxt = {
     'behavior waypoint',
     '{',
     '  latitude = 36.6963;',
     '  longitude = -121.9360;',
     '  duration = 1000;',
     '  depth = 70;',
     '  speed = 1.500000;',
     '}',
     'behavior descend',
     '{',
     '  horizontalMode = rudder;',
     '  horizontal = 3.0;',
     '  pitch = -35.000000;',
     '  speed = 1.500000;',
     '  maxDepth = 20;',
     '  minAltitude = 15;',
     '  duration = 300;',
     '}',
     'behavior descend',
     '{',
     '  horizontalMode = heading;',
     '  horizontal = 270.0;',
     '  pitch = -35.000000;',
     '  speed = 1.500000;',
     '  maxDepth = 3.000000;',
     '  minAltitude = 15.000000;',
     '  duration = 300;',
     '}',
     '#',
     '# setpoint on surface to gather momentum ',
     'behavior setpoint',
     '{',
     '  duration = 30;',
     '  heading = 270.0;',
     '  speed = 1.500000;',
     '  verticalMode = pitch;',
     '  pitch = 0;',
     '}',
     '#', 
     '# acquire gps fix ',
     'behavior getgps',
     '{',
     '  duration = 600;',
     '  minHits = 10;',
     '  abortOnTimeout = True;',
     '}',
 };
 for( i=1:length(initSegTxt) )
   fprintf(fid,'%s\n',char(initSegTxt(i)));
 end
 return
 
 
 
 
 
 
     
