Mantid
Loading...
Searching...
No Matches
SaveNISTDAT.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
16
17#include <fstream> // used to get ofstream
18
19namespace Mantid::DataHandling {
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(SaveNISTDAT)
23
24using namespace Kernel;
25using namespace API;
26using namespace Geometry;
27
29 auto wsValidator = std::make_shared<CompositeValidator>();
30 wsValidator->add(std::make_shared<WorkspaceUnitValidator>("MomentumTransfer"));
31 wsValidator->add<HistogramValidator>();
32 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator));
33 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".dat"),
34 "The filename of the output text file");
35}
36
38 MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
39 const std::string filename = getPropertyValue("Filename");
40
41 // prepare to save to file
42 std::ofstream out_File(filename.c_str());
43 if (!out_File) {
44 g_log.error("Failed to open file:" + filename);
45 throw Exception::FileError("Failed to open file:", filename);
46 }
47 out_File << "Data columns Qx - Qy - I(Qx,Qy) - err(I)\r\n";
48 out_File << "ASCII data\r\n";
49
50 // Set up the progress reporting object
51 Progress progress(this, 0.0, 1.0, 2);
52 progress.report("Save I(Qx,Qy)");
53
54 if (inputWS->axes() > 1 && inputWS->getAxis(1)->isNumeric()) {
55 const Axis &axis = *inputWS->getAxis(1);
56 for (size_t i = 0; i < axis.length() - 1; i++) {
57 const double qy = (axis(i) + axis(i + 1)) / 2.0;
58 const auto &XIn = inputWS->x(i);
59 const auto &YIn = inputWS->y(i);
60 const auto &EIn = inputWS->e(i);
61
62 for (size_t j = 0; j < XIn.size() - 1; j++) {
63 // Don't write out Q bins without data
64 if (YIn[j] == 0 && EIn[j] == 0)
65 continue;
66 // Exclude NaNs
67 if (YIn[j] == YIn[j]) {
68 out_File << (XIn[j] + XIn[j + 1]) / 2.0;
69 out_File << " " << qy;
70 out_File << " " << YIn[j];
71 out_File << " " << EIn[j] << "\r\n";
72 }
73 }
74 }
75 }
76 out_File.close();
77 progress.report("Save I(Qx,Qy)");
78}
79
80} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
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
Class to represent the axis of a workspace.
Definition: Axis.h:30
virtual std::size_t length() const =0
Get the length of the axis.
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
A validator which checks that a workspace contains histogram data (the default) or point data as requ...
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Execution code.
Definition: SaveNISTDAT.cpp:37
void init() override
Initialisation code.
Definition: SaveNISTDAT.cpp:28
Records the filename and the description of failure.
Definition: Exception.h:98
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
@ Input
An input workspace.
Definition: Property.h:53