77 g_log.
warning() <<
"This is intended as a proof of principle and not a long "
78 "term implementation.\n";
79 g_log.
warning() <<
"(the created arrays in the NeXus file will have the '_new' suffix)\n";
88 std::filesystem::path originalFile(
m_filename);
90 if (std::filesystem::exists(originalFile)) {
91 std::filesystem::path destinationFile = std::filesystem::temp_directory_path() / originalFile.filename();
94 std::filesystem::copy_file(originalFile, destinationFile);
97 }
catch (Poco::FileAccessDeniedException &) {
98 throw std::runtime_error(
"A Problem occurred in making a copy of the "
99 "NeXus file. Failed to copy " +
100 originalFile.string() +
" to " + destinationFile.string() +
101 ". Please check file permissions.");
104 g_log.
error() <<
"Cannot copy a file that doesn't exist! (" << originalFile <<
").\n";
109 const auto perms = std::filesystem::status(std::filesystem::path(
m_filename)).permissions();
110 if ((perms & std::filesystem::perms::owner_write) == std::filesystem::perms::none) {
111 throw std::runtime_error(
"The specified NeXus file (" +
m_filename +
") is not writable.");
119 throw std::runtime_error(
"Failed to get instrument name from " +
m_filename +
120 ". Can't identify instrument definition file.");
137 g_log.
debug() <<
"Run LoadNexusLogs Child Algorithm.\n";
141 throw std::runtime_error(
"Failed to run LoadNexusLogs Child Algorithm.");
144 g_log.
debug() <<
"Run LoadInstrument Child Algorithm.\n";
148 throw std::runtime_error(
"Failed to run LoadInstrument Child Algorithm.");
152 const size_t numDetectors =
ws->getInstrument()->getDetectorIDs().size();
168 std::map<std::string, std::string>::const_iterator root_iter;
169 std::map<std::string, std::string> entries = nxfile.getEntries();
171 for (root_iter = entries.begin(); root_iter != entries.end(); ++root_iter) {
173 if (root_iter->second ==
"NXentry") {
174 nxfile.openGroup(root_iter->first,
"NXentry");
177 std::map<std::string, std::string> entry_items = nxfile.getEntries();
179 std::map<std::string, std::string>::const_iterator entry_iter;
181 for (entry_iter = entry_items.begin(); entry_iter != entry_items.end(); ++entry_iter) {
183 if (entry_iter->second ==
"NXinstrument") {
185 nxfile.openGroup(entry_iter->first,
"NXinstrument");
186 std::map<std::string, std::string> instr_items = nxfile.getEntries();
187 std::map<std::string, std::string>::const_iterator instr_iter;
188 for (instr_iter = instr_items.begin(); instr_iter != instr_items.end(); ++instr_iter) {
190 if (instr_iter->second ==
"NXdetector") {
191 g_log.
debug() <<
"Detector called '" << instr_iter->first <<
"' found.\n";
192 std::string bankName = instr_iter->first;
193 std::vector<Geometry::IDetector_const_sptr> dets;
194 ws->getInstrument()->getDetectorsInBank(dets, bankName);
197 nxfile.openGroup(bankName,
"NXdetector");
201 std::vector<int> pixel_id;
202 std::vector<double> distance;
203 std::vector<double> polar_angle;
204 std::vector<double> azimuthal_angle;
206 pixel_id.reserve(dets.size());
207 distance.reserve(dets.size());
208 polar_angle.reserve(dets.size());
209 azimuthal_angle.reserve(dets.size());
211 for (
auto &det : dets) {
212 pixel_id.emplace_back(det->getID());
213 distance.emplace_back(det->getDistance(*sample));
214 azimuthal_angle.emplace_back(det->getPhi());
215 polar_angle.emplace_back(
ws->detectorTwoTheta(*det));
219 nxfile.writeData(
"pixel_id_new", pixel_id);
222 nxfile.writeData(
"distance_new", distance);
223 nxfile.openData(
"distance_new");
224 nxfile.putAttr(
"units",
"metre");
228 nxfile.writeData(
"polar_angle_new", polar_angle);
229 nxfile.openData(
"polar_angle_new");
230 nxfile.putAttr(
"units",
"radian");
234 nxfile.writeData(
"azimuthal_angle_new", azimuthal_angle);
235 nxfile.openData(
"azimuthal_angle_new");
236 nxfile.putAttr(
"units",
"radian");
243 throw std::runtime_error(
"Could not find any detectors for the bank named " + bankName +
244 " that is listed in the NeXus file."
245 "Check that it exists in the Instrument Definition File.");
254 else if (entry_iter->second ==
"NXmonitor") {
255 g_log.
debug() <<
"Monitor called '" << entry_iter->first <<
"' found.\n";
256 nxfile.openGroup(entry_iter->first,
"NXmonitor");
263 double source_monitor = source->getDistance(*monitor);
264 double source_sample = source->getDistance(*sample);
266 g_log.
debug() <<
"source->monitor=" << source_monitor <<
'\n';
267 g_log.
debug() <<
"source->sample=" << source_sample <<
'\n';
268 g_log.
debug() <<
"sample->monitor=" << (source_monitor - source_sample) <<
'\n';
271 nxfile.writeData(
"distance_new", (source_monitor - source_sample));
272 nxfile.openData(
"distance_new");
273 nxfile.putAttr(
"units",
"metre");
281 g_log.
error() <<
"There are no NXentry nodes in the specified NeXus file.\n";