source: subversion/applications/utils/amr2wav/wherewasi.py @ 29057

Last change on this file since 29057 was 14494, checked in by gjones, 10 years ago

initial import

File size: 8.5 KB
Line 
1#!/usr/bin/python
2#
3#    This file is part of wherewasi.
4#
5#    Wherewasi is free software: you can redistribute it and/or modify
6#    it under the terms of the GNU General Public License as published by
7#    the Free Software Foundation, either version 3 of the License, or
8#    (at your option) any later version.
9#
10#    This wherewasi is distributed in the hope that it will be useful,
11#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#    GNU General Public License for more details.
14#
15#    You should have received a copy of the GNU General Public License
16#    along with wherewasi.  If not, see <http://www.gnu.org/licenses/>.
17#
18#    Copyright Graham Jones 2009
19#
20
21import sys
22import os
23from parseGPX import parseGPX
24#from geometry import *
25from time import *
26#from analyseGPX_gui import analyseGPX_gui
27from optparse import OptionParser
28
29
30
31usage = "Usage %prog [options] filename"
32version = "0.1"
33parser = OptionParser(usage=usage,version=version)
34parser.add_option("-S", "--ss", "--startseg", dest="s_seg", type="int",
35                  help="Analysis Start segment",
36                  metavar="s_seg")
37parser.add_option("-s", "--sp", "--startpt", dest="s_pt", type="int",
38                  help="Analysis Start point number",
39                  metavar="s_pt")
40parser.add_option("-E", "--es", "--endseg", dest="e_seg", type="int",
41                  help="Analysis End segment",
42                  metavar="e_seg")
43parser.add_option("-e", "--ep", "--endpt", dest="e_pt", type="int",
44                  help="Analysis End point number",
45                  metavar="e_pt")
46parser.add_option("-l", "--list", action="store_true", dest="list",
47                  help="produce a summary list of the contents of the file")
48parser.add_option("--summary", "--sum", action="store_true", dest="summary",
49                  help="produce a summary table of the file data")
50parser.add_option("--eprofile", "--eprof", action="store_true", dest="eprofile",
51                  help="produce an elevation profile listing (and graph if -g used too)")
52parser.add_option("--dprofile", "--dprof", action="store_true", dest="dprofile",
53                  help="produce a distance profile listing (and graph if -g used too")
54parser.add_option("-g", "--graph", action="store_true", dest="graph",
55                  help="produce graphical output using GnuPlot")
56parser.add_option("-f", "--file", dest="outfile",
57                  help="filename to use for output",
58                  metavar="FILE")
59parser.add_option("-v", "--verbose", action="store_true",dest="verbose",
60                  help="Include verbose output")
61parser.add_option("-d", "--debug", action="store_true",dest="debug",
62                  help="Include debug output")
63parser.set_defaults(
64    s_seg = 0,
65    s_pt = 0,
66    e_seg = -1,
67    e_pt = -1,
68    filename="gpx_analsys.out",
69    debug=False,
70    verbose=False)
71(options,args)=parser.parse_args()
72
73if (options.debug):
74    options.verbose = True
75    print "options   = %s" % options
76    print "arguments = %s" % args
77       
78if len(args)<1:
79    print "Error: You must supply at least one GPX file to analyse.\n"
80    sys.exit(-1)
81else:
82    pgpx = parseGPX(args[0],options.debug,options.verbose)
83
84    if options.e_seg==-1:
85        options.e_seg = pgpx.getNumTrkSeg()-1
86        if options.debug: print "Default e_seg used - set to %d" % options.e_seg
87    else:
88        if options.debug: print "e_seg=%s" % options.e_seg
89
90
91    if options.e_pt==-1:
92        options.e_pt = pgpx.getNumPts(options.e_seg)-1
93        if options.debug: print "Default e_pt used - set to %d" \
94                % options.e_pt
95    else:
96        if options.debug: print "e_pt=%d" % options.e_pt
97
98    if options.list:
99        for seg in range(0,pgpx.getNumTrkSeg()):
100            segData = pgpx.getTrackAnalysis(seg,0,seg,-1)
101        #print "SegmentData    = %s" % segData
102            sTimeStruct = localtime(segData['minTime'])
103            eTimeStruct = localtime(segData['maxTime'])
104            sTimeStr = strftime("%d/%m/%Y %X",sTimeStruct)
105            eTimeStr = strftime("%d/%m/%Y %X",eTimeStruct)
106            print "Segment %d: (%s to %s) - %d points." % \
107                (seg,sTimeStr,eTimeStr,segData['npts'])
108
109
110    if options.summary:
111        segData = pgpx.getTrackAnalysis(options.s_seg,
112                                        options.s_pt,
113                                        options.e_seg,
114                                        options.e_pt)
115        if options.debug: print "Segment 0 Analysis is %s\n" % segData
116        print "\n"
117        print "\t\tWhereWasI Output"
118        print "\t\t=================\n"
119        print "\tFilename=%s" % args[0]
120        print "\tNumber of Track Segments\t= %d" % pgpx.getNumTrkSeg()
121        print "\t---------------------------------------------"
122        print "\tStart Segment / Point \t= %d/%d" % \
123            (options.s_seg,options.s_pt)
124        print "\tEnd Segment / Point \t= %d/%d" % \
125            (options.e_seg,options.e_pt)
126        sTimeStruct = localtime(segData['minTime'])
127        eTimeStruct = localtime(segData['maxTime'])
128        sTimeStr = strftime("%d/%m/%Y %X",sTimeStruct)
129        eTimeStr = strftime("%d/%m/%Y %X",eTimeStruct)
130        print "\tStart Time            \t= %s" % sTimeStr
131        print "\tEnd Time              \t= %s" % eTimeStr
132        print "\tNumber of Track Points\t= %s" % segData['npts']
133        print "\t---------------------------------------------"
134        t_hrs = int(segData['time']/3600.0)
135        t_min = int((segData['time']-3600.*t_hrs)/60.)
136        print "\tTotal Time            \t= %02d:%02d" % (t_hrs,t_min)
137        print "\tTotal Distance        \t= %5.2f km" % segData['dist']
138        print "\tTotal Climb           \t= %5.0f m" % segData['climb']
139        print "\t---------------------------------------------"
140        print "\tAverage Speed         \t= %5.2f km/hr" % segData['avSpeed']
141        print "\tMaximum Speed         \t= %5.2f km/hr" % segData['maxSpeed']
142        print "\t---------------------------------------------\n"
143
144
145    if options.eprofile or options.dprofile:
146        if options.debug: print "profile listing"
147        profdata = pgpx.getProfileData(options.s_seg,
148                                       options.s_pt,
149                                       options.e_seg,
150                                       options.e_pt)
151        if options.debug: print "profdata=%s" % profdata
152
153        print "\n"
154        print "\t\tanalyseGPX Output"
155        print "\t\t=================\n"
156        print "\tFilename=%s" % args[0]
157        print "\tNumber of Track Segments\t= %d" % pgpx.getNumTrkSeg()
158        print "\t---------------------------------------------"
159        print "\tStart Segment / Point \t= %d/%d" % \
160            (options.s_seg,options.s_pt)
161        print "\tEnd Segment / Point \t= %d/%d" % \
162            (options.e_seg,options.e_pt)
163        print "\t---------------------------------------------"
164        print "Date         Time          time_t      sec  Distance (km) Elevation (m)"
165        edistprofXY = []
166        etimeprofXY = []
167        dtimeprofXY = []
168        for rec in profdata:
169            timeStruct = localtime(rec[0])
170            timeStr = strftime("%d/%m/%Y %X",timeStruct)
171            print "%s \t %d \t %d \t %6.2f \t %6.0f" % \
172                (timeStr,rec[0],rec[1], rec[2], rec[3])
173            edistprofXY.append([rec[2],rec[3]])
174            etimeprofXY.append([rec[1]/3600.,rec[3]])
175            dtimeprofXY.append([rec[1]/3600.,rec[2]])
176
177        if options.graph:
178            try:
179                import Gnuplot
180                g=Gnuplot.Gnuplot()
181                if options.eprofile:
182                    g.title("GPX Track Elevation Profile")
183                    g.xlabel('Distance from Start (km)')
184                    g.ylabel('Elevation (m)')
185                    g.plot(edistprofXY)
186                    raw_input('Please press return to continue...\n')
187                    g.reset()
188                if options.dprofile:
189                    g.title("GPX Track Elevation Profile")
190                    g.xlabel('Time from Start (hours)')
191                    g.ylabel('Distance(km)')
192                    g.plot(dtimeprofXY)
193                    raw_input('Please press return to continue...\n')
194                    g.reset()
195
196
197            except: # python-gnuplot is not available or is broken
198                print 'ERROR: python gnuplot interface is not found'
199
200
201
202if not (options.summary or options.eprofile or \
203            options.dprofile or options.list):
204    print("Error - I think you should specify one of the output options?")
205    print(" Something like --summary??")
206
207
208#gui = analyseGPX_gui()
209#gui.main()
210
211if options.debug: print "WhereWasI exiting"
212
213
Note: See TracBrowser for help on using the repository browser.