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 
#define PRINT_ERROR_NAMED(msg)
Definition: console.h:260
Time Series (trajectory resp. sequence of values w.r.t. time)
Definition: time_series.h:54
const TimeSeries * getTimeSeries() const
Read access to the underlying time series object (returns null if not initialized) ...
Definition: signals.h:298
bool exportTimeSeries(const std::string &filename, const TimeSeries &time_series) override
Definition: tsv_export.cpp:37
int getValueDimension() const
Return dimension of the value vector.
Definition: time_series.h:79
int getTimeDimension() const
Return dimension of the time vector.
Definition: time_series.h:81
ValuesMatConstMap getValuesMatrixView() const
Read access to the complete values matrix in Eigen matrix format [getValueDimension() x getTimeDimens...
Definition: time_series.h:164
const std::vector< double > & getTime() const
Read access to the underlying time values [getTimeDimension() x 1].
Definition: time_series.h:169
Time Series signal (trajectory resp. sequence of values w.r.t. time)
Definition: signals.h:244
const std::vector< std::string > & getValueLabels() const
Read individual value labels (non-empty if provided)
Definition: time_series.h:128
bool exportTimeSeriesSignal(const std::string &filename, const TimeSeriesSignal &signal) override
Definition: tsv_export.cpp:31
Eigen::Map< const Eigen::VectorXd > getValuesMap(int time_idx) const
Read access to a value vector at a given time idx (< getTimeDimension) without copying.


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:56