Mantid
Loading...
Searching...
No Matches
LoadSNSspec.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"
18
19#include <cstring>
20#include <fstream>
21
22namespace Mantid::DataHandling {
24
25
31int LoadSNSspec::confidence(Kernel::FileDescriptor &descriptor) const {
32 if (!descriptor.isAscii())
33 return 0;
34
35 auto &file = descriptor.data();
36
37 int confidence(0);
38 size_t axiscols(0), datacols(0);
39 std::string str;
41 const std::string sep = " ";
42 bool snsspec(false);
43
44 while (std::getline(file, str)) {
45 // File is opened in binary mode so getline will leave a \r at the end of an
46 // empty line if it exists
47 if (str.empty() || str == "\r")
48 continue;
49
50 try {
51 // if it's comment line
53 if (str.at(0) == '#') {
54 if (str.at(1) == 'L') {
55 axiscols = tok.count();
56 // if the file contains a comment line starting with "#L" followed
57 // by three columns this could be loadsnsspec file
58 if (axiscols > 2) {
59 snsspec = true;
60 }
61 }
62 } else {
63 // check first data line is a 3 column line
64 datacols = tok.count();
65 break;
66 }
67 } catch (std::out_of_range &) {
68 }
69 }
70 if (snsspec && datacols == 3) // three column data
71 {
72 confidence = 80;
73 }
74 return confidence;
75}
76
77using namespace Kernel;
78using namespace API;
79
82 useAlgorithm("LoadSpec");
83 deprecatedDate("2017-01-30");
84}
85
88 const std::vector<std::string> exts{".dat", ".txt"};
89 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
90 "The name of the text file to read, including its full or "
91 "relative path. The file extension must be .txt or .dat.");
92 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
93 "The name of the workspace that will be created, filled with the read-in "
94 "data and stored in the [[Analysis Data Service]].");
95
96 std::vector<std::string> units = UnitFactory::Instance().getKeys();
97 declareProperty("Unit", "Energy", std::make_shared<Kernel::StringListValidator>(units),
98 "The unit to assign to the X axis (anything known to the "
99 "[[Unit Factory]] or \"Dimensionless\") (default: Energy)");
100}
101
103 auto alg = createChildAlgorithm("LoadSpec");
104 alg->setPropertyValue("Filename", getPropertyValue("Filename"));
105 alg->setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
106 alg->setPropertyValue("unit", getPropertyValue("unit"));
107 alg->execute();
108
109 MatrixWorkspace_sptr ws = alg->getProperty("OutputWorkspace");
110 setProperty("OutputWorkspace", ws);
111}
112
113} // namespace Mantid::DataHandling
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
Class for marking algorithms as deprecated.
void deprecatedDate(const std::string &)
The date the algorithm was deprecated on.
void useAlgorithm(const std::string &, const int version=-1)
The algorithm to use instead of this one.
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
A property class for workspaces.
Loads a workspace from an SNS spec file.
Definition: LoadSNSspec.h:35
LoadSNSspec()
Empty constructor.
Definition: LoadSNSspec.cpp:81
void exec() override
Virtual method - must be overridden by concrete algorithm.
void init() override
Initialisation method.
Definition: LoadSNSspec.cpp:87
Defines a wrapper around an open file.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
@ TOK_IGNORE_EMPTY
ignore empty tokens
std::size_t count() const
Get the total number of tokens.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Output
An output workspace.
Definition: Property.h:54