18#include <nexus/napi.h>
20#include <boost/algorithm/string/predicate.hpp>
26Kernel::Logger
g_log(
"LoadHelper");
29namespace DataHandling {
30using namespace Kernel;
38 std::string insNamePath;
39 std::vector<Mantid::NeXus::NXClassInfo> v = firstEntry.
groups();
40 for (
auto it = v.begin(); it < v.end(); it++) {
41 if (it->nxclass ==
"NXinstrument") {
42 insNamePath = it->nxname;
54 return firstEntry.
getFloat(nexusPath);
62 const std::string &nexusPath) {
65 timeBinningNexus.
load();
67 size_t numberOfBins =
static_cast<size_t>(timeBinningNexus.
dim0()) + 1;
69 float *timeBinning_p = &timeBinningNexus[0];
70 std::vector<double> timeBinning(numberOfBins);
71 std::copy(timeBinning_p, timeBinning_p + timeBinningNexus.
dim0(), std::begin(timeBinning));
73 timeBinning[numberOfBins - 1] = timeBinning[numberOfBins - 2] + timeBinning[1] - timeBinning[0];
95 if (wavelength <= 0) {
96 throw std::runtime_error(
"Wavelenght is <= 0");
101 return distance / velocity;
110 std::vector<std::string> prop =
workspace->getInstrument()->getStringParameter(s);
114 return boost::lexical_cast<double>(prop[0]);
133 std::string emptyStr;
135 char nxname[NX_MAXNAMELEN], nxclass[NX_MAXNAMELEN];
136 if (!entryName.empty()) {
137 strcpy(nxname, entryName.c_str());
146 NXstatus getnextentry_status = NXgetnextentry(nxfileID, nxname, nxclass, &datatype);
147 if (getnextentry_status == NX_OK) {
148 if ((NXopengroup(nxfileID, nxname, nxclass)) == NX_OK) {
150 NXclosegroup(nxfileID);
169 std::string &parent_class,
int level,
bool useFullPath) {
171 NXstatus getnextentry_status;
174 char nxname[NX_MAXNAMELEN], nxclass[NX_MAXNAMELEN];
178 bool has_entry =
true;
180 getnextentry_status = NXgetnextentry(nxfileID, nxname, nxclass, &datatype);
182 if (getnextentry_status == NX_OK) {
183 NXstatus opengroup_status;
184 NXstatus opendata_status;
185 NXstatus getinfo_status;
187 std::string property_name = (parent_name.empty() ? nxname : parent_name +
"." + nxname);
188 if ((opengroup_status = NXopengroup(nxfileID, nxname, nxclass)) == NX_OK) {
189 if (std::string(nxclass) !=
"ILL_data_scan_vars" && std::string(nxclass) !=
"NXill_data_scan_vars") {
191 std::string p_nxname = useFullPath ? property_name : nxname;
192 std::string p_nxclass(nxclass);
197 NXclosegroup(nxfileID);
199 else if ((opendata_status = NXopendata(nxfileID, nxname)) == NX_OK) {
200 if (parent_class !=
"NXData" && parent_class !=
"NXMonitor" &&
201 std::string(nxname) !=
"data") {
203 int dims[4] = {0, 0, 0, 0};
207 if ((getinfo_status = NXgetinfo(nxfileID, &rank, dims, &type)) == NX_OK) {
210 bool build_small_float_array =
false;
211 bool read_property =
true;
213 if ((type == NX_FLOAT32) || (type == NX_FLOAT64)) {
214 if ((rank == 1) && (dims[0] <= 9)) {
215 build_small_float_array =
true;
217 read_property =
false;
219 }
else if (type != NX_CHAR) {
220 if ((rank > 1) || (dims[0] > 1) || (dims[1] > 1) || (dims[2] > 1) || (dims[3] > 1)) {
221 read_property =
false;
224 if ((rank > 1) || (dims[1] > 1) || (dims[2] > 1) || (dims[3] > 1)) {
225 read_property =
false;
232 NXmalloc(&dataBuffer, rank, dims, type);
234 if (NXgetdata(nxfileID, dataBuffer) == NX_OK) {
236 if (type == NX_CHAR) {
237 std::string property_value(
reinterpret_cast<const char *
>(dataBuffer));
238 if (boost::algorithm::ends_with(property_name,
"_time")) {
244 runDetails.
addProperty(property_name, property_value);
247 runDetails.
addProperty(property_name, property_value);
250 <<
" was set twice. Please check the Nexus file and your inputs." << std::endl;
254 }
else if ((type == NX_FLOAT32) || (type == NX_FLOAT64) || (type == NX_INT16) || (type == NX_INT32) ||
255 (type == NX_UINT16)) {
258 NXstatus units_status;
259 char units_sbuf[NX_MAXNAMELEN];
260 int units_len = NX_MAXNAMELEN;
261 int units_type = NX_CHAR;
263 char unitsAttrName[] =
"units";
264 units_status = NXgetattr(nxfileID, unitsAttrName, units_sbuf, &units_len, &units_type);
265 if ((type == NX_FLOAT32) || (type == NX_FLOAT64)) {
267 double property_double_value = 0.0;
271 if (type == NX_FLOAT32) {
272 property_double_value = *(
reinterpret_cast<float *
>(dataBuffer));
273 }
else if (type == NX_FLOAT64) {
274 property_double_value = *(
reinterpret_cast<double *
>(dataBuffer));
278 if (units_status != NX_ERROR)
279 runDetails.
addProperty(property_name, property_double_value, std::string(units_sbuf));
281 runDetails.
addProperty(property_name, property_double_value);
284 <<
" was set twice. Please check the Nexus file and your inputs." << std::endl;
286 }
else if (build_small_float_array) {
290 for (
int dim_index = 0; dim_index < dims[0]; dim_index++) {
291 if (type == NX_FLOAT32) {
292 property_double_value = (
reinterpret_cast<float *
>(dataBuffer))[dim_index];
293 }
else if (type == NX_FLOAT64) {
294 property_double_value = (
reinterpret_cast<double *
>(dataBuffer))[dim_index];
296 std::string indexed_property_name =
299 if (!runDetails.
hasProperty(indexed_property_name)) {
300 if (units_status != NX_ERROR)
301 runDetails.
addProperty(indexed_property_name, property_double_value,
302 std::string(units_sbuf));
304 runDetails.
addProperty(indexed_property_name, property_double_value);
307 <<
"Property " << property_name
308 <<
" was set twice. Please check the Nexus file and your inputs." << std::endl;
315 int property_int_value = 0;
316 if (type == NX_INT16) {
317 property_int_value = *(
reinterpret_cast<short int *
>(dataBuffer));
318 }
else if (type == NX_INT32) {
319 property_int_value = *(
reinterpret_cast<int *
>(dataBuffer));
320 }
else if (type == NX_UINT16) {
321 property_int_value = *(
reinterpret_cast<short unsigned int *
>(dataBuffer));
325 if (units_status != NX_ERROR)
326 runDetails.
addProperty(property_name, property_int_value, std::string(units_sbuf));
328 runDetails.
addProperty(property_name, property_int_value);
331 <<
" was set twice. Please check the Nexus file and your inputs." << std::endl;
338 dataBuffer =
nullptr;
344 NXclosedata(nxfileID);
346 }
else if (getnextentry_status == NX_EOD) {
370 std::vector<char> buff(128);
373 while (NXgetnextattr(nxfileID, pName, &iLength, &iType) != NX_EOD) {
375 while (NXgetnextattra(nxfileID, pName, &rank, dims, &iType) != NX_EOD) {
377 throw std::runtime_error(
"Encountered attribute with multi-dimensional array value");
380 if (iType != NX_CHAR && iLength != 1) {
381 throw std::runtime_error(
"Encountered attribute with array value");
387 if (iLength >
static_cast<int>(buff.size())) {
388 buff.resize(iLength);
390 int nz = iLength + 1;
391 NXgetattr(nxfileID, pName, buff.data(), &nz, &iType);
396 NXgetattr(nxfileID, pName, &
value, &iLength, &iType);
401 NXgetattr(nxfileID, pName, &
value, &iLength, &iType);
405 short unsigned int value;
406 NXgetattr(nxfileID, pName, &
value, &iLength, &iType);
422 namespace bt = boost::posix_time;
426 bt::from_iso_extended_string(dateToParse);
432 const std::locale format = std::locale(std::locale::classic(),
new bt::time_input_facet(
"%d-%b-%y %H:%M:%S"));
435 std::istringstream is(dateToParse);
439 if (pt != bt::ptime()) {
441 std::string s = bt::to_iso_extended_string(pt);
458 throw std::invalid_argument(
"Instrument component " + componentName +
" not found");
460 auto &componentInfo = ws->mutableComponentInfo();
461 const auto componentIndex = componentInfo.indexOf(component->getComponentID());
462 componentInfo.setPosition(componentIndex, newPos);
476 throw std::invalid_argument(
"Instrument component " + componentName +
" not found");
478 auto &componentInfo = ws->mutableComponentInfo();
479 const auto componentIndex = componentInfo.indexOf(component->getComponentID());
480 componentInfo.setRotation(componentIndex, rot);
492 throw std::invalid_argument(
"Instrument component " + componentName +
" not found");
494 V3D pos = component->getPos();
double value
The value of the point.
IPeaksWorkspace_sptr workspace
bool hasProperty(const std::string &name) const
Does the property exist on the object.
Kernel::Property * getProperty(const std::string &name) const
Returns the named property as a pointer.
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
This class stores information regarding an experimental run as a series of log entries.
void warning(const std::string &msg)
Logs at warning level.
virtual std::string setValue(const std::string &)=0
Set the value of the property via a string.
std::vector< NXClassInfo > & groups() const
Returns a list of all classes (or groups) in this NXClass.
float getFloat(const std::string &name) const
Returns a float.
NXFloat openNXFloat(const std::string &name) const
Creates and opens a float dataset.
std::string getString(const std::string &name) const
Returns a string.
Templated class implementation of NXDataSet.
void load(const int blocksize=1, int i=-1, int j=-1, int k=-1, int l=-1) override
Implementation of the virtual NXDataSet::load(...) method.
int dim0() const
Returns the number of elements along the first dimension.
Implements NXentry Nexus class.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
double getInstrumentProperty(const API::MatrixWorkspace_sptr &, const std::string &)
double calculateEnergy(double)
Calculate Neutron Energy from wavelength: .
void recurseAndAddNexusFieldsToWsRun(NXhandle nxfileID, API::Run &runDetails, std::string &parent_name, std::string &parent_class, int level, bool useFullPath)
Recursively add properties from a nexus file to the workspace run.
void rotateComponent(const API::MatrixWorkspace_sptr &ws, const std::string &componentName, const Kernel::Quat &rot)
LoadHelper::rotateComponent.
std::string getStringFromNexusPath(const Mantid::NeXus::NXEntry &, const std::string &)
std::vector< double > getTimeBinningFromNexusPath(const Mantid::NeXus::NXEntry &, const std::string &)
Gets the time binning from a Nexus float array Adds an extra bin at the end.
void addNexusFieldsToWsRun(NXhandle nxfileID, API::Run &runDetails, const std::string &entryName="", bool useFullPath=false)
Add properties from a nexus file to the workspace run.
double getDoubleFromNexusPath(const Mantid::NeXus::NXEntry &, const std::string &)
std::string findInstrumentNexusPath(const Mantid::NeXus::NXEntry &)
Finds the path for the instrument name in the nexus file Usually of the form: entry0/<NXinstrument cl...
void moveComponent(const API::MatrixWorkspace_sptr &ws, const std::string &componentName, const Kernel::V3D &newPos)
LoadHelper::moveComponent.
void dumpNexusAttributes(NXhandle nxfileID)
Show attributes attached to the current Nexus entry.
double calculateTOF(double, double)
Calculate TOF from distance.
std::string dateTimeInIsoFormat(const std::string &)
Parses the date as formatted at the ILL: 29-Jun-12 11:27:26 and converts it to the ISO format used in...
Kernel::V3D getComponentPosition(const API::MatrixWorkspace_sptr &ws, const std::string &componentName)
LoadHelper::getComponentPosition.
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
static constexpr double NeutronMass
Mass of the neutron in kg.
static constexpr double h
Planck constant in J*s.
static constexpr double meV
1 meV in Joules.
Helper class which provides the Collimation Length for SANS instruments.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
std::string to_string(const wide_integer< Bits, Signed > &n)