Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
CSVExport.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <fstream>
12#include <iomanip>
13#include <ostream>
14#include <string>
15#include <vector>
16
19
20namespace vulcan::io {
21
24 char delimiter = ',';
25 int precision = 15;
26 bool include_header = true;
27 std::vector<std::string> signals;
28};
29
36inline void export_to_csv(const HDF5Reader &reader, std::ostream &out,
37 const CSVExportOptions &options = {}) {
38 // Determine signals to export
39 std::vector<std::string> signals;
40 if (options.signals.empty()) {
41 signals = reader.signal_names();
42 } else {
43 signals = options.signals;
44 }
45
46 // Get frame count
47 size_t n_frames = reader.frame_count();
48 if (n_frames == 0) {
49 return;
50 }
51
52 // Read all data upfront
53 std::vector<double> times = reader.times();
54 std::vector<std::vector<double>> signal_data;
55 signal_data.reserve(signals.size());
56
57 for (const auto &sig : signals) {
58 signal_data.push_back(reader.read_double(sig));
59 }
60
61 // Set output precision
62 out << std::setprecision(options.precision);
63
64 // Write header
65 if (options.include_header) {
66 out << "time";
67 for (const auto &sig : signals) {
68 out << options.delimiter << sig;
69 }
70 out << "\n";
71 }
72
73 // Write data rows
74 for (size_t i = 0; i < n_frames; ++i) {
75 out << times[i];
76 for (size_t j = 0; j < signals.size(); ++j) {
77 out << options.delimiter << signal_data[j][i];
78 }
79 out << "\n";
80 }
81}
82
89inline void export_to_csv(const std::string &hdf5_path,
90 const std::string &csv_path,
91 const CSVExportOptions &options = {}) {
92 HDF5Reader reader(hdf5_path);
93 std::ofstream ofs(csv_path);
94 if (!ofs) {
95 throw vulcan::IOError("Failed to open CSV file: " + csv_path);
96 }
97 export_to_csv(reader, ofs, options);
98}
99
100} // namespace vulcan::io
HDF5 telemetry reader for post-simulation analysis.
Exception hierarchy for Vulcan aerospace library.
File/stream I/O errors.
Definition VulcanError.hpp:27
HDF5 telemetry file reader.
Definition HDF5Reader.hpp:41
std::vector< double > times() const
Read all timestamps.
Definition HDF5Reader.hpp:65
std::vector< double > read_double(const std::string &signal) const
Read double signal.
Definition HDF5Reader.hpp:68
std::vector< std::string > signal_names() const
Get all signal names.
Definition HDF5Reader.hpp:118
size_t frame_count() const
Get number of frames in file.
Definition HDF5Reader.hpp:62
Definition CSVExport.hpp:20
void export_to_csv(const HDF5Reader &reader, std::ostream &out, const CSVExportOptions &options={})
Export HDF5 reader to output stream.
Definition CSVExport.hpp:36
CSV export options.
Definition CSVExport.hpp:23
int precision
Decimal precision for doubles.
Definition CSVExport.hpp:25
char delimiter
Column delimiter.
Definition CSVExport.hpp:24
std::vector< std::string > signals
Signal subset (empty = all).
Definition CSVExport.hpp:27
bool include_header
Include column names in first row.
Definition CSVExport.hpp:26