23 #include <pybind11/pybind11.h> 24 #include <pybind11/stl.h> 25 #include <pybind11/numpy.h> 27 #include "openPMD/Datatype.hpp" 36 dtype_from_numpy( pybind11::dtype
const dt )
40 if( dt.is(pybind11::dtype(
"b")) )
41 return Datatype::CHAR;
42 else if( dt.is(pybind11::dtype(
"B")) )
43 return Datatype::UCHAR;
44 else if( dt.is(pybind11::dtype(
"short")) )
45 return Datatype::SHORT;
46 else if( dt.is(pybind11::dtype(
"intc")) )
48 else if( dt.is(pybind11::dtype(
"int_")) )
49 return Datatype::LONG;
50 else if( dt.is(pybind11::dtype(
"longlong")) )
51 return Datatype::LONGLONG;
52 else if( dt.is(pybind11::dtype(
"ushort")) )
53 return Datatype::USHORT;
54 else if( dt.is(pybind11::dtype(
"uintc")) )
55 return Datatype::UINT;
56 else if( dt.is(pybind11::dtype(
"uint")) )
57 return Datatype::ULONG;
58 else if( dt.is(pybind11::dtype(
"ulonglong")) )
59 return Datatype::ULONGLONG;
60 else if( dt.is(pybind11::dtype(
"clongdouble")) )
61 return Datatype::CLONG_DOUBLE;
62 else if( dt.is(pybind11::dtype(
"cdouble")) )
63 return Datatype::CDOUBLE;
64 else if( dt.is(pybind11::dtype(
"csingle")) )
65 return Datatype::CFLOAT;
66 else if( dt.is(pybind11::dtype(
"longdouble")) )
67 return Datatype::LONG_DOUBLE;
68 else if( dt.is(pybind11::dtype(
"double")) )
69 return Datatype::DOUBLE;
70 else if( dt.is(pybind11::dtype(
"single")) )
71 return Datatype::FLOAT;
72 else if( dt.is(pybind11::dtype(
"bool")) )
73 return Datatype::BOOL;
76 throw std::runtime_error(
"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(
"dtype_from_bufferformat: Unknown " 128 "Python type '" + fmt +
"'");
131 inline pybind11::dtype
141 return pybind11::dtype(
"b");
145 return pybind11::dtype(
"B");
153 return pybind11::dtype(
"short");
157 return pybind11::dtype(
"intc");
161 return pybind11::dtype(
"int_");
164 case DT::VEC_LONGLONG:
165 return pybind11::dtype(
"longlong");
169 return pybind11::dtype(
"ushort");
173 return pybind11::dtype(
"uintc");
177 return pybind11::dtype(
"uint");
180 case DT::VEC_ULONGLONG:
181 return pybind11::dtype(
"ulonglong");
185 return pybind11::dtype(
"single");
190 return pybind11::dtype(
"double");
192 case DT::LONG_DOUBLE:
193 case DT::VEC_LONG_DOUBLE:
194 return pybind11::dtype(
"longdouble");
198 return pybind11::dtype(
"csingle");
201 case DT::VEC_CDOUBLE:
202 return pybind11::dtype(
"cdouble");
204 case DT::CLONG_DOUBLE:
205 case DT::VEC_CLONG_DOUBLE:
206 return pybind11::dtype(
"clongdouble");
209 return pybind11::dtype(
"bool");
214 throw std::runtime_error(
"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:42
Public definitions of openPMD-api.
Definition: Date.cpp:28