source: issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp@ 13990

Last change on this file since 13990 was 13990, checked in by jschierm, 12 years ago

NEW: Allow FetchPythonData to read double inputs from float, int, or bool.

File size: 14.2 KB
Line 
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 "./pythonio.h"
15#include "../../c/include/include.h"
16#include "../../c/shared/shared.h"
17
18/*Primitive data types*/
19/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
20void FetchData(double* pscalar,PyObject* py_float){
21
22 double scalar;
23
24 /*return internal value: */
25 scalar=PyFloat_AsDouble(py_float);
26
27 /*output: */
28 *pscalar=scalar;
29}
30/*}}}*/
31/*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{*/
32void FetchData(int* pinteger, PyObject* py_long){
33
34 int integer;
35
36 /*return internal value: */
37 integer=(int)PyLong_AsLong(py_long);
38
39 /*output: */
40 *pinteger=integer;
41}
42/*}}}*/
43/*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{*/
44void FetchData(bool* pboolean,PyObject* py_boolean){
45
46 bool boolean;
47
48 /*check this is indeed a subtype of long type: */
49 if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
50
51 /*extract boolean: */
52 boolean=(bool)PyLong_AsLong(py_boolean);
53
54 /*simple copy: */
55 *pboolean=boolean;
56
57}
58/*}}}*/
59/*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
60void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
61
62 /*output: */
63 double* dmatrix=NULL;
64 double* matrix=NULL;
65 int M,N;
66 int ndim;
67 npy_intp* dims=NULL;
68
69 /*intermediary:*/
70 long* lmatrix=NULL;
71 bool* bmatrix=NULL;
72 int i;
73
74 /*retrieve dimensions: */
75 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
76 if(ndim!=2)_error_("expecting an MxN matrix in input!");
77 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
78 M=dims[0]; N=dims[1];
79
80 if (M && N) {
81 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
82 /*retrieve internal value: */
83 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
84
85 /*copy matrix: */
86 matrix=xNew<double>(M*N);
87 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
88 }
89
90 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
91 /*retrieve internal value: */
92 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
93
94 /*transform into double matrix: */
95 matrix=xNew<double>(M*N);
96 for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
97 }
98
99 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
100 /*retrieve internal value: */
101 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
102
103 /*transform into double matrix: */
104 matrix=xNew<double>(M*N);
105 for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
106 }
107
108 else
109 _error_("unrecognized matrix type in input!");
110 }
111 else
112 matrix=NULL;
113
114 /*output: */
115 if(pM)*pM=M;
116 if(pN)*pN=N;
117 if(pmatrix)*pmatrix=matrix;
118}
119/*}}}*/
120/*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
121void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
122
123 /*output: */
124 int* matrix=NULL;
125 int M,N;
126 int ndim;
127 npy_intp* dims=NULL;
128
129 /*intermediary:*/
130 double* dmatrix=NULL;
131 long* lmatrix=NULL;
132 bool* bmatrix=NULL;
133 int i;
134
135 /*retrieve dimensions: */
136 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
137 if(ndim!=2)_error_("expecting an MxN matrix in input!");
138 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
139 M=dims[0]; N=dims[1];
140
141 if (M && N) {
142 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
143 /*retrieve internal value: */
144 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
145
146 /*transform into integer matrix: */
147 matrix=xNew<int>(M*N);
148 for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
149 }
150
151 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
152 /*retrieve internal value: */
153 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
154
155 /*transform into integer matrix: */
156 matrix=xNew<int>(M*N);
157 for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
158 }
159
160 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
161 /*retrieve internal value: */
162 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
163
164 /*transform into integer matrix: */
165 matrix=xNew<int>(M*N);
166 for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
167 }
168
169 else
170 _error_("unrecognized matrix type in input!");
171 }
172 else
173 matrix=NULL;
174
175 /*output: */
176 if(pM)*pM=M;
177 if(pN)*pN=N;
178 if(pmatrix)*pmatrix=matrix;
179}
180/*}}}*/
181/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
182void FetchData(double** pvector,int* pM,PyObject* py_vector){
183
184 /*output: */
185 double* dvector=NULL;
186 double* vector=NULL;
187 int M;
188 int ndim;
189 npy_intp* dims=NULL;
190
191 /*intermediary:*/
192 long* lvector=NULL;
193 bool* bvector=NULL;
194 int i;
195
196 /*retrieve dimensions: */
197 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
198 if(ndim!=1)_error_("expecting an Mx1 vector in input!");
199 dims=PyArray_DIMS((PyArrayObject*)py_vector);
200 M=dims[0];
201
202 if (M) {
203 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
204 /*retrieve internal value: */
205 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
206
207 /*copy vector: */
208 vector=xNew<double>(M);
209 memcpy(vector,dvector,(M)*sizeof(double));
210 }
211
212 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
213 /*retrieve internal value: */
214 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
215
216 /*transform into double vector: */
217 vector=xNew<double>(M);
218 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
219 }
220
221 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
222 /*retrieve internal value: */
223 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
224
225 /*transform into double vector: */
226 vector=xNew<double>(M);
227 for(i=0;i<M;i++)vector[i]=(double)bvector[i];
228 }
229
230 else
231 _error_("unrecognized vector type in input!");
232 }
233 else
234 vector=NULL;
235
236 /*output: */
237 if(pM)*pM=M;
238 if(pvector)*pvector=vector;
239}
240/*}}}*/
241
242/*ISSM objects*/
243/*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
244void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
245
246 /*Initialize output*/
247 BamgGeom* bamggeom=new BamgGeom();
248
249 /*Fetch all fields*/
250 FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
251 FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
252 FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
253 FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
254 FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
255 FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
256 FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
257
258 /*Assign output pointers:*/
259 *pbamggeom=bamggeom;
260}
261/*}}}*/
262/*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
263void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
264
265 /*Initialize output*/
266 BamgMesh* bamgmesh=new BamgMesh();
267
268 /*Fetch all fields*/
269 FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
270 FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
271 FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
272 FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
273 FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
274 FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
275 FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
276 FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
277
278 /*Assign output pointers:*/
279 *pbamgmesh=bamgmesh;
280}
281/*}}}*/
282/*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
283void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
284
285 /*Initialize output*/
286 BamgOpts* bamgopts=new BamgOpts();
287
288 /*Fetch all fields*/
289 FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
290 FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
291 FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
292 FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
293 FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
294 FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
295 FetchData(&bamgopts->MaxCornerAngle,PyDict_GetItemString(py_dict,"MaxCornerAngle"));
296 FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
297 FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
298 FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
299 FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
300 FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
301 FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
302 FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
303 FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
304
305 FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
306 FetchData(&bamgopts->geometricalmetric,PyDict_GetItemString(py_dict,"geometricalmetric"));
307 FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
308 FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
309
310 FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
311 FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
312 FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
313 FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
314 FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
315 FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
316 FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
317 FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
318
319 /*Additional checks*/
320 bamgopts->Check();
321
322 /*Assign output pointers:*/
323 *pbamgopts=bamgopts;
324}
325/*}}}*/
326/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
327void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
328
329 char *name = NULL;
330 Option *option = NULL;
331
332 /*Initialize output*/
333 Options* options=new Options();
334
335 /*Fetch all options*/
336 for (int i=istart; i<nrhs; i=i+2){
337 if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
338
339 FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
340 if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
341
342 _pprintLine_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!");
343
344// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
345// options->AddOption(option);
346// option=NULL;
347 }
348
349 /*Assign output pointers:*/
350 *poptions=options;
351}
352/*}}}*/
353/*FUNCTION FetchData(DataSet** pcontours,PyObject* py_list){{{*/
354void FetchData(DataSet** pcontours,PyObject* py_list){
355
356 int numcontours,test1,test2;
357 char *contourname = NULL;
358 DataSet *contours = NULL;
359 Contour<double> *contouri = NULL;
360 PyObject *py_dicti = NULL;
361 PyObject *py_item = NULL;
362
363 if (PyString_Check(py_list)){
364 FetchData(&contourname,py_list);
365 contours=DomainOutlineRead<double>(contourname);
366 }
367 else if(PyList_Check(py_list)){
368
369 contours=new DataSet(0);
370 numcontours=(int)PyList_Size(py_list);
371
372 for(int i=0;i<numcontours;i++){
373
374 contouri=xNew<Contour<double> >(1);
375 py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
376
377 py_item = PyDict_GetItemString(py_dicti,"nods");
378 if(!py_item) _error_("input structure does not have a 'nods' field");
379 FetchData(&contouri->nods,py_item);
380
381 py_item = PyDict_GetItemString(py_dicti,"x");
382 if(!py_item) _error_("input structure does not have a 'x' field");
383 FetchData(&contouri->x,&test1,&test2,py_item);
384 if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
385
386 py_item = PyDict_GetItemString(py_dicti,"y");
387 if(!py_item) _error_("input structure does not have a 'y' field");
388 FetchData(&contouri->y,&test1,&test2,py_item);
389 if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
390
391 contours->AddObject(contouri);
392 }
393 }
394 else{
395 _error_("Contour is neither a string nor a structure and cannot be loaded");
396 }
397
398 /*clean-up and assign output pointer*/
399 xDelete<char>(contourname);
400 *pcontours=contours;
401}
402/*}}}*/
403
404/*Python version dependent: */
405#if _PYTHON_MAJOR_ >= 3
406/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
407void FetchData(char** pstring,PyObject* py_unicode){
408
409 PyObject* py_bytes;
410 char* string=NULL;
411
412 /*convert to bytes format: */
413 PyUnicode_FSConverter(py_unicode,&py_bytes);
414
415 /*convert from bytes to string: */
416 string=PyBytes_AS_STRING(py_bytes);
417
418 *pstring=string;
419}
420/*}}}*/
421#else
422/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
423void FetchData(char** pstring,PyObject* py_string){
424
425 char* string=NULL;
426
427 /*extract internal string: */
428 string=PyString_AsString(py_string);
429
430 /*copy string (note strlen does not include trailing NULL): */
431 *pstring=xNew<char>(strlen(string)+1);
432 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
433}
434/*}}}*/
435#endif
Note: See TracBrowser for help on using the repository browser.