Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/route_1htoDay.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-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 route_1htoDay.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 11.09.2009
18
19
"""
20
Uses "route_departOffset.py" for building 24 route files which describe
21
a whole day assuming the given route files describes an hour.
22
"""
23
from __future__ import absolute_import
24
from __future__ import print_function
25
26
import sys
27
import os
28
import route_departOffset
29
if 'SUMO_HOME' in os.environ:
30
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
31
sys.path.append(os.path.join(tools))
32
from sumolib.options import ArgumentParser # noqa
33
from sumolib.xml import parse # noqa
34
from sumolib.miscutils import openz # noqa
35
import sumolib # noqa
36
else:
37
sys.exit("please declare environment variable 'SUMO_HOME'")
38
39
40
def get_options(args=None):
41
op = ArgumentParser()
42
op.add_argument("routes", category='input',
43
help="Provide an input route file for 1h")
44
op.add_argument("-o", "--output-file", category='output', dest="output",
45
help="Set an output file to contain all routes")
46
op.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False,
47
help="tell me what you are doing")
48
return op.parse_args(args=args)
49
50
51
def main(options):
52
output_files = []
53
for i in range(0, 24):
54
out = options.routes
55
out = out[:out.find(".")] + "_" + str(i) + out[out.find("."):]
56
output_files.append(out)
57
if options.verbose:
58
print("Building routes for hour " + str(i) + " into '" + out + "'...")
59
route_departOffset.main(
60
route_departOffset.get_options([
61
"--input-file", options.routes,
62
"--output-file", out,
63
"--depart-offset", str(i * 3600),
64
"--modify-ids"]))
65
66
if options.output:
67
with openz(options.output, 'w') as outf:
68
sumolib.writeXMLHeader(outf, "$Id$", "routes", options=options)
69
for out in output_files:
70
with openz(out) as tmpf:
71
for line in tmpf:
72
if "<routes" not in line and "</routes>" not in line:
73
outf.write(line)
74
os.remove(out)
75
outf.write('</routes>\n')
76
77
78
if __name__ == "__main__":
79
main(get_options())
80
81