File: Reader.py

package info (click to toggle)
sumo 0.28.0+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,612 kB
  • ctags: 38,766
  • sloc: xml: 534,021; cpp: 183,697; python: 66,271; java: 43,017; ansic: 36,466; sh: 11,391; makefile: 1,411; perl: 450
file content (124 lines) | stat: -rw-r--r-- 4,032 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: Latin-1 -*-
"""
@file    Reader.py
@author  Sascha Krieg
@author  Daniel Krajzewicz
@author  Michael Behrisch
@date    2008-04-18
@version $Id: Reader.py 20433 2016-04-13 08:00:14Z behrisch $

Contains reader modules which are needed frequently

SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
Copyright (C) 2008-2016 DLR (http://www.dlr.de/) and contributors

This file is part of SUMO.
SUMO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
"""
from __future__ import absolute_import

import util.Path as path
from analysis.Taxi import *
from cPickle import dump
from cPickle import load
from os.path import exists


def readVLS_Edges():
    """Reads the net file and returns a list of all edges."""
    inputFile = open(path.net, 'r')
    for line in inputFile:
        if line.find(" <edges") != -1:
            #         delete edges tag at start and end
            words = line[line.find(">") + 1:line.find("</")].split(" ")
            break
    inputFile.close()
    return words


def readRoute_Edges(buildNew=False):
    """Reads the route file of the 'normal' traffic. 
       Goal is to fetch all edges of the net where traffic was generated."""
    edgesSet = set()
    if not exists(path.drivenEdges) or buildNew:
        inputFile = open(path.normalTrafficRoutes, 'r')
        for line in inputFile:
            if line.find("<route>") != -1:
                words = line[line.find(">") + 1:line.find("</")].split(" ")
                # add edges to set but ignore the first and the last edge
                edgesSet = edgesSet | set(words[1:-1])
        inputFile.close()
        dump(edgesSet, open(path.drivenEdges, 'w'))
    else:
        edgesSet = load(open(path.drivenEdges, 'r'))
    return edgesSet


def readRoute_EdgesMod(buildNew=False):
    """Reads a selectedLane file which contains all edges of the net with 'good' traffic."""
    edgesSet = set()
    if not exists(path.drivenEdges) or buildNew:
        inputFile = open(
            "D:/Krieg/Projekte/Diplom/Daten/auswertung/used2.txt", 'r')
        for line in inputFile:
            edge = line[5:-3]
            edgesSet.add(edge)
        inputFile.close()
        dump(edgesSet, open(path.drivenEdges, 'w'))
    else:
        edgesSet = load(open(path.drivenEdges, 'r'))
    return edgesSet


def readAnalysisInfo(WEE=False):
    """Reads the analysis file and returns a list off taxis with all available information."""
    taxiList = []
    i = -1
    if WEE:
        inputFile = open(path.analysisWEE, 'r')
    else:
        inputFile = open(path.analysis, 'r')
    for line in inputFile:
        words = line.split('"')
        if words[0] == '\t<vehicle id=':
            taxiList.append(Taxi(words[1]))
            i += 1
        if words[0] == '\t\t<step time=':
            taxiList[i].addStep(
                Step(words[1], words[3], words[5], words[7], words[9], words[11], words[13]))

    inputFile.close()
    return taxiList


def readEdgesLength():
    """Reads the length of each edge with occurs in the analysis-File and saves them in a pickled file."""
    edgeDict = {}

    # search the edges
    taxiList = readAnalysisInfo()
    for taxi in taxiList:
        for step in taxi.getSteps():
            if step.source == SOURCE_FCD or step.source == SOURCE_SIMFCD:
                edgeDict[step.edge] = 0.0

    # search the length  (in m)
    inputFile = open(path.net, 'r')

    for line in inputFile:
        words = line.split('"')
        try:
            index = words.index(" length=")
            if words[1][:-2] in edgeDict and words[1][-2:] == '_0':
                edgeDict[words[1][:-2]] = float(words[index + 1])
        except ValueError:
            # do nothing
            None

    # pickles the edgeDict
    dump(edgeDict, open(path.edgeLengthDict, 'w'))
    inputFile.close()