简单的python脚本来读取GPX文件+ optparse

时间:2014-07-29 23:04:27

标签: python optparse gpx

我是python的新手,并尝试编写一个简单的脚本来从GPX文件中提取特定信息。我的脚本中的限制问题是它似乎无法在else语句中找到dtime,但可以在第一个if语句中看到它。也许我对python的新鲜感正在逃避我,因为我非常确定有一个简单的解决方案。任何人都可以告诉我如何让它看到变量dtime或我做错了什么?

此外,有更好的方法让它使用标志。 -D和-P永远不会被用户输入,但目的是在定义这两个标志时读取GPX文件。

以下是以下脚本:

#!/usr/bin/env pnpython3

import gpxpy.parser
import os
def get_args () :
    ''' Parse input args
            -x   gpx filename
            -d   default deploy time yyyy:jjj:hh:mm:ss.sss
            -p   default pickup time yyyy:jjj:hh:mm:ss.sss
            -D   read deploy time from GPX file
            -P   read pickup time from GPX file
            -l   line number (array)
    '''
    global GPX, DEPLOY, PICKUP, LINE

    from optparse import OptionParser

    oparser = OptionParser ()
    oparser.usage = "munge_wp.py -x gps_file_name -d deploy_yyyy:jjj:hh:mm:ss.sss -ppickup_yyyy:jjj:hh:mm:ss.sss -l line_number"
    oparser.description = "Read a GPX way point file and produce a CSV file."
    oparser.add_option ("-x", "--gpx", dest = "gpx_file",help = "GPX input file",metavar = "gpx_file")
    oparser.add_option ("-d", "--deploy", dest = "deploy_time",help="Deploy time yyyy:jjj:hh:mm:ss.sss",metavar = "deploy_time")
    oparser.add_option ("-p", "--pickup", dest = "pickup_time",
                    help="Pickup time yyyy:jjj:hh:mm:ss.sss",
                    metavar = "pickup_time")
    oparser.add_option ("-D", "--Deploy", dest = "D_time",help="D_time will be read from GPX file and put in the following format yyyy:jjj:hh:mm:ss.sss",metavar = "D_time")
    oparser.add_option ("-P", dest = "P_time", help="P_time will be read from GPX file and put 
                    in the following format yyyy:jjj:hh:mm:ss.sss",
                    metavar = "P_time")
    oparser.add_option ("-l", "--line", dest = "line_number",help = "The line number. Caution: Assumes that all stations in GPX file are on same line",metavar = "line_number")

    options, args = oparser.parse_args()
    #print options.outfile

    GPX = options.gpx_file
    DEPLOY = options.deploy_time
    PICKUP = options.pickup_time
    LINE = options.line_number
    DTIME = options.D_time
    PTIME = options.P_time


def open_gpx () :
    fh = open (GPX)
    gpx_parser = gpxpy.parser.GPXParser (fh)
    gpx_parser.parse ()
    fh.close ()
    return gpx_parser.get_gpx ()
if __name__ == '__main__' :
    get_args ()
    gpx = open_gpx ()
    print "#STA\tDAS\tLAT\tLON\tELEV\tDEPLOY\tPICKUP\tLINE"
    for wp in gpx.waypoints :
        s=wp.name
        staname = s.split('-')[0]
        #print staname
        das = s.split('-')[1]
        #print das
        t=wp.time
        dtme=os.popen("date -d t +%Y:%j:%H:%M:%S")
        dtime=dtme.read()
        if "DEPLOY"  in locals() or "PICKUP" in locals():
                                                         line = "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format (staname,
                                                                                                                     das,
                                                                                                                     wp.latitude,
                                                                                                                     wp.longitude,
                                                                                                                     wp.elevation,
                                                                                                                     DEPLOY,
                                                                                                                     PICKUP,
                                                                                                                     LINE)
                                                   print line
        else:
            line = "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format (staname,
                                                                das,
                                                                wp.latitude,
                                                                wp.longitude,
                                                                wp.elevation,
                                                                dtime,
                                                                dtime,
                                                                LINE)
            print line

0 个答案:

没有答案