Path: blob/main/tools/output/generateMeanDataDefinitions.py
169674 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file generateMeanDataDefinitions.py14# @author Karol Stosiek15# @author Michael Behrisch16# @date 2011-10-251718from __future__ import absolute_import1920import os21import logging22import sys2324sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))25import sumolib # noqa262728def generate_mean_data_xml(detectors_xml,29detectors_type,30detectors_frequency,31detectors_suffix,32detectors_output_type):33""" Generates mean data definitions in XML format.34- detectors_xml is the detectors XML read by xml.dom.minidom.35- detectors_type is one of the supported detectors: 'e1', 'e2' or 'e3'36- detectors_frequency is either an empty string or a positive integer.37- detectors_suffix is the suffix appended to each detector id to form38a detector's aggregated results filename. It's appended with .xml39string.40"""4142meandata_xml = sumolib.xml.create_document("additional")4344for detector in detectors_xml:45detector_id = detector.getAttribute('id')46meandata_element = meandata_xml.addChild(detectors_output_type)47meandata_element.setAttribute("id", detector_id)48meandata_element.setAttribute("freq", str(detectors_frequency))49meandata_element.setAttribute("file", detector_id + detectors_suffix + ".xml")5051return meandata_xml525354if __name__ == "__main__":55# pylint: disable-msg=C01035657def get_detector_file(provided_options):58""" Returns validated detector file name located in59provided_options. Exits, if the provided60detector file is invalid (None or empty). """6162if (provided_options.detector_file is None or63provided_options.detector_file == ""):64logging.fatal("Invalid input file. \n" +65option_parser.format_help())66exit()67return sumolib.xml.parse(provided_options.detector_file, get_detector_type(provided_options) + "Detector")6869def get_detector_type(provided_options):70""" Returns validated detector type located in provided_options.71Checks if the detector type is one of e1, e2 or e3. """7273if provided_options.detector_type not in ('e1', 'e2', 'e3'):74logging.fatal("Invalid detector type.\n" +75option_parser.format_help())76exit()77return provided_options.detector_type7879def get_detector_frequency(provided_options):80""" Returns validated detector frequency located in provided_options.81Validated frequency is either an empty string or is a positive82integer. """8384if provided_options.frequency != "":85try:86frequency = int(provided_options.frequency)87if frequency < 0:88raise ValueError89return frequency90except ValueError:91logging.fatal("Invalid time range length specified.\n" +92option_parser.format_help())93exit()94return ""9596def get_detector_suffix(provided_options):97""" Returns detector suffix located in provided_options. """9899return provided_options.output_suffix100101def get_detector_output_type(provided_options):102"""If provided_options indicated that edge-based traffic should be103created, then returns \"edgeData\"; returns \"laneData\" otherwise.104"""105106if provided_options.edge_based_dump:107return "edgeData"108else:109return "laneData"110111logging.basicConfig()112113option_parser = sumolib.options.ArgumentParser()114option_parser.add_option("-d", "--detector-file", required=True,115help="Input detector FILE. Mandatory.")116option_parser.add_option("-t", "--detector-type", required=True,117choices=('e1', 'e2', 'e3'),118help="Type of detectors defined in the input. "119"Allowed values: e1, e2, e3. Mandatory.")120option_parser.add_option("-f", "--frequency",121help="The aggregation period the values the "122"detector collects shall be summed up. "123"If not given, the whole time interval "124"from begin to end is aggregated, which is "125"the default. If specified, must be a "126"positive integer (seconds) representing "127"time range length.",128default="")129option_parser.add_option("-l", "--lane-based-dump",130help="Generate lane based dump instead of "131"edge-based dump.",132dest="edge_based_dump",133action="store_false")134option_parser.add_option("-e", "--edge-based-dump",135help="Generate edge-based dump instead of "136"lane-based dump. This is the default.",137dest="edge_based_dump",138action="store_true",139default=True)140option_parser.add_option("-p", "--output-suffix",141help="Suffix to append to aggregated detector "142"output. For each detector, the detector's "143"aggregated results file with have the name "144"build from the detector's ID and this "145"suffix, with '.xml' extension. Defaults "146"to -results-aggregated.",147default="-results-aggregated")148option_parser.add_option("-o", "--output",149help="Output to write the mean data definition "150"to. Defaults to stdout.")151152options = option_parser.parse_args()153154output = sys.stdout155if options.output is not None:156output = open(options.output, "w")157sumolib.xml.writeHeader(output)158159output.write(160generate_mean_data_xml(161get_detector_file(options),162get_detector_type(options),163get_detector_frequency(options),164get_detector_suffix(options),165get_detector_output_type(options)).toXML())166167output.close()168169170