Mantid
Loading...
Searching...
No Matches
SaveCSV.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11#include <fstream> // used to get ofstream
12#include <iomanip>
13
14/* @class SaveCSV
15
16 @author Anders J. Markvardsen, ISIS, RAL
17 @date 15/10/2007
18 */
19
20namespace Mantid::DataHandling {
21
22// Register the class into the algorithm factory
23DECLARE_ALGORITHM(SaveCSV)
24
25using namespace Kernel;
26using namespace API;
27using API::MatrixWorkspace;
29using API::WorkspaceProperty;
30using DataObjects::Workspace2D;
32
34SaveCSV::SaveCSV() = default;
35
40 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
41 "The filename of the output CSV file");
42 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".csv"),
43 "The name of the workspace containing the data you want to save to\n"
44 "a CSV file");
45 declareProperty("Separator", ",",
46 "The separator that will go between the numbers on a line in the\n"
47 "output file (default ',')");
48 getPointerToProperty("Separator")->setAutoTrim(false);
49 declareProperty("LineSeparator", "\n",
50 "The string to place at the end of lines (default new line\n"
51 "character)");
52 getPointerToProperty("LineSeparator")->setAutoTrim(false);
53 declareProperty(std::make_unique<PropertyWithValue<bool>>("SaveXerrors", false, Direction::Input),
54 "This option saves out the x errors if any are present. If you have x "
55 "errors\n"
56 "in your workspace and you do not select this option, then the x errors\n"
57 "are not saved to the file.");
58}
59
69 // Gets the name of the file to save the workspace to; and the
70 // separator and Lineseparator properties if they are provided by the user.
71
72 // Retrieve the filename from the properties
73 m_filename = getPropertyValue("Filename");
74
75 // Get the values of the optional parameters
76 m_separator = getPropertyValue("Separator");
77 m_lineSeparator = getPropertyValue("LineSeparator");
78 g_log.debug() << "Parameters: Filename='" << m_filename << "' "
79 << "Seperator='" << m_separator << "' "
80 << "LineSeparator='" << m_lineSeparator << "' \n";
81
82 // prepare to save to file
83
84 std::ofstream outCSV_File(m_filename.c_str());
85
86 if (!outCSV_File) {
87 g_log.error("Failed to open file:" + m_filename);
88 throw Exception::FileError("Failed to open file:", m_filename);
89 }
90
91 // Get the input workspace
92 const MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
93
94 // get workspace ID string. to check the data type
95
96 const std::string workspaceID = inputWorkspace->id();
97 // seperating out code depending on the workspace ID
98
99 if (workspaceID.find("Workspace2D") != std::string::npos) {
100 const Workspace2D_sptr localworkspace = std::dynamic_pointer_cast<Workspace2D>(inputWorkspace);
101
102 // Get info from 2D workspace
103 const size_t numberOfHist = localworkspace->getNumberHistograms();
104
105 // Add first x-axis line to output file
106 {
107 auto &xValue = localworkspace->x(0);
108
109 outCSV_File << "A";
110
111 for (double j : xValue) {
112 outCSV_File << std::setw(15) << j << m_separator;
113 }
114
115 outCSV_File << m_lineSeparator;
116 progress(0.2);
117 }
118 Progress p(this, 0.2, 1.0, 2 * numberOfHist);
119 for (size_t i = 0; i < numberOfHist; i++) {
120 // check if x-axis has changed. If yes print out new x-axis line
121
122 if (i > 0) {
123 auto &xValue = localworkspace->x(i);
124 auto &xValuePrevious = localworkspace->x(i - 1);
125
126 if (xValue.rawData() != xValuePrevious.rawData()) {
127 outCSV_File << "A";
128
129 for (double j : xValue) {
130 outCSV_File << std::setw(15) << j << m_separator;
131 }
132
133 outCSV_File << m_lineSeparator;
134 }
135 }
136
137 // add y-axis line for histogram (detector) i
138
139 auto &yValue = localworkspace->y(i);
140
141 outCSV_File << i;
142
143 for (double j : yValue) {
144 outCSV_File << std::setw(15) << j << m_separator;
145 }
146
147 outCSV_File << m_lineSeparator;
148 p.report();
149 }
150 // print out errors
151
152 outCSV_File << "\nERRORS\n";
153
154 for (size_t i = 0; i < numberOfHist; i++) {
155 auto &eValue = localworkspace->e(i);
156
157 outCSV_File << i;
158
159 for (double j : eValue) {
160 outCSV_File << std::setw(15) << j << m_separator;
161 }
162 outCSV_File << m_lineSeparator;
163 p.report();
164 }
165
166 // Potentially save the x errors
167 if (getProperty("SaveXerrors")) {
168 saveXerrors(outCSV_File, localworkspace, numberOfHist);
169 }
170
171 } else {
172 outCSV_File.close(); // and should probably delete file from disk as well
173 throw Exception::NotImplementedError("SaveCSV currently only works for 2D workspaces.");
174 }
175 outCSV_File.close();
176}
177
179 const size_t numberOfHist) {
180 // If there isn't a dx values present in the first entry then return
181 if (!workspace->hasDx(0)) {
182 return;
183 }
184 Progress p(this, 0.0, 1.0, numberOfHist);
185 stream << "\nXERRORS\n";
186 for (size_t i = 0; i < numberOfHist; i++) {
187 stream << i;
188
189 for (double j : workspace->dx(i)) {
190 stream << std::setw(15) << j << m_separator;
191 }
192 stream << m_lineSeparator;
193 p.report("Saving x errors...");
194 }
195}
196
197} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
Definition: Algorithm.cpp:2033
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
std::string m_lineSeparator
The line seperator for the CSV file.
Definition: SaveCSV.h:102
SaveCSV()
Default constructor.
void exec() override
Overwrites Algorithm method.
Definition: SaveCSV.cpp:68
std::string m_filename
The name of the file used for storing the workspace.
Definition: SaveCSV.h:96
void saveXerrors(std::ofstream &stream, const Mantid::DataObjects::Workspace2D_sptr &workspace, const size_t numberOfHist)
Saves out x errors.
Definition: SaveCSV.cpp:178
void init() override
Overwrites Algorithm method. Does nothing at present.
Definition: SaveCSV.cpp:39
std::string m_separator
The seperator for the CSV file.
Definition: SaveCSV.h:99
Records the filename and the description of failure.
Definition: Exception.h:98
Marks code as not implemented yet.
Definition: Exception.h:138
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
The concrete, templated class for properties.
void setAutoTrim(const bool &setting)
Sets if the property is set to automatically trim string unput values of whitespace.
Definition: Property.cpp:371
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
@ Input
An input workspace.
Definition: Property.h:53