Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/generateMeanDataDefinitions.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 generateMeanDataDefinitions.py
15
# @author Karol Stosiek
16
# @author Michael Behrisch
17
# @date 2011-10-25
18
19
from __future__ import absolute_import
20
21
import os
22
import logging
23
import sys
24
25
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
26
import sumolib # noqa
27
28
29
def generate_mean_data_xml(detectors_xml,
30
detectors_type,
31
detectors_frequency,
32
detectors_suffix,
33
detectors_output_type):
34
""" Generates mean data definitions in XML format.
35
- detectors_xml is the detectors XML read by xml.dom.minidom.
36
- detectors_type is one of the supported detectors: 'e1', 'e2' or 'e3'
37
- detectors_frequency is either an empty string or a positive integer.
38
- detectors_suffix is the suffix appended to each detector id to form
39
a detector's aggregated results filename. It's appended with .xml
40
string.
41
"""
42
43
meandata_xml = sumolib.xml.create_document("additional")
44
45
for detector in detectors_xml:
46
detector_id = detector.getAttribute('id')
47
meandata_element = meandata_xml.addChild(detectors_output_type)
48
meandata_element.setAttribute("id", detector_id)
49
meandata_element.setAttribute("freq", str(detectors_frequency))
50
meandata_element.setAttribute("file", detector_id + detectors_suffix + ".xml")
51
52
return meandata_xml
53
54
55
if __name__ == "__main__":
56
# pylint: disable-msg=C0103
57
58
def get_detector_file(provided_options):
59
""" Returns validated detector file name located in
60
provided_options. Exits, if the provided
61
detector file is invalid (None or empty). """
62
63
if (provided_options.detector_file is None or
64
provided_options.detector_file == ""):
65
logging.fatal("Invalid input file. \n" +
66
option_parser.format_help())
67
exit()
68
return sumolib.xml.parse(provided_options.detector_file, get_detector_type(provided_options) + "Detector")
69
70
def get_detector_type(provided_options):
71
""" Returns validated detector type located in provided_options.
72
Checks if the detector type is one of e1, e2 or e3. """
73
74
if provided_options.detector_type not in ('e1', 'e2', 'e3'):
75
logging.fatal("Invalid detector type.\n" +
76
option_parser.format_help())
77
exit()
78
return provided_options.detector_type
79
80
def get_detector_frequency(provided_options):
81
""" Returns validated detector frequency located in provided_options.
82
Validated frequency is either an empty string or is a positive
83
integer. """
84
85
if provided_options.frequency != "":
86
try:
87
frequency = int(provided_options.frequency)
88
if frequency < 0:
89
raise ValueError
90
return frequency
91
except ValueError:
92
logging.fatal("Invalid time range length specified.\n" +
93
option_parser.format_help())
94
exit()
95
return ""
96
97
def get_detector_suffix(provided_options):
98
""" Returns detector suffix located in provided_options. """
99
100
return provided_options.output_suffix
101
102
def get_detector_output_type(provided_options):
103
"""If provided_options indicated that edge-based traffic should be
104
created, then returns \"edgeData\"; returns \"laneData\" otherwise.
105
"""
106
107
if provided_options.edge_based_dump:
108
return "edgeData"
109
else:
110
return "laneData"
111
112
logging.basicConfig()
113
114
option_parser = sumolib.options.ArgumentParser()
115
option_parser.add_option("-d", "--detector-file", required=True,
116
help="Input detector FILE. Mandatory.")
117
option_parser.add_option("-t", "--detector-type", required=True,
118
choices=('e1', 'e2', 'e3'),
119
help="Type of detectors defined in the input. "
120
"Allowed values: e1, e2, e3. Mandatory.")
121
option_parser.add_option("-f", "--frequency",
122
help="The aggregation period the values the "
123
"detector collects shall be summed up. "
124
"If not given, the whole time interval "
125
"from begin to end is aggregated, which is "
126
"the default. If specified, must be a "
127
"positive integer (seconds) representing "
128
"time range length.",
129
default="")
130
option_parser.add_option("-l", "--lane-based-dump",
131
help="Generate lane based dump instead of "
132
"edge-based dump.",
133
dest="edge_based_dump",
134
action="store_false")
135
option_parser.add_option("-e", "--edge-based-dump",
136
help="Generate edge-based dump instead of "
137
"lane-based dump. This is the default.",
138
dest="edge_based_dump",
139
action="store_true",
140
default=True)
141
option_parser.add_option("-p", "--output-suffix",
142
help="Suffix to append to aggregated detector "
143
"output. For each detector, the detector's "
144
"aggregated results file with have the name "
145
"build from the detector's ID and this "
146
"suffix, with '.xml' extension. Defaults "
147
"to -results-aggregated.",
148
default="-results-aggregated")
149
option_parser.add_option("-o", "--output",
150
help="Output to write the mean data definition "
151
"to. Defaults to stdout.")
152
153
options = option_parser.parse_args()
154
155
output = sys.stdout
156
if options.output is not None:
157
output = open(options.output, "w")
158
sumolib.xml.writeHeader(output)
159
160
output.write(
161
generate_mean_data_xml(
162
get_detector_file(options),
163
get_detector_type(options),
164
get_detector_frequency(options),
165
get_detector_suffix(options),
166
get_detector_output_type(options)).toXML())
167
168
output.close()
169
170