diff --git a/.gitattributes b/.gitattributes index 8e948c5..4ec8e92 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ * text=auto !eol +/quakes2kml.py -text diff --git a/quakes2kml.py b/quakes2kml.py new file mode 100755 index 0000000..d72785e --- /dev/null +++ b/quakes2kml.py @@ -0,0 +1,95 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" + Creates KML file with station info. + + License + Copyright 2012 Kasper D. Fischer + + This program is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License along + with this program. If not, see http://www.gnu.org/licenses/. + + $Id: stations.py 311 2012-01-31 14:34:31Z kasper $ +""" + +import csv, os, shutil, sys +import simplekml +from obspy.core import UTCDateTime + +# create new empty KML file +kml = simplekml.Kml() + +# create empyt folder to contain styles and stations +quakes_folder = kml.newfolder(name="Earthquakes") + +# style of broad-band stations +quake1m_style = simplekml.Style() +quake1m_style.iconstyle.color = 'ff0000ff' +quake1m_style.iconstyle.icon.href = 'circle' + +# style of short periode stations +quake1y_style = simplekml.Style() +quake1y_style.iconstyle.color = 'ffff00ff' + +# style of short periode stations +quakeAll_style = simplekml.Style() +quakeAll_style.iconstyle.color = 'ff00ffff' + +# reading earthquake list +events = [] +num_header = 0 +with open('quakes.txt', 'rb') as f: + reader = csv.reader(f, delimiter='\t') + try: + for row in reader: + if ( reader.line_num <= num_header ): + header = row + else: + events.append(row) + except csv.Error, e: + sys.exit('file {0}, line {1}: {2}'.format('quakes.txt', reader.line_num, e)) + +# adding station markers +for event in events: + eventTime = UTCDateTime(event[0]+' '+event[1]) + nowTime = UTCDateTime() + lat = event[2] + lon = event[3] + mag = event[4] + region = event[5] + + pnt = quakes_folder.newpoint( + name = region, + coords = [(lon, lat)], + description = "

Magnitude {0}
Time {1}
Location {2}N {3}E

".format( + mag, eventTime.strftime('%Y-%m-%d %H:%M:%S'), lat, lon) + ) + pnt.extendeddata.newdata('mag', mag) + pnt.extendeddata.newdata('lat', lat) + pnt.extendeddata.newdata('lon', lon) + pnt.extendeddata.newdata('time', eventTime.strftime('%Y-%m-%d %H:%M:%S')) + pnt.extendeddata.newdata('age', nowTime-eventTime) + pnt.extendeddata.newdata('pntsize', float(mag)*3.5+2) + if (( nowTime - eventTime ) > 60*60*24*30 ): + pnt.extendeddata.newdata('pntcolor', '#ffa500') + pnt.style = quake1y_style + elif (( nowTime - eventTime ) > 60*60*24*365 ): + pnt.extendeddata.newdata('pntcolor', '#ffff00') + pnt.style = quakeAll_style + else: + pnt.extendeddata.newdata('pntcolor', '#ff0000') + pnt.style = quake1m_style + + +# saving the KML file +kml.save('quakes.kml')