23 #include "openPMD/Datatype.hpp" 24 #include "openPMD/binding/python/Variant.hpp" 26 #include <pybind11/pybind11.h> 27 #include <pybind11/stl.h> 28 #include <pybind11/numpy.h> 37 dtype_from_numpy( pybind11::dtype
const dt )
41 if( dt.is(pybind11::dtype(
"b")) )
42 return Datatype::CHAR;
43 else if( dt.is(pybind11::dtype(
"B")) )
44 return Datatype::UCHAR;
45 else if( dt.is(pybind11::dtype(
"short")) )
46 return Datatype::SHORT;
47 else if( dt.is(pybind11::dtype(
"intc")) )
49 else if( dt.is(pybind11::dtype(
"int_")) )
50 return Datatype::LONG;
51 else if( dt.is(pybind11::dtype(
"longlong")) )
52 return Datatype::LONGLONG;
53 else if( dt.is(pybind11::dtype(
"ushort")) )
54 return Datatype::USHORT;
55 else if( dt.is(pybind11::dtype(
"uintc")) )
56 return Datatype::UINT;
57 else if( dt.is(pybind11::dtype(
"uint")) )
58 return Datatype::ULONG;
59 else if( dt.is(pybind11::dtype(
"ulonglong")) )
60 return Datatype::ULONGLONG;
61 else if( dt.is(pybind11::dtype(
"clongdouble")) )
62 return Datatype::CLONG_DOUBLE;
63 else if( dt.is(pybind11::dtype(
"cdouble")) )
64 return Datatype::CDOUBLE;
65 else if( dt.is(pybind11::dtype(
"csingle")) )
66 return Datatype::CFLOAT;
67 else if( dt.is(pybind11::dtype(
"longdouble")) )
68 return Datatype::LONG_DOUBLE;
69 else if( dt.is(pybind11::dtype(
"double")) )
70 return Datatype::DOUBLE;
71 else if( dt.is(pybind11::dtype(
"single")) )
72 return Datatype::FLOAT;
73 else if( dt.is(pybind11::dtype(
"bool")) )
74 return Datatype::BOOL;
77 throw std::runtime_error(
"Datatype '...' not known in 'dtype_from_numpy'!");
93 if( fmt.find(
"?") != std::string::npos )
95 else if( fmt.find(
"b") != std::string::npos )
97 else if( fmt.find(
"h") != std::string::npos )
99 else if( fmt.find(
"i") != std::string::npos )
101 else if( fmt.find(
"l") != std::string::npos )
103 else if( fmt.find(
"q") != std::string::npos )
105 else if( fmt.find(
"B") != std::string::npos )
107 else if( fmt.find(
"H") != std::string::npos )
109 else if( fmt.find(
"I") != std::string::npos )
111 else if( fmt.find(
"L") != std::string::npos )
113 else if( fmt.find(
"Q") != std::string::npos )
114 return DT::ULONGLONG;
115 else if( fmt.find(
"Zf") != std::string::npos )
117 else if( fmt.find(
"Zd") != std::string::npos )
119 else if( fmt.find(
"Zg") != std::string::npos )
120 return DT::CLONG_DOUBLE;
121 else if( fmt.find(
"f") != std::string::npos )
123 else if( fmt.find(
"d") != std::string::npos )
125 else if( fmt.find(
"g") != std::string::npos )
126 return DT::LONG_DOUBLE;
128 throw std::runtime_error(
"dtype_from_bufferformat: Unknown " 129 "Python type '" + fmt +
"'");
132 inline pybind11::dtype
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(
"dtype_to_numpy: Invalid Datatype '{...}'!");
Datatype dtype_from_bufferformat(std::string const &fmt)
Return openPMD::Datatype from py::buffer_info::format.
Definition: Numpy.hpp:84
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:42
Public definitions of openPMD-api.
Definition: Date.cpp:29