14#include <boost/numeric/conversion/cast.hpp>
24const std::string NX_ATTR_CLASS(
"NX_class");
25const std::string CAN_SAS_ATTR_CLASS(
"canSAS_class");
32template <
typename NumT> DataType
getType() {
throw DataTypeIException(); }
34template <> MANTID_DATAHANDLING_DLL DataType
getType<float>() {
return PredType::NATIVE_FLOAT; }
36template <> MANTID_DATAHANDLING_DLL DataType
getType<double>() {
return PredType::NATIVE_DOUBLE; }
38template <> MANTID_DATAHANDLING_DLL DataType
getType<int32_t>() {
return PredType::NATIVE_INT32; }
40template <> MANTID_DATAHANDLING_DLL DataType
getType<uint32_t>() {
return PredType::NATIVE_UINT32; }
42template <> MANTID_DATAHANDLING_DLL DataType
getType<int64_t>() {
return PredType::NATIVE_INT64; }
44template <> MANTID_DATAHANDLING_DLL DataType
getType<uint64_t>() {
return PredType::NATIVE_UINT64; }
47 hsize_t dims[] = {length};
48 return DataSpace(1, dims);
51template <
typename NumT> DataSpace
getDataSpace(
const std::vector<NumT> &data) {
57template <
typename NumT> H5::DataSet writeScalarDataSet(
Group &group,
const std::string &name,
const NumT &
value) {
58 static_assert(std::is_integral<NumT>::value || std::is_floating_point<NumT>::value,
59 "The writeNumAttribute function only accepts integral of "
60 "floating point values.");
61 auto dataType = getType<NumT>();
63 H5::DataSet data = group.createDataSet(name, dataType, dataSpace);
64 data.write(&
value, dataType);
69H5::DataSet writeScalarDataSet<std::string>(
Group &group,
const std::string &name,
const std::string &
value) {
70 StrType dataType(0,
value.length() + 1);
72 H5::DataSet data = group.createDataSet(name, dataType, dataSpace);
73 data.write(
value, dataType);
84 auto group = file.createGroup(name);
90 auto outGroup = group.createGroup(name);
95Group createGroupCanSAS(H5File &file,
const std::string &name,
const std::string &nxtype,
const std::string &cstype) {
108 DSetCreatPropList propList;
109 hsize_t chunk_dims[1] = {length};
110 propList.setChunk(1, chunk_dims);
111 propList.setDeflate(deflateLevel);
115template <
typename LocationType>
117 StrType attrType(0, H5T_VARIABLE);
118 DataSpace attrSpace(H5S_SCALAR);
119 auto attribute = location.createAttribute(name, attrType, attrSpace);
120 attribute.write(attrType,
value);
123template <
typename NumT,
typename LocationType>
125 static_assert(std::is_integral<NumT>::value || std::is_floating_point<NumT>::value,
126 "The writeNumAttribute function only accepts integral or "
127 "floating point values.");
128 auto attrType = getType<NumT>();
129 DataSpace attrSpace(H5S_SCALAR);
131 auto attribute = location.createAttribute(name, attrType, attrSpace);
133 std::array<NumT, 1> valueArray = {{
value}};
134 attribute.write(attrType, valueArray.data());
137template <
typename NumT,
typename LocationType>
139 static_assert(std::is_integral<NumT>::value || std::is_floating_point<NumT>::value,
140 "The writeNumAttribute function only accepts integral of "
141 "floating point values.");
142 auto attrType = getType<NumT>();
145 auto attribute = location.createAttribute(name, attrType, attrSpace);
146 attribute.write(attrType,
value.data());
149void write(
Group &group,
const std::string &name,
const std::string &
value) { writeScalarDataSet(group, name,
value); }
153 const std::map<std::string, std::string> &attributes) {
154 auto data = writeScalarDataSet(group, name,
value);
155 for (
const auto &attribute : attributes) {
160template <
typename NumT>
void writeArray1D(
Group &group,
const std::string &name,
const std::vector<NumT> &values) {
161 DataType dataType(getType<NumT>());
166 auto data = group.createDataSet(name, dataType, dataSpace, propList);
167 data.write(values.data(), dataType);
174std::string
readString(H5::H5File &file,
const std::string &path) {
176 auto data = file.openDataSet(path);
178 }
catch (H5::FileIException &e) {
181 }
catch (H5::GroupIException &e) {
187std::string
readString(H5::Group &group,
const std::string &name) {
189 auto data = group.openDataSet(name);
191 }
catch (H5::GroupIException &e) {
199 dataset.read(
value, dataset.getDataType(), dataset.getSpace());
212 std::vector<std::string> result;
214 DataSet dataset = group.openDataSet(name);
215 DataSpace dataspace = dataset.getSpace();
216 DataType datatype = dataset.getDataType();
218 dataspace.getSimpleExtentDims(dims,
nullptr);
220 rdata =
new char *[dims[0]];
221 dataset.read(rdata, datatype);
223 for (
size_t i = 0; i < dims[0]; ++i)
224 result.emplace_back(std::string(rdata[i]));
226 dataset.vlenReclaim(rdata, datatype, dataspace);
233template <
typename LocationType>
235 auto attribute = location.openAttribute(attributeName);
237 attribute.read(attribute.getDataType(),
value);
241template <
typename NumT> std::vector<NumT>
readArray1DCoerce(H5::Group &group,
const std::string &name) {
242 std::vector<NumT> result;
245 DataSet dataset = group.openDataSet(name);
246 result = readArray1DCoerce<NumT>(dataset);
247 }
catch (H5::GroupIException &e) {
250 }
catch (H5::DataTypeIException &e) {
259template <
typename InputNumT,
typename OutputNumT>
260std::vector<OutputNumT> convertingRead(DataSet &dataset,
const DataType &dataType) {
261 DataSpace dataSpace = dataset.getSpace();
263 std::vector<InputNumT> temp(dataSpace.getSelectNpoints());
264 dataset.read(temp.data(), dataType, dataSpace);
266 std::vector<OutputNumT> result;
267 result.resize(temp.size());
269 std::transform(temp.begin(), temp.end(), result.begin(),
270 [](
const InputNumT a) {
271 return boost::numeric_cast<OutputNumT>(a);
277template <
typename InputNumT,
typename OutputNumT>
278std::vector<OutputNumT> convertingNumArrayAttributeRead(
Attribute &attribute,
const DataType &dataType) {
279 DataSpace dataSpace = attribute.getSpace();
281 std::vector<InputNumT> temp(dataSpace.getSelectNpoints());
282 attribute.read(dataType, temp.data());
284 std::vector<OutputNumT> result;
285 result.resize(temp.size());
287 std::transform(temp.begin(), temp.end(), result.begin(),
288 [](
const InputNumT a) { return boost::numeric_cast<OutputNumT>(a); });
293template <
typename InputNumT,
typename OutputNumT>
294OutputNumT convertingRead(
Attribute &attribute,
const DataType &dataType) {
296 attribute.read(dataType, &temp);
297 auto result = boost::numeric_cast<OutputNumT>(temp);
303template <
typename NumT,
typename LocationType>
305 auto attribute = location.openAttribute(attributeName);
306 auto dataType = attribute.getDataType();
310 if (getType<NumT>() == dataType) {
311 attribute.read(dataType, &
value);
312 }
else if (PredType::NATIVE_INT32 == dataType) {
313 value = convertingRead<int32_t, NumT>(attribute, dataType);
314 }
else if (PredType::NATIVE_UINT32 == dataType) {
315 value = convertingRead<uint32_t, NumT>(attribute, dataType);
316 }
else if (PredType::NATIVE_INT64 == dataType) {
317 value = convertingRead<int64_t, NumT>(attribute, dataType);
318 }
else if (PredType::NATIVE_UINT64 == dataType) {
319 value = convertingRead<uint64_t, NumT>(attribute, dataType);
320 }
else if (PredType::NATIVE_FLOAT == dataType) {
321 value = convertingRead<float, NumT>(attribute, dataType);
322 }
else if (PredType::NATIVE_DOUBLE == dataType) {
323 value = convertingRead<double, NumT>(attribute, dataType);
326 throw DataTypeIException();
331template <
typename NumT,
typename LocationType>
333 auto attribute = location.openAttribute(attributeName);
334 auto dataType = attribute.getDataType();
336 std::vector<NumT>
value;
338 if (getType<NumT>() == dataType) {
339 DataSpace dataSpace = attribute.getSpace();
340 value.resize(dataSpace.getSelectNpoints());
341 attribute.read(dataType,
value.data());
342 }
else if (PredType::NATIVE_INT32 == dataType) {
343 value = convertingNumArrayAttributeRead<int32_t, NumT>(attribute, dataType);
344 }
else if (PredType::NATIVE_UINT32 == dataType) {
345 value = convertingNumArrayAttributeRead<uint32_t, NumT>(attribute, dataType);
346 }
else if (PredType::NATIVE_INT64 == dataType) {
347 value = convertingNumArrayAttributeRead<int64_t, NumT>(attribute, dataType);
348 }
else if (PredType::NATIVE_UINT64 == dataType) {
349 value = convertingNumArrayAttributeRead<uint64_t, NumT>(attribute, dataType);
350 }
else if (PredType::NATIVE_FLOAT == dataType) {
351 value = convertingNumArrayAttributeRead<float, NumT>(attribute, dataType);
352 }
else if (PredType::NATIVE_DOUBLE == dataType) {
353 value = convertingNumArrayAttributeRead<double, NumT>(attribute, dataType);
356 throw DataTypeIException();
362 DataType dataType = dataset.getDataType();
364 if (getType<NumT>() == dataType) {
365 std::vector<NumT> result;
366 DataSpace dataSpace = dataset.getSpace();
367 result.resize(dataSpace.getSelectNpoints());
368 dataset.read(result.data(), dataType, dataSpace);
372 if (PredType::NATIVE_INT32 == dataType) {
373 return convertingRead<int32_t, NumT>(dataset, dataType);
374 }
else if (PredType::NATIVE_UINT32 == dataType) {
375 return convertingRead<uint32_t, NumT>(dataset, dataType);
376 }
else if (PredType::NATIVE_INT64 == dataType) {
377 return convertingRead<int64_t, NumT>(dataset, dataType);
378 }
else if (PredType::NATIVE_UINT64 == dataType) {
379 return convertingRead<uint64_t, NumT>(dataset, dataType);
380 }
else if (PredType::NATIVE_FLOAT == dataType) {
381 return convertingRead<float, NumT>(dataset, dataType);
382 }
else if (PredType::NATIVE_DOUBLE == dataType) {
383 return convertingRead<double, NumT>(dataset, dataType);
387 throw DataTypeIException();
393template MANTID_DATAHANDLING_DLL
void writeStrAttribute(H5::Group &location,
const std::string &name,
394 const std::string &
value);
396template MANTID_DATAHANDLING_DLL
void writeStrAttribute(H5::DataSet &location,
const std::string &name,
397 const std::string &
value);
403template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
405template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
407template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
408 const double &
value);
409template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
410 const double &
value);
411template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
412 const int32_t &
value);
413template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
414 const int32_t &
value);
415template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
416 const uint32_t &
value);
417template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
418 const uint32_t &
value);
419template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
420 const int64_t &
value);
421template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
422 const int64_t &
value);
423template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
424 const uint64_t &
value);
425template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
426 const uint64_t &
value);
428template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
429 const std::vector<float> &
value);
430template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
431 const std::vector<float> &
value);
432template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
433 const std::vector<double> &
value);
434template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
435 const std::vector<double> &
value);
436template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
437 const std::vector<int32_t> &
value);
438template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
439 const std::vector<int32_t> &
value);
440template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
441 const std::vector<uint32_t> &
value);
442template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
443 const std::vector<uint32_t> &
value);
444template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
445 const std::vector<int64_t> &
value);
446template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
447 const std::vector<int64_t> &
value);
448template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::Group &location,
const std::string &name,
449 const std::vector<uint64_t> &
value);
450template MANTID_DATAHANDLING_DLL
void writeNumAttribute(H5::DataSet &location,
const std::string &name,
451 const std::vector<uint64_t> &
value);
457 const std::string &attributeName);
460 const std::string &attributeName);
467template MANTID_DATAHANDLING_DLL
double readNumAttributeCoerce(H5::Group &location,
const std::string &attributeName);
468template MANTID_DATAHANDLING_DLL
double readNumAttributeCoerce(H5::DataSet &location,
const std::string &attributeName);
469template MANTID_DATAHANDLING_DLL int32_t
readNumAttributeCoerce(H5::Group &location,
const std::string &attributeName);
471 const std::string &attributeName);
472template MANTID_DATAHANDLING_DLL uint32_t
readNumAttributeCoerce(H5::Group &location,
const std::string &attributeName);
474 const std::string &attributeName);
475template MANTID_DATAHANDLING_DLL int64_t
readNumAttributeCoerce(H5::Group &location,
const std::string &attributeName);
477 const std::string &attributeName);
478template MANTID_DATAHANDLING_DLL uint64_t
readNumAttributeCoerce(H5::Group &location,
const std::string &attributeName);
480 const std::string &attributeName);
486 const std::string &attributeName);
488 const std::string &attributeName);
490 const std::string &attributeName);
492 const std::string &attributeName);
494 const std::string &attributeName);
496 const std::string &attributeName);
498 const std::string &attributeName);
500 const std::string &attributeName);
502 const std::string &attributeName);
504 const std::string &attributeName);
506 const std::string &attributeName);
508 const std::string &attributeName);
513template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
514 const std::vector<float> &values);
515template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
516 const std::vector<double> &values);
517template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
518 const std::vector<int32_t> &values);
519template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
520 const std::vector<uint32_t> &values);
521template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
522 const std::vector<int64_t> &values);
523template MANTID_DATAHANDLING_DLL
void writeArray1D(H5::Group &group,
const std::string &name,
524 const std::vector<uint64_t> &values);
529template MANTID_DATAHANDLING_DLL
void
531 const std::map<std::string, std::string> &attributes);
532template MANTID_DATAHANDLING_DLL
void
534 const std::map<std::string, std::string> &attributes);
535template MANTID_DATAHANDLING_DLL
void
537 const std::map<std::string, std::string> &attributes);
538template MANTID_DATAHANDLING_DLL
void
540 const std::map<std::string, std::string> &attributes);
541template MANTID_DATAHANDLING_DLL
void
543 const std::map<std::string, std::string> &attributes);
544template MANTID_DATAHANDLING_DLL
void
546 const std::map<std::string, std::string> &attributes);
547template MANTID_DATAHANDLING_DLL
void
549 const std::map<std::string, std::string> &attributes);
554template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<float> &data);
555template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<double> &data);
556template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<int32_t> &data);
557template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<uint32_t> &data);
558template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<int64_t> &data);
559template MANTID_DATAHANDLING_DLL DataSpace
getDataSpace(
const std::vector<uint64_t> &data);
564template MANTID_DATAHANDLING_DLL std::vector<float>
readArray1DCoerce(H5::Group &group,
const std::string &name);
565template MANTID_DATAHANDLING_DLL std::vector<double>
readArray1DCoerce(H5::Group &group,
const std::string &name);
566template MANTID_DATAHANDLING_DLL std::vector<int32_t>
readArray1DCoerce(H5::Group &group,
const std::string &name);
567template MANTID_DATAHANDLING_DLL std::vector<uint32_t>
readArray1DCoerce(H5::Group &group,
const std::string &name);
568template MANTID_DATAHANDLING_DLL std::vector<int64_t>
readArray1DCoerce(H5::Group &group,
const std::string &name);
569template MANTID_DATAHANDLING_DLL std::vector<uint64_t>
readArray1DCoerce(H5::Group &group,
const std::string &name);
double value
The value of the point.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Attribute is a non-fitting parameter.
The class Group represents a set of symmetry operations (or symmetry group).
The Logger class is in charge of the publishing messages from the framework through various channels.
void information(const std::string &msg)
Logs at information level.
Kernel::Logger g_log("ExperimentInfo")
static logger object
MANTID_DATAHANDLING_DLL H5::DSetCreatPropList setCompressionAttributes(const std::size_t length, const int deflateLevel=6)
Sets up the chunking and compression rate.
MANTID_DATAHANDLING_DLL DataType getType< int32_t >()
template MANTID_DATAHANDLING_DLL std::vector< double > readArray1DCoerce< double >(DataSet &dataset)
MANTID_DATAHANDLING_DLL DataType getType< uint64_t >()
MANTID_DATAHANDLING_DLL std::string readString(H5::H5File &file, const std::string &path)
MANTID_DATAHANDLING_DLL std::vector< std::string > readStringVector(H5::Group &, const std::string &)
MANTID_DATAHANDLING_DLL H5::Group createGroupCanSAS(H5::Group &group, const std::string &name, const std::string &nxtype, const std::string &cstype)
template MANTID_DATAHANDLING_DLL std::vector< int64_t > readArray1DCoerce< int64_t >(DataSet &dataset)
void writeNumAttribute(LocationType &location, const std::string &name, const NumT &value)
MANTID_DATAHANDLING_DLL DataType getType< int64_t >()
template MANTID_DATAHANDLING_DLL std::vector< float > readArray1DCoerce< float >(DataSet &dataset)
void writeArray1D(H5::Group &group, const std::string &name, const std::vector< NumT > &values)
MANTID_DATAHANDLING_DLL H5::DataSpace getDataSpace(const size_t length)
H5Util : TODO: DESCRIPTION.
MANTID_DATAHANDLING_DLL H5::Group createGroupNXS(H5::H5File &file, const std::string &name, const std::string &nxtype)
std::string readAttributeAsString(LocationType &dataset, const std::string &attributeName)
NumT readNumAttributeCoerce(LocationType &location, const std::string &attributeName)
MANTID_DATAHANDLING_DLL void write(H5::Group &group, const std::string &name, const std::string &value)
MANTID_DATAHANDLING_DLL DataType getType< uint32_t >()
std::vector< NumT > readNumArrayAttributeCoerce(LocationType &location, const std::string &attributeName)
template MANTID_DATAHANDLING_DLL std::vector< int32_t > readArray1DCoerce< int32_t >(DataSet &dataset)
void writeStrAttribute(LocationType &location, const std::string &name, const std::string &value)
template MANTID_DATAHANDLING_DLL std::vector< uint64_t > readArray1DCoerce< uint64_t >(DataSet &dataset)
std::vector< NumT > readArray1DCoerce(H5::Group &group, const std::string &name)
void writeScalarDataSetWithStrAttributes(H5::Group &group, const std::string &name, const T &value, const std::map< std::string, std::string > &attributes)
MANTID_DATAHANDLING_DLL DataType getType< double >()
H5::DataType getType()
Convert a primitive type to the appropriate H5::DataType.
MANTID_DATAHANDLING_DLL DataType getType< float >()
template MANTID_DATAHANDLING_DLL std::vector< uint32_t > readArray1DCoerce< uint32_t >(DataSet &dataset)