function writeKML(filenameBase, ...
                  DUTName, ...
                  MasterName, ...
                  PosError, ...
                  Time, ...
                  TruthLat, ...
                  TruthLon, ...
                  TruthHgt, ...
                  DUTLat, ...
                  DUTLon, ...
                  DUTHgt);

kmlFile = [filenameBase,'.kml'];

fid = fopen(kmlFile,'w');

fprintf(fid,'%s\n',['<?xml version="1.0" encoding="UTF-8"?>']);
fprintf(fid,'%s\n',['<kml xmlns="http://earth.google.com/kml/2.2" ', ...
                    'xmlns:gx="http://www.google.com/kml/ext/2.2" ', ...
                    'xmlns:kml="http://www.opengis.net/kml/2.2">']);

fprintf(fid,'%s\n',['<Document><name>',MasterName,'</name>']);

fprintf(fid,'%s\n',writeKMLMarkerStyle('marker_truth','ffffff'));
fprintf(fid,'%s\n',writeKMLMarkerStyle('marker_DUT','00ff00'));

for i = 1:length(Time)
  fprintf(fid,'%s\n',['<Folder><name>T=',num2str(Time(i),'%.02f'),'</name>']);

  % Place Marker for the truth
  fprintf(fid,'%s\n',writePoint(Time(i), ...
                                'POSPac Truth', ...
                                'marker_truth', ...
                                'Error', ...
                                0, ...
                                TruthLat(i), ...
                                TruthLon(i), ...
                                TruthHgt(i)));

  % Place Marker for Device under test (DUT) position
  fprintf(fid,'%s\n',writePoint(Time(i), ...
                                DUTName, ...
                                'marker_DUT', ...
                                'Error', ...
                                PosError(i), ...
                                DUTLat(i), ...
                                DUTLon(i), ...
                                DUTHgt(i)));

  % Draw a line between the truth and the DUT positions                                
  fprintf(fid,'%s\n',['<Placemark><LineString><coordinates>']);
  fprintf(fid,'%.9f,%.9f,%.3f\n',TruthLon(i), TruthLat(i), TruthHgt(i));
  fprintf(fid,'%.9f,%.9f,%.3f\n',DUTLon(i), DUTLat(i), DUTHgt(i));
  fprintf(fid,'%s\n',['</coordinates></LineString></Placemark>']);
  fprintf(fid,'%s\n',['</Folder>']);
end

% Close the document
docEnd = ['</Document></kml>'];
fprintf(fid,'%s\n',docEnd);
fclose(fid);

% Now convert from KML to KMZ by zipping and renaming
zipFile = [filenameBase,'.zip'];

if ispc
  zip(zipFile,kmlFile);
  system(['del "',kmlFile,'"' ]);
  system(['move "',zipFile,'" "', filenameBase, '.kmz"']);
else
  system(['zip -9 "',zipFile,'" "',kmlFile,'"' ]);
  system(['rm -f "',kmlFile,'"' ]);
  system(['mv "',zipFile,'" "', filenameBase, '.kmz"']);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           LOCAL FUNCTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function outStr = writePoint(time,title,marker,string,err,lat,lon,hgt)

  outStr = ['<Placemark><visibility>1</visibility><description>', ...
            '<![CDATA[<table cellpadding="1" cellspacing="0">'];
  outStr = [outStr,'<tr><th colspan="2">',title,'</th></tr>'];
  outStr = [outStr,'<tr><td>Time</td><td>',num2str(time,'%.03f'),'</td></tr>'];
  if(err ~= 0)
    outStr = [outStr,'<tr><td>',string,'</td><td>',num2str(err,'%.2f'),'m</td></tr>'];
  end
  outStr = [outStr,'<tr><td>Lat</td><td>',num2str(lat,'%.9f'),'°</td></tr>'];
  outStr = [outStr,'<tr><td>Lon</td><td>',num2str(lon,'%.9f'),'°</td></tr>'];
  outStr = [outStr,'<tr><td>Hgt</td><td>',num2str(hgt,'%.3f'),'m</td></tr>'];
  outStr = [outStr,'</table>]]></description><styleUrl>#',marker,'</styleUrl>'];
  outStr = [outStr,'<Point><coordinates>'];
  outStr = [outStr,num2str(lon,'%.9f'),',',num2str(lat,'%.9f'),',',num2str(hgt,'%.3f')];
  outStr = [outStr,'</coordinates></Point></Placemark>'];


function outStr = writeKMLMarkerStyle(name,color)
  outStr = ['<Style id="',name,'">'];
  outStr = [outStr,'<IconStyle><color>ff',color,'</color><scale>0.6</scale>'];
  outStr = [outStr,'<Icon>http://maps.google.com/mapfiles/kml/shapes/placemark_square.png</Icon>'];
  outStr = [outStr,'</IconStyle><ListStyle></ListStyle><LabelStyle><color>ff000000</color>'];
  outStr = [outStr,'</LabelStyle>'];
  outStr = [outStr,'<BalloonStyle><text><![CDATA[$[description]]]></text></BalloonStyle>'];
  outStr = [outStr,'</Style>'];


