casacore
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: aips2-request@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 //# $Id: PycArray.h,v 1.4 2006/11/06 00:14:44 gvandiep Exp $
27 
28 
29 #ifndef PYRAP_PYCARRAY_H
30 #define PYRAP_PYCARRAY_H
31 
32 //# Includes
33 // include first to avoid _POSIX_C_SOURCE redefined warnings
34 #include <boost/python.hpp>
35 #include <boost/python/object.hpp>
36 #include <casacore/casa/Arrays/Array.h>
37 #include <casacore/casa/Containers/ValueHolder.h>
38 #include <casacore/casa/Utilities/DataType.h>
39 #include <casacore/casa/Exceptions/Error.h>
40 #include <iostream>
41 
42 namespace casacore { namespace python {
43 
44 
45  // <summary>
46  // A class to convert an Array to/from Python objects.
47  // </summary>
48 
49  // <use visibility=export>
50  // <reviewed reviewer="" date="" tests="">
51  // </reviewed>
52 
53  // <synopsis>
54  // </synopsis>
55 
56  // Check if the PyObject is an array object.
57  Bool PycArrayCheck (PyObject* obj_ptr);
58 
59  // Check if the PyObject is an array scalar object.
60  Bool PycArrayScalarCheck (PyObject* obj_ptr);
61 
62  // Get the data type of the array scalar object.
63  // It returns TpBool, TpInt, TpFloat, or TpComplex.
64  // TpOther is returned if unrecognized.
65  DataType PycArrayScalarType (PyObject* obj_ptr);
66 
68  {
69  // Constructs an Array from a Python object.
70  // If copyData=False, the array data is only copied if needed meaning
71  // that the Array object in the ValueHolder can reference the data in
72  // Python array.
73  // That should only be used if the ValueHolder and its Array will be
74  // destructed before the Python array.
75  static ValueHolder makeArray(PyObject* obj_ptr, Bool copyData=False);
76 
77  // Construct an Array<String> from a special Python dict object.
78  static ValueHolder makeArrayFromDict (PyObject* obj_ptr);
79 
80  // Construct a scalar from an array scalar (i.e. element in array).
81  static ValueHolder makeScalar (PyObject* obj_ptr);
82  };
83 
84  // Do the actual making of the PyArrayObject.
85  // Specialize for strings.
86  // <group>
87  template <typename T>
88  boost::python::object makePyArrayObject (casacore::Array<T> const& arr);
89  template <>
90  boost::python::object makePyArrayObject (casacore::Array<String> const& arr);
91  // </group>
92 
93  // Convert Array to Python.
94  template <typename T>
96  {
97  static boost::python::object makeobject (Array<T> const& arr)
98  { return makePyArrayObject (arr); }
99  static PyObject* convert (Array<T> const& c)
100  { return boost::python::incref(makeobject(c).ptr()); }
101  };
102 
103 }}
104 
105 #endif
Bool PycArrayCheck(PyObject *obj_ptr)
const Double c
Fundamental physical constants (SI units):
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.
this file contains all the compiler specific defines
Definition: mainpage.dox:28
const Bool False
Definition: aipstype.h:44
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
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:96
static boost::python::object makeobject(Array< T > const &arr)
Definition: PycArray.h:97
static PyObject * convert(Array< T > const &c)
Definition: PycArray.h:99