76 g_log.
warning() <<
"This is intended as a proof of principle and not a long "
77 "term implementation.\n";
78 g_log.
warning() <<
"(the created arrays in the NeXus file will have the '_new' suffix)\n";
87 std::filesystem::path originalFile(
m_filename);
89 if (std::filesystem::exists(originalFile)) {
90 std::filesystem::path destinationFile = std::filesystem::temp_directory_path() / originalFile.filename();
93 std::filesystem::copy_file(originalFile, destinationFile);
96 }
catch (std::filesystem::filesystem_error &) {
97 throw std::runtime_error(
"A Problem occurred in making a copy of the "
98 "NeXus file. Failed to copy " +
99 originalFile.string() +
" to " + destinationFile.string() +
100 ". Please check file permissions.");
103 g_log.
error() <<
"Cannot copy a file that doesn't exist! (" << originalFile <<
").\n";
108 const auto perms = std::filesystem::status(std::filesystem::path(
m_filename)).permissions();
109 if ((perms & std::filesystem::perms::owner_write) == std::filesystem::perms::none) {
110 throw std::runtime_error(
"The specified NeXus file (" +
m_filename +
") is not writable.");
118 throw std::runtime_error(
"Failed to get instrument name from " +
m_filename +
119 ". Can't identify instrument definition file.");
136 g_log.
debug() <<
"Run LoadNexusLogs Child Algorithm.\n";
140 throw std::runtime_error(
"Failed to run LoadNexusLogs Child Algorithm.");
143 g_log.
debug() <<
"Run LoadInstrument Child Algorithm.\n";
147 throw std::runtime_error(
"Failed to run LoadInstrument Child Algorithm.");
151 const size_t numDetectors =
ws->getInstrument()->getDetectorIDs().size();
167 std::map<std::string, std::string>::const_iterator root_iter;
168 std::map<std::string, std::string> entries = nxfile.getEntries();
170 for (root_iter = entries.begin(); root_iter != entries.end(); ++root_iter) {
172 if (root_iter->second ==
"NXentry") {
173 nxfile.openGroup(root_iter->first,
"NXentry");
176 std::map<std::string, std::string> entry_items = nxfile.getEntries();
178 std::map<std::string, std::string>::const_iterator entry_iter;
180 for (entry_iter = entry_items.begin(); entry_iter != entry_items.end(); ++entry_iter) {
182 if (entry_iter->second ==
"NXinstrument") {
184 nxfile.openGroup(entry_iter->first,
"NXinstrument");
185 std::map<std::string, std::string> instr_items = nxfile.getEntries();
186 std::map<std::string, std::string>::const_iterator instr_iter;
187 for (instr_iter = instr_items.begin(); instr_iter != instr_items.end(); ++instr_iter) {
189 if (instr_iter->second ==
"NXdetector") {
190 g_log.
debug() <<
"Detector called '" << instr_iter->first <<
"' found.\n";
191 std::string bankName = instr_iter->first;
192 std::vector<Geometry::IDetector_const_sptr> dets;
193 ws->getInstrument()->getDetectorsInBank(dets, bankName);
196 nxfile.openGroup(bankName,
"NXdetector");
200 std::vector<int> pixel_id;
201 std::vector<double> distance;
202 std::vector<double> polar_angle;
203 std::vector<double> azimuthal_angle;
205 pixel_id.reserve(dets.size());
206 distance.reserve(dets.size());
207 polar_angle.reserve(dets.size());
208 azimuthal_angle.reserve(dets.size());
210 for (
auto &det : dets) {
211 pixel_id.emplace_back(det->getID());
212 distance.emplace_back(det->getDistance(*sample));
213 azimuthal_angle.emplace_back(det->getPhi());
214 polar_angle.emplace_back(
ws->detectorTwoTheta(*det));
218 nxfile.writeData(
"pixel_id_new", pixel_id);
221 nxfile.writeData(
"distance_new", distance);
222 nxfile.openData(
"distance_new");
223 nxfile.putAttr(
"units",
"metre");
227 nxfile.writeData(
"polar_angle_new", polar_angle);
228 nxfile.openData(
"polar_angle_new");
229 nxfile.putAttr(
"units",
"radian");
233 nxfile.writeData(
"azimuthal_angle_new", azimuthal_angle);
234 nxfile.openData(
"azimuthal_angle_new");
235 nxfile.putAttr(
"units",
"radian");
242 throw std::runtime_error(
"Could not find any detectors for the bank named " + bankName +
243 " that is listed in the NeXus file."
244 "Check that it exists in the Instrument Definition File.");
253 else if (entry_iter->second ==
"NXmonitor") {
254 g_log.
debug() <<
"Monitor called '" << entry_iter->first <<
"' found.\n";
255 nxfile.openGroup(entry_iter->first,
"NXmonitor");
262 double source_monitor = source->getDistance(*monitor);
263 double source_sample = source->getDistance(*sample);
265 g_log.
debug() <<
"source->monitor=" << source_monitor <<
'\n';
266 g_log.
debug() <<
"source->sample=" << source_sample <<
'\n';
267 g_log.
debug() <<
"sample->monitor=" << (source_monitor - source_sample) <<
'\n';
270 nxfile.writeData(
"distance_new", (source_monitor - source_sample));
271 nxfile.openData(
"distance_new");
272 nxfile.putAttr(
"units",
"metre");
280 g_log.
error() <<
"There are no NXentry nodes in the specified NeXus file.\n";