tsv_export.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #include <corbo-core/tsv_export.h>
26 
27 #include <fstream>
28 
29 namespace corbo {
30 
31 bool TsvExporter::exportTimeSeriesSignal(const std::string& filename, const TimeSeriesSignal& signal)
32 {
33  if (!signal.getTimeSeries()) return false;
34  return exportTimeSeries(filename, *signal.getTimeSeries());
35 }
36 
37 bool TsvExporter::exportTimeSeries(const std::string& filename, const TimeSeries& time_series)
38 {
39  std::ofstream file;
40  file.open(filename);
41  if (!file.is_open()) return false;
42 
43  if (time_series.getTimeDimension() != time_series.getValuesMatrixView().cols())
44  {
45  PRINT_ERROR_NAMED("time dimension does not match number of columns in values matrix");
46  return false;
47  }
48 
49  // add field names (first line)
50  file << "t";
51  for (int i = 0; i < time_series.getValueDimension(); ++i)
52  {
53  // use value label if present
54  if (i < time_series.getValueLabels().size())
55  file << "\t" << time_series.getValueLabels()[i];
56  else
57  file << "\tx" << i;
58  }
59  file << "\n"; // add new line. Note, std:endl also flushes the stream
60 
61  // now add values
62  for (int i = 0; i < time_series.getTimeDimension(); ++i)
63  {
64  // add time
65  file << time_series.getTime()[i];
66  // add values
67  for (int j = 0; j < time_series.getValueDimension(); ++j)
68  {
69  file << "\t" << time_series.getValuesMap(i)[j];
70  }
71  // start new line
72  file << "\n";
73  }
74 
75  file.close();
76  return true;
77 }
78 
79 
80 } // namespace corbo
81 
PRINT_ERROR_NAMED
#define PRINT_ERROR_NAMED(msg)
Definition: console.h:260
corbo
Definition: communication/include/corbo-communication/utilities.h:37
corbo::TsvExporter::exportTimeSeries
bool exportTimeSeries(const std::string &filename, const TimeSeries &time_series) override
Definition: tsv_export.cpp:59
relicense.filename
filename
Definition: relicense.py:57
corbo::TsvExporter::exportTimeSeriesSignal
bool exportTimeSeriesSignal(const std::string &filename, const TimeSeriesSignal &signal) override
Definition: tsv_export.cpp:53
tsv_export.h


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:07:13