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("skip-first", 's', new Option_Bool(false));
110  oc.addDescription("skip-first", "Processing", "If set, the first line of the read file is skipped.");
111 
112  oc.doRegister("kmh", new Option_Bool(false));
113  oc.addDescription("kmh", "Processing", "If set, the given speed is interpreted as being given in km/h.");
114 
115  oc.doRegister("have-slope", new Option_Bool(false));
116  oc.addDescription("have-slope", "Processing", "If set, the fourth column is read and used as slope (in deg).");
117 
118  oc.doRegister("slope", new Option_Float(0));
119  oc.addDescription("slope", "Processing", "Sets a global slope (in deg) that is used if the file does not contain slope information.");
120 
121  oc.addOptionSubTopic("Output");
122  oc.doRegister("output-file", 'o', new Option_String());
123  oc.addSynonyme("output", "output-file");
124  oc.addDescription("output", "Output", "Defines the file to write the emission cycle results into. ");
125 
126  oc.doRegister("emission-output", new Option_FileName());
127  oc.addDescription("emission-output", "Output", "Save the emission values of each vehicle in XML");
128 
129  oc.doRegister("sum-output", new Option_FileName());
130  oc.addSynonyme("sum", "sum-output");
131  oc.addDescription("sum-output", "Output", "Save the aggregated and normed emission values of each vehicle in CSV");
132 
133  oc.addOptionSubTopic("Emissions");
134  oc.doRegister("phemlight-path", new Option_FileName("./PHEMlight/"));
135  oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from.");
136 
138  oc.doRegister("quiet", 'q', new Option_Bool(false));
139  oc.addDescription("quiet", "Report", "Not writing anything.");
140 
141  // run
142  int ret = 0;
143  bool quiet = false;
144  try {
145  // initialise the application system (messaging, xml, options)
146  XMLSubSys::init();
147  OptionsIO::setArgs(argc, argv);
150  if (oc.processMetaOptions(argc < 2)) {
152  return 0;
153  }
154 
155  quiet = oc.getBool("quiet");
156  if (!oc.isSet("timeline-file") && !oc.isSet("netstate-file")) {
157  throw ProcessError("Either a timeline or a netstate / amitran file must be given.");
158  }
159  if (!oc.isSet("output-file") && (oc.isSet("timeline-file") || !oc.isSet("emission-output"))) {
160  throw ProcessError("The output file must be given.");
161  }
162  std::ostream* out = 0;
163  if (oc.isSet("output-file")) {
164  out = new std::ofstream(oc.getString("output-file").c_str());
165  }
166  OutputDevice::createDeviceByOption("emission-output", "emission-export", "emission_file.xsd");
167  OutputDevice* xmlOut = 0;
168  if (oc.isSet("emission-output")) {
169  xmlOut = &OutputDevice::getDeviceByOption("emission-output");
170  } else if (out == 0) {
171  out = &std::cout;
172  }
173  std::ostream* sumOut = 0;
174  if (oc.isSet("sum-output")) {
175  sumOut = new std::ofstream(oc.getString("sum-output").c_str());
176  (*sumOut) << "Vehicle,Cycle,Time,Speed,Gradient,Acceleration,FC,FCel,CO2,NOx,CO,HC,PM" << std::endl;
177  }
178 
179  const SUMOEmissionClass defaultClass = PollutantsInterface::getClassByName(oc.getString("emission-class"));
180  const bool computeA = oc.getBool("compute-a") || oc.getBool("compute-a.forward");
181  TrajectoriesHandler handler(computeA, oc.getBool("compute-a.forward"), defaultClass, oc.getFloat("slope"), out, xmlOut);
182 
183  if (oc.isSet("timeline-file")) {
184  int skip = oc.getBool("skip-first") ? 1 : oc.getInt("timeline-file.skip");
185  const bool inKMH = oc.getBool("kmh");
186  const bool haveSlope = oc.getBool("have-slope");
187  SUMOReal l = 0;
188  SUMOReal totalA = 0;
189  SUMOReal totalS = 0;
190  int time = 0;
191 
192  LineReader lr(oc.getString("timeline-file"));
193  while (lr.hasMore()) {
194  std::string line = lr.readLine();
195  if (skip > 0) {
196  skip--;
197  continue;
198  }
199  StringTokenizer st(StringUtils::prune(line), oc.getString("timeline-file.separator"));
200  if (st.size() < 2) {
201  throw ProcessError("Each line must at least include the time and the speed.");
202  }
203  try {
204  const SUMOReal t = TplConvert::_2SUMOReal<char>(st.next().c_str());
205  SUMOReal v = TplConvert::_2SUMOReal<char>(st.next().c_str());
206  if (inKMH) {
207  v /= 3.6;
208  }
209  SUMOReal a = !computeA && st.hasNext() ? TplConvert::_2SUMOReal<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
210  SUMOReal s = haveSlope ? TplConvert::_2SUMOReal<char>(st.next().c_str()) : TrajectoriesHandler::INVALID_VALUE;
211  if (handler.writeEmissions(*out, "", defaultClass, t, v, a, s)) {
212  l += v;
213  totalA += a;
214  totalS += s;
215  time++;
216  }
217  } catch (EmptyData&) {
218  throw ProcessError("Missing an entry in line '" + line + "'.");
219  } catch (NumberFormatException&) {
220  throw ProcessError("Not numeric entry in line '" + line + "'.");
221  }
222  }
223  if (!quiet) {
224  std::cout << "sums" << std::endl
225  << "length:" << l << std::endl;
226  }
227  if (sumOut != 0) {
228  (*sumOut) << oc.getString("emission-class") << "," << lr.getFileName() << "," << time << ","
229  << (l / time * 3.6) << "," << (totalS / time) << "," << (totalA / time) << ",";
230  handler.writeNormedSums(*sumOut, "", l);
231  }
232  }
233  if (oc.isSet("netstate-file")) {
234  XMLSubSys::runParser(handler, oc.getString("netstate-file"));
235  }
236  if (!quiet) {
237  handler.writeSums(std::cout, "");
238  }
239  } catch (InvalidArgument& e) {
241  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
242  ret = 1;
243  } catch (ProcessError& e) {
244  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
246  }
247  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
248  ret = 1;
249 #ifndef _DEBUG
250  } catch (...) {
251  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
252  ret = 1;
253 #endif
254  }
256  if (ret == 0 && !quiet) {
257  std::cout << "Success." << std::endl;
258  }
259  return ret;
260 }
261 
262 
263 
264 /****************************************************************************/
265 
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
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.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
int main(int argc, char **argv)
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
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
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
int SUMOEmissionClass
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 void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
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:108
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.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.