casacore
Loading...
Searching...
No Matches
PycArray.h
Go to the documentation of this file.
1// # PycArray.h: Class to convert an Array to/from Python
2// # Copyright (C) 2006
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef PYRAP_PYCARRAY_H
27#define PYRAP_PYCARRAY_H
28
29// # Includes
30// include first to avoid _POSIX_C_SOURCE redefined warnings
31#include <boost/python.hpp>
32#include <boost/python/object.hpp>
33#include <casacore/casa/Arrays/Array.h>
34#include <casacore/casa/Containers/ValueHolder.h>
35#include <casacore/casa/Utilities/DataType.h>
36#include <casacore/casa/Exceptions/Error.h>
37#include <iostream>
38
39namespace casacore {
40namespace python {
41
42// <summary>
43// A class to convert an Array to/from Python objects.
44// </summary>
45
46// <use visibility=export>
47// <reviewed reviewer="" date="" tests="">
48// </reviewed>
49
50// <synopsis>
51// </synopsis>
52
53// Check if the PyObject is an array object.
54Bool PycArrayCheck(PyObject* obj_ptr);
55
56// Check if the PyObject is an array scalar object.
57Bool PycArrayScalarCheck(PyObject* obj_ptr);
58
59// Get the data type of the array scalar object.
60// It returns TpBool, TpInt, TpFloat, or TpComplex.
61// TpOther is returned if unrecognized.
62DataType PycArrayScalarType(PyObject* obj_ptr);
63
65 // Constructs an Array from a Python object.
66 // If copyData=False, the array data is only copied if needed meaning
67 // that the Array object in the ValueHolder can reference the data in
68 // Python array.
69 // That should only be used if the ValueHolder and its Array will be
70 // destructed before the Python array.
71 static ValueHolder makeArray(PyObject* obj_ptr, Bool copyData = False);
72
73 // Construct an Array<String> from a special Python dict object.
74 static ValueHolder makeArrayFromDict(PyObject* obj_ptr);
75
76 // Construct a scalar from an array scalar (i.e. element in array).
77 static ValueHolder makeScalar(PyObject* obj_ptr);
78};
79
80// Do the actual making of the PyArrayObject.
81// Specialize for strings.
82// <group>
83template <typename T>
84boost::python::object makePyArrayObject(casacore::Array<T> const& arr);
85template <>
86boost::python::object makePyArrayObject(casacore::Array<String> const& arr);
87// </group>
88
89// Convert Array to Python.
90template <typename T>
92 static boost::python::object makeobject(Array<T> const& arr) { return makePyArrayObject(arr); }
93 static PyObject* convert(Array<T> const& c) { return boost::python::incref(makeobject(c).ptr()); }
94};
95
96} // namespace python
97} // namespace casacore
98
99#endif
Bool PycArrayCheck(PyObject *obj_ptr)
DataType PycArrayScalarType(PyObject *obj_ptr)
Get the data type of the array scalar object.
Bool PycArrayScalarCheck(PyObject *obj_ptr)
Check if the PyObject is an array scalar object.
boost::python::object makePyArrayObject(casacore::Array< T > const &arr)
Do the actual making of the PyArrayObject.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
static ValueHolder makeArray(PyObject *obj_ptr, Bool copyData=False)
Constructs an Array from a Python object.
static ValueHolder makeScalar(PyObject *obj_ptr)
Construct a scalar from an array scalar (i.e.
static ValueHolder makeArrayFromDict(PyObject *obj_ptr)
Construct an Array<String> from a special Python dict object.
Convert Array to Python.
Definition PycArray.h:91
static PyObject * convert(Array< T > const &c)
Definition PycArray.h:93
static boost::python::object makeobject(Array< T > const &arr)
Definition PycArray.h:92