#!/usr/bin/env python

__version__ = '$Revision: 8449 $'.split()[1]
__date__ = '$Date: 2008-01-31 11:11:50 -0500 (Thu, 31 Jan 2008) $'.split()[1]
__author__ = 'Kurt Schwehr'

__doc__="""
Convert xy mmsi time files to Google Earth ship tracks

@requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3
@requires: U{pyproj<>}
@requires: grid.py

@author: """+__author__+"""
@version: """ + __version__ +"""
@var __date__: Date of last svn commit
@undocumented: __version__ __author__ __doc__ parser
@status: under development
@license: GPL v2
@since: 2007-May
@organization: U{CCOM<http://ccom.unh.edu/>}
@todo: Decimate ships that are not moving and updating fast

"""

import sys
import time

def addStyle(out,lineColor='a0a0a0',polyColor="808080",polyOpacity=.25,lineOpacity=.5,lineWidth=5,indent='    ',styleName='style'):
    '''
    @param polyOpacity: 0..1 where 1 is opaque, and 0 is not visible
    '''
    o=out
    lo = (int(lineOpacity*255)).__hex__()
    lo = lo[lo.find('x')+1:]
    if len(lo)==1: lo='0'+lo
    po = (int(polyOpacity*255)).__hex__()
    po = po[po.find('x')+1:]
    if len(po)==1: po='0'+po
    
    o.write(indent+'<Style id="'+styleName+'">\n')
    o.write(indent+'\t<LineStyle>'+'\n')
    o.write(indent+'\t  <color>'+lo+str(lineColor)+'</color>\n')
    o.write(indent+'\t  <width>'+str(lineWidth)+'</width>\n')
    o.write(indent+'\t</LineStyle>'+'\n')
    o.write(indent+'\t<PolyStyle>'+'\n')
    o.write(indent+'\t  <color>'+po+str(polyColor)+'</color>\n')
    o.write(indent+'\t</PolyStyle>'+'\n')
    o.write(indent+'</Style>'+'\n')


if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser(usage="%prog [options] [file1] [file2] ...",
                          version="%prog "+__version__+' ('+__date__+')')

    parser.add_option('-S','--with-style',dest='withStyle'
                      ,default=False
                      , action='store_true'
                      ,help='Include a style reference')

    parser.add_option('-c','--hide-children',dest='hideChildren'
                      ,default=False
                      , action='store_true'
                      ,help='Make the folder not open')


    parser.add_option('--line-color'  ,dest='lineColor'  ,type='string',default='ffffff',help=' [default: %default (white)]')
    parser.add_option('--line-opacity',dest='lineOpacity',type='float' ,default=1       ,help=' 0..1 [default: %default]')
    parser.add_option('--line-width'  ,dest='lineWidth'  ,type='float',default=1        ,help=' [default: %default]')
    parser.add_option('--style-name',dest='styleName',default='s',help='Name of the style for the polygon [default: %default]')

    parser.add_option('-z',dest='z',default=None,type='float',help='Add a z component [Default: %default]')


    (options,args) = parser.parse_args()

    print '''<?xml version="1.0" encoding="UTF-8"?>
<!-- xymt2kml - by Kurt Schwehr from noaadata-py -->
<kml xmlns="http://earth.google.com/kml/2.1">
  <Document>
'''
    if options.withStyle:
        addStyle(sys.stdout
                 ,lineColor=options.lineColor
                 ,lineOpacity=options.lineOpacity
                 ,lineWidth=options.lineWidth
                 ,styleName=options.styleName
                 )

    ships={}

    for filename in args:
        last = None
        print '<Folder>\n\t<name>%s</name>' % filename
        if options.hideChildren:
            print '<Style><ListStyle><listItemType>checkHideChildren</listItemType></ListStyle></Style>\n'
        print '\t<Placemark>'
        if options.withStyle: print '\t  <styleUrl>#'+options.styleName+'</styleUrl>'
        print '\t  <LineString><coordinates>'
        for point in file(filename):
            x,y = point.split()[:2]
            if options.z is not None:
                print '\t\t',x+','+y+','+str(options.z)
            else:
                print '\t\t'+x+','+y+',0'
        print '\t  </coordinates></LineString>'
        print '\t</Placemark>'
        print '</Folder>'

    print '''    
</Document>
</kml>
'''
