27 #include <boost/python.hpp>
28 #include <numpy/arrayobject.h>
30 #if PY_MAJOR_VERSION >= 3
41 return PyArray_Check (obj_ptr);
46 using namespace boost::python;
48 const char* modStr =
"modules";
49 PyObject* mods = PySys_GetObject(
const_cast<char*
>(modStr));
50 dict d = extract<dict>(mods)();
69 {
throw AipsError (
"PycArray: unknown casa type"); }
74 static NPY_TYPES
pyType() {
return NPY_BOOL; }
79 static NPY_TYPES
pyType() {
return NPY_UINT16; }
84 static NPY_TYPES
pyType() {
return NPY_INT16; }
89 static NPY_TYPES
pyType() {
return NPY_UINT16; }
94 static NPY_TYPES
pyType() {
return NPY_INT32; }
99 static NPY_TYPES
pyType() {
return NPY_UINT32; }
104 static NPY_TYPES
pyType() {
return NPY_INT64; }
109 static NPY_TYPES
pyType() {
return NPY_UINT64; }
114 static NPY_TYPES
pyType() {
return NPY_FLOAT32; }
119 static NPY_TYPES
pyType() {
return NPY_FLOAT64; }
124 static NPY_TYPES
pyType() {
return NPY_COMPLEX64; }
129 static NPY_TYPES
pyType() {
return NPY_COMPLEX128; }
134 static NPY_TYPES
pyType() {
return NPY_OBJECT; }
142 {
throw AipsError (
"PycArray: unknown casa type"); }
146 template <
typename T>
150 ::memcpy (to, from, nr*
sizeof(T));
154 for (
size_t i=0; i<nr; i++) {
159 template <
typename T>
163 ::memcpy (to, from, nr*
sizeof(T));
167 for (
size_t i=0; i<nr; i++) {
172 template <
typename T>
174 void* data,
bool copy)
183 return Array<T> (
shape,
static_cast<T*
>(data),
SHARE);
187 fromPy (arr.data(), data, arr.size());
195 throw AipsError(
"PycArray: size of Complex data type mismatches");
197 ::memcpy (to, from, nr*
sizeof(
Complex));
202 throw AipsError(
"PycArray: size of Complex data type mismatches");
204 ::memcpy (to, from, nr*
sizeof(
Complex));
207 void* data,
bool copy)
214 Array<Complex> arr(
shape);
215 fromPy (arr.data(), data, arr.size());
223 throw AipsError(
"PycArray: size of DComplex data type mismatches");
225 ::memcpy (to, from, nr*
sizeof(
DComplex));
230 throw AipsError(
"PycArray: size of DComplex data type mismatches");
232 ::memcpy (to, from, nr*
sizeof(
DComplex));
235 void* data,
bool copy)
242 Array<DComplex> arr(
shape);
243 fromPy (arr.data(), data, arr.size());
250 PyObject** dst =
static_cast<PyObject**
>(to);
251 for (
size_t i=0; i<nr; i++) {
253 dst[i] = PyUnicode_FromString(from[i].chars());
255 dst[i] = PyString_FromString(from[i].chars());
261 using namespace boost::python;
262 PyObject** src = (PyObject**)from;
263 for (
size_t i=0; i<nr; i++) {
264 handle<> py_elem_hdl(src[i]);
265 object py_elem_obj(py_elem_hdl);
266 extract<String> elem_proxy(py_elem_obj);
267 to[i] = elem_proxy();
273 Array<String> arr(
shape);
274 fromPy (arr.data(), data, arr.size());
281 throw AipsError (
"PycArray: python object is not an array");
283 PyArrayObject* po = (PyArrayObject*)obj_ptr;
284 boost::python::object obj;
285 bool docopy = copyData;
286 if (! PyArray_ISCONTIGUOUS(po)
287 || ! PyArray_ISALIGNED(po)
288 || PyArray_ISBYTESWAPPED(po)) {
289 boost::python::handle<> py_hdl(obj_ptr);
290 boost::python::object py_obj(py_hdl);
292 boost::python::incref(obj_ptr);
293 obj = py_obj.attr(
"copy")();
294 po = (PyArrayObject*)(obj.ptr());
299 int nd = PyArray_NDIM(po);
303 for (
int i=0; i<nd; i++) {
304 shp[i] = PyArray_DIMS(po)[nd-i-1];
309 if (shp.product() > 0) {
312 && !PyArray_ISBYTESWAPPED(po), AipsError);
315 switch (PyArray_TYPE(po)) {
344 if (PyArray_TYPE(po) == NPY_UINT64) {
346 Array<Int64> res(arr.shape());
347 convertArray (res, arr);
348 return ValueHolder(res);
349 }
else if (PyArray_TYPE(po) == NPY_INT8) {
351 Array<Short> res(arr.shape());
352 convertArray (res, arr);
353 return ValueHolder(res);
354 }
else if (PyArray_TYPE(po) == NPY_UINT8) {
357 Array<Short> res(arr.shape());
359 Array<uChar>* uarr =
static_cast<Array<uChar>*
>(varr);
360 convertArray (res, *uarr);
361 return ValueHolder(res);
362 }
else if (PyArray_TYPE(po) == NPY_STRING) {
365 slen = PyArray_STRIDES(po)[nd-1];
368 }
else if (PyArray_TYPE(po) == NPY_UNICODE) {
371 slen = PyArray_STRIDES(po)[nd-1];
377 throw AipsError (
"PycArray: unknown python array data type");
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
template boost::python::object makePyArrayObject(casacore::Array< Bool > const &arr)
Bool PycArrayCheck(PyObject *obj_ptr)
Check if the PyObject is an array object.
Bool isImported()
Check if the API is or can be imported.
ValueHolder makeArray(PyObject *obj_ptr, Bool copyData)
Convert the python array to a Casacore array in the ValueHolder.
Array< String > ArrayCopyStr_toArray(const IPosition &shape, void *data, size_t slen)
Array< String > ArrayCopyUnicode_toArray(const IPosition &shape, void *data, size_t slen)
String: the storage and methods of handling collections of characters.
@ SHARE
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
std::complex< Float > Complex
std::complex< Double > DComplex
this file contains all the compiler specific defines
StatsData< AccumType > copy(const StatsData< AccumType > &stats)
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
bool Bool
Define the standard types used by Casacore.
unsigned long long uInt64
Copy/convert the array data as needed.
static void toPy(void *to, const T *from, size_t nr)
static void fromPy(T *to, const void *from, size_t nr)
static Array< T > toArray(const IPosition &shape, void *data, bool copy)
static NPY_TYPES pyType()
static NPY_TYPES pyType()
casacore::Complex casa_type
npy_complex64 python_type
npy_complex128 python_type
static NPY_TYPES pyType()
casacore::DComplex casa_type
static NPY_TYPES pyType()
casacore::Double casa_type
static NPY_TYPES pyType()
casacore::Float casa_type
casacore::Int64 casa_type
static NPY_TYPES pyType()
static NPY_TYPES pyType()
static NPY_TYPES pyType()
casacore::Short casa_type
static NPY_TYPES pyType()
casacore::String casa_type
casacore::uChar casa_type
static NPY_TYPES pyType()
static NPY_TYPES pyType()
casacore::uInt64 casa_type
static NPY_TYPES pyType()
static NPY_TYPES pyType()
casacore::uShort casa_type
static NPY_TYPES pyType()
static NPY_TYPES pyType()