function generateFlightKML( filename, flightDate, dataFreq, outputFile )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Developed by Matthew Sienkiewicz
%  Stony Brook University
%  11 June 2015
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Input arguments are the full path to the AIMMS-20 processed output in
%ASCII format, the date of the flight in MATLAB datestring format the
%frequency of the data in Hz, and the full path of the KML file to be
%created.
%
%The KML file is output to the 
%
%Please remove all headers from the processed output file before using this
%program.
%
%EXAMPLE: For a 20 Hz dataset from a flight that took place on January 1st,
%2014:
%
%filename = '/path/to/file/20140101.txt';
%flightDate = '01-Jan-2014';
%dataFreq = 20;
%outputFile = '/path/to/file/20140101.kml';
%
%So the full command looks like:
%generateFlightKML('/path/to/file/20140101.txt','01-Jan-2014',20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Import the flight data and convert decimal hour to MATLAB datenum format%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rawFlight = importdata(filename);
rawFlight(:,1) = datenum([zeros(length(rawFlight),1),...
    zeros(length(rawFlight),1),zeros(length(rawFlight),1),...
    rawFlight(:,1),zeros(length(rawFlight),1),...
    zeros(length(rawFlight),1)] + repmat(datevec(flightDate),...
    length(rawFlight),1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Import variables to structure array%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
flight.time = rawFlight(:,1);       %Date/time of obs in MATLAB datenum
flight.lat = rawFlight(:,7);        %GPS Latitude
flight.lon = rawFlight(:,8);        %GPS Longitude
flight.alt = rawFlight(:,9)+37.7;   %GPS Altitude w/ correction of 37.7m
flight.u = rawFlight(:,6);          %Zonal wind
flight.v = rawFlight(:,5);          %Meridional wind
flight.w = rawFlight(:,17);         %Vertical motion
flight.wsp = sqrt((flight.u.^2)...
    +(flight.v.^2));                %Wind speed
flight.wdr = (180 / pi).*atan2(...
    (-1.*flight.u),(-1.*flight.v)); %Wind Direction
flight.wdr(flight.wdr<0) = 360 + flight.wdr(flight.wdr<0);
flight.tmp = rawFlight(:,2);        %Air temperature
flight.hum = rawFlight(:,3);        %Relative Humidity
flight.prs = rawFlight(:,4);        %Atmospheric Pressure
flight.theta = (flight.tmp +...
    273.15).*((100000./...
    flight.prs).^( 287 / 1004));    %Potential Temperature
e_s = 6.11.*exp(((2.5e6.*...
    18.016)./(1000.*8.3145)).*...
    ((1./273.15)-(1./(flight.tmp...
    +273.15))));                    %Saturation vapor pressure
w_s = 0.622.*(e_s./...
    ((flight.prs./100)-e_s));       %Saturation mixing ratio
flight.mix = flight.hum .* w_s;     %Mixing ratio
flight.thetav = flight.theta...
    .*(1+(0.61.*flight.mix ));      %Virtual Potential Temperature
flight.head = rawFlight(:,15);      %Aircraft Heading
flight.tas = rawFlight(:,16);       %Aircraft air speed
flight.slip = rawFlight(:,18);      %Aircraft sideslip angle
flight.roll = rawFlight(:,13);      %Aircraft roll angle
flight.pitch = rawFlight(:,14);     %Aircraft pitch angle
flight.date = datenum(datestr(...
    flight.time(1),...
    'dd-mmm-yyyy'),'dd-mmm-yyyy');  %Flight Date
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Averaging interval in seconds%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
avgInt = (dataFreq * 30) / 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate start time of flight, rounded down to nearest 10 minutes%%%%%%%%
startTime = datevec(flight.time(1));
startTime(5) = floor(startTime(5)/10)*10;
startTime(6) = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate end time of flight, rounded up to nearest 10 minutes%%%%%%%%%%%%
endTime = datevec(flight.time(end));
endTime(5) = ceil(endTime(5)/10)*10;
endTime(6) = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Average flight values over 30 seconds at 30 second intervals%%%%%%%%%%%%%%
times = nan(20,length(datenum(startTime):datenum([0,0,0,0,10,0]):...
    (datenum(endTime)-datenum([0,0,0,0,10,0]))));
output = nan(20,10,length(datenum(startTime):datenum([0,0,0,0,10,0]):...
    (datenum(endTime)-datenum([0,0,0,0,10,0]))));
j = 1;
for indTime=datenum(startTime):datenum([0,0,0,0,10,0]):...
        (datenum(endTime)-datenum([0,0,0,0,10,0]))
    i = 1;
    %If beginning of flight...
    if indTime < flight.time(1)
        newTime = datevec(flight.time(1));
        newTime(6) = ceil(newTime(6)/30)*30 + 30;
        newTime = datenum(newTime);
        if newTime < (indTime+datenum([0,0,0,0,9,30]))
            for indTime2=newTime:datenum([0,0,0,0,0,30]):...
                    indTime+datenum([0,0,0,0,9,30])
                ind2 = find(abs(flight.time-indTime2)==...
                    min(abs(flight.time-indTime2)));
                if length(ind2) > 1
                 ind2 = round(mean(ind2));
                end
                times(i,j) = indTime2;
                output(i,1,j) = mean(flight.time(ind2-avgInt:ind2+avgInt));
                output(i,2,j) = flight.lat(ind2);
                output(i,3,j) = flight.lon(ind2);
                output(i,4,j) = flight.alt(ind2);
                output(i,5,j) = flight.head(ind2);
                output(i,6,j) = mean(flight.wsp(ind2-avgInt:...
                    ind2+avgInt))*1.94384; %convert winds to knots
                output(i,7,j) = mean(flight.wdr(ind2-avgInt:ind2+avgInt));
                output(i,8,j) = mean(flight.tmp(ind2-avgInt:ind2+avgInt));
                output(i,9,j) = mean(flight.prs(ind2-avgInt:ind2+avgInt));
                output(i,10,j) = mean(flight.hum(ind2-avgInt:ind2+avgInt));
                i = i + 1;
            end
        end
    %If end of flight...
    elseif (indTime+datenum([0,0,0,0,9,45])) > flight.time(end)
        newTime = datevec(flight.time(end));
        newTime(6) = floor(newTime(6)/30)*30;
        newTime(6) = newTime(6) - 30;
        newTime = datenum(newTime);
        for indTime2=indTime:datenum([0,0,0,0,0,30]):newTime
            ind2 = find(abs(flight.time-indTime2)==...
                min(abs(flight.time-indTime2)));
            if length(ind2) > 1
             ind2 = round(mean(ind2));
            end
            times(i,j) = indTime2;
            output(i,1,j) = mean(flight.time(ind2-avgInt:ind2+avgInt));
            output(i,2,j) = flight.lat(ind2);
            output(i,3,j) = flight.lon(ind2);
            output(i,4,j) = flight.alt(ind2);
            output(i,5,j) = flight.head(ind2);
            output(i,6,j) = mean(flight.wsp(ind2-avgInt:ind2+avgInt))...
                *1.94384; %convert winds to knots
            output(i,7,j) = mean(flight.wdr(ind2-avgInt:ind2+avgInt));
            output(i,8,j) = mean(flight.tmp(ind2-avgInt:ind2+avgInt));
            output(i,9,j) = mean(flight.prs(ind2-avgInt:ind2+avgInt));
            output(i,10,j) = mean(flight.hum(ind2-avgInt:ind2+avgInt));
            i = i + 1;
        end
    %Else...
    else
        for indTime2=indTime:datenum([0,0,0,0,0,30]):...
                indTime+datenum([0,0,0,0,9,30])
            ind2 = find(abs(flight.time-indTime2)==...
                min(abs(flight.time-indTime2)));
            if length(ind2) > 1
                ind2 = round(mean(ind2));
            end
            times(i,j) = indTime2;
            output(i,1,j) = mean(flight.time(ind2-avgInt:ind2+avgInt));
            output(i,2,j) = flight.lat(ind2);
            output(i,3,j) = flight.lon(ind2);
            output(i,4,j) = flight.alt(ind2);
            output(i,5,j) = flight.head(ind2);
            output(i,6,j) = mean(flight.wsp(ind2-avgInt:ind2+avgInt))...
                *1.94384; %convert winds to knots
            output(i,7,j) = mean(flight.wdr(ind2-avgInt:ind2+avgInt));
            output(i,8,j) = mean(flight.tmp(ind2-avgInt:ind2+avgInt));
            output(i,9,j) = mean(flight.prs(ind2-avgInt:ind2+avgInt));
            output(i,10,j) = mean(flight.hum(ind2-avgInt:ind2+avgInt));
            i = i + 1;
        end
    end
    j = j + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate centerpoint of KMZ file%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cenLon = mean(unique(round(flight.lon.*10)./10));
cenLat = mean(unique(round(flight.lat.*10)./10));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Create string of lons/lats/alts for creation of flight path in KMZ file%%%
coordString = horzcat(num2str(flight.lon(1)),',',num2str(flight.lat(1))...
    ,',',num2str(flight.alt(1)*3),' ');
for i=2:20:length(flight.lat)
    coordString = strcat(coordString,horzcat(' ',num2str(flight.lon(i))...
        ,',',num2str(flight.lat(i)),',',num2str(flight.alt(i)*3),' '));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Begin printing to KML file%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fileID = fopen(outputFile,'w');
fprintf(fileID,'<?xml version="1.0" encoding="UTF-8"?>\n');
fprintf(fileID,'<kml xmlns="http://earth.google.com/kml/2.2">\n');
fprintf(fileID,'\t<Document>\n');
fprintf(fileID,'\t\t<name>AIRCRAFT IOP</name>\n');
fprintf(fileID,['\t\t\t<description><![CDATA[<table width=300>'...
    '<br><br>#notes about mission#</td></tr></table>]]></description>\n']);
fprintf(fileID,'\t\t<Snippet maxLines="1">%s</Snippet>\n',...
    datestr(flight.time(1),'mmm. dd, yyyy'));
fprintf(fileID,'\t\t<open>1</open>\n');
fprintf(fileID,'\t\t<LookAt>\n');
fprintf(fileID,'\t\t\t<longitude>%5.1f</longitude>\n',cenLon);
fprintf(fileID,'\t\t\t<latitude>%4.1f</latitude>\n',cenLat);
fprintf(fileID,'\t\t\t<altitude>0</altitude>\n');
fprintf(fileID,'\t\t\t<range>248032.0323319686</range>\n');
fprintf(fileID,'\t\t\t<tilt>0</tilt>\n');
fprintf(fileID,'\t\t\t<heading>0</heading>\n');
fprintf(fileID,'\t\t\t<altitudeMode>relativeToGround</altitudeMode>\n');
fprintf(fileID,'\t\t</LookAt>\n');
fprintf(fileID,'\t\t<Style id="h1">\n');
fprintf(fileID,'\t\t\t<ListStyle>\n');
fprintf(fileID,'\t\t\t\t<listItemType>checkHideChildren</listItemType>\n');
fprintf(fileID,'\t\t\t</ListStyle>\n');
fprintf(fileID,'\t\t</Style>\n');
fprintf(fileID,'\t\t<Style id="e">\n');
fprintf(fileID,'\t\t\t<BalloonStyle>\n');
fprintf(fileID,'\t\t\t\t<bgColor>ffffffff</bgColor>\n');
fprintf(fileID,['\t\t\t\t<text><![CDATA[<table width="420" style='...
    '"font-family:arial;font-size:12px;color:#000000"><tr><td>'...
    '$[description]</td></tr></table>]]></text>\n']);
fprintf(fileID,'\t\t\t</BalloonStyle>\n');
fprintf(fileID,'\t\t</Style>\n');
fprintf(fileID,'\t\t<Style id="d">\n');
fprintf(fileID,'\t\t\t<BalloonStyle>\n');
fprintf(fileID,'\t\t\t\t<bgColor>ffffffff</bgColor>\n');
fprintf(fileID,['\t\t\t\t<text><![CDATA[<table width="480"'...
    'style="font-family:arial;font-size:12px;color:#000000"><tr>'...
    '<td width="165" align="right">$[description]</td></tr><tr>'...
    '<td colspan="2"><center>(<font style="font-family:arial;'...
    'font-size:14px;color:#CC0000"><b>*</b></font>)'...
    'Denotes suspect data</center></td></tr></table>]]></text>\n']);
fprintf(fileID,'\t\t\t</BalloonStyle>\n');
fprintf(fileID,'\t\t</Style>\n');
fprintf(fileID,'\t\t<Folder>\n');
fprintf(fileID,'\t\t\t<name>Observations</name>\n');
fprintf(fileID,'\t\t\t<open>0</open>\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Print each observation point to KML file%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[m,~,~] = size(output);
j = 1;
for indTime=datenum(startTime):datenum([0,0,0,0,10,0]):...
        (datenum(endTime)-datenum([0,0,0,0,10,0]))
    fprintf(fileID,'\t\t\t<Folder>\n');
    fprintf(fileID,'\t\t\t\t<name>OB %0i (%9s)</name>\n',...
        j,datestr(indTime,'HH:MM:SSZ'));
    fprintf(fileID,'\t\t\t\t<open>0</open>\n');
    for i=1:1:m
        if isnan(times(i,j))
            continue;
        else
            wspImg = round(output(i,6,j)/5)*5;
            if wspImg == 0
                colString = 'ffffffff';
            elseif wspImg == 5
                colString = 'ffffaa55';
            elseif wspImg == 10
                colString = 'ffff0000';
            elseif wspImg == 15
                colString = 'ff00aa00';
            elseif wspImg == 20
                colString = 'ff00ffff';
            elseif wspImg == 25
                colString = 'ff7f55ff';
            elseif wspImg == 30
                colString = 'ffffaccf';
            elseif wspImg == 35
                colString = 'ff0055aa';
            elseif wspImg == 40
                colString = 'ffff00ff';
            elseif wspImg == 45
                colString = 'ffff00aa';
            elseif wspImg == 50
                colString = 'ff0000dc';
            elseif wspImg == 55
                colString = 'ff0000aa';
            elseif wspImg >= 60
                colString = 'ff00aaff';
            end
            fprintf(fileID,'\t\t\t\t<Placemark>\n');
            fprintf(fileID,'\t\t\t\t\t<Style>\n');
            fprintf(fileID,'\t\t\t\t\t\t<IconStyle>\n');
            fprintf(fileID,['\t\t\t\t\t\t\t<Icon><href>%i.png</href>'...
                '</Icon>\n'],round(output(i,6,j)/5)*5);
            fprintf(fileID,'\t\t\t\t\t\t\t<scale>2.4</scale>\n');
            fprintf(fileID,'\t\t\t\t\t\t\t<heading>%i</heading>\n',...
                round(output(i,7,j)));
            fprintf(fileID,'\t\t\t\t\t\t\t<color>%s</color>\n',colString);
            fprintf(fileID,'\t\t\t\t\t\t</IconStyle>\n');
            fprintf(fileID,'\t\t\t\t\t</Style>\n');
            fprintf(fileID,'\t\t\t\t\t<name></name>\n');
            fprintf(fileID,'\t\t\t\t\t<Snippet></Snippet>\n');
            formatSpec = ['\t\t\t\t\t<description><![CDATA[Time:</td>'...
                '<td width="315"> %9s</td></tr><br><tr>'...
                '<td align="right">Coordinates:</td><td> %6.3fN %6.3fW'...
                '</td></tr><tr><br><td align="right">Acft. Air Press:'...
                '</td><td> %5.1f mb</td></tr><br><tr><td align="right">'...
                'Acft. Altitude:</td><td> %i meters</td></tr><br><tr>'...
                '<td align="right">Flt. Lvl. Wind (30s):</td><td>'...
                'From %i&deg; at %i knots</td></tr><br><tr>'...
                '<td align="right">Air Temp:</td><td> %5.1f&deg;C</td>'...
                '</tr><br><tr><td align="right">Rel. Hum.:</td><td>'...
                '%i</td><td>]]></description>\n'];
            fprintf(fileID,formatSpec,datestr(times(i,j),'HH:MM:SSZ'),...
                output(i,2,j),abs(output(i,3,j)),output(i,9,j)/100,...
                round(output(i,4,j)),round(output(i,7,j)),...
                round(output(i,6,j)),output(i,8,j),...
                round(output(i,10,j)*100));
            fprintf(fileID,'\t\t\t\t\t<styleURL>#d</styleURL>\n');
            fprintf(fileID,'\t\t\t\t\t<Point>\n');
            fprintf(fileID,['\t\t\t\t\t\t<altitudeMode>absolute'...
                '</altitudeMode>\n']);
            fprintf(fileID,['\t\t\t\t\t\t<coordinates>%7.4f,%6.4f,%6.1f'...
                '</coordinates>\n'],output(i,3,j),output(i,2,j),...
                output(i,4,j)*3);
            fprintf(fileID,'\t\t\t\t\t</Point>\n');
            fprintf(fileID,'\t\t\t\t</Placemark>\n');
        end
    end
    fprintf(fileID,'\t\t\t</Folder>\n');
    j = j + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Print end of KML file and close%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(fileID,'\t\t</Folder>\n');
fprintf(fileID,'\t\t<Placemark>\n');
fprintf(fileID,'\t\t\t<name>Flight Path</name>\n');
fprintf(fileID,'\t\t\t<Style>\n');
fprintf(fileID,'\t\t\t\t<LineStyle>\n');
fprintf(fileID,'\t\t\t\t\t<color>410000ff</color>\n');
fprintf(fileID,'\t\t\t\t\t<width>4</width>\n');
fprintf(fileID,'\t\t\t\t</LineStyle>\n');
fprintf(fileID,'\t\t\t</Style>\n');
fprintf(fileID,'\t\t\t<LineString>\n');
fprintf(fileID,'\t\t\t\t<tessellate>0</tessellate>\n');
fprintf(fileID,'\t\t\t\t<coordinates>%s</coordinates>\n',coordString);
fprintf(fileID,'\t\t\t\t<altitudeMode>absolute</altitudeMode>\n');
fprintf(fileID,'\t\t\t</LineString>\n');
fprintf(fileID,'\t\t</Placemark>\n');
fprintf(fileID,'\t\t<ScreenOverlay id="Legend">\n');
fprintf(fileID,'\t\t\t<name>Legend</name>\n');
fprintf(fileID,'\t\t\t<Icon><href>legend.png</href></Icon>\n');
fprintf(fileID,['\t\t\t<overlayXY x="0" y="1" xunits="fraction" '...
    'yunits="fraction"/>\n']);
fprintf(fileID,['\t\t\t<screenXY x="0" y="1" xunits="fraction" '...
    'yunits="fraction"/>\n']);
fprintf(fileID,['\t\t\t<rotationXY x="0" y="0" xunits="fraction" '...
    'yunits="fraction"/>\n']);
fprintf(fileID,['\t\t\t<size x="0" y="0" xunits="pixels" '...
    'yunits="pixels"/>\n']);
fprintf(fileID,'\t\t</ScreenOverlay>\n');
fprintf(fileID,'\t</Document>\n');
fprintf(fileID,'\t</kml>\n');

fclose(fileID);
fclose('all');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end

