* Creates a CSV file using sample observers as input. * * The CSV file is created in the applications temporary directory and the file name is unique. * Note that if the observer list only have one channel, the file will add the samples as first column. * The same applies if the first channel not is a master channel. * @param list Sample observer list containing samples. * @return Full path
| 36 | * @return Full path (ANSI coding) to the created file or an empty string if the function fails. |
| 37 | */ |
| 38 | std::string CreateCsvFile(const mdf::ChannelObserverList& list) { |
| 39 | if (list.empty()) { |
| 40 | LOG_ERROR() << "No observers in the list."; |
| 41 | return ""; |
| 42 | } |
| 43 | |
| 44 | const auto& app = wxGetApp(); |
| 45 | std::string csv_file; |
| 46 | try { |
| 47 | std::filesystem::path temp_file(app.GetMyTempDir()); |
| 48 | const auto unique = boost::filesystem::unique_path(); |
| 49 | temp_file.append(unique.string()); |
| 50 | temp_file.replace_extension(".csv"); |
| 51 | csv_file = temp_file.generic_string(); |
| 52 | } catch (const std::exception& error) { |
| 53 | LOG_ERROR() << "Failed to create the CSV file. Error: " << error.what(); |
| 54 | } |
| 55 | |
| 56 | util::plot::CsvWriter csv(csv_file); |
| 57 | |
| 58 | const bool master = list.size() > 1 && list[0]->IsMaster(); |
| 59 | |
| 60 | // Add the header first |
| 61 | if (!master) { |
| 62 | // First column is sample number |
| 63 | csv.AddColumnHeader("Sample", ""); |
| 64 | } |
| 65 | for (const auto& channel : list) { |
| 66 | csv.AddColumnHeader(channel->Name(),channel->Unit()); |
| 67 | } |
| 68 | csv.AddRow(); |
| 69 | // Add the samples |
| 70 | for (size_t sample = 0; sample < list[0]->NofSamples(); ++sample) { |
| 71 | // Add the sample number first |
| 72 | if (!master) { |
| 73 | // First column is sample number |
| 74 | csv.AddColumnValue(sample); |
| 75 | } |
| 76 | for (const auto& channel : list) { |
| 77 | std::string value; |
| 78 | const bool valid = channel->GetEngValue(sample, value); // Note that we may lose some digits here |
| 79 | csv.AddColumnValue(valid ? value : ""); |
| 80 | } |
| 81 | csv.AddRow(); |
| 82 | } |
| 83 | |
| 84 | return csv_file; |
| 85 | } |
| 86 | |
| 87 | std::string CreateGnuPlotFile(const mdf::ChannelObserverList& list, const std::string& csv_file, const std::string& title) { |
| 88 | if (list.empty() || csv_file.empty()) { |
no test coverage detected