#!/usr/bin/env python
# encoding: utf-8
from GPX import *
"""
supplement.py

Created by Joel Carranza on 2011-02-12.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""

import sys
import getopt
from itertools import groupby
import pytz
from datetime import timedelta
from datetime import datetime

help_message = '''
The help message goes here.
'''

gpx = GPX()
gpx.load("data/JMT_gps_log_trimmed.gpx")
jmt = GPX()
jmt.load("data/JMT.gpx")

# reverse because they are in opposite order
jmtpts = list(jmt.tracks[0].points())[::-1]

tz = pytz.timezone('America/Los_Angeles')

def dayKey(w):
  t = w.time
  t = tz.normalize(t.astimezone(tz))
  return (t.year,t.month,t.day)
  
def closestPt(pts,pt):
  i = min(xrange(len(pts)),key=lambda i:pts[i].dist(pt))
  d = pts[i].dist(pt)
  return (i,d)
  
def tinterp(t0,t1,u):
  #print u
  # 0 < u < 1
  delta = (t1-t0).seconds
  return t0+timedelta(seconds=u*delta)
  
def selectJmtPts(pts, start,finish):
  a,da = closestPt(pts,start)
  b,db = closestPt(pts,finish)
  if da > 200 or db > 200:
    return None
  if a == b:
    return None
  if b < a:
    a,b = (b,a)
  result = pts[a:b]
  if start.time and finish.time:
    totald = wptdistance(result) + result[0].dist(start)+result[-1].dist(finish)
    d = result[0].dist(start)
    for i in xrange(0,len(result)):
      if i:
        d += result[i].dist(result[i-1])
      result[i].time = tinterp(start.time,finish.time,d/totald)
    
  del pts[a:b]  
  return result

def ptime(str):
  return tz.localize(datetime.strptime(str,"%Y-%m-%d %H:%M"))

def createTrack(pts, start, finish):
  return Track(points=selectJmtPts(pts,start,finish))

gpxpts = list(gpx.allpoints())
gpx.waypoints.append(Waypoint(37.73,-119.559,time=ptime('2010-8-29 09:00')))
gpx.waypoints.append(Waypoint(37.7888,-119.434,time=ptime('2010-8-29 13:00')))
gpx.waypoints.append(Waypoint(37.7888,-119.434,time=ptime('2010-8-30 09:00')))
gpx.waypoints.append(Waypoint(37.841085,-119.286346,time=ptime('2010-8-30 13:00')))
gpx.waypoints.append(Waypoint(37.841085,-119.286346,time=ptime('2010-8-31 09:00')))
gpx.waypoints.append(Waypoint( 37.744712,-119.212257,time=ptime('2010-8-31 13:00')))
# additional fixed pts
gpx.waypoints.append(Waypoint(  37.609801,-119.075117,time=ptime('2010-9-2 13:11')))
gpx.waypoints.append(Waypoint(37.412684,-118.924876,time=ptime('2010-9-4 13:16')))
 
gpx.waypoints.append(Waypoint(37.412684,-118.924876,time=ptime('2010-9-5 8:39')))
gpx.waypoints.append(Waypoint(36.634764,-118.385863,time=ptime('2010-9-13 8:00')))
gpxpts.extend(gpx.waypoints)
gpxpts.sort(key=lambda w:w.time)

gpx.tracks = []

for day,result in groupby(gpxpts,key=dayKey):
  print day
  pts = list(result)
  out = [pts[0]]
  for ix in xrange(1,len(pts)):
    pa = pts[ix-1]
    pb = pts[ix]
    dt = pb.time-pa.time
    if dt.seconds > 5*60 and pa.dist(pb) > 50:
      np = selectJmtPts(jmtpts,pts[ix-1],pts[ix])
      if np is not None:
        out.extend(np)
      else:
        print "selectJmtPts failed %s to %s" % (pa,pb)
        #gpx.newWaypoint(pa.lat,pa.lon,name="Failed! %s - %s" % (pa.time.astimezone(tz),pb.time.astimezone(tz)))
    out.append(pb)
  gpx.newTrack(points=out,name="%d-%d-%d" % day)
  
gpx.write("jmt-tracks.gpx")
@@ -301,16 +301,32 @@
 datetime
+%0Aimport argparse
 %0A%0Ahelp_m
@@ -375,59 +375,380 @@
 ''%0A%0A
-gpx = GPX()%0Agpx.load(%22data/JMT_gps_log_trimmed.gpx%22
+parser = argparse.ArgumentParser(description='Trim GPX file to time')%0Aparser.add_argument('-i', metavar='file',type=argparse.FileType('r'),default=sys.stdin)%0Aparser.add_argument('-t', metavar='file',type=argparse.FileType('r'))%0Aparser.add_argument('-o', metavar='file',type=argparse.FileType('w'),default=sys.stdout)%0Aargs = parser.parse_args()%0A%0Agpx = GPX()%0Agpx.load(args.i
 )%0Ajm
@@ -770,22 +770,14 @@
 oad(
-%22data/JMT.gpx%22
+args.t
 )%0A%0A#
@@ -1981,904 +1981,58 @@
 s = 
-list(gpx.allpoints())%0Agpx.waypoints.append(Waypoint(37.73,-119.559,time=ptime('2010-8-29 09:00')))%0Agpx.waypoints.append(Waypoint(37.7888,-119.434,time=ptime('2010-8-29 13:00')))%0Agpx.waypoints.append(Waypoint(37.7888,-119.434,time=ptime('2010-8-30 09:00')))%0Agpx.waypoints.append(Waypoint(37.841085,-119.286346,time=ptime('2010-8-30 13:00')))%0A
+%5Bw for w in 
 gpx.
-way
+all
 points
-.append(Waypoint(37.841085,-119.286346,time=ptime('2010-8-31 09:00')))%0Agpx.waypoints.append(Waypoint( 37.744712,-119.212257,time=ptime('2010-8-31 13:00')))%0A# additional fixed pts%0Agpx.waypoints.append(Waypoint(  37.609801,-119.075117,time=ptime('2010-9-2 13:11')))%0Agpx.waypoints.append(Waypoint(37.412684,-118.924876,time=ptime('2010-9-4 13:16')))%0A %0Agpx.waypoints.append(Waypoint(37.412684,-118.924876,time=ptime('2010-9-5 8:39')))%0Agpx.waypoints.append(Waypoint(36.634764,-118.385863,time=ptime('2010-9-13 8:00')))%0Agpxpts.extend(gpx.waypoints)
+() if w.time is not None%5D
 %0Agpx
@@ -2678,21 +2678,11 @@
 ite(
-%22jmt-tracks.gpx%22
+args.o
 )

build scripts