SUMO - Simulation of Urban MObility
emissionsDrivingCycle_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Main for an emissions calculator
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #ifdef HAVE_VERSION_H
33 #include <version.h>
34 #endif
35 
37 #include <iostream>
38 #include <string>
39 #include <ctime>
41 #include <utils/options/Option.h>
46 #include <utils/common/ToString.h>
47 #include <utils/xml/XMLSubSys.h>
55 #include "TrajectoriesHandler.h"
56 
57 
58 // ===========================================================================
59 // functions
60 // ===========================================================================
61 
62 
63 /* -------------------------------------------------------------------------
64  * main
65  * ----------------------------------------------------------------------- */
66 int
67 main(int argc, char** argv) {
68  // build options
70  // give some application descriptions
71  oc.setApplicationDescription("Computes emissions by driving a time line.");
72  oc.setApplicationName("emissionsDrivingCycle", "SUMO emissionsDrivingCycle Version " VERSION_STRING);
73  // add options
74 
76  oc.addOptionSubTopic("Input");
77  oc.doRegister("timeline-file", 't', new Option_FileName());
78  oc.addSynonyme("timeline", "timeline-file");
79  oc.addDescription("timeline-file", "Input", "Defines the file to read the driving cycle from.");
80 
81  oc.doRegister("timeline-file.skip", new Option_Integer(0));
82  oc.addSynonyme("timeline.skip", "timeline-file.skip");
83  oc.addDescription("timeline-file.skip", "Input", "Skips the firs NUM lines.");
84 
85  oc.doRegister("timeline-file.separator", new Option_String(";"));
86  oc.addSynonyme("timeline.separator", "timeline-file.separator");
87  oc.addDescription("timeline-file.separator", "Input", "Defines the entry separator.");
88 
89  oc.doRegister("netstate-file", 'n', new Option_FileName());
90  oc.addSynonyme("netstate", "netstate-file");
91  oc.addSynonyme("amitran", "netstate-file");
92  oc.addDescription("netstate-file", "Input", "Defines the netstate, route and trajectory files to read the driving cycles from.");
93 
94  oc.doRegister("emission-class", 'e', new Option_String("unknown"));
95  oc.addDescription("emission-class", "Input", "Defines for which emission class the emissions shall be generated. ");
96 
97 
98  oc.addOptionSubTopic("Processing");
99  oc.doRegister("compute-a", 'a', new Option_Bool(false));
100  oc.addDescription("compute-a", "Processing", "If set, the acceleration is computed instead of being read from the file. ");
101 
102  oc.doRegister("compute-a.forward", new Option_Bool(false));
103  oc.addDescription("compute-a.forward", "Processing", "If set, the acceleration for time t is computed from v(t+1) - v(t) instead of v(t) - v(t-1). ");
104 
105  oc.doRegister("compute-a.zero-correction", new Option_Bool(false));
106  oc.addDescription("compute-a.zero-correction", "Processing", "If set, the acceleration for time t is set to 0 if the speed is 0. ");
107 
108  oc.doRegister("skip-first", 's', new Option_Bool(false));
109  oc.addDescription("skip-first", "Processing", "If set, the first line of the read file is skipped.");
110 
111  oc.doRegister("kmh", new Option_Bool(false));
112  oc.addDescription("kmh", "Processing", "If set, the given speed is interpreted as being given in km/h.");
113 
114  oc.doRegister("have-slope", new Option_Bool(false));
115  oc.addDescription("have-slope", "Processing", "If set, the fourth column is read and used as slope (in deg).");
116 
117  oc.doRegister("slope", new Option_Float(0));
118  oc.addDescription("slope", "Processing", "Sets a global slope (in deg) that is used if the file does not contain slope information.");
119 
120  oc.addOptionSubTopic("Output");
121  oc.doRegister("output-file", 'o', new Option_String());
122  oc.addSynonyme("output", "output-file");
123  oc.addDescription("output", "Output", "Defines the file to write the emission cycle results into. ");
124 
125  oc.doRegister("emission-output", new Option_FileName());
126  oc.addDescription("emission-output", "Output", "Save the emission values of each vehicle in XML");
127 
128  oc.doRegister("sum-output", new Option_FileName());
129  oc.addSynonyme("sum", "sum-output");
130  oc.addDescription("sum-output", "Output", "Save the aggregated and normed emission values of each vehicle in CSV");
131 
132  oc.addOptionSubTopic("Emissions");
133  oc.doRegister("phemlight-path", new Option_FileName("./PHEMlight/"));
134  oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from.");
135 
137  oc.doRegister("quiet", 'q', new Option_Bool(false));
138  oc.addDescription("quiet", "Report", "Not writing anything.");
139 
140  // run
141  int ret = 0;
142  bool quiet = false;
143  try {
144  // initialise the application system (messaging, xml, options)
145  XMLSubSys::init();
146  OptionsIO::setArgs(argc, argv);
149  if (oc.processMetaOptions(argc < 2)) {
151  return 0;
152  }
153 
154  quiet = oc.getBool("quiet");
155  if (!oc.isSet("timeline-file") && !oc.isSet("netstate-file")) {
156  throw ProcessError("Either a timeline or a netstate / amitran file must be given.");
157  }
158  if (!oc.isSet("output-file") && (oc.isSet("timeline-file") || !oc.isSet("emission-output"))) {
159  throw ProcessError("The output file must be given.");
160  }
161  std::ostream* out = 0;
162  if (oc.isSet("output-file")) {
163  out = new std::ofstream(oc.getString("output-file").c_str());
164  }
165  OutputDevice::createDeviceByOption("emission-output", "emission-export", "emission_file.xsd");
166  OutputDevice* xmlOut = 0;
167  if (oc.isSet("emission-output")) {
168  xmlOut = &OutputDevice::getDeviceByOption("emission-output");
169  } else if (out == 0) {
170  out = &std::cout;
171  }
172  std::ostream* sumOut = 0;
173  if (oc.isSet("sum-output")) {
174  sumOut = new std::ofstream(oc.getString("sum-output").c_str());
175  (*sumOut) << "Vehicle,Cycle,Time,Speed,Gradient,Acceleration,FC,FCel,CO2,NOx,CO,HC,PM" << std::endl;
176  }
177 
178  const SUMOEmissionClass defaultClass = PollutantsInterface::getClassByName(oc.getString("emission-class"));
179  const bool computeA = oc.getBool("compute-a") || oc.getBool("compute-a.forward");
180  TrajectoriesHandler handler(computeA, oc.getBool("compute-a.forward"), oc.getBool("compute-a.zero-correction"), defaultClass, oc.getFloat("slope"), out, xmlOut);
181 
182  if (oc.isSet("timeline-file")) {
183  int skip = oc.getBool("skip-first") ? 1 : oc.getInt("timeline-file.skip");
184  const bool inKMH = oc.getBool("kmh");
185  const bool haveSlope = oc.getBool("have-slope");
186  double l = 0;
187  double totalA = 0;
188  double totalS = 0;
189  int time = 0;
190 
191  LineReader lr(oc.getString("timeline-file"));
192  while (lr.hasMore()) {
193  std::string line = lr.readLine();
194  if (skip > 0) {
195  skip--;
196  continue;
197  }
198  StringTokenizer st(StringUtils::prune(line), oc.getString("timeline-file.separator"));
199  if (st.hasNext()) {
200  try {
201  double t = TplConvert::_2double<char>(st.next().c_str());
202  double v = 0;
203  if (st.hasNext()) {
204  v = TplConvert::_2double<char>(st.next().c_str());
205  } else {
206  v = t;
207  t = time;
208  }
209  if (inKMH) {
210  v /= 3.6;
211  }
212  double a = !computeA && st.hasNext() ? TplConvert::_2double<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
213  double s = haveSlope && st.hasNext() ? TplConvert::_2double<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
214  if (handler.writeEmissions(*out, "", defaultClass, t, v, a, s)) {
215  l += v;
216  totalA += a;
217  totalS += s;
218  time++;
219  }
220  } catch (EmptyData&) {
221  throw ProcessError("Missing an entry in line '" + line + "'.");
222  } catch (NumberFormatException&) {
223  throw ProcessError("Not numeric entry in line '" + line + "'.");
224  }
225  }
226  }
227  if (!quiet) {
228  std::cout << "sums" << std::endl
229  << "length:" << l << std::endl;
230  }
231  if (sumOut != 0) {
232  (*sumOut) << oc.getString("emission-class") << "," << lr.getFileName() << "," << time << ","
233  << (l / time * 3.6) << "," << (totalS / time) << "," << (totalA / time) << ",";
234  handler.writeNormedSums(*sumOut, "", l);
235  }
236  }
237  if (oc.isSet("netstate-file")) {
238  XMLSubSys::runParser(handler, oc.getString("netstate-file"));
239  }
240  if (!quiet) {
241  handler.writeSums(std::cout, "");
242  }
243  } catch (InvalidArgument& e) {
245  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
246  ret = 1;
247  } catch (ProcessError& e) {
248  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
250  }
251  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
252  ret = 1;
253 #ifndef _DEBUG
254  } catch (...) {
255  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
256  ret = 1;
257 #endif
258  }
260  if (ret == 0 && !quiet) {
261  std::cout << "Success." << std::endl;
262  }
263  return ret;
264 }
265 
266 
267 
268 /****************************************************************************/
269 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:72
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:76
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:58
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
int main(int argc, char **argv)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:110
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:47
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
static const int INVALID_VALUE
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int SUMOEmissionClass
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
#define VERSION_STRING
Definition: config.h:210
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:52
An integer-option.
Definition: Option.h:313
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
An XML-Handler for amitran and netstate trajectories.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.