1 | /*\file FetchData.cpp:
|
---|
2 | * \brief: general I/O interface to fetch data in python
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifdef HAVE_CONFIG_H
|
---|
6 | #include <config.h>
|
---|
7 | #else
|
---|
8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | #define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
|
---|
12 | #define NO_IMPORT
|
---|
13 |
|
---|
14 | #include "../../toolkits/toolkits.h"
|
---|
15 | #include "../../include/include.h"
|
---|
16 | #include "../../shared/shared.h"
|
---|
17 | #include "../../io/io.h"
|
---|
18 |
|
---|
19 | /*Primitive data types*/
|
---|
20 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
21 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
22 |
|
---|
23 | double scalar;
|
---|
24 |
|
---|
25 | /*return internal value: */
|
---|
26 | scalar=PyFloat_AsDouble(py_float);
|
---|
27 |
|
---|
28 | /*output: */
|
---|
29 | *pscalar=scalar;
|
---|
30 | }
|
---|
31 | /*}}}*/
|
---|
32 | /*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{*/
|
---|
33 | void FetchData(int* pinteger, PyObject* py_long){
|
---|
34 |
|
---|
35 | int integer;
|
---|
36 |
|
---|
37 | /*return internal value: */
|
---|
38 | integer=(int)PyLong_AsLong(py_long);
|
---|
39 |
|
---|
40 | /*output: */
|
---|
41 | *pinteger=integer;
|
---|
42 | }
|
---|
43 | /*}}}*/
|
---|
44 | /*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{*/
|
---|
45 | void FetchData(bool* pboolean,PyObject* py_boolean){
|
---|
46 |
|
---|
47 | bool boolean;
|
---|
48 |
|
---|
49 | /*check this is indeed a subtype of long type: */
|
---|
50 | if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
|
---|
51 |
|
---|
52 | /*extract boolean: */
|
---|
53 | boolean=(bool)PyLong_AsLong(py_boolean);
|
---|
54 |
|
---|
55 | /*simple copy: */
|
---|
56 | *pboolean=boolean;
|
---|
57 |
|
---|
58 | }
|
---|
59 | /*}}}*/
|
---|
60 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
61 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
62 |
|
---|
63 | /*output: */
|
---|
64 | double* matrix=NULL;
|
---|
65 | int M,N;
|
---|
66 | int ndim;
|
---|
67 | npy_intp* dims=NULL;
|
---|
68 |
|
---|
69 | /*retrive dimensions: */
|
---|
70 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
71 | if(ndim!=2)_error_("expecting an MxN matrix in input!");
|
---|
72 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
73 | M=dims[0]; N=dims[1];
|
---|
74 |
|
---|
75 | /*retrieve internal value: */
|
---|
76 | matrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
77 |
|
---|
78 | /*output: */
|
---|
79 | if(pM)*pM=M;
|
---|
80 | if(pN)*pN=N;
|
---|
81 | if(pmatrix)*pmatrix=matrix;
|
---|
82 | }
|
---|
83 | /*}}}*/
|
---|
84 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
85 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
86 |
|
---|
87 | /*output: */
|
---|
88 | double* dmatrix=NULL;
|
---|
89 | int* matrix=NULL;
|
---|
90 | int M,N;
|
---|
91 |
|
---|
92 | /*intermediary:*/
|
---|
93 | int i;
|
---|
94 | int ndim;
|
---|
95 | npy_intp* dims=NULL;
|
---|
96 |
|
---|
97 | /*retrive dimensions: */
|
---|
98 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
99 | if(ndim!=2)_error_("expecting an MxN matrix in input!");
|
---|
100 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
101 | M=dims[0]; N=dims[1];
|
---|
102 |
|
---|
103 | /*retrieve internal value: */
|
---|
104 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
105 |
|
---|
106 | /*transform into integer matrix: */
|
---|
107 | matrix=xNew<int>(M*N);
|
---|
108 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
109 |
|
---|
110 | /*output: */
|
---|
111 | if(pM)*pM=M;
|
---|
112 | if(pN)*pN=N;
|
---|
113 | if(pmatrix)*pmatrix=matrix;
|
---|
114 | }
|
---|
115 | /*}}}*/
|
---|
116 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
117 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
118 |
|
---|
119 | /*output: */
|
---|
120 | double* vector=NULL;
|
---|
121 | int M;
|
---|
122 | int ndim;
|
---|
123 | npy_intp* dims=NULL;
|
---|
124 |
|
---|
125 | /*retrive dimensions: */
|
---|
126 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
127 | if(ndim!=1)_error_("expecting an Mx1 vector in input!");
|
---|
128 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
129 | M=dims[0];
|
---|
130 |
|
---|
131 | /*retrieve internal value: */
|
---|
132 | vector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
133 |
|
---|
134 | /*output: */
|
---|
135 | if(pM)*pM=M;
|
---|
136 | if(pvector)*pvector=vector;
|
---|
137 | }
|
---|
138 | /*}}}*/
|
---|
139 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* arguments){{{*/
|
---|
140 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* arguments){
|
---|
141 |
|
---|
142 | /*Initialize output*/
|
---|
143 | Options* options=new Options();
|
---|
144 |
|
---|
145 | _pprintLine_("FetchData for Options not implemented yet, ignoring them!");
|
---|
146 |
|
---|
147 | /*Assign output pointers:*/
|
---|
148 | *poptions=options;
|
---|
149 |
|
---|
150 | }
|
---|
151 | /*}}}*/
|
---|
152 |
|
---|
153 | /*Python version dependent: */
|
---|
154 | #if _PYTHON_MAJOR_ >= 3
|
---|
155 | /*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
|
---|
156 | void FetchData(char** pstring,PyObject* py_unicode){
|
---|
157 |
|
---|
158 | PyObject* py_bytes;
|
---|
159 | char* string=NULL;
|
---|
160 |
|
---|
161 |
|
---|
162 | /*convert to bytes format: */
|
---|
163 | PyUnicode_FSConverter(py_unicode,&py_bytes);
|
---|
164 |
|
---|
165 | /*convert from bytes to string: */
|
---|
166 | string=PyBytes_AS_STRING(py_bytes);
|
---|
167 |
|
---|
168 | *pstring=string;
|
---|
169 | }
|
---|
170 | /*}}}*/
|
---|
171 | #else
|
---|
172 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
173 | void FetchData(char** pstring,PyObject* py_string){
|
---|
174 |
|
---|
175 | char* string=NULL;
|
---|
176 |
|
---|
177 | /*extract internal string: */
|
---|
178 | string=PyString_AsString(py_string);
|
---|
179 |
|
---|
180 | *pstring=string;
|
---|
181 | }
|
---|
182 | /*}}}*/
|
---|
183 | #endif
|
---|