19#include <boost/tokenizer.hpp>
27using namespace Kernel;
36 "The name of the workspace containing the data you want to save to a "
38 const std::vector<std::string> asciiExts{
".dat",
".txt",
".csv"};
40 "The filename of the output Ascii file.");
42 auto mustBeNonNegative = std::make_shared<BoundedValidator<int>>();
43 mustBeNonNegative->setLower(0);
44 declareProperty(
"WorkspaceIndexMin", 0, mustBeNonNegative,
"The starting workspace index.");
48 declareProperty(
"WriteXError",
false,
"If true, the error on X will be written as the fourth column.");
50 declareProperty(
"CommentIndicator",
"",
"Character(s) to put in front of comment lines.");
53 std::string spacers[6][2] = {{
"CSV",
","}, {
"Tab",
"\t"}, {
"Space",
" "},
54 {
"Colon",
":"}, {
"SemiColon",
";"}, {
"UserDefined",
"UserDefined"}};
55 std::vector<std::string> sepOptions;
56 for (
auto &spacer : spacers) {
57 std::string option = spacer[0];
58 m_separatorIndex.insert(std::pair<std::string, std::string>(option, spacer[1]));
59 sepOptions.emplace_back(option);
62 declareProperty(
"Separator",
"CSV", std::make_shared<StringListValidator>(sepOptions),
63 "Character(s) to put as separator between X, Y, E values.");
66 "If present, will override any specified choice given to Separator.");
70 std::make_unique<VisibleWhenProperty>(
"Separator",
IS_EQUAL_TO,
"UserDefined"));
72 declareProperty(
"ColumnHeader",
true,
"If true, put column headers into file. ");
74 declareProperty(
"ICEFormat",
false,
"If true, special column headers for ICE in file. ");
83 auto nSpectra =
static_cast<int>(ws->getNumberHistograms());
84 auto nBins =
static_cast<int>(ws->blocksize());
87 std::vector<int> spec_list =
getProperty(
"SpectrumList");
99 if (!custom.empty()) {
103 else if (choice !=
"UserDefined") {
109 g_log.
notice() <<
"\"UserDefined\" has been selected, but no custom "
110 "separator has been entered. Using default instead.";
114 std::string errstr =
"E";
116 std::string comstr =
" , ";
125 comment =
"#features:";
134 throw std::invalid_argument(
"Inconsistent spectra interval");
135 for (
int spec = spec_min; spec <= spec_max; spec++)
140 if (!spec_list.empty()) {
141 for (
auto &spec : spec_list) {
143 throw std::invalid_argument(
"Inconsistent spectra list");
149 nSpectra =
static_cast<int>(idx.size());
152 throw std::runtime_error(
"Trying to save an empty workspace");
155 std::ofstream file(filename.c_str());
158 g_log.
error(
"Unable to create file: " + filename);
164 file << comment <<
"X";
166 for (
int spec = 0; spec <
nSpectra; spec++) {
167 file << comstr <<
"Y" << spec << comstr << errstr << spec << errstr2;
169 file <<
" , DX" << spec;
172 for (
auto spec : idx) {
173 file << comstr <<
"Y" << spec << comstr << errstr << spec << errstr2;
175 file <<
" , DX" << spec;
183 file.precision(prec);
186 auto pointDeltas = ws->pointStandardDeviations(0);
187 auto points = ws->points(0);
188 for (
int bin = 0; bin < nBins; bin++) {
192 for (
int spec = 0; spec <
nSpectra; spec++) {
194 file << ws->y(spec)[bin];
196 file << ws->e(spec)[bin];
199 for (
auto spec : idx) {
201 file << ws->y(spec)[bin];
203 file << ws->e(spec)[bin];
208 file << pointDeltas[bin];
#define DECLARE_ALGORITHM(classname)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
@ Save
to specify a file to write to, the file may or may not exist
Helper class for reporting progress from algorithms.
A property class for workspaces.
void init() override
Overwrites Algorithm method.
std::map< std::string, std::string > m_separatorIndex
Map the separator options to their string equivalents.
SaveAscii()
Default constructor.
void exec() override
Overwrites Algorithm method.
Support for a property that holds an array of values.
Records the filename and the description of failure.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
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.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
@ Input
An input workspace.