19#include "MantidNexus/NexusFile.h"
37const std::vector<std::string>
EVENT_NEXUS_EXT = {
"_event.nxs",
".nxs",
".nxs.h5"};
41const std::vector<std::string>
RAW_EXT = {
".raw"};
47constexpr double BYTES_TO_GiB = 1. / 1024. / 1024. / 1024.;
64 std::set<std::string> exts_set;
69 std::vector<std::string> exts(exts_set.begin(), exts_set.end());
71 "The name of the event nexus, runinfo.xml, raw, or histo nexus file to "
72 "read, including its full or relative path. The Event NeXus file name is "
73 "typically of the form INST_####_event.nxs (N.B. case sensitive if "
74 "running on Linux).");
76 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
77 mustBePositive->setLower(0.0);
79 "Get chunking strategy for chunks with this number of "
80 "Gbytes. File will not be loaded if this option is set.");
83 "An output workspace.");
90 double maxChunk = this->
getProperty(
"MaxChunkSize");
92 g_log.
debug() <<
"Converting maxChunk=0 to maxChunk=EMPTY_DBL\n";
97 int m_numberOfSpectra = 0;
104 strategy->addColumn(
"int",
"ChunkNumber");
105 strategy->addColumn(
"int",
"TotalChunks");
107 strategy->addColumn(
"int",
"SpectrumMin");
108 strategy->addColumn(
"int",
"SpectrumMax");
112 if (maxChunk == 0 ||
isEmpty(maxChunk)) {
116 std::filesystem::path fileinfo(filename);
117 const double fileSizeGiB =
static_cast<double>(std::filesystem::file_size(fileinfo)) * BYTES_TO_GiB;
122 if (fileType !=
PRENEXUS_FILE && 6. * fileSizeGiB < maxChunk)
126 double wkspSizeGiB = 0;
129 vector<string> eventFilenames;
133 std::filesystem::path dataPath(dataDir);
134 for (
const auto &eventFilename : eventFilenames) {
137 wkspSizeGiB +=
static_cast<double>(eventfile.
getNumElements()) * 48.0 * BYTES_TO_GiB;
144 Nexus::File file(filename);
148 file.openGroup(m_top_entry_name,
"NXentry");
151 map<string, string> entries = file.getEntries();
152 map<string, string>::const_iterator it = entries.begin();
153 std::string classType =
"NXevent_data";
154 size_t total_events = 0;
155 for (; it != entries.end(); ++it) {
156 const std::string entry_class(it->second);
157 if (entry_class == classType) {
160 const std::string entry_name(it->first);
162 file.openGroup(entry_name, entry_class);
163 file.openData(
"total_counts");
165 std::vector<uint64_t> bank_events;
166 file.getData(bank_events);
167 total_events += bank_events[0];
169 std::vector<int> bank_events;
170 file.getDataCoerce(bank_events);
171 total_events += bank_events[0];
176 g_log.
error() <<
"Unable to find total counts to determine "
177 "chunking strategy.\n";
187 wkspSizeGiB =
static_cast<double>(total_events) * 48.0 * BYTES_TO_GiB;
190 wkspSizeGiB = fileSizeGiB * 24.0;
191 g_log.
notice() <<
"Wksp size is " << wkspSizeGiB <<
" GB\n";
196 iraw.
ioRAW(file,
true);
199 m_numberOfSpectra = iraw.
t_nsp1;
200 g_log.
notice() <<
"Spectra size is " << m_numberOfSpectra <<
" spectra\n";
206 wkspSizeGiB = fileSizeGiB * 144.0;
207 g_log.
notice() <<
"Wksp size is " << wkspSizeGiB <<
" GB\n";
212 std::vector<std::string> bankNames;
214 m_numberOfSpectra =
static_cast<int>(lp.
m_numPixels);
215 g_log.
notice() <<
"Spectra size is " << m_numberOfSpectra <<
" spectra\n";
217 throw(std::invalid_argument(
"unsupported file type"));
223 numChunks =
static_cast<int>(wkspSizeGiB / maxChunk);
227 if (numChunks <= 1 ||
isEmpty(maxChunk)) {
228 g_log.
information() <<
"Everything can be done in a single chunk returning empty table\n";
233 for (
int i = 1; i <= numChunks; i++) {
236 row << i << numChunks;
238 int spectraPerChunk = m_numberOfSpectra / numChunks;
239 int first = (i - 1) * spectraPerChunk + 1;
240 int last = first + spectraPerChunk - 1;
242 last = m_numberOfSpectra;
243 row << first << last;
250 std::string top_entry_name;
251 using string_map_t = std::map<std::string, std::string>;
253 string_map_t::const_iterator it;
254 Nexus::File file = Nexus::File(filename);
255 string_map_t entries = file.getEntries();
258 top_entry_name = entries.begin()->first;
260 for (it = entries.begin(); it != entries.end(); ++it) {
261 if (((it->first ==
"entry") || (it->first ==
"raw_data_1")) && (it->second ==
"NXentry")) {
262 top_entry_name = it->first;
266 }
catch (
const std::exception &) {
267 g_log.
error() <<
"Unable to determine name of top level NXentry - assuming "
269 top_entry_name =
"entry";
271 return top_entry_name;
284 g_log.
information() <<
"Determined \'" << filename <<
"\' is a prenexus file\n";
290 g_log.
information() <<
"Determined \'" << filename <<
"\' is a histogram nexus file\n";
296 g_log.
information() <<
"Determined \'" << filename <<
"\' is an event nexus file\n";
302 g_log.
information() <<
"Determined \'" << filename <<
"\' is an ISIS raw file\n";
306 throw std::invalid_argument(
"Unsupported file type");
310 const std::vector<std::string> &fileExtensions) {
311 return std::any_of(fileExtensions.cbegin(), fileExtensions.cend(),
312 [&filename](
const auto &extension) { return filename.find(extension) != std::string::npos; });
#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
bool filenameHasExtension(const std::string &filename, const std::vector< std::string > &fileExtensions)
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...
Class that provides for a standard Nexus exception.
static unsigned short constexpr UINT64
ISIS VMS raw file definitions.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
const std::vector< std::string > RAW_EXT
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::vector< std::string > PRENEXUS_EXT
Valid extensions for prenexus files.
const std::vector< std::string > HISTO_NEXUS_EXT
Valid extensions for histogram nexus files.
const std::vector< std::string > EVENT_NEXUS_EXT
Valid extensions for event nexus files.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
@ Output
An output workspace.