#!/usr/bin/python
############################################################
# getPos.py
#
# This function provide a basic interface to the gps. It 
# requires gps babel to be running and basically only performs
# a simple parsing of the output kml file generated by
# gpsbabel
############################################################
import sys
from lxml import etree


xmlns="http://www.opengis.net/kml/2.2"

############################################################
#Function to parse the correct position
############################################################
def getPosition( fileName ):
  inFile = "temp.kml" 
  xmlData = etree.parse(fileName) #etree.parse() opens and parses the data

  # find all "Message" records in the document, regardless of level
  messages = xmlData.findall(".//", namespaces={xmlns})
 
  for msg in messages:
    #first, get the data in the "tags" of the record
    if "coord" in msg.tag:
      location = msg.text.split(',')

      return location


############################################################
# main
############################################################
def main ():

  #parse command line options
  if len(sys.argv) != 2:
    print "usage: getPos.py sourceFile\n"
    return;

  filename = sys.argv[1]

  location = getPosition(filename)
  
  print "Coordinates: ("+location[0]+","+location[1]+","+location[2]+")\n"


if __name__ == "__main__":
    main()
