26#include <nexus/NeXusFile.hpp>
27#include <nexus/NeXusException.hpp>
32#include <boost/algorithm/string/predicate.hpp>
36struct StartAndEndTime {
37 Mantid::Types::Core::DateAndTime startTime;
38 Mantid::Types::Core::DateAndTime endTime;
41StartAndEndTime getStartAndEndTimesFromRawFile(
const std::string &filename) {
42 FILE *rawFile = fopen(filename.c_str(),
"rb");
44 throw std::runtime_error(
"Cannot open RAW file for reading: " + filename);
47 const bool fromFile(
true), readData(
false);
48 isisRaw.
ioRAW(rawFile, fromFile, readData);
50 StartAndEndTime startAndEndTime;
55 return startAndEndTime;
58StartAndEndTime getStartAndEndTimesFromNexusFile(
const std::string &filename,
59 const Mantid::Types::Core::DateAndTime &startTimeDefault,
60 const Mantid::Types::Core::DateAndTime &endTimeDefault) {
61 StartAndEndTime startAndEndTime;
66 startAndEndTime.startTime = startTimeDefault;
67 startAndEndTime.endTime = endTimeDefault;
70 return startAndEndTime;
80using namespace HistogramData;
98 using namespace Kernel;
101 "An instrument name or filename ( a full path or string "
102 "containing an xml extension).",
107 "A comma separated list of first bin boundary, width, last "
108 "bin boundary. See Rebin for more details");
118 "An optional filename (currently RAW or ISIS NeXus) that "
119 "contains UDET & SPEC tables to access hardware grouping");
121 declareProperty(
"SetErrors",
false,
"Whether to set histogram bin errors to sqrt of intensity.");
142 const bool enableLogging(
false);
150 loadInstrument->setProperty(
"Workspace", tempWS);
151 const std::string instrProp =
getProperty(
"Instrument");
152 if (boost::algorithm::ends_with(instrProp,
".xml")) {
153 loadInstrument->setPropertyValue(
"Filename", instrProp);
155 loadInstrument->setPropertyValue(
"InstrumentName", instrProp);
158 loadInstrument->executeAsChildAlg();
159 tempWS = loadInstrument->getProperty(
"Workspace");
170 const size_t xlength = binBoundaries.size();
171 const size_t ylength = xlength - 1;
181 m_progress = std::make_shared<Progress>(
this, 0.5, 0.75, nhistograms);
184 for (int64_t i = 0; i < static_cast<int64_t>(nhistograms); ++i) {
196 const std::string detTableFile =
getProperty(
"DetectorTableFilename");
197 if (boost::algorithm::ends_with(detTableFile,
".raw") || boost::algorithm::ends_with(detTableFile,
".RAW") ||
198 boost::algorithm::ends_with(detTableFile,
".nxs") || boost::algorithm::ends_with(detTableFile,
".NXS")) {
209 const std::string detTableFile =
getProperty(
"DetectorTableFilename");
210 if (detTableFile.empty()) {
222 const std::vector<detid_t> detids =
m_instrument->getDetectorIDs(
true);
223 const size_t nhist = detids.size();
226 for (
size_t i = 0; i < nhist; ++i) {
227 std::set<detid_t> group;
228 group.insert(detids[i]);
238 if (boost::algorithm::ends_with(filename,
".raw") || boost::algorithm::ends_with(filename,
".RAW")) {
240 }
else if (boost::algorithm::ends_with(filename,
".nxs") || boost::algorithm::ends_with(filename,
".NXS")) {
250 FILE *rawFile = fopen(filename.c_str(),
"rb");
252 throw std::runtime_error(
"Cannot open RAW file for reading: " + filename);
255 const bool fromFile(
true), readData(
false);
256 isisRaw.
ioRAW(rawFile, fromFile, readData);
258 int ndet = isisRaw.
i_det;
259 int *specTable = isisRaw.
spec;
260 int *udetTable = isisRaw.
udet;
273 ::NeXus::File nxsFile(filename);
275 nxsFile.openPath(
"/raw_data_1/isis_vms_compat");
276 }
catch (::NeXus::Exception &) {
277 throw std::runtime_error(
"Cannot find path to isis_vms_compat. Is the file an ISIS NeXus file?");
279 using NXIntArray = std::unique_ptr<std::vector<int32_t>>;
281 nxsFile.openData(
"NDET");
282 NXIntArray ndets(nxsFile.getData<int32_t>());
285 nxsFile.openData(
"SPEC");
286 NXIntArray specTable(nxsFile.getData<int32_t>());
289 nxsFile.openData(
"UDET");
290 NXIntArray udetTable(nxsFile.getData<int32_t>());
304 for (
int i = 0; i < ndets; ++i) {
305 int specNo = specTable[i];
306 int detID = udetTable[i];
312 iter->second.insert(detID);
314 std::set<detid_t> group;
315 group.insert(
static_cast<detid_t>(detID));
325 const std::vector<double> rbparams =
getProperty(
"BinParams");
328 if (numBoundaries <= 2) {
329 throw std::invalid_argument(
"Error in BinParams - Gave invalid number of bin boundaries: " +
332 return BinEdges(std::move(newBins));
341 auto &spectrum =
m_outputWS->getSpectrum(wsIndex);
342 spectrum.setSpectrumNo(
static_cast<specnum_t>(wsIndex + 1));
343 spectrum.clearDetectorIDs();
344 spectrum.addDetectorIDs(detGroup.second);
357 std::shared_ptr<Geometry::Parameter> updateDets =
pmap.get(instrument->getComponentID(),
"det-pos-source");
361 std::string
value = updateDets->value<std::string>();
362 if (
value.substr(0, 8) ==
"datafile") {
365 updateInst->setPropertyValue(
"Filename", filename);
366 if (
value ==
"datafile-ignore-phi") {
367 updateInst->setProperty(
"IgnorePhi",
true);
369 "the data file except for the phi values");
371 g_log.
information(
"Detector positions in IDF updated with positions in the data file");
375 updateInst->execute();
385 const std::string detTableFile =
getProperty(
"DetectorTableFilename");
386 auto hasDetTableFile = !detTableFile.empty();
389 Types::Core::DateAndTime startTime;
390 Types::Core::DateAndTime endTime;
393 startTime = run.startTime();
394 endTime = run.endTime();
395 }
catch (std::runtime_error &) {
396 startTime = Types::Core::DateAndTime::getCurrentTime();
397 endTime = Types::Core::DateAndTime::getCurrentTime();
400 if (hasDetTableFile) {
401 if (boost::algorithm::ends_with(detTableFile,
".raw") || boost::algorithm::ends_with(detTableFile,
".RAW")) {
402 auto startAndEndTime = getStartAndEndTimesFromRawFile(detTableFile);
403 startTime = startAndEndTime.startTime;
404 endTime = startAndEndTime.endTime;
405 }
else if (boost::algorithm::ends_with(detTableFile,
".nxs") || boost::algorithm::ends_with(detTableFile,
".NXS")) {
406 auto startAndEndTime = getStartAndEndTimesFromNexusFile(detTableFile, startTime, endTime);
407 startTime = startAndEndTime.startTime;
408 endTime = startAndEndTime.endTime;
412 run.setStartAndEndTime(startTime, endTime);
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
IPeaksWorkspace_sptr workspace
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
int ioRAW(FILE *file, bool from_file, bool read_data=true) override
Loads the headers of the file, leaves the file pointer at a specific position.
int i_det
number of detectors NDET
int * udet
user detector number for each detector (size NDET)
int * spec
spectrum number table (size NDET)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
A property class for workspaces.
API::MatrixWorkspace_sptr m_outputWS
Pointer to the new workspace.
void createGroupingsFromTables(const int *specTable, const int *udetTable, int ndets)
Create the grouping map from the tables.
void init() override
Initialize the algorithm's properties.
std::shared_ptr< API::Progress > m_progress
Pointer to a progress object.
std::map< specnum_t, std::set< detid_t > > m_detGroups
List of detector groupings.
void createOutputWorkspace()
Creates the output workspace.
void setStartDate(const API::MatrixWorkspace_sptr &workspace)
Set start date for dummy workspace.
void createInstrument()
Create the instrument.
const std::string name() const override
Algorithm's name for identification.
int version() const override
Algorithm's version for identification.
void loadMappingFromFile(const std::string &filename)
Load the detector mapping from a file.
void applyDetectorMapping()
Apply the created mapping to the workspace.
void exec() override
Execute the algorithm.
void createOneToOneMapping()
Create a one to one mapping from the spectrum numbers to detector IDs.
void loadMappingFromISISNXS(const std::string &filename)
Load the detector mapping from a NXS file.
Geometry::Instrument_const_sptr m_instrument
Pointer to the new instrument.
size_t createDetectorMapping()
Creates the detector grouping list.
HistogramData::BinEdges createBinBoundaries() const
Returns new Xbins.
const std::string category() const override
Algorithm's category for identification.
void adjustInstrument(const std::string &filename)
Apply any instrument adjustments from the file.
void loadMappingFromRAW(const std::string &filename)
Load the detector mapping from a RAW file.
static Types::Core::DateAndTime extractEndTime(ISISRAW &isisRaw)
Extract the end time from a raw file.
static Types::Core::DateAndTime extractStartTime(ISISRAW &isisRaw)
Extract the start time from a raw file.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
void information(const std::string &msg)
Logs at information level.
Validator to check that a property is not left empty.
OptionalBool : Tri-state bool.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Mantid::Types::Core::DateAndTime DLLExport extractEndTime(const std::string &filename)
Gets the start time from the nexus file.
Mantid::Types::Core::DateAndTime DLLExport extractStartTime(const std::string &filename)
Extracts the start and the end time from a Nexus file.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector< double > ¶ms, std::vector< double > &xnew, const bool resize_xnew=true, const bool full_bins_only=false, const double xMinHint=std::nan(""), const double xMaxHint=std::nan(""), const bool useReverseLogarithmic=false, const double power=-1)
Creates a new output X array given a 'standard' set of rebinning parameters.
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
int32_t detid_t
Typedef for a detector ID.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
int32_t specnum_t
Typedef for a spectrum Number.
Generate a tableworkspace to store the calibration results.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
@ Output
An output workspace.