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

Last change on this file since 22674 was 22674, checked in by Mathieu Morlighem, 7 years ago

CHG: working on making Chaco and MeshPartition python friendly

File size: 27.6 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/shared/shared.h"
16
17/*Primitive data types*/
18/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
19void FetchData(double* pscalar,PyObject* py_float){
20
21 double dscalar;
22
23 /*return internal value: */
24 if (PyFloat_Check(py_float))
25 dscalar=PyFloat_AsDouble(py_float);
26 else if (PyLong_Check(py_float)){
27 #if _PYTHON_MAJOR_ == 3
28 dscalar=(double)PyLong_AsLong(py_float);
29 #else
30 dscalar=PyLong_AsDouble(py_float);
31 #endif
32 }
33 else if (PyInt_Check(py_float))
34 dscalar=(double)PyInt_AsLong(py_float);
35 else if (PyBool_Check(py_float))
36 dscalar=(double)PyLong_AsLong(py_float);
37 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
38 FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
39 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
40 FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
41 else
42 _error_("unrecognized float type in input!");
43
44 /*output: */
45 *pscalar=dscalar;
46}
47/*}}}*/
48/*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
49void FetchData(float* pscalar,PyObject* py_float){
50
51 float fscalar;
52
53 /*return internal value: */
54 if (PyFloat_Check(py_float))
55 fscalar=PyFloat_AsDouble(py_float);
56 else if (PyLong_Check(py_float)){
57 #if _PYTHON_MAJOR_ == 3
58 fscalar=(float)PyLong_AsLong(py_float);
59 #else
60 fscalar=(float)PyLong_AsDouble(py_float);
61 #endif
62 }
63 else if (PyInt_Check(py_float))
64 fscalar=(float)PyInt_AsLong(py_float);
65 else if (PyBool_Check(py_float))
66 fscalar=(float)PyLong_AsLong(py_float);
67 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
68 FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
69 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
70 FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
71 else
72 _error_("unrecognized float type in input!");
73
74 /*output: */
75 *pscalar=fscalar;
76}
77/*}}}*/
78/*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
79void FetchData(int* pscalar, PyObject* py_long){
80
81 int iscalar;
82
83 /*return internal value: */
84 if (PyLong_Check(py_long))
85 iscalar=(int)PyLong_AsLong(py_long);
86 else if (PyInt_Check(py_long))
87 iscalar=(int)PyInt_AsLong(py_long);
88 else if (PyFloat_Check(py_long))
89 iscalar=(int)PyFloat_AsDouble(py_long);
90 else if (PyBool_Check(py_long))
91 iscalar=(int)PyLong_AsLong(py_long);
92 else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
93 FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
94 else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
95 FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
96 else
97 _error_("unrecognized long type in input!");
98
99 /*output: */
100 *pscalar=iscalar;
101}
102/*}}}*/
103/*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
104void FetchData(bool* pscalar,PyObject* py_boolean){
105
106 bool bscalar;
107
108 /*return internal value: */
109 if (PyBool_Check(py_boolean))
110 bscalar=(bool)PyLong_AsLong(py_boolean);
111 else if (PyLong_Check(py_boolean))
112 bscalar=(bool)PyLong_AsLong(py_boolean);
113 else if (PyLong_Check(py_boolean))
114 bscalar=(bool)PyLong_AsLong(py_boolean);
115 else if (PyInt_Check(py_boolean))
116 bscalar=(bool)PyInt_AsLong(py_boolean);
117 else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
118 FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
119 else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
120 FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
121 else
122 _error_("unrecognized boolean type in input!");
123
124 /*output: */
125 *pscalar=bscalar;
126}
127/*}}}*/
128/*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
129void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
130
131 /*output: */
132 double* dmatrix=NULL;
133 double* matrix=NULL;
134 int M,N;
135 int ndim;
136 npy_intp* dims=NULL;
137
138 /*intermediary:*/
139 long* lmatrix=NULL;
140 bool* bmatrix=NULL;
141 int i;
142 PyObject* py_matrix2=NULL;
143
144 if (PyArray_Check((PyArrayObject*)py_matrix)) {
145 /*retrieve dimensions: */
146 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
147 if (ndim==2) {
148 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
149 M=dims[0]; N=dims[1];
150 }
151 else if (ndim==1) {
152 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
153 M=dims[0]; N=1;
154 }
155 else
156 _error_("expecting an MxN matrix or M vector in input!");
157
158 if (M && N) {
159 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
160 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
161 py_matrix=py_matrix2;
162 }
163
164 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
165 /*retrieve internal value: */
166 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
167
168 /*copy matrix: */
169 matrix=xNew<double>(M*N);
170// if (PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
171 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
172// }
173
174// else {
175// int j,k,ipt=0;
176// int mstride,nstride;
177// mstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,0)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
178// if (ndim > 1)
179// nstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,1)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
180// else
181// nstride=1;
182// for (i=0; i<M; i++) {
183// k=i*mstride;
184// for (j=0; j<N; j++) {
185// matrix[ipt++]=dmatrix[k];
186// k+=nstride;
187// }
188// }
189// }
190
191 }
192
193 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
194 /*retrieve internal value: */
195 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
196
197 /*transform into double matrix: */
198 matrix=xNew<double>(M*N);
199 for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
200 }
201
202 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
203 /*retrieve internal value: */
204 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
205
206 /*transform into double matrix: */
207 matrix=xNew<double>(M*N);
208 for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
209 }
210
211 else
212 _error_("unrecognized double pyarray type in input!");
213
214 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
215 }
216 else
217 matrix=NULL;
218 }
219
220 else {
221 M=1;
222 N=1;
223 matrix=xNew<double>(M*N);
224 FetchData(&(matrix[0]),py_matrix);
225 }
226
227 /*output: */
228 if(pM)*pM=M;
229 if(pN)*pN=N;
230 if(pmatrix)*pmatrix=matrix;
231}
232/*}}}*/
233/*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
234void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
235
236 /*output: */
237 int* matrix=NULL;
238 int M,N;
239 int ndim;
240 npy_intp* dims=NULL;
241
242 /*intermediary:*/
243 double* dmatrix=NULL;
244 long* lmatrix=NULL;
245 bool* bmatrix=NULL;
246 int i;
247 PyObject* py_matrix2=NULL;
248
249 if (PyArray_Check((PyArrayObject*)py_matrix)) {
250 /*retrieve dimensions: */
251 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
252 if (ndim==2) {
253 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
254 M=dims[0]; N=dims[1];
255 }
256 else if (ndim==1) {
257 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
258 M=dims[0]; N=1;
259 }
260 else
261 _error_("expecting an MxN matrix or M vector in input!");
262
263 if (M && N) {
264 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
265 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
266 py_matrix=py_matrix2;
267 }
268
269 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
270 /*retrieve internal value: */
271 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
272
273 /*transform into integer matrix: */
274 matrix=xNew<int>(M*N);
275 for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
276 }
277
278 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
279 /*retrieve internal value: */
280 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
281
282 /*transform into integer matrix: */
283 matrix=xNew<int>(M*N);
284 for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
285 }
286
287 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
288 /*retrieve internal value: */
289 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
290
291 /*transform into integer matrix: */
292 matrix=xNew<int>(M*N);
293 for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
294 }
295
296 else
297 _error_("unrecognized int pyarray type in input!");
298
299 /* These lines are causing a segfault
300 if (py_matrix2)
301 delete(py_matrix2);
302 */
303 }
304 else
305 matrix=NULL;
306 }
307
308 else {
309 M=1;
310 N=1;
311 matrix=xNew<int>(M*N);
312 FetchData(&(matrix[0]),py_matrix);
313 }
314
315 /*output: */
316 if(pM)*pM=M;
317 if(pN)*pN=N;
318 if(pmatrix)*pmatrix=matrix;
319}
320/*}}}*/
321/*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
322void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
323
324 /*output: */
325 bool* bmatrix=NULL;
326 bool* matrix=NULL;
327 int M,N;
328 int ndim;
329 npy_intp* dims=NULL;
330
331 /*intermediary:*/
332 double* dmatrix=NULL;
333 long* lmatrix=NULL;
334 int i;
335 PyObject* py_matrix2=NULL;
336
337 if (PyArray_Check((PyArrayObject*)py_matrix)) {
338 /*retrieve dimensions: */
339 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
340 if (ndim==2) {
341 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
342 M=dims[0]; N=dims[1];
343 }
344 else if (ndim==1) {
345 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
346 M=dims[0]; N=1;
347 }
348 else
349 _error_("expecting an MxN matrix or M vector in input!");
350
351 if (M && N) {
352 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
353 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
354 py_matrix=py_matrix2;
355 }
356
357 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
358 /*retrieve internal value: */
359 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
360
361 /*transform into bool matrix: */
362 matrix=xNew<bool>(M*N);
363 for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
364 }
365
366 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
367 /*retrieve internal value: */
368 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
369
370 /*transform into bool matrix: */
371 matrix=xNew<bool>(M*N);
372 for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
373 }
374
375 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
376 /*retrieve internal value: */
377 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
378
379 /*copy matrix: */
380 matrix=xNew<bool>(M*N);
381 memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
382 }
383
384 else
385 _error_("unrecognized bool pyarray type in input!");
386
387 if (py_matrix2)
388 delete(py_matrix2);
389 }
390 else
391 matrix=NULL;
392 }
393
394 else {
395 M=1;
396 N=1;
397 matrix=xNew<bool>(M*N);
398 FetchData(&(matrix[0]),py_matrix);
399 }
400
401 /*output: */
402 if(pM)*pM=M;
403 if(pN)*pN=N;
404 if(pmatrix)*pmatrix=matrix;
405}
406/*}}}*/
407/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
408void FetchData(double** pvector,int* pM,PyObject* py_vector){
409
410 /*output: */
411 double* dvector=NULL;
412 double* vector=NULL;
413 int M;
414 int ndim;
415 npy_intp* dims=NULL;
416
417 /*intermediary:*/
418 long* lvector=NULL;
419 bool* bvector=NULL;
420 int i;
421 PyObject* py_vector2=NULL;
422
423 if (PyArray_Check((PyArrayObject*)py_vector)) {
424 /*retrieve dimensions: */
425 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
426 if (ndim==1) {
427 dims=PyArray_DIMS((PyArrayObject*)py_vector);
428 M=dims[0];
429 }
430 else if (ndim==2) {
431 dims=PyArray_DIMS((PyArrayObject*)py_vector);
432 if (dims[1]==1)
433 M=dims[0];
434 else
435 _error_("expecting an Mx1 matrix or M vector in input!");
436 }
437 else
438 _error_("expecting an Mx1 matrix or M vector in input!");
439
440 if (M) {
441 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
442 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
443 py_vector=py_vector2;
444 }
445
446 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
447 /*retrieve internal value: */
448 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
449
450 /*copy vector: */
451 vector=xNew<double>(M);
452 memcpy(vector,dvector,(M)*sizeof(double));
453 }
454
455 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
456 /*retrieve internal value: */
457 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
458
459 /*transform into double vector: */
460 vector=xNew<double>(M);
461 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
462 }
463
464 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
465 /*retrieve internal value: */
466 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
467
468 /*transform into double vector: */
469 vector=xNew<double>(M);
470 for(i=0;i<M;i++)vector[i]=(double)bvector[i];
471 }
472
473 else
474 _error_("unrecognized double pyarray type in input!");
475
476 /* Causing a seg fault.
477 if (py_vector2)
478 delete(py_vector2);
479 */
480 }
481 else
482 vector=NULL;
483 }
484
485 else {
486 M=1;
487 vector=xNew<double>(M);
488 FetchData(&(vector[0]),py_vector);
489 }
490
491 /*output: */
492 if(pM)*pM=M;
493 if(pvector)*pvector=vector;
494}
495/*}}}*/
496/*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
497void FetchData(float** pvector,int* pM,PyObject* py_vector){
498
499 /*output: */
500 float* vector=NULL;
501 int M;
502 int ndim;
503 npy_intp* dims=NULL;
504
505 /*intermediary:*/
506 long* lvector=NULL;
507 bool* bvector=NULL;
508 double* dvector=NULL;
509 int i;
510 PyObject* py_vector2=NULL;
511
512 if (PyArray_Check((PyArrayObject*)py_vector)) {
513 /*retrieve dimensions: */
514 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
515 if (ndim==1) {
516 dims=PyArray_DIMS((PyArrayObject*)py_vector);
517 M=dims[0];
518 }
519 else if (ndim==2) {
520 dims=PyArray_DIMS((PyArrayObject*)py_vector);
521 if (dims[1]==1)
522 M=dims[0];
523 else
524 _error_("expecting an Mx1 matrix or M vector in input!");
525 }
526 else
527 _error_("expecting an Mx1 matrix or M vector in input!");
528
529 if (M) {
530 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
531 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
532 py_vector=py_vector2;
533 }
534
535 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
536 /*retrieve internal value: */
537 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
538
539 /*transform into int vector: */
540 vector=xNew<float>(M);
541 for(i=0;i<M;i++)vector[i]=(float)lvector[i];
542 }
543
544 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
545 /*retrieve internal value: */
546 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
547
548 /*transform into int vector: */
549 vector=xNew<float>(M);
550 for(i=0;i<M;i++)vector[i]=(float)lvector[i];
551 }
552
553 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
554 /*retrieve internal value: */
555 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
556
557 /*transform into int vector: */
558 vector=xNew<float>(M);
559 for(i=0;i<M;i++)vector[i]=(float)bvector[i];
560 }
561
562 else
563 _error_("unrecognized int pyarray type in input!");
564
565 if(py_vector2) delete(py_vector2);
566 }
567 else
568 vector=NULL;
569 }
570 else{
571 M=1;
572 vector=xNew<float>(M);
573 FetchData(&(vector[0]),py_vector);
574 }
575
576 /*output: */
577 if(pM)*pM=M;
578 if(pvector)*pvector=vector;
579}
580/*}}}*/
581/*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
582void FetchData(int** pvector,int* pM,PyObject* py_vector){
583
584 /*output: */
585 int* vector=NULL;
586 int M;
587 int ndim;
588 npy_intp* dims=NULL;
589
590 /*intermediary:*/
591 long* lvector=NULL;
592 bool* bvector=NULL;
593 double* dvector=NULL;
594 int i;
595 PyObject* py_vector2=NULL;
596
597 if (PyArray_Check((PyArrayObject*)py_vector)) {
598 /*retrieve dimensions: */
599 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
600 if (ndim==1) {
601 dims=PyArray_DIMS((PyArrayObject*)py_vector);
602 M=dims[0];
603 }
604 else if (ndim==2) {
605 dims=PyArray_DIMS((PyArrayObject*)py_vector);
606 if (dims[1]==1)
607 M=dims[0];
608 else
609 _error_("expecting an Mx1 matrix or M vector in input!");
610 }
611 else
612 _error_("expecting an Mx1 matrix or M vector in input!");
613
614 if (M) {
615 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
616 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
617 py_vector=py_vector2;
618 }
619
620 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
621 /*retrieve internal value: */
622 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
623
624 /*transform into int vector: */
625 vector=xNew<int>(M);
626 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
627 }
628
629 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
630 /*retrieve internal value: */
631 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
632
633 /*transform into int vector: */
634 vector=xNew<int>(M);
635 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
636 }
637
638 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
639 /*retrieve internal value: */
640 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
641
642 /*transform into int vector: */
643 vector=xNew<int>(M);
644 for(i=0;i<M;i++)vector[i]=(int)bvector[i];
645 }
646
647 else
648 _error_("unrecognized int pyarray type in input!");
649
650 if (py_vector2)
651 delete(py_vector2);
652 }
653 else
654 vector=NULL;
655 }
656
657 else {
658 M=1;
659 vector=xNew<int>(M);
660 FetchData(&(vector[0]),py_vector);
661 }
662
663 /*output: */
664 if(pM)*pM=M;
665 if(pvector)*pvector=vector;
666}
667/*}}}*/
668/*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
669void FetchData(bool** pvector,int* pM,PyObject* py_vector){
670
671 /*output: */
672 bool* bvector=NULL;
673 bool* vector=NULL;
674 int M;
675 int ndim;
676 npy_intp* dims=NULL;
677
678 /*intermediary:*/
679 double* dvector=NULL;
680 long* lvector=NULL;
681 int i;
682 PyObject* py_vector2=NULL;
683
684 if (PyArray_Check((PyArrayObject*)py_vector)) {
685 /*retrieve dimensions: */
686 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
687 if (ndim==1) {
688 dims=PyArray_DIMS((PyArrayObject*)py_vector);
689 M=dims[0];
690 }
691 else if (ndim==2) {
692 dims=PyArray_DIMS((PyArrayObject*)py_vector);
693 if (dims[1]==1)
694 M=dims[0];
695 else
696 _error_("expecting an Mx1 matrix or M vector in input!");
697 }
698 else
699 _error_("expecting an Mx1 matrix or M vector in input!");
700
701 if (M) {
702 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
703 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
704 py_vector=py_vector2;
705 }
706
707 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
708 /*retrieve internal value: */
709 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
710
711 /*transform into bool vector: */
712 vector=xNew<bool>(M);
713 for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
714 }
715
716 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
717 /*retrieve internal value: */
718 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
719
720 /*transform into bool vector: */
721 vector=xNew<bool>(M);
722 for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
723 }
724
725 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
726 /*retrieve internal value: */
727 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
728
729 /*copy vector: */
730 vector=xNew<bool>(M);
731 memcpy(vector,bvector,(M)*sizeof(bool));
732 }
733
734 else
735 _error_("unrecognized bool pyarray type in input!");
736
737 if (py_vector2)
738 delete(py_vector2);
739 }
740 else
741 vector=NULL;
742 }
743
744 else {
745 M=1;
746 vector=xNew<bool>(M);
747 FetchData(&(vector[0]),py_vector);
748 }
749
750 /*output: */
751 if(pM)*pM=M;
752 if(pvector)*pvector=vector;
753}
754/*}}}*/
755
756/*ISSM objects*/
757/*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
758void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
759
760 /*Initialize output*/
761 BamgGeom* bamggeom=new BamgGeom();
762
763 /*Fetch all fields*/
764 FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
765 FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
766 FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
767 FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
768 FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
769 FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
770 FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
771
772 /*Assign output pointers:*/
773 *pbamggeom=bamggeom;
774}
775/*}}}*/
776/*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
777void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
778
779 /*Initialize output*/
780 BamgMesh* bamgmesh=new BamgMesh();
781
782 /*Fetch all fields*/
783 FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
784 FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
785 FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
786 FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
787 FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
788 FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
789 FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
790 FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
791
792 /*Assign output pointers:*/
793 *pbamgmesh=bamgmesh;
794}
795/*}}}*/
796/*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
797void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
798
799 /*Initialize output*/
800 BamgOpts* bamgopts=new BamgOpts();
801
802 /*Fetch all fields*/
803 FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
804 FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
805 FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
806 FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
807 FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
808 FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
809 FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
810 FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
811 FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
812 FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
813 FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
814 FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
815 FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
816 FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
817
818 FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
819 FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
820 FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
821
822 FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
823 FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
824 FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
825 FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
826 FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
827 FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
828 FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
829 FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
830
831 /*Additional checks*/
832 bamgopts->Check();
833
834 /*Assign output pointers:*/
835 *pbamgopts=bamgopts;
836}
837/*}}}*/
838/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
839void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
840
841 char *name = NULL;
842 Option *option = NULL;
843
844 /*Initialize output*/
845 Options* options=new Options();
846
847 /*Fetch all options*/
848 for (int i=istart; i<nrhs; i=i+2){
849 #if _PYTHON_MAJOR_ >= 3
850 if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
851 #else
852 if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
853 #endif
854
855 FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
856 if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
857
858 _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
859
860// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
861// options->AddOption(option);
862// option=NULL;
863 }
864
865 /*Assign output pointers:*/
866 *poptions=options;
867}
868/*}}}*/
869/*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
870void FetchData(Contours** pcontours,PyObject* py_list){
871
872 int numcontours,test1,test2;
873 char *contourname = NULL;
874 Contours *contours = NULL;
875 Contour<double> *contouri = NULL;
876 PyObject *py_dicti = NULL;
877 PyObject *py_item = NULL;
878
879 #if _PYTHON_MAJOR_ >= 3
880 if (PyUnicode_Check(py_list)){
881 FetchData(&contourname,py_list);
882 contours=ExpRead<double>(contourname);
883 }
884 #else
885 if (PyString_Check(py_list)){
886 FetchData(&contourname,py_list);
887 contours=ExpRead<double>(contourname);
888 }
889 #endif
890 else if(PyList_Check(py_list)){
891
892 contours=new Contours();
893 numcontours=(int)PyList_Size(py_list);
894
895 for(int i=0;i<numcontours;i++){
896
897 contouri=new Contour<double>();
898 py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
899
900 py_item = PyDict_GetItemString(py_dicti,"nods");
901 if(!py_item) _error_("input structure does not have a 'nods' field");
902 FetchData(&contouri->nods,py_item);
903
904 py_item = PyDict_GetItemString(py_dicti,"x");
905 if(!py_item) _error_("input structure does not have a 'x' field");
906 FetchData(&contouri->x,&test1,&test2,py_item);
907 if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
908
909 py_item = PyDict_GetItemString(py_dicti,"y");
910 if(!py_item) _error_("input structure does not have a 'y' field");
911 FetchData(&contouri->y,&test1,&test2,py_item);
912 if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
913
914 contours->AddObject(contouri);
915 }
916 }
917 else{
918 _error_("Contour is neither a string nor a structure and cannot be loaded");
919 }
920
921 /*clean-up and assign output pointer*/
922 xDelete<char>(contourname);
923 *pcontours=contours;
924}
925/*}}}*/
926void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN,PyObject* EWGTS_IN){
927 _error_("Nathan... I need your help here");
928}
929
930/*Python version dependent: */
931#if _PYTHON_MAJOR_ >= 3
932/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
933void FetchData(char** pstring,PyObject* py_unicode){
934
935 PyObject* py_bytes;
936 char* string=NULL;
937
938 /*convert to bytes format: */
939 PyUnicode_FSConverter(py_unicode,&py_bytes);
940
941 /*convert from bytes to string: */
942 string=PyBytes_AS_STRING(py_bytes);
943
944 /*copy string (note strlen does not include trailing NULL): */
945 *pstring=xNew<char>(strlen(string)+1);
946 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
947}
948/*}}}*/
949#else
950/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
951void FetchData(char** pstring,PyObject* py_string){
952
953 char* string=NULL;
954
955 /*extract internal string: */
956 string=PyString_AsString(py_string);
957
958 /*copy string (note strlen does not include trailing NULL): */
959 *pstring=xNew<char>(strlen(string)+1);
960 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
961}
962/*}}}*/
963#endif
Note: See TracBrowser for help on using the repository browser.