SUMO - Simulation of Urban MObility
RODFDetFlowLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A loader for detector flows
21 /****************************************************************************/
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <fstream>
33 #include <sstream>
41 #include "RODFDetFlowLoader.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  RODFDetectorFlows& into,
49  SUMOTime startTime, SUMOTime endTime,
50  SUMOTime timeOffset, SUMOTime timeScale)
51  : myStorage(into), myTimeOffset(timeOffset), myTimeScale(timeScale),
52  myStartTime(startTime), myEndTime(endTime), myDetectorContainer(dets),
53  myHaveWarnedAboutOverridingBoundaries(false), myHaveWarnedAboutPartialDefs(false) {}
54 
55 
56 
58 
59 
60 void
61 RODFDetFlowLoader::read(const std::string& file) {
62  LineReader lr(file);
63  // parse first line
64  myLineHandler.reinit(lr.readLine(), ";", ";", true, true);
65  // parse values
66  while (lr.hasMore()) {
67  std::string line = lr.readLine();
68  if (line.find(';') == std::string::npos) {
69  continue;
70  }
72  try {
73  std::string detName = myLineHandler.get("detector");
74  if (!myDetectorContainer.knows(detName)) {
75  continue;
76  }
77  const double parsedTime = TplConvert::_2double((myLineHandler.get("time").c_str())) * myTimeScale - myTimeOffset;
78  // parsing as float to handle values which would cause int overflow
79  if (parsedTime < myStartTime || parsedTime >= myEndTime) {
82  WRITE_WARNING("At least one value lies beyond given time boundaries.");
83  }
84  continue;
85  }
86  const SUMOTime time = (SUMOTime)(parsedTime + .5);
87  FlowDef fd;
88  fd.isLKW = 0;
89  fd.qPKW = TplConvert::_2double(myLineHandler.get("qpkw").c_str());
90  fd.vPKW = 0;
91  if (myLineHandler.know("vPKW")) {
92  fd.vPKW = TplConvert::_2double(myLineHandler.get("vpkw").c_str());
93  }
94  fd.qLKW = 0;
95  if (myLineHandler.know("qLKW")) {
96  fd.qLKW = TplConvert::_2double(myLineHandler.get("qlkw").c_str());
97  }
98  fd.vLKW = 0;
99  if (myLineHandler.know("vLKW")) {
100  fd.vLKW = TplConvert::_2double(myLineHandler.get("vlkw").c_str());
101  }
102  if (fd.qLKW < 0) {
103  fd.qLKW = 0;
104  }
105  if (fd.qPKW < 0) {
106  fd.qPKW = 0;
107  }
108  myStorage.addFlow(detName, time, fd);
111  WRITE_WARNING("At least one line does not contain the correct number of columns.");
112  }
113  continue;
114  } catch (UnknownElement&) {} catch (OutOfBoundsException&) {} catch (NumberFormatException&) {}
115  throw ProcessError("The detector-flow-file '" + lr.getFileName() + "' is corrupt;\n"
116  + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n"
117  + " The according column names must be given in the first line of the file.");
118  }
119 }
120 
121 
122 /****************************************************************************/
123 
bool myHaveWarnedAboutOverridingBoundaries
Whether a warning about overriding boundaries was already written.
const SUMOTime myTimeOffset
The time offset to apply to read time values.
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:75
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:178
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:57
const SUMOTime myEndTime
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
const RODFDetectorCon & myDetectorContainer
Container holding known detectors.
static double fd[10]
Definition: odrSpiral.cpp:94
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
RODFDetFlowLoader(const RODFDetectorCon &dets, RODFDetectorFlows &into, SUMOTime startTime, SUMOTime endTime, SUMOTime timeOffset, SUMOTime timeScale)
Constructor.
A container for flows.
A container for RODFDetectors.
Definition: RODFDetector.h:227
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
double vPKW
RODFDetectorFlows & myStorage
The container for read detector values.
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
bool know(const std::string &name) const
Returns the information whether the named column is known.
double vLKW
bool knows(const std::string &id) const
void read(const std::string &file)
Reads the given file assuming it contains detector values.
Definition of the traffic during a certain time containing the flows and speeds.
double qPKW
const SUMOTime myTimeScale
The time scale to apply to read time values.
bool myHaveWarnedAboutPartialDefs
Whether a warning about partial definitions was already written.
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:59
NamedColumnsParser myLineHandler
The value extractor.
double isLKW
double qLKW
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
~RODFDetFlowLoader()
Destructor.
long long int SUMOTime
Definition: TraCIDefs.h:51
void parseLine(const std::string &line)
Parses the contents of the line.