23#include <nexus/NeXusFile.hpp>
24#include <nexus/NeXusException.hpp>
28#include <boost/mpi.hpp>
29namespace mpi = boost::mpi;
37using namespace ::
NeXus;
63constexpr double BYTES_TO_GiB = 1. / 1024. / 1024. / 1024.;
80 std::set<std::string> exts_set;
85 std::vector<std::string> exts(exts_set.begin(), exts_set.end());
87 "The name of the event nexus, runinfo.xml, raw, or histo nexus file to "
88 "read, including its full or relative path. The Event NeXus file name is "
89 "typically of the form INST_####_event.nxs (N.B. case sensitive if "
90 "running on Linux).");
92 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
93 mustBePositive->setLower(0.0);
95 "Get chunking strategy for chunks with this number of "
96 "Gbytes. File will not be loaded if this option is set.");
99 "An output workspace.");
106 double maxChunk = this->
getProperty(
"MaxChunkSize");
108 g_log.
debug() <<
"Converting maxChunk=0 to maxChunk=EMPTY_DBL\n";
113 int m_numberOfSpectra = 0;
120 strategy->addColumn(
"int",
"ChunkNumber");
121 strategy->addColumn(
"int",
"TotalChunks");
123 strategy->addColumn(
"int",
"SpectrumMin");
124 strategy->addColumn(
"int",
"SpectrumMax");
130 if (maxChunk == 0 ||
isEmpty(maxChunk)) {
135 Poco::File fileinfo(filename);
136 const double fileSizeGiB =
static_cast<double>(fileinfo.getSize()) * BYTES_TO_GiB;
142 if (fileType !=
PRENEXUS_FILE && 6. * fileSizeGiB < maxChunk)
147 double wkspSizeGiB = 0;
150 vector<string> eventFilenames;
154 for (
auto &eventFilename : eventFilenames) {
157 wkspSizeGiB +=
static_cast<double>(eventfile.
getNumElements()) * 48.0 * BYTES_TO_GiB;
164 ::NeXus::File file(filename);
168 file.openGroup(m_top_entry_name,
"NXentry");
171 map<string, string> entries = file.getEntries();
172 map<string, string>::const_iterator it = entries.begin();
173 std::string classType =
"NXevent_data";
174 size_t total_events = 0;
175 for (; it != entries.end(); ++it) {
176 std::string entry_name(it->first);
177 std::string entry_class(it->second);
178 if (entry_class == classType) {
182 file.openGroup(entry_name, entry_class);
183 file.openData(
"total_counts");
184 if (file.getInfo().type == NX_UINT64) {
185 std::vector<uint64_t> bank_events;
186 file.getData(bank_events);
187 total_events += bank_events[0];
189 std::vector<int> bank_events;
190 file.getDataCoerce(bank_events);
191 total_events += bank_events[0];
195 }
catch (::NeXus::Exception &) {
196 g_log.
error() <<
"Unable to find total counts to determine "
197 "chunking strategy.\n";
207 wkspSizeGiB =
static_cast<double>(total_events) * 48.0 * BYTES_TO_GiB;
210 wkspSizeGiB = fileSizeGiB * 24.0;
211 g_log.
notice() <<
"Wksp size is " << wkspSizeGiB <<
" GB\n";
216 iraw.
ioRAW(file,
true);
219 m_numberOfSpectra = iraw.
t_nsp1;
220 g_log.
notice() <<
"Spectra size is " << m_numberOfSpectra <<
" spectra\n";
226 wkspSizeGiB = fileSizeGiB * 144.0;
227 g_log.
notice() <<
"Wksp size is " << wkspSizeGiB <<
" GB\n";
232 std::vector<std::string> bankNames;
234 m_numberOfSpectra =
static_cast<int>(lp.
m_numPixels);
235 g_log.
notice() <<
"Spectra size is " << m_numberOfSpectra <<
" spectra\n";
237 throw(std::invalid_argument(
"unsupported file type"));
243 numChunks =
static_cast<int>(wkspSizeGiB / maxChunk);
247 if (numChunks <= 1 ||
isEmpty(maxChunk)) {
251 g_log.
information() <<
"Everything can be done in a single chunk returning empty table\n";
259 if (mpi::communicator().size() > 1) {
260 int imult = numChunks / mpi::communicator().size() + 1;
261 numChunks = imult * mpi::communicator().size();
265 for (
int i = 1; i <= numChunks; i++) {
267 if (mpi::communicator().size() > 1) {
269 if ((i - 1) % mpi::communicator().size() != mpi::communicator().rank())
275 row << i << numChunks;
277 int spectraPerChunk = m_numberOfSpectra / numChunks;
278 int first = (i - 1) * spectraPerChunk + 1;
279 int last = first + spectraPerChunk - 1;
281 last = m_numberOfSpectra;
282 row << first << last;
289 std::string top_entry_name;
290 using string_map_t = std::map<std::string, std::string>;
292 string_map_t::const_iterator it;
293 ::NeXus::File file = ::NeXus::File(filename);
294 string_map_t entries = file.getEntries();
297 top_entry_name = entries.begin()->first;
299 for (it = entries.begin(); it != entries.end(); ++it) {
300 if (((it->first ==
"entry") || (it->first ==
"raw_data_1")) && (it->second ==
"NXentry")) {
301 top_entry_name = it->first;
305 }
catch (
const std::exception &) {
306 g_log.
error() <<
"Unable to determine name of top level NXentry - assuming "
308 top_entry_name =
"entry";
310 return top_entry_name;
323 if (filename.find(extension) != std::string::npos) {
324 g_log.
information() <<
"Determined \'" << filename <<
"\' is a prenexus file\n";
331 if (filename.find(extension) != std::string::npos) {
332 g_log.
information() <<
"Determined \'" << filename <<
"\' is a histogram nexus file\n";
339 if (filename.find(extension) != std::string::npos) {
340 g_log.
information() <<
"Determined \'" << filename <<
"\' is an event nexus file\n";
346 for (
const auto &extension :
RAW_EXT) {
347 if (filename.find(extension) != std::string::npos) {
348 g_log.
information() <<
"Determined \'" << filename <<
"\' is an ISIS raw file\n";
353 throw std::invalid_argument(
"Unsupported file type");
#define DECLARE_ALGORITHM(classname)
int t_nsp1
number of spectra in time regime 1
virtual int ioRAW(FILE *file, bool from_file, bool read_data=true)
stuff
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
@ Load
allowed here which will be passed to the algorithm
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
void init() override
Virtual method - must be overridden by concrete algorithm.
int version() const override
function to return a version of the algorithm, must be overridden in all algorithms
void exec() override
Virtual method - must be overridden by concrete algorithm.
const std::string name() const override
function to return a name of the algorithm, must be overridden in all algorithms
FileType getFileType(const std::string &filename)
Determine the file type using the filename.
const std::string category() const override
function to return a category of the algorithm.
std::string setTopEntryName(const std::string &filename)
set the name of the top level NXentry m_top_entry_name
LoadPreNexus : Workflow algorithm to load a collection of preNeXus files.
void parseRuninfo(const std::string &runinfo, std::string &dataDir, std::vector< std::string > &eventFilenames)
Parse the runinfo file to find the names of the neutron event files.
Helper class for LoadRaw algorithms.
FILE * openRawFile(const std::string &fileName)
Opens Raw File.
Loads a NeXus file that conforms to the TOFRaw instrument definition format and stores it in a 2D wor...
int m_signalNo
Signal # to load. Default 1.
static std::string getEntryName(const std::string &filename)
void countPixels(const std::string &nexusfilename, const std::string &entry_name, std::vector< std::string > &bankNames)
Goes thoguh a histogram NXS file and counts the number of pixels.
size_t m_numPixels
Number of pixels.
The BinaryFile template is a helper function for loading simple binary files.
size_t getNumElements() const
Returns the # of elements in the file (cached result of getFileSize)
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
ISIS VMS raw file definitions.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
const int NUM_EXT_HISTO_NEXUS(1)
Number of histogram nexus extensions.
const int NUM_EXT_RAW(1)
Number of raw file extensions.
const std::string HISTO_NEXUS_EXT[NUM_EXT_HISTO_NEXUS]
Valid extensions for histogram nexus files.
const std::string PRENEXUS_EXT[NUM_EXT_PRENEXUS]
Valid extensions for prenexus files.
const int NUM_EXT_EVENT_NEXUS(3)
Number of event nexus extensions.
const std::string RAW_EXT[NUM_EXT_RAW]
Valid extensions for ISIS raw files.
FileType
Allowed file types.
@ RAW_FILE
ISIS raw files.
@ PRENEXUS_FILE
PreNeXus files.
@ HISTO_NEXUS_FILE
Histogram NeXus files.
@ EVENT_NEXUS_FILE
Event NeXus files.
const std::string EVENT_NEXUS_EXT[NUM_EXT_EVENT_NEXUS]
Valid extensions for event nexus files.
const int NUM_EXT_PRENEXUS(1)
Number of prenexus extensions.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
@ Output
An output workspace.