28 #if PY_MAJOR_VERSION >= 3 39 return PyArray_Check (obj_ptr);
44 using namespace boost::python;
46 const char* modStr =
"modules";
47 PyObject* mods = PySys_GetObject(const_cast<char*>(modStr));
48 dict d = extract<dict>(mods)();
67 {
throw AipsError (
"PycArray: unknown casa type"); }
72 static NPY_TYPES
pyType() {
return NPY_BOOL; }
77 static NPY_TYPES
pyType() {
return NPY_UINT16; }
82 static NPY_TYPES
pyType() {
return NPY_INT16; }
87 static NPY_TYPES
pyType() {
return NPY_UINT16; }
92 static NPY_TYPES
pyType() {
return NPY_INT32; }
97 static NPY_TYPES
pyType() {
return NPY_UINT32; }
102 static NPY_TYPES
pyType() {
return NPY_INT64; }
107 static NPY_TYPES
pyType() {
return NPY_UINT64; }
112 static NPY_TYPES
pyType() {
return NPY_FLOAT32; }
117 static NPY_TYPES
pyType() {
return NPY_FLOAT64; }
122 static NPY_TYPES
pyType() {
return NPY_COMPLEX64; }
127 static NPY_TYPES
pyType() {
return NPY_COMPLEX128; }
132 static NPY_TYPES
pyType() {
return NPY_OBJECT; }
140 {
throw AipsError (
"PycArray: unknown casa type"); }
144 template <
typename T>
148 ::memcpy (to, from, nr*
sizeof(T));
152 for (
uInt i=0; i<nr; i++) {
157 template <
typename T>
161 ::memcpy (to, from, nr*
sizeof(T));
165 for (
uInt i=0; i<nr; i++) {
170 template <
typename T>
172 void* data,
bool copy)
181 return Array<T> (
shape,
static_cast<T*
>(data),
SHARE);
185 fromPy (arr.data(), data, arr.size());
193 throw AipsError(
"PycArray: size of Complex data type mismatches");
195 ::memcpy (to, from, nr*
sizeof(
Complex));
200 throw AipsError(
"PycArray: size of Complex data type mismatches");
202 ::memcpy (to, from, nr*
sizeof(
Complex));
205 void* data,
bool copy)
212 Array<Complex> arr(shape);
213 fromPy (arr.data(), data, arr.size());
221 throw AipsError(
"PycArray: size of DComplex data type mismatches");
223 ::memcpy (to, from, nr*
sizeof(
DComplex));
228 throw AipsError(
"PycArray: size of DComplex data type mismatches");
230 ::memcpy (to, from, nr*
sizeof(
DComplex));
233 void* data,
bool copy)
240 Array<DComplex> arr(shape);
241 fromPy (arr.data(), data, arr.size());
248 PyObject** dst =
static_cast<PyObject**
>(to);
249 for (
uInt i=0; i<nr; i++) {
251 dst[i] = PyUnicode_FromString(from[i].chars());
253 dst[i] = PyString_FromString(from[i].chars());
259 using namespace boost::python;
260 PyObject** src = (PyObject**)from;
261 for (
uInt i=0; i<nr; i++) {
262 handle<> py_elem_hdl(src[i]);
263 object py_elem_obj(py_elem_hdl);
264 extract<std::string> elem_proxy(py_elem_obj);
265 to[i] = elem_proxy();
271 Array<String> arr(shape);
272 fromPy (arr.data(), data, arr.size());
279 throw AipsError (
"PycArray: python object is not an array");
281 PyArrayObject* po = (PyArrayObject*)obj_ptr;
282 boost::python::object obj;
283 bool docopy = copyData;
284 if (! PyArray_ISCONTIGUOUS(po)
285 || ! PyArray_ISALIGNED(po)
286 || PyArray_ISBYTESWAPPED(po)) {
287 boost::python::handle<> py_hdl(obj_ptr);
288 boost::python::object py_obj(py_hdl);
290 boost::python::incref(obj_ptr);
291 obj = py_obj.attr(
"copy")();
292 po = (PyArrayObject*)(obj.ptr());
297 int nd = PyArray_NDIM(po);
301 for (
int i=0; i<nd; i++) {
302 shp[i] = PyArray_DIMS(po)[nd-i-1];
307 if (shp.product() > 0) {
310 && !PyArray_ISBYTESWAPPED(po), AipsError);
313 switch (PyArray_TYPE(po)) {
342 if (PyArray_TYPE(po) == NPY_UINT64) {
344 Array<Int64> res(arr.shape());
345 convertArray (res, arr);
346 return ValueHolder(res);
347 }
else if (PyArray_TYPE(po) == NPY_INT8) {
349 Array<Short> res(arr.shape());
350 convertArray (res, arr);
351 return ValueHolder(res);
352 }
else if (PyArray_TYPE(po) == NPY_UINT8) {
355 Array<Short> res(arr.shape());
357 Array<uChar>* uarr =
static_cast<Array<uChar>*
>(varr);
358 convertArray (res, *uarr);
359 return ValueHolder(res);
360 }
else if (PyArray_TYPE(po) == NPY_STRING) {
363 slen = PyArray_STRIDES(po)[nd-1];
369 throw AipsError (
"PycArray: unknown python array data type");
static void fromPy(T *to, const void *from, uInt nr)
static NPY_TYPES pyType()
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
casacore::Float casa_type
static NPY_TYPES pyType()
StatsData< AccumType > copy(const StatsData< AccumType > &stats)
std::complex< Float > Complex
npy_complex64 python_type
static NPY_TYPES pyType()
Bool PycArrayCheck(PyObject *obj_ptr)
A class to convert an Array to/from Python objects.
unsigned long long uInt64
Bool isImported()
Check if the API is or can be imported.
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
casacore::Int64 casa_type
static NPY_TYPES pyType()
static NPY_TYPES pyType()
static void toPy(void *to, const T *from, uInt nr)
ValueHolder makeArray(PyObject *obj_ptr, Bool copyData)
Convert the python array to a Casacore array in the ValueHolder.
std::complex< Double > DComplex
static NPY_TYPES pyType()
casacore::uShort casa_type
casacore::Complex casa_type
template boost::python::object makePyArrayObject(casacore::Array< Bool > const &arr)
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
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()
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
Base class for all Casacore library errors.
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
casacore::uChar casa_type
static NPY_TYPES pyType()
Array< String > ArrayCopyStr_toArray(const IPosition &shape, void *data, uInt slen)
String: the storage and methods of handling collections of characters.
static NPY_TYPES pyType()
this file contains all the compiler specific defines
casacore::Double casa_type
static NPY_TYPES pyType()