Mantid
Loading...
Searching...
No Matches
FixGSASInstrumentFile.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 +
8
10#include "MantidAPI/TableRow.h"
13
14#include <fstream>
15
16#include <boost/algorithm/string.hpp>
17#include <boost/algorithm/string/finder.hpp>
18#include <boost/algorithm/string/iter_find.hpp>
19#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/trim.hpp>
21
22using namespace Mantid;
23using namespace Mantid::API;
24using namespace Mantid::Kernel;
25
26using namespace std;
27
28namespace Mantid::Algorithms {
29
30DECLARE_ALGORITHM(FixGSASInstrumentFile)
31
32const size_t LINESIZE = 80;
33
34//----------------------------------------------------------------------------------------------
38
39 std::initializer_list<std::string> exts = {".prm", ".iparm"};
40
41 // Input file
42 declareProperty(std::make_unique<FileProperty>("InputFilename", "", FileProperty::Load, exts),
43 "Name of the GSAS instrument parameter file to get fixed for format. ");
44
45 // Output file
46 declareProperty(std::make_unique<FileProperty>("OutputFilename", "", FileProperty::Save, exts),
47 "Name of the output GSAS instrument parameter file to have format "
48 "fixed. ");
49}
50
51//----------------------------------------------------------------------------------------------
55 // Properties
56 string infilename = getProperty("InputFilename");
57
58 // Parse file
59 vector<string> vec_line;
60 ifstream infile;
61 infile.open(infilename.c_str(), ios::in);
62 if (!infile.is_open()) {
63 stringstream errss;
64 errss << "File " << infilename << " cannot be opened for reading. "
65 << ".\n";
66 g_log.error(errss.str());
67 throw runtime_error(errss.str());
68 }
69 {
70 string line;
71 while (getline(infile, line)) {
72 // Split "\n"
73 vector<string> fields;
74 boost::algorithm::split(fields, line, boost::algorithm::is_any_of("\n"));
75 if (fields.empty())
76 throw runtime_error("Impossible to have an empty line. ");
77 vec_line.emplace_back(fields[0]);
78 }
79 }
80 infile.close();
81
82 // Write out
83 string outfilename = getPropertyValue("OutputFilename");
84 ofstream ofile;
85 ofile.open(outfilename.c_str(), ios::out);
86 if (!ofile.is_open()) {
87 stringstream errss;
88 errss << "File " << outfilename << " cannot be opened for writing. "
89 << ".\n";
90 g_log.error(errss.str());
91 throw runtime_error(errss.str());
92 }
93
94 for (const auto &line : vec_line) {
95 ofile << line;
96 for (size_t j = line.size(); j < LINESIZE; ++j)
97 ofile << " ";
98 ofile << "\n";
99 }
100
101 ofile.close();
102}
103
104} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
FixGSASInstrumentFile : TODO: DESCRIPTION.
void exec() override
Implement abstract Algorithm methods.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.