Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/detector/aggregateFlows.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2007-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file aggregateFlows.py
15
# @author Michael Behrisch
16
# @author Mirko Barthauer
17
# @date 2007-06-28
18
19
"""
20
This script aggregates flows as they come from the Datenverteiler
21
into files suitable for the dfrouter.
22
It takes as an optional argument a file listing the detectors of
23
interest (one per line, additional whitespace separated entries
24
do not hurt, for instance a bab_map.txt will do)
25
"""
26
from __future__ import absolute_import
27
from __future__ import print_function
28
import os
29
import sys
30
import zipfile
31
32
SUMO_HOME = os.environ.get('SUMO_HOME',
33
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
34
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
35
import sumolib # noqa
36
37
38
class Entry:
39
40
def __init__(self):
41
self.carFlow = 0
42
self.truckFlow = 0
43
self.carSpeed = 0.0
44
self.truckSpeed = 0.0
45
46
def add(self, qCar, qTruck, vCar, vTruck):
47
if qCar > 0:
48
self.carSpeed = (
49
self.carFlow * self.carSpeed + qCar * vCar) / (self.carFlow + qCar)
50
self.carFlow += qCar
51
if qTruck > 0:
52
self.truckSpeed = (
53
self.truckFlow * self.truckSpeed + qTruck * vTruck) / (self.truckFlow + qTruck)
54
self.truckFlow += qTruck
55
56
def __repr__(self):
57
return str(self.carFlow) + ";" + str(self.truckFlow) + ";" + str(self.carSpeed) + ";" + str(self.truckSpeed)
58
59
60
def readLines(lines):
61
for fl in lines:
62
flowDef = fl.split()
63
if not cityDets or flowDef[0] in cityDets:
64
if not flowDef[0] in totalFlow:
65
totalFlow[flowDef[0]] = Entry()
66
totalFlow[flowDef[0]].add(
67
int(flowDef[1]), int(flowDef[2]), float(flowDef[3]), float(flowDef[4]))
68
69
70
parser = sumolib.options.ArgumentParser(usage="usage: %(prog)s [options] [flow.txt|flows.zip]+")
71
parser.add_option("-d", "--det-file", dest="detfile", category="input", type=parser.file,
72
help="read detectors of interest from FILE", metavar="FILE")
73
parser.add_option("flowFiles", category="input", nargs="+", type=parser.file,
74
help="one or more flow input files", metavar="FILE")
75
options = parser.parse_args()
76
77
if options.detfile:
78
cityDets = set()
79
for line in open(options.detfile):
80
cityDets.add(line.split()[0])
81
else:
82
cityDets = None
83
totalFlow = {}
84
for f in options.flowFiles:
85
if os.access(f, os.R_OK):
86
if f.endswith(".zip"):
87
zipf = zipfile.ZipFile(f)
88
for fileName in zipf.namelist():
89
readLines(zipf.read(fileName).splitlines())
90
zipf.close()
91
else:
92
readLines(open(f))
93
else:
94
print("Cannot read", f, file=sys.stderr)
95
print("Detector;Time;qPKW;qLKW;vPKW;vLKW")
96
for det, flow in totalFlow.items():
97
print(det + ";0;" + str(flow))
98
99