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-2016 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 #ifdef CHECK_MEMORY_LEAKS
58 #include <foreign/nvwa/debug_new.h>
59 #endif // CHECK_MEMORY_LEAKS
60 
61 
62 // ===========================================================================
63 // functions
64 // ===========================================================================
65 
66 
67 /* -------------------------------------------------------------------------
68  * main
69  * ----------------------------------------------------------------------- */
70 int
71 main(int argc, char** argv) {
72  // build options
74  // give some application descriptions
75  oc.setApplicationDescription("Computes emissions by driving a time line.");
76  oc.setApplicationName("emissionsDrivingCycle", "SUMO emissionsDrivingCycle Version " VERSION_STRING);
77  // add options
78 
80  oc.addOptionSubTopic("Input");
81  oc.doRegister("timeline-file", 't', new Option_FileName());
82  oc.addSynonyme("timeline", "timeline-file");
83  oc.addDescription("timeline-file", "Input", "Defines the file to read the driving cycle from.");
84 
85  oc.doRegister("timeline-file.skip", new Option_Integer(0));
86  oc.addSynonyme("timeline.skip", "timeline-file.skip");
87  oc.addDescription("timeline-file.skip", "Input", "Skips the firs NUM lines.");
88 
89  oc.doRegister("timeline-file.separator", new Option_String(";"));
90  oc.addSynonyme("timeline.separator", "timeline-file.separator");
91  oc.addDescription("timeline-file.separator", "Input", "Defines the entry separator.");
92 
93  oc.doRegister("netstate-file", 'n', new Option_FileName());
94  oc.addSynonyme("netstate", "netstate-file");
95  oc.addSynonyme("amitran", "netstate-file");
96  oc.addDescription("netstate-file", "Input", "Defines the netstate, route and trajectory files to read the driving cycles from.");
97 
98  oc.doRegister("emission-class", 'e', new Option_String("unknown"));
99  oc.addDescription("emission-class", "Input", "Defines for which emission class the emissions shall be generated. ");
100 
101 
102  oc.addOptionSubTopic("Processing");
103  oc.doRegister("compute-a", 'a', new Option_Bool(false));
104  oc.addDescription("compute-a", "Processing", "If set, the acceleration is computed instead of being read from the file. ");
105 
106  oc.doRegister("compute-a.forward", new Option_Bool(false));
107  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). ");
108 
109  oc.doRegister("compute-a.zero-correction", new Option_Bool(false));
110  oc.addDescription("compute-a.zero-correction", "Processing", "If set, the acceleration for time t is set to 0 if the speed is 0. ");
111 
112  oc.doRegister("skip-first", 's', new Option_Bool(false));
113  oc.addDescription("skip-first", "Processing", "If set, the first line of the read file is skipped.");
114 
115  oc.doRegister("kmh", new Option_Bool(false));
116  oc.addDescription("kmh", "Processing", "If set, the given speed is interpreted as being given in km/h.");
117 
118  oc.doRegister("have-slope", new Option_Bool(false));
119  oc.addDescription("have-slope", "Processing", "If set, the fourth column is read and used as slope (in deg).");
120 
121  oc.doRegister("slope", new Option_Float(0));
122  oc.addDescription("slope", "Processing", "Sets a global slope (in deg) that is used if the file does not contain slope information.");
123 
124  oc.addOptionSubTopic("Output");
125  oc.doRegister("output-file", 'o', new Option_String());
126  oc.addSynonyme("output", "output-file");
127  oc.addDescription("output", "Output", "Defines the file to write the emission cycle results into. ");
128 
129  oc.doRegister("emission-output", new Option_FileName());
130  oc.addDescription("emission-output", "Output", "Save the emission values of each vehicle in XML");
131 
132  oc.doRegister("sum-output", new Option_FileName());
133  oc.addSynonyme("sum", "sum-output");
134  oc.addDescription("sum-output", "Output", "Save the aggregated and normed emission values of each vehicle in CSV");
135 
136  oc.addOptionSubTopic("Emissions");
137  oc.doRegister("phemlight-path", new Option_FileName("./PHEMlight/"));
138  oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from.");
139 
141  oc.doRegister("quiet", 'q', new Option_Bool(false));
142  oc.addDescription("quiet", "Report", "Not writing anything.");
143 
144  // run
145  int ret = 0;
146  bool quiet = false;
147  try {
148  // initialise the application system (messaging, xml, options)
149  XMLSubSys::init();
150  OptionsIO::setArgs(argc, argv);
153  if (oc.processMetaOptions(argc < 2)) {
155  return 0;
156  }
157 
158  quiet = oc.getBool("quiet");
159  if (!oc.isSet("timeline-file") && !oc.isSet("netstate-file")) {
160  throw ProcessError("Either a timeline or a netstate / amitran file must be given.");
161  }
162  if (!oc.isSet("output-file") && (oc.isSet("timeline-file") || !oc.isSet("emission-output"))) {
163  throw ProcessError("The output file must be given.");
164  }
165  std::ostream* out = 0;
166  if (oc.isSet("output-file")) {
167  out = new std::ofstream(oc.getString("output-file").c_str());
168  }
169  OutputDevice::createDeviceByOption("emission-output", "emission-export", "emission_file.xsd");
170  OutputDevice* xmlOut = 0;
171  if (oc.isSet("emission-output")) {
172  xmlOut = &OutputDevice::getDeviceByOption("emission-output");
173  } else if (out == 0) {
174  out = &std::cout;
175  }
176  std::ostream* sumOut = 0;
177  if (oc.isSet("sum-output")) {
178  sumOut = new std::ofstream(oc.getString("sum-output").c_str());
179  (*sumOut) << "Vehicle,Cycle,Time,Speed,Gradient,Acceleration,FC,FCel,CO2,NOx,CO,HC,PM" << std::endl;
180  }
181 
182  const SUMOEmissionClass defaultClass = PollutantsInterface::getClassByName(oc.getString("emission-class"));
183  const bool computeA = oc.getBool("compute-a") || oc.getBool("compute-a.forward");
184  TrajectoriesHandler handler(computeA, oc.getBool("compute-a.forward"), oc.getBool("compute-a.zero-correction"), defaultClass, oc.getFloat("slope"), out, xmlOut);
185 
186  if (oc.isSet("timeline-file")) {
187  int skip = oc.getBool("skip-first") ? 1 : oc.getInt("timeline-file.skip");
188  const bool inKMH = oc.getBool("kmh");
189  const bool haveSlope = oc.getBool("have-slope");
190  SUMOReal l = 0;
191  SUMOReal totalA = 0;
192  SUMOReal totalS = 0;
193  int time = 0;
194 
195  LineReader lr(oc.getString("timeline-file"));
196  while (lr.hasMore()) {
197  std::string line = lr.readLine();
198  if (skip > 0) {
199  skip--;
200  continue;
201  }
202  StringTokenizer st(StringUtils::prune(line), oc.getString("timeline-file.separator"));
203  if (st.hasNext()) {
204  try {
205  SUMOReal t = TplConvert::_2SUMOReal<char>(st.next().c_str());
206  SUMOReal v = 0;
207  if (st.hasNext()) {
208  v = TplConvert::_2SUMOReal<char>(st.next().c_str());
209  } else {
210  v = t;
211  t = time;
212  }
213  if (inKMH) {
214  v /= 3.6;
215  }
216  SUMOReal a = !computeA && st.hasNext() ? TplConvert::_2SUMOReal<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
217  SUMOReal s = haveSlope && st.hasNext() ? TplConvert::_2SUMOReal<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
218  if (handler.writeEmissions(*out, "", defaultClass, t, v, a, s)) {
219  l += v;
220  totalA += a;
221  totalS += s;
222  time++;
223  }
224  } catch (EmptyData&) {
225  throw ProcessError("Missing an entry in line '" + line + "'.");
226  } catch (NumberFormatException&) {
227  throw ProcessError("Not numeric entry in line '" + line + "'.");
228  }
229  }
230  }
231  if (!quiet) {
232  std::cout << "sums" << std::endl
233  << "length:" << l << std::endl;
234  }
235  if (sumOut != 0) {
236  (*sumOut) << oc.getString("emission-class") << "," << lr.getFileName() << "," << time << ","
237  << (l / time * 3.6) << "," << (totalS / time) << "," << (totalA / time) << ",";
238  handler.writeNormedSums(*sumOut, "", l);
239  }
240  }
241  if (oc.isSet("netstate-file")) {
242  XMLSubSys::runParser(handler, oc.getString("netstate-file"));
243  }
244  if (!quiet) {
245  handler.writeSums(std::cout, "");
246  }
247  } catch (InvalidArgument& e) {
249  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
250  ret = 1;
251  } catch (ProcessError& e) {
252  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
254  }
255  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
256  ret = 1;
257 #ifndef _DEBUG
258  } catch (...) {
259  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
260  ret = 1;
261 #endif
262  }
264  if (ret == 0 && !quiet) {
265  std::cout << "Success." << std::endl;
266  }
267  return ret;
268 }
269 
270 
271 
272 /****************************************************************************/
273 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
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:72
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:75
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:80
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:114
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:50
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:65
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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.
#define VERSION_STRING
Definition: config.h:225
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
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:89
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
#define SUMOReal
Definition: config.h:213
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.