Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file RODFDetFlowLoader.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Eric Nicolay 17 : /// @author Jakob Erdmann 18 : /// @author Michael Behrisch 19 : /// @date Thu, 16.03.2006 20 : /// 21 : // A loader for detector flows 22 : /****************************************************************************/ 23 : #include <config.h> 24 : 25 : #include <string> 26 : #include <fstream> 27 : #include <sstream> 28 : #include <utils/importio/LineReader.h> 29 : #include <utils/options/OptionsCont.h> 30 : #include <utils/common/StringTokenizer.h> 31 : #include <utils/common/MsgHandler.h> 32 : #include <utils/common/FileHelpers.h> 33 : #include <utils/common/StringUtils.h> 34 : #include <utils/common/UtilExceptions.h> 35 : #include "RODFDetFlowLoader.h" 36 : 37 : 38 : // =========================================================================== 39 : // method definitions 40 : // =========================================================================== 41 110 : RODFDetFlowLoader::RODFDetFlowLoader(const RODFDetectorCon& dets, 42 : RODFDetectorFlows& into, 43 : SUMOTime startTime, SUMOTime endTime, 44 110 : SUMOTime timeOffset, SUMOTime timeScale) 45 110 : : myStorage(into), myTimeOffset(timeOffset), myTimeScale(timeScale), 46 220 : myStartTime(startTime), myEndTime(endTime), myDetectorContainer(dets), 47 110 : myHaveWarnedAboutOverridingBoundaries(false), myHaveWarnedAboutPartialDefs(false) {} 48 : 49 : 50 : 51 110 : RODFDetFlowLoader::~RODFDetFlowLoader() {} 52 : 53 : 54 : void 55 110 : RODFDetFlowLoader::read(const std::string& file) { 56 110 : LineReader lr(file); 57 : // parse first line 58 220 : myLineHandler.reinit(lr.readLine(), ";", ";", true, true); 59 : // parse values 60 9678 : while (lr.hasMore()) { 61 9572 : std::string line = lr.readLine(); 62 9572 : if (line.find(';') == std::string::npos) { 63 2 : continue; 64 : } 65 9570 : myLineHandler.parseLine(line); 66 : try { 67 9570 : std::string detName = myLineHandler.get("detector"); 68 9568 : if (!myDetectorContainer.knows(detName)) { 69 48 : continue; 70 : } 71 9520 : const SUMOTime time = (SUMOTime)(StringUtils::toDouble(myLineHandler.get("time")) * (double)myTimeScale + .5) - myTimeOffset; 72 : // parsing as float to handle values which would cause int overflow 73 9519 : if (time < myStartTime || time >= myEndTime) { 74 1451 : if (!myHaveWarnedAboutOverridingBoundaries) { 75 2 : myHaveWarnedAboutOverridingBoundaries = true; 76 4 : WRITE_WARNING(TL("At least one value lies beyond given time boundaries.")); 77 : } 78 1451 : continue; 79 : } 80 : FlowDef fd; 81 8068 : fd.isLKW = 0; 82 8068 : fd.qPKW = StringUtils::toDouble(myLineHandler.get("qpkw")); 83 8067 : fd.vPKW = 0; 84 16134 : if (myLineHandler.know("vPKW")) { 85 8023 : fd.vPKW = StringUtils::toDouble(myLineHandler.get("vpkw")); 86 : } 87 8067 : fd.qLKW = 0; 88 16134 : if (myLineHandler.know("qLKW")) { 89 8023 : fd.qLKW = StringUtils::toDouble(myLineHandler.get("qlkw")); 90 : } 91 8067 : fd.vLKW = 0; 92 16134 : if (myLineHandler.know("vLKW")) { 93 8023 : fd.vLKW = StringUtils::toDouble(myLineHandler.get("vlkw")); 94 : } 95 8067 : if (fd.qLKW < 0) { 96 0 : fd.qLKW = 0; 97 : } 98 8067 : if (fd.qPKW < 0) { 99 0 : fd.qPKW = 0; 100 : } 101 8067 : myStorage.addFlow(detName, time, fd); 102 8067 : if (!myHaveWarnedAboutPartialDefs && !myLineHandler.hasFullDefinition()) { 103 3 : myHaveWarnedAboutPartialDefs = true; 104 8 : WRITE_WARNING(TL("At least one line does not contain the correct number of columns.")); 105 : } 106 8067 : continue; 107 4 : } catch (ProcessError& e) { 108 8 : throw ProcessError(toString(e.what()) + " in '" + lr.getFileName() + "', line " + toString(lr.getLineNumber()) + ";\n" 109 8 : + " The following values must be supplied : 'Detector', 'Time', 'qPKW'\n" 110 16 : + " The according column names must be given in the first line of the file."); 111 4 : } 112 : } 113 110 : } 114 : 115 : 116 : /****************************************************************************/