DataTrack.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 #pragma once
14 #include <fstream>
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 #include <biogears/cdm/substance/SESubstance.h>
20 
21 namespace biogears {
22 class PressureUnit;
23 class SEElectricalCircuit;
24 class SEFluidCircuit;
25 class SEThermalCircuit;
26 class SEGasCompartmentGraph;
27 class SELiquidCompartmentGraph;
28 
30 typedef std::map<std::string, double> ProbeMap;
31 typedef std::map<std::string, double>::iterator ProbeMapItr;
32 typedef std::map<std::string, std::vector<double>*> TrackMap;
33 typedef std::map<std::string, std::vector<double>*>::iterator TrackMapItr;
34 
35 typedef std::map<std::string, SEDecimalFormat> FormattingMap;
36 
37 class BIOGEARS_API DataTrack : public Loggable {
38 public:
39  DataTrack();
40  DataTrack(Logger* m_Log);
41  DataTrack(DataTrack&&) = default;
42  DataTrack& operator=(DataTrack&&) = default;
43  ~DataTrack();
44 
45  void Clear();
46  void Reset();
47 
48  void UseTabDelimiter() { m_Delimiter = '\t'; }
49  void UseCommaDelimiter() { m_Delimiter = ','; }
50 
51  std::vector<std::string>& GetHeadings();
52 
53  void SetFormatting(const std::string& name, const SEDecimalFormat& f);
54  void SetFormatting(const std::string& name, std::streamsize precision);
55  void SetDefaultFormatting(std::streamsize precision);
56 
57  void Probe(const std::string& name, double value, int i);
58  void Probe(const std::string& name, double value);
59  void Probe(const std::string& name, std::vector<double>& values);
60  void Probe(const SEFluidCircuit& c);
61  void Probe(const SEElectricalCircuit& c);
62  void Probe(const SEThermalCircuit& c);
63  void Probe(const SELiquidCompartmentGraph& graph);
64  double GetProbe(const std::string& name);
65  ProbeMap* GetProbes();
66 
67  void Track(const std::string& name, double time, double value);
68  void Track(double time, const SEElectricalCircuit& circuit);
69  void Track(double time, const SEFluidCircuit& circuit);
70  void Track(double time, const SEThermalCircuit& circuit);
71  void Track(double time, const SEGasCompartmentGraph& graph, std::vector<SESubstance*>* substances = nullptr);
72  void Track(double time, const SELiquidCompartmentGraph& graph, std::vector<SESubstance*>* substances = nullptr);
73 
74  // Reads the entire file and stores contents into memory, returns the column headings
75  std::vector<std::string> ReadTrackFromFile(const char* fileName);
76  // Only Reads the column headings and returns them
77  // Holds onto the file handle for streaming line by line
78  std::vector<std::string> StreamDataFromFile(const char* fileName);
79 
80  // Reads a line from the file and returns the time associated with the time
81  double StreamDataFromFile(std::vector<std::string>* headings);
82 
83  // Get a specific track value at a specific time
84  double GetTrack(const std::string& name, double time);
85 
86  // Get the entire list of values for a track label
87  std::vector<double>* GetTrack(const std::string& name);
88 
89  // Get all the times
90  std::vector<double>& GetTimes();
91 
92  // Creates the file and writes the headers to that file
93  void CreateFile(const char* fileName, std::ofstream& newFile, std::ios_base::openmode mode = std::ios_base::trunc); // TODO C++11
94  void CreateFile(const std::string& fileName, std::ofstream& newFile, std::ios_base::openmode mode = std::ios_base::trunc); // TODO C++11
95  // Write all the track to a file
96  void WriteTrackToFile(const char* fileName, std::ios_base::openmode mode = std::ios_base::trunc);
97  // Writes data from the provided headings to the file, in the order things were tracked
98  void StreamTrackToFile(std::ofstream& file);
99  // Writes prob values to file in the order things were tracked
100  void StreamProbesToFile(double time, std::ofstream& file);
101 
102 protected:
103  TrackMap m_Track;
104  ProbeMap m_Probe;
105  FormattingMap m_Formatting;
106 
108  double m_LastTime = -1.0;
109  std::vector<double> m_Time;
110  std::vector<std::string> m_HeadingOrder;
111  std::streamsize m_DefaultPrecision = 3;
112 
113  std::ifstream m_FileStream;
114 };
115 }
std::vector< std::string > m_HeadingOrder
Definition: DataTrack.h:110
Definition: SELiquidCompartmentGraph.h:24
std::map< std::string, double >::iterator ProbeMapItr
Definition: DataTrack.h:31
TrackMap m_Track
Definition: DataTrack.h:103
Definition: SEElectricalCircuit.h:21
FormattingMap m_Formatting
Definition: DataTrack.h:105
std::map< std::string, std::vector< double > * >::iterator TrackMapItr
Definition: DataTrack.h:33
Definition: Logger.h:27
Definition: Logger.h:75
ProbeMap m_Probe
Definition: DataTrack.h:104
Definition: SEFluidCircuit.h:20
std::ifstream m_FileStream
Definition: DataTrack.h:113
std::map< std::string, double > ProbeMap
Definition: DataTrack.h:29
Definition: DataTrack.h:37
void UseTabDelimiter()
Definition: DataTrack.h:48
Definition: SEDecimalFormat.h:25
Definition: SEGasCompartmentGraph.h:25
char m_Delimiter
Definition: DataTrack.h:107
std::vector< double > m_Time
Definition: DataTrack.h:109
Definition: SEThermalCircuit.h:20
Definition: SEElectricalCircuit.h:18
std::map< std::string, std::vector< double > * > TrackMap
Definition: DataTrack.h:32
std::map< std::string, SEDecimalFormat > FormattingMap
Definition: DataTrack.h:35
void UseCommaDelimiter()
Definition: DataTrack.h:49