27 #include <Eigen/Dense> 28 #include <boost/filesystem.hpp> 29 #include <boost/property_tree/ptree.hpp> 39 const Eigen::Ref<const Eigen::MatrixXd>& data,
42 std::string headermessage =
"") {
43 std::ofstream csvwriter;
46 csvwriter.open(filename, std::ofstream::out | std::ofstream::app);
49 csvwriter.open(filename, std::ofstream::out);
52 if (csvwriter.fail()) {
53 std::cout <<
"************** ERROR IN write_to_csv **************" 55 std::cout <<
"Unable to write to target file " << filename << std::endl;
57 <<
"Verify existence/permissions of the chosen target directory." 61 auto t = std::time(
nullptr);
62 auto localtime = std::localtime(&t);
64 std::vector<char> buffer;
69 }
while (std::strftime(
70 buffer.data(), size,
"%Y%m%d_%H%M%S", localtime) == 0u);
71 std::string timestamp(buffer.data());
76 std::string filename_override =
"CSV_DUMP_" + timestamp +
"_%%%%%%.csv";
78 boost::filesystem::unique_path(filename_override).string();
80 std::cout <<
"Data will be redirected to the current work directory" 82 std::cout <<
"and written to file " << filename_override << std::endl;
83 std::cout <<
"***************************************************" 87 csvwriter.open(filename_override,
88 std::ofstream::out | std::ofstream::app);
89 csvwriter <<
"*** The following data was originally destined for file " 90 << filename <<
"***" << std::endl;
93 csvwriter << headermessage;
95 int Nrows = data.rows();
96 int Ncols = data.cols();
98 for (
int nrow = 0; nrow < Nrows; nrow++) {
99 for (
int ncol = 0; ncol < Ncols; ncol++) {
100 csvwriter << data(nrow, ncol);
102 if (ncol == (Ncols - 1)) {
103 csvwriter << std::endl;
123 std::size_t skipheaderlines = 0) {
124 std::ifstream csvreader;
125 csvreader.open(filename);
126 if (csvreader.fail()) {
127 std::cout <<
"Error in read_from_csv:" << std::endl;
128 std::cout <<
"Unable to open " << filename << std::endl;
133 std::string linereader;
134 std::vector<double> databuffer;
140 for (std::size_t nskip = 0; nskip < skipheaderlines; nskip++) {
141 getline(csvreader, linereader,
'\n');
147 getline(csvreader, linereader,
'\n');
150 std::count(linereader.begin(), linereader.end(), colsep) + 1;
152 while (csvreader.good()) {
153 std::stringstream extractor(linereader);
155 for (std::size_t ncol = 0; ncol < Ncols; ncol++) {
156 extractor >> datavalue;
157 databuffer.emplace_back(datavalue);
161 getline(csvreader, linereader,
'\n');
168 int Nrows = databuffer.size() / Ncols;
170 Eigen::MatrixXd result(Nrows, Ncols);
173 for (std::size_t idx = 0; idx < databuffer.size(); idx++) {
174 std::size_t nrow = idx / Ncols;
175 std::size_t ncol = idx % Ncols;
176 result(nrow, ncol) = databuffer.at(idx);
188 std::string field_as_attribute =
"<xmlattr>." + field;
191 result = v.second.get<C>(field);
193 catch (boost::property_tree::ptree_bad_path& bad_path_error) {
195 result = v.second.get<C>(field_as_attribute);
197 catch (boost::property_tree::ptree_bad_path& bad_path_error) {
198 std::cout <<
"XML error:" << std::endl;
199 std::cout <<
"Mandatory attribute \"" << field
200 <<
"\" is missing in element \"" << v.first <<
"\"" 218 std::string field_as_attribute =
"<xmlattr>." + field;
221 read_content = v.second.get<C>(field);
224 catch (boost::property_tree::ptree_bad_path& bad_path_error) {
226 read_content = v.second.get<C>(field_as_attribute);
229 catch (boost::property_tree::ptree_bad_path& bad_path_error) {
C parseXMLfield(boost::property_tree::ptree::value_type const &v, std::string field)
Extract a value from an XML tree.
Definition: io_utils.hpp:185
Definition: analytic1d.hpp:26
void write_to_csv(const std::string filename, const Eigen::Ref< const Eigen::MatrixXd > &data, char colsep, bool append=false, std::string headermessage="")
Save a Matrix of double precision numbers as a csv file.
Definition: io_utils.hpp:38
Eigen::MatrixXd read_from_csv(const std::string filename, char colsep, std::size_t skipheaderlines=0)
Read a csv file into a Matrix.
Definition: io_utils.hpp:121
bool probeXMLfield(boost::property_tree::ptree::value_type const &v, std::string field)
Check if an XML field is present.
Definition: io_utils.hpp:214