# -*- coding: utf-8 -*-
"""
Created on Wed Jul 05 23:27:55 2017

@author: MSI_me
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 05 22:40:48 2017

@author: MSI_me
"""

import sys
import csv
import numpy as np

#print 'print the number of arguments', len(sys.argv)
#print 'arguments', str(sys.argv)


main_name,extention=str(sys.argv[1]).split('.')
#print 'main_name:',main_name
#print 'extention:',extention
out_kml_name=main_name+'.kml'
print out_kml_name
times=[]
longitudes=[]
latitudes=[]

with  open(str(sys.argv[1]), 'rb') as csvfile:
    spamreader = csv.reader(csvfile)
    next(spamreader, None)  # skip the headers
    i=0
    for row in spamreader:
        times.append(row[0])
        longitudes.append(row[1])
        latitudes.append(row[2])
        
#print latitudes,longitudes,times
with  open(out_kml_name, 'w') as kml_file:
    #kml_file.write('Hi there!')
    kml_file.write('''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<!-- 

TimeStamp is recommended for Point.

Each Point represents a sample from a GPS.

-->

  <Document>
    <name>Points with TimeStamps</name>
	<Style id="sn_cabs3">
		<IconStyle>
			<color>ff0000ff</color>
			<scale>0.7</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>
	<Style id="sh_cabs3">
		<IconStyle>
			<color>ff0000ff</color>
			<scale>0.816667</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>
	<StyleMap id="msn_cabs3">
		<Pair>
			<key>normal</key>
			<styleUrl>#sn_cabs3</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#sh_cabs3</styleUrl>
		</Pair>
	</StyleMap>

	<Style id="sn_cabs2">
		<IconStyle>
			<color>ffff0000</color>
			<scale>0.7</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>
	<Style id="sh_cabs2">
		<IconStyle>
			<color>ffff0000</color>
			<scale>0.816667</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>
	<StyleMap id="msn_cabs2">
		<Pair>
			<key>normal</key>
			<styleUrl>#sn_cabs2</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#sh_cabs2</styleUrl>
		</Pair>
	</StyleMap>

	<StyleMap id="msn_cabs1">
		<Pair>
			<key>normal</key>
			<styleUrl>#sn_cabs1</styleUrl>
		</Pair>
		<Pair>
			<key>highlight</key>
			<styleUrl>#sh_cabs1</styleUrl>
		</Pair>
	</StyleMap>
	<Style id="sh_cabs1">
		<IconStyle>
			<color>ff00ff00</color>
			<scale>0.7</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>
	<Style id="sn_cabs1">
		<IconStyle>
			<color>ff00ff00</color>
			<scale>0.7</scale>
			<Icon>
				<href>http://maps.google.com/mapfiles/kml/shapes/cabs.png</href>
			</Icon>
			<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
		</IconStyle>
		<ListStyle>
		</ListStyle>
	</Style>''')
    kml_file.write('\n')
    for i in range(len(latitudes)):
        kml_file.write('<Placemark><TimeStamp><when>'+times[i]+','+'</when></TimeStamp><styleUrl>#msn_cabs1</styleUrl><Point><coordinates>'+longitudes[i]+','+latitudes[i]+','+',0</coordinates></Point></Placemark>\n')
        #print latitudes[i],longitudes[i]
    kml_file.write('''\t</Document>                                                                                                           
</kml>''')
