casacore
Loading...
Searching...
No Matches
PycArrayNP.h
Go to the documentation of this file.
1// # PycArrayNP.h: Class to convert an Array to/from a Python numpy array
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_PYCARRAYNP_H
27#define PYRAP_PYCARRAYNP_H
29// include first to avoid _POSIX_C_SOURCE redefined warnings
30#include <boost/python.hpp>
31#include <boost/python/object.hpp>
32#include <casacore/casa/Containers/ValueHolder.h>
33#include <casacore/casa/Arrays/Array.h>
34#include <numpy/arrayobject.h>
36namespace casacore {
37namespace python {
38namespace numpy {
39
40#define PYC_USE_PYARRAY "numpy"
41#include <casacore/python/Converters/PycArrayComH.h>
42#undef PYC_USE_PYARRAY
43
44// # Define functions to deal with numpy array scalars.
45
46// Check if it is an array scalar object.
47bool PycArrayScalarCheck(PyObject* obj, int& type);
49// Get the data type of the array scalar object.
50// It returns TpBool, TpInt, TpFloat, or TpComplex.
51// TpOther is returned if unrecognized.
52DataType PycArrayScalarType(PyObject* obj_ptr);
53
54// Make a scalar object.
55ValueHolder makeScalar(PyObject* obj, int type);
57// Register all array scalar converters.
59
60// Templated helper function to get a value from a ValueHolder.
61// Specialize for each type supported.
62// <group>
63template <typename T>
65template <>
66inline Bool getScalar(const ValueHolder& vh) {
67 return vh.asBool();
69template <>
70inline Char getScalar(const ValueHolder& vh) {
71 return vh.asShort();
72}
73template <>
74inline uChar getScalar(const ValueHolder& vh) {
75 return vh.asuChar();
76}
77template <>
78inline Short getScalar(const ValueHolder& vh) {
79 return vh.asShort();
80}
81template <>
82inline uShort getScalar(const ValueHolder& vh) {
83 return vh.asuShort();
84}
85template <>
86inline Int getScalar(const ValueHolder& vh) {
87 return vh.asInt();
88}
89template <>
90inline uInt getScalar(const ValueHolder& vh) {
91 return vh.asuInt();
92}
93template <>
94inline Long getScalar(const ValueHolder& vh) {
95 return vh.asInt();
96}
97template <>
98inline uLong getScalar(const ValueHolder& vh) {
99 return vh.asuInt();
100}
101template <>
102inline Int64 getScalar(const ValueHolder& vh) {
103 return vh.asInt();
104}
105template <>
106inline uInt64 getScalar(const ValueHolder& vh) {
107 return vh.asuInt();
108}
109template <>
110inline Float getScalar(const ValueHolder& vh) {
111 return vh.asFloat();
112}
113template <>
114inline Double getScalar(const ValueHolder& vh) {
115 return vh.asDouble();
116}
117template <>
118inline Complex getScalar(const ValueHolder& vh) {
119 return vh.asComplex();
120}
121template <>
122inline DComplex getScalar(const ValueHolder& vh) {
123 return vh.asDComplex();
124}
125// </group>
126
127// Struct with static functions to convert a numpy array scalar to
128// the templated type (e.g. Int).
129template <typename T>
132 boost::python::converter::registry::push_back(&convertible, &construct,
133 boost::python::type_id<T>());
134 }
135
136 // Check if it is a type we can convert.
137 static void* convertible(PyObject* obj_ptr) {
138 int type;
139 if (PycArrayScalarCheck(obj_ptr, type)) {
140 return obj_ptr;
141 }
142 return 0;
143 }
144
145 // Constructs a T from a Python array scalar object.
146 static void construct(PyObject* obj_ptr,
147 boost::python::converter::rvalue_from_python_stage1_data* data) {
148 using namespace boost::python;
149 void* storage = ((converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
150 new (storage) T();
151 data->convertible = storage;
152 int type;
153 PycArrayScalarCheck(obj_ptr, type);
154 *static_cast<T*>(storage) = getScalar<T>(makeScalar(obj_ptr, type));
155 }
156};
157
158} // namespace numpy
159} // namespace python
160} // namespace casacore
161
162#endif
Complex asComplex() const
Double asDouble() const
Float asFloat() const
Short asShort() const
uChar asuChar() const
uShort asuShort() const
Bool asBool() const
Get the value.
DComplex asDComplex() const
ValueHolder makeScalar(PyObject *obj, int type)
Make a scalar object.
T getScalar(const ValueHolder &)
Templated helper function to get a value from a ValueHolder.
void register_convert_arrayscalars()
Register all array scalar converters.
bool PycArrayScalarCheck(PyObject *obj, int &type)
Check if it is an array scalar object.
DataType PycArrayScalarType(PyObject *obj_ptr)
Get the data type of the array scalar object.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
unsigned long uLong
Definition aipstype.h:51
long Long
Definition aipstype.h:50
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
T * storage()
If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to...
Definition Block.h:559
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44
unsigned long long uInt64
Definition aipsxtype.h:37
static void * convertible(PyObject *obj_ptr)
Check if it is a type we can convert.
Definition PycArrayNP.h:137
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Constructs a T from a Python array scalar object.
Definition PycArrayNP.h:146