Mantid
Loading...
Searching...
No Matches
hdf5_type_helper.cpp
Go to the documentation of this file.
2#include <assert.h>
3#include <cstring>
4#include <map>
5#include <memory>
6#include <stdlib.h>
7#include <string.h>
8#include <time.h>
9
10NXnumtype hdf5ToNXType(H5T_class_t tclass, hid_t atype) {
11 // NOTE this is exploiting the bit-level representation of NXnumtype
12 // where the first hex stands for: 2 = float, 1 = signed int, 0 = unsigned int
13 // and where the second hex is the width in bytes (which is given by H5Tget_size)
14 // and where CHAR (0XF0) and BINARY (0xF1) are special values
15 unsigned short size = static_cast<unsigned short>(H5Tget_size(atype));
17 if (tclass == H5T_STRING) {
18 iPtype = NXnumtype::CHAR;
19 } else if (tclass == H5T_BITFIELD) {
20 // underused datatype, would be great for masks
21 iPtype = NXnumtype::BINARY;
22 } else if (tclass == H5T_INTEGER) {
23 unsigned short type = size;
24 H5T_sign_t sign = H5Tget_sign(atype);
25 if (sign == H5T_SGN_2) {
26 type += 0x10; // signed data type
27 }
28 iPtype = NXnumtype(type);
29 } else if (tclass == H5T_FLOAT) {
30 iPtype = NXnumtype(0x20u + size);
31 }
32 return iPtype;
33}
34
36 hid_t type;
37 switch (datatype) {
38 case NXnumtype::CHAR: {
39 type = H5T_C_S1;
40 break;
41 }
42 case NXnumtype::INT8: {
43 type = H5T_NATIVE_CHAR;
44 break;
45 }
46 case NXnumtype::UINT8: {
47 type = H5T_NATIVE_UCHAR;
48 break;
49 }
50 case NXnumtype::INT16: {
51 type = H5T_NATIVE_SHORT;
52 break;
53 }
54 case NXnumtype::UINT16: {
55 type = H5T_NATIVE_USHORT;
56 break;
57 }
58 case NXnumtype::INT32: {
59 type = H5T_NATIVE_INT;
60 break;
61 }
62 case NXnumtype::UINT32: {
63 type = H5T_NATIVE_UINT;
64 break;
65 }
66 case NXnumtype::INT64: {
67 type = H5T_NATIVE_INT64;
68 break;
69 }
70 case NXnumtype::UINT64: {
71 type = H5T_NATIVE_UINT64;
72 break;
73 }
74 case NXnumtype::FLOAT32: {
75 type = H5T_NATIVE_FLOAT;
76 break;
77 }
78 case NXnumtype::FLOAT64: {
79 type = H5T_NATIVE_DOUBLE;
80 break;
81 }
82 default: {
83 type = -1;
84 }
85 }
86 return type;
87}
88
90 // NOTE is this function actually necessary?
91 hid_t memtype_id = -1;
92 size_t size;
93 H5T_sign_t sign;
94 H5T_class_t tclass;
95
96 tclass = H5Tget_class(atype);
97
98 if (tclass == H5T_INTEGER) {
99 size = H5Tget_size(atype);
100 sign = H5Tget_sign(atype);
101 if (size == 1) {
102 if (sign == H5T_SGN_2) {
103 memtype_id = H5T_NATIVE_INT8;
104 } else {
105 memtype_id = H5T_NATIVE_UINT8;
106 }
107 } else if (size == 2) {
108 if (sign == H5T_SGN_2) {
109 memtype_id = H5T_NATIVE_INT16;
110 } else {
111 memtype_id = H5T_NATIVE_UINT16;
112 }
113 } else if (size == 4) {
114 if (sign == H5T_SGN_2) {
115 memtype_id = H5T_NATIVE_INT32;
116 } else {
117 memtype_id = H5T_NATIVE_UINT32;
118 }
119 } else if (size == 8) {
120 if (sign == H5T_SGN_2) {
121 memtype_id = H5T_NATIVE_INT64;
122 } else {
123 memtype_id = H5T_NATIVE_UINT64;
124 }
125 }
126 } else if (tclass == H5T_FLOAT) {
127 size = H5Tget_size(atype);
128 if (size == 4) {
129 memtype_id = H5T_NATIVE_FLOAT;
130 } else if (size == 8) {
131 memtype_id = H5T_NATIVE_DOUBLE;
132 }
133 }
134 return memtype_id;
135}
int64_t hid_t
This class defines data types which are used as part of the Nexus API.
The primitive types published by this API.
static unsigned short constexpr UINT16
static unsigned short constexpr UINT64
static unsigned short constexpr INT8
static unsigned short constexpr INT64
static unsigned short constexpr INT16
static unsigned short constexpr UINT32
static unsigned short constexpr CHAR
static unsigned short constexpr BINARY
static unsigned short constexpr UINT8
static unsigned short constexpr INT32
static unsigned short constexpr FLOAT32
static unsigned short constexpr BAD
static unsigned short constexpr FLOAT64
hid_t nxToHDF5Type(NXnumtype datatype)
NXnumtype hdf5ToNXType(H5T_class_t tclass, hid_t atype)
hid_t h5MemType(hid_t atype)