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>
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));
214 Array<Complex> arr(shape);
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));
242 Array<DComplex> arr(shape);
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);
281 throw AipsError (
"PycArray: python object is not an array");
283 PyArrayObject* po = (PyArrayObject*)obj_ptr;
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);
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");
static NPY_TYPES pyType()
Array< String > ArrayCopyStr_toArray(const IPosition &shape, void *data, size_t slen)
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
casacore::Float casa_type
static NPY_TYPES pyType()
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
StatsData< AccumType > copy(const StatsData< AccumType > &stats)
std::complex< Float > Complex
npy_complex64 python_type
static NPY_TYPES pyType()
unsigned long long uInt64
static NPY_TYPES pyType()
Copy/convert the array data as needed.
casacore::DComplex casa_type
static NPY_TYPES pyType()
casacore::uInt64 casa_type
casacore::Short casa_type
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
casacore::Int64 casa_type
static NPY_TYPES pyType()
static void toPy(void *to, const T *from, size_t nr)
std::string object(const std::string &name)
static NPY_TYPES pyType()
std::complex< Double > DComplex
static NPY_TYPES pyType()
casacore::uShort casa_type
casacore::Complex casa_type
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Bool isImported()
Check if the API is or can be imported.
bool Bool
Define the standard types used by Casacore.
static NPY_TYPES pyType()
static NPY_TYPES pyType()
casacore::String casa_type
static NPY_TYPES pyType()
ValueHolder makeArray(PyObject *obj_ptr, Bool copyData)
Convert the python array to a Casacore array in the ValueHolder.
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape...
static NPY_TYPES pyType()
static Array< T > toArray(const IPosition &shape, void *data, bool copy)
npy_complex128 python_type
static void fromPy(T *to, const void *from, size_t nr)
Bool PycArrayCheck(PyObject *obj_ptr)
Check if the PyObject is an array object.
casacore::uChar casa_type
boost::python::object makePyArrayObject(casacore::Array< T > const &arr)
Convert a Casacore array to a Python array object.
static NPY_TYPES pyType()
String: the storage and methods of handling collections of characters.
static NPY_TYPES pyType()
casacore::Double casa_type
static NPY_TYPES pyType()
Array< String > ArrayCopyUnicode_toArray(const IPosition &shape, void *data, size_t slen)
#define casacore
<X11/Intrinsic.h> #defines true, false, casacore::Bool, and String.