Newer
Older
/*WIKI*
TODO: Enter a full wiki-markup description of your algorithm here. You can then use the Build/wiki_maker.py script to generate your full wiki page.
*WIKI*/
#include "MantidDataHandling/LoadHelper.h"
namespace Mantid {
namespace DataHandling {
using namespace Kernel;
using namespace API;
LoadHelper::LoadHelper() :
g_log(Kernel::Logger::get("Algorithm")) {
}
LoadHelper::~LoadHelper() {
}
/**
* Finds the path for the instrument name in the nexus file
* Usually of the form: entry0/\<NXinstrument class\>/name
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
*/
std::string LoadHelper::findInstrumentNexusPath(
const NeXus::NXEntry &firstEntry) {
std::string insNamePath = "";
std::vector<NeXus::NXClassInfo> v = firstEntry.groups();
for (auto it = v.begin(); it < v.end(); it++) {
if (it->nxclass == "NXinstrument") {
insNamePath = it->nxname;
break;
}
}
return insNamePath;
}
std::string LoadHelper::getStringFromNexusPath(const NeXus::NXEntry &firstEntry,
const std::string &nexusPath) {
return firstEntry.getString(nexusPath);
}
double LoadHelper::getDoubleFromNexusPath(const NeXus::NXEntry &firstEntry,
const std::string &nexusPath) {
return firstEntry.getFloat(nexusPath);
}
/**
* Gets the time binning from a Nexus float array
* Adds an extra bin at the end
*/
std::vector<double> LoadHelper::getTimeBinningFromNexusPath(
const NeXus::NXEntry &firstEntry, const std::string &nexusPath) {
NeXus::NXFloat timeBinningNexus = firstEntry.openNXFloat(nexusPath);
timeBinningNexus.load();
size_t numberOfBins = static_cast<size_t>(timeBinningNexus.dim0()) + 1; // boundaries
float* timeBinning_p = &timeBinningNexus[0];
std::vector<double> timeBinning(numberOfBins);
timeBinning.assign(timeBinning_p, timeBinning_p + numberOfBins);
// calculate the extra bin at the end
timeBinning[numberOfBins - 1] = timeBinning[numberOfBins - 2]
+ timeBinning[1] - timeBinning[0];
return timeBinning;
}
/**
* Calculate Neutron Energy from wavelength: \f$ E = h^2 / 2m\lambda ^2 \f$
* @param wavelength :: wavelength in \f$ \AA \f$
* @return tof in seconds
*/
double LoadHelper::calculateEnergy(double wavelength) {
double e = (PhysicalConstants::h * PhysicalConstants::h)
/ (2 * PhysicalConstants::NeutronMass * wavelength * wavelength
* 1e-20) / PhysicalConstants::meV;
return e;
}
/**
* Calculate TOF from distance
* @param distance :: distance in meters
* @param wavelength :: wavelength to calculate TOF from
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
* @return tof in seconds
*/
double LoadHelper::calculateTOF(double distance,double wavelength) {
if (wavelength <= 0) {
g_log.error("Wavelenght is <= 0");
throw std::runtime_error("Wavelenght is <= 0");
}
double velocity = PhysicalConstants::h
/ (PhysicalConstants::NeutronMass * wavelength * 1e-10); //m/s
return distance / velocity;
}
double LoadHelper::getL1(const API::MatrixWorkspace_sptr& workspace) {
Geometry::Instrument_const_sptr instrument =
workspace->getInstrument();
Geometry::IObjComponent_const_sptr sample = instrument->getSample();
double l1 = instrument->getSource()->getDistance(*sample);
return l1;
}
double LoadHelper::getL2(const API::MatrixWorkspace_sptr& workspace, int detId) {
// Get a pointer to the instrument contained in the workspace
Geometry::Instrument_const_sptr instrument =
workspace->getInstrument();
// Get the distance between the source and the sample (assume in metres)
Geometry::IObjComponent_const_sptr sample = instrument->getSample();
// Get the sample-detector distance for this detector (in metres)
double l2 = workspace->getDetector(detId)->getPos().distance(
sample->getPos());
return l2;
}
/*
* Get instrument property as double
* @s - input property name
*
*/
double LoadHelper::getInstrumentProperty(const API::MatrixWorkspace_sptr& workspace, std::string s) {
std::vector<std::string> prop =
workspace->getInstrument()->getStringParameter(s);
if (prop.empty()) {
g_log.debug("Property <" + s + "> doesn't exist!");
return EMPTY_DBL();
} else {
g_log.debug() << "Property <" + s + "> = " << prop[0] << std::endl;
return boost::lexical_cast<double>(prop[0]);
}
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Recursively add properties from a nexus file
*
* @param nxfileID :: The Nexus file to be parsed
* @param runDetails :: where to add properties
* @param parent_name :: nexus caller name
* @param parent_class :: nexus caller class
* @param level :: current level in nexus tree
*
*/
void LoadHelper::RecurseForProperties(NXhandle nxfileID,
API::Run& runDetails,
std::string& parent_name,
std::string& parent_class,
int level) {
std::string indent_str(level*2, ' ');// Two space by indent level
// Link ?
// Attributes ?
//dump_attributes(nxfileID, indent_str);
// Classes
NXstatus stat; ///< return status
int datatype; ///< NX data type if a dataset, e.g. NX_CHAR, NX_FLOAT32, see napi.h
char nxname[NX_MAXNAMELEN],nxclass[NX_MAXNAMELEN];
while(NXgetnextentry(nxfileID,nxname,nxclass,&datatype) != NX_EOD)
{
g_log.debug()<<indent_str<<parent_name<<"."<<nxname<<" ; "<<nxclass<<std::endl;
if((stat=NXopengroup(nxfileID,nxname,nxclass))==NX_OK){
// Go down to one level
std::string p_nxname(nxname);//current names can be useful for next level
std::string p_nxclass(nxclass);
RecurseForProperties(nxfileID, runDetails, p_nxname, p_nxclass, level+1);
NXclosegroup(nxfileID);
}// if(NXopengroup
else if ((stat=NXopendata (nxfileID, nxname))==NX_OK)
{
//dump_attributes(nxfileID, indent_str);
g_log.debug()<<indent_str<<nxname<<" opened."<<std::endl;
if (parent_class=="NXData") {
g_log.debug()<<indent_str<<"skipping NXData"<<std::endl;
/* nothing */
} else if (parent_class=="NXMonitor") {
g_log.debug()<<indent_str<<"skipping NXMonitor"<<std::endl;
/* nothing */
} else { // create a property
int rank;
int dims[4];
int type;
std::string property_name;
// Exclude "entry0" from name for level 1 property
if (parent_name == "entry0")
property_name = nxname;
else
property_name = parent_name+"."+nxname;
g_log.debug()<<indent_str<<"considering property "<<property_name<<std::endl;
// Get the value
NXgetinfo(nxfileID, &rank, dims, &type);
// Note, we choose to ignore "multidim properties
if (rank!=1) {
g_log.debug()<<indent_str<<"ignored multi dimension data on "<<property_name<<std::endl;
} else {
void *dataBuffer;
NXmalloc (&dataBuffer, rank, dims, type);
if (NXgetdata(nxfileID, dataBuffer) != NX_OK) {
NXfree(&dataBuffer);
throw std::runtime_error("Cannot read data from NeXus file");
}
if (type==NX_CHAR) {
runDetails.addProperty(property_name, std::string((const char *)dataBuffer));
} else if ((type==NX_FLOAT32)
||(type==NX_FLOAT64)
||(type==NX_INT16)
||(type==NX_INT32)
||(type==NX_UINT16)
) {
// Look for "units"
NXstatus units_status;
char units_sbuf[NX_MAXNAMELEN];
int units_len=NX_MAXNAMELEN;
int units_type=NX_CHAR;
units_status=NXgetattr(nxfileID,const_cast<char*>("units"),(void *)units_sbuf,&units_len,&units_type);
if(units_status!=NX_ERROR)
{
g_log.debug()<<indent_str<<"[ "<<property_name<<" has unit "<<units_sbuf<<" ]"<<std::endl;
}
if ((type==NX_FLOAT32)||(type==NX_FLOAT64)) {
// Mantid numerical properties are double only.
double property_double_value=0.0;
if (type==NX_FLOAT32) {
property_double_value = *((float*)dataBuffer);
} else if (type==NX_FLOAT64) {
property_double_value = *((double*)dataBuffer);
}
if(units_status!=NX_ERROR)
runDetails.addProperty(property_name, property_double_value, std::string(units_sbuf));
else
runDetails.addProperty(property_name, property_double_value);
} else {
// int case
int property_int_value=0;
if (type==NX_INT16) {
property_int_value = *((short int*)dataBuffer);
} else if (type==NX_INT32) {
property_int_value = *((int*)dataBuffer);
}else if (type==NX_UINT16) {
property_int_value = *((short unsigned int*)dataBuffer);
}
if(units_status!=NX_ERROR)
runDetails.addProperty(property_name, property_int_value, std::string(units_sbuf));
else
runDetails.addProperty(property_name, property_int_value);
}// if (type==...
} else {
g_log.debug()<<indent_str<<"unexpected data on "<<property_name<<std::endl;
}
NXfree(&dataBuffer);
}
}
NXclosedata(nxfileID);
} else {
g_log.debug()<<indent_str<<"unexpected status ("<<stat<<") on "<<nxname<<std::endl;
}
}// while NXgetnextentry
}// RecurseForProperties
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Parses the date as formatted at the ILL:
* 29-Jun-12 11:27:26
* and converts it to the ISO format used in Mantid:
* ISO8601 format string: "yyyy-mm-ddThh:mm:ss[Z+-]tz:tz"
*
* @param dateToParse :: date as string
* @return date as required in Mantid
*/
std::string LoadHelper::dateTimeInIsoFormat(std::string dateToParse) {
namespace bt = boost::posix_time;
// parsing format
const std::locale format = std::locale(std::locale::classic(),
new bt::time_input_facet("%d-%b-%y %H:%M:%S"));
bt::ptime pt;
std::istringstream is(dateToParse);
is.imbue(format);
is >> pt;
if (pt != bt::ptime()) {
// Converts to ISO
std::string s = bt::to_iso_extended_string(pt);
return s;
} else {
return "";
}
}
} // namespace DataHandling
} // namespace Mantid