%script to process the Yellowstone earthquake data to make a KML
%JRA 4/27/09

% Load the text file
%--------------------------------------------------------------------------
load ystone_eqs_sort.txt;
data = ystone_eqs_sort;

% Assign columns to variables
%--------------------------------------------------------------------------
year = data(:,1);
decimalyear = data(:,2);
lat = data(:,3);
long = data(:,4);
depth = data(:,5);
mag = data(:,6);

% Assign columns to variables
%--------------------------------------------------------------------------
figure(1)
clf
plot(long, lat, 'k.')

%let's try to write the kml:

% Open the output file to write the kml
%--------------------------------------------------------------------------
fid =fopen('eq4.kml','w+');

% Print the xml header and kml namespace declaration
%--------------------------------------------------------------------------
fprintf(fid, '<?xml version="1.0" encoding="UTF-8"?>\n');
fprintf(fid, '<kml xmlns="http://www.opengis.net/kml/2.2">\n');
fprintf(fid, '<Document>\n');

% Define the icon for the points
%--------------------------------------------------------------------------
fprintf(fid, '<Style id="dot">\n');
fprintf(fid, '<IconStyle>\n');
fprintf(fid, '<scale>0.5</scale>\n');
fprintf(fid, '<Icon>\n');
fprintf(fid, '<href>http://arrowsmith410-598.asu.edu/Lectures/Lecture21/dot1.png</href>\n');
fprintf(fid, '</Icon>\n');
fprintf(fid, '</IconStyle>\n');
fprintf(fid, '</Style>\n');

% Write the kml for each point in our text file
%--------------------------------------------------------------------------
for i=1:length(lat)
    fprintf(fid, '<Placemark>\n');
    fprintf(fid, '<name> %3.1f </name>\n',mag(i));
    fprintf(fid, '<description> <![CDATA[<h1>Yellowstone EQs</h1><p><font color="red">Year = %3.2f Depth = %3.2f Mag = %3.2f.</font></p>]]></description>\n', year(i), depth(i), mag(i));
    fprintf(fid, '<styleUrl>#dot</styleUrl>\n');
    fprintf(fid, '<Point>\n');
    fprintf(fid, '<coordinates>%3.2f,%3.2f,0</coordinates>\n',long(i),lat(i));
    fprintf(fid, '</Point>\n');
    fprintf(fid, '</Placemark>\n');
end

% Close the Document and kml tags
%--------------------------------------------------------------------------
fprintf(fid, '</Document>\n');
fprintf(fid, '</kml>\n');

% Close the output file
%--------------------------------------------------------------------------
fclose(fid);
