-
Notifications
You must be signed in to change notification settings - Fork 122
/
FixGSASInstrumentFile.cpp
106 lines (88 loc) · 3.06 KB
/
FixGSASInstrumentFile.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/FixGSASInstrumentFile.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidKernel/ArrayProperty.h"
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <boost/algorithm/string/iter_find.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace std;
namespace Mantid {
namespace Algorithms {
DECLARE_ALGORITHM(FixGSASInstrumentFile)
const size_t LINESIZE = 80;
//----------------------------------------------------------------------------------------------
/** Implement abstract Algorithm methods
*/
void FixGSASInstrumentFile::init() {
std::initializer_list<std::string> exts = {".prm", ".iparm"};
// Input file
declareProperty(std::make_unique<FileProperty>("InputFilename", "", FileProperty::Load, exts),
"Name of the GSAS instrument parameter file to get fixed for format. ");
// Output file
declareProperty(std::make_unique<FileProperty>("OutputFilename", "", FileProperty::Save, exts),
"Name of the output GSAS instrument parameter file to have format "
"fixed. ");
}
//----------------------------------------------------------------------------------------------
/** Execution
*/
void FixGSASInstrumentFile::exec() {
// Properties
string infilename = getProperty("InputFilename");
// Parse file
vector<string> vec_line;
ifstream infile;
infile.open(infilename.c_str(), ios::in);
if (!infile.is_open()) {
stringstream errss;
errss << "File " << infilename << " cannot be opened for reading. "
<< ".\n";
g_log.error(errss.str());
throw runtime_error(errss.str());
}
{
string line;
while (getline(infile, line)) {
// Split "\n"
vector<string> fields;
boost::algorithm::split(fields, line, boost::algorithm::is_any_of("\n"));
if (fields.empty())
throw runtime_error("Impossible to have an empty line. ");
vec_line.emplace_back(fields[0]);
}
}
infile.close();
// Write out
string outfilename = getPropertyValue("OutputFilename");
ofstream ofile;
ofile.open(outfilename.c_str(), ios::out);
if (!ofile.is_open()) {
stringstream errss;
errss << "File " << outfilename << " cannot be opened for writing. "
<< ".\n";
g_log.error(errss.str());
throw runtime_error(errss.str());
}
for (const auto &line : vec_line) {
ofile << line;
for (size_t j = line.size(); j < LINESIZE; ++j)
ofile << " ";
ofile << "\n";
}
ofile.close();
}
} // namespace Algorithms
} // namespace Mantid