23 #include "openPMD/Datatype.hpp" 24 #include "openPMD/binding/python/Variant.hpp" 26 #include <pybind11/numpy.h> 27 #include <pybind11/pybind11.h> 28 #include <pybind11/stl.h> 35 inline Datatype dtype_from_numpy(pybind11::dtype
const dt)
39 if (dt.is(pybind11::dtype(
"b")))
40 return Datatype::CHAR;
41 else if (dt.is(pybind11::dtype(
"B")))
42 return Datatype::UCHAR;
43 else if (dt.is(pybind11::dtype(
"short")))
44 return Datatype::SHORT;
45 else if (dt.is(pybind11::dtype(
"intc")))
47 else if (dt.is(pybind11::dtype(
"int_")))
48 return Datatype::LONG;
49 else if (dt.is(pybind11::dtype(
"longlong")))
50 return Datatype::LONGLONG;
51 else if (dt.is(pybind11::dtype(
"ushort")))
52 return Datatype::USHORT;
53 else if (dt.is(pybind11::dtype(
"uintc")))
54 return Datatype::UINT;
55 else if (dt.is(pybind11::dtype(
"uint")))
56 return Datatype::ULONG;
57 else if (dt.is(pybind11::dtype(
"ulonglong")))
58 return Datatype::ULONGLONG;
59 else if (dt.is(pybind11::dtype(
"clongdouble")))
60 return Datatype::CLONG_DOUBLE;
61 else if (dt.is(pybind11::dtype(
"cdouble")))
62 return Datatype::CDOUBLE;
63 else if (dt.is(pybind11::dtype(
"csingle")))
64 return Datatype::CFLOAT;
65 else if (dt.is(pybind11::dtype(
"longdouble")))
66 return Datatype::LONG_DOUBLE;
67 else if (dt.is(pybind11::dtype(
"double")))
68 return Datatype::DOUBLE;
69 else if (dt.is(pybind11::dtype(
"single")))
70 return Datatype::FLOAT;
71 else if (dt.is(pybind11::dtype(
"bool")))
72 return Datatype::BOOL;
76 throw std::runtime_error(
77 "Datatype '...' not known in 'dtype_from_numpy'!");
92 if (fmt.find(
"?") != std::string::npos)
94 else if (fmt.find(
"b") != std::string::npos)
96 else if (fmt.find(
"h") != std::string::npos)
98 else if (fmt.find(
"i") != std::string::npos)
100 else if (fmt.find(
"l") != std::string::npos)
102 else if (fmt.find(
"q") != std::string::npos)
104 else if (fmt.find(
"B") != std::string::npos)
106 else if (fmt.find(
"H") != std::string::npos)
108 else if (fmt.find(
"I") != std::string::npos)
110 else if (fmt.find(
"L") != std::string::npos)
112 else if (fmt.find(
"Q") != std::string::npos)
113 return DT::ULONGLONG;
114 else if (fmt.find(
"Zf") != std::string::npos)
116 else if (fmt.find(
"Zd") != std::string::npos)
118 else if (fmt.find(
"Zg") != std::string::npos)
119 return DT::CLONG_DOUBLE;
120 else if (fmt.find(
"f") != std::string::npos)
122 else if (fmt.find(
"d") != std::string::npos)
124 else if (fmt.find(
"g") != std::string::npos)
125 return DT::LONG_DOUBLE;
127 throw std::runtime_error(
128 "dtype_from_bufferformat: Unknown " 133 inline pybind11::dtype dtype_to_numpy(
Datatype const dt)
142 return pybind11::dtype(
"b");
146 return pybind11::dtype(
"B");
154 return pybind11::dtype(
"short");
158 return pybind11::dtype(
"intc");
162 return pybind11::dtype(
"int_");
165 case DT::VEC_LONGLONG:
166 return pybind11::dtype(
"longlong");
170 return pybind11::dtype(
"ushort");
174 return pybind11::dtype(
"uintc");
178 return pybind11::dtype(
"uint");
181 case DT::VEC_ULONGLONG:
182 return pybind11::dtype(
"ulonglong");
186 return pybind11::dtype(
"single");
191 return pybind11::dtype(
"double");
193 case DT::LONG_DOUBLE:
194 case DT::VEC_LONG_DOUBLE:
195 return pybind11::dtype(
"longdouble");
199 return pybind11::dtype(
"csingle");
202 case DT::VEC_CDOUBLE:
203 return pybind11::dtype(
"cdouble");
205 case DT::CLONG_DOUBLE:
206 case DT::VEC_CLONG_DOUBLE:
207 return pybind11::dtype(
"clongdouble");
210 return pybind11::dtype(
"bool");
215 throw std::runtime_error(
216 "dtype_to_numpy: Invalid Datatype '{...}'!");
Datatype dtype_from_bufferformat(std::string const &fmt)
Return openPMD::Datatype from py::buffer_info::format.
Definition: Numpy.hpp:83
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
Public definitions of openPMD-api.
Definition: Date.cpp:28