-
Notifications
You must be signed in to change notification settings - Fork 122
/
ImportMDHistoWorkspace.cpp
109 lines (87 loc) · 3.43 KB
/
ImportMDHistoWorkspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "MantidMDAlgorithms/ImportMDHistoWorkspace.h"
#include "MantidAPI/FileProperty.h"
#include <deque>
#include <fstream>
#include <iterator>
namespace Mantid {
namespace MDAlgorithms {
using namespace API;
using namespace DataObjects;
using namespace Kernel;
using namespace Geometry;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ImportMDHistoWorkspace)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ImportMDHistoWorkspace::name() const {
return "ImportMDHistoWorkspace";
}
/// Algorithm's version for identification. @see Algorithm::version
int ImportMDHistoWorkspace::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string ImportMDHistoWorkspace::category() const {
return "MDAlgorithms\\DataHandling";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ImportMDHistoWorkspace::init() {
std::vector<std::string> fileExtensions{".txt"};
declareProperty(Kernel::make_unique<API::FileProperty>(
"Filename", "", API::FileProperty::Load, fileExtensions),
"File of type txt");
// Initialize generic dimension properties on the base class.
this->initGenericImportProps();
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ImportMDHistoWorkspace::exec() {
std::string filename = getProperty("Filename");
/*
Base class creates an empty output workspace, with the correct dimensionality
according to the algorithm inputs (see base class).
*/
MDHistoWorkspace_sptr ws = this->createEmptyOutputWorkspace();
// Open the file
std::ifstream file;
try {
file.open(filename.c_str(), std::ios::in);
} catch (std::ifstream::failure &e) {
g_log.error() << "Cannot open file: " << filename;
throw(e);
}
// Copy each string present in the file stream into a deque.
typedef std::deque<std::string> box_collection;
box_collection box_elements;
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(box_elements));
//// Release the resource.
file.close();
const size_t nElements = this->getBinProduct() * 2;
// Handle the case that the number of elements is wrong.
if (box_elements.size() != nElements) {
throw std::invalid_argument("The number of data entries in the file, does "
"not match up with the specified "
"dimensionality.");
}
// Fetch out raw pointers to workspace arrays.
double *signals = ws->getSignalArray();
double *errors = ws->getErrorSquaredArray();
// Write to the signal and error array from the deque.
size_t currentBox = 0;
for (auto it = box_elements.begin(); it != box_elements.end(); it += 2) {
auto temp = it;
double signal = atof((*(temp)).c_str());
double error = atof((*(++temp)).c_str());
signals[currentBox] = signal;
errors[currentBox] = error * error;
++currentBox;
}
// Set the output.
setProperty("OutputWorkspace", ws);
}
} // namespace Mantid
} // namespace MDAlgorithms