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
RevLine 
[12011]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
[13749]14#include "./pythonio.h"
15#include "../../c/shared/shared.h"
[12011]16
[12073]17/*Primitive data types*/
[12365]18/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
[12011]19void FetchData(double* pscalar,PyObject* py_float){
20
[14093]21 double dscalar;
[12011]22
23 /*return internal value: */
[14097]24 if (PyFloat_Check(py_float))
[14093]25 dscalar=PyFloat_AsDouble(py_float);
[19896]26 else if (PyLong_Check(py_float)){
27 #if _PYTHON_MAJOR_ == 3
[19895]28 dscalar=(double)PyLong_AsLong(py_float);
[19896]29 #else
[14093]30 dscalar=PyLong_AsDouble(py_float);
[19896]31 #endif
32 }
33 else if (PyInt_Check(py_float))
34 dscalar=(double)PyInt_AsLong(py_float);
[14097]35 else if (PyBool_Check(py_float))
[14093]36 dscalar=(double)PyLong_AsLong(py_float);
[14097]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));
[14093]41 else
42 _error_("unrecognized float type in input!");
[12011]43
44 /*output: */
[14093]45 *pscalar=dscalar;
[12011]46}
47/*}}}*/
[22674]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/*}}}*/
[14093]78/*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
79void FetchData(int* pscalar, PyObject* py_long){
[12011]80
[14093]81 int iscalar;
[12011]82
83 /*return internal value: */
[19895]84 if (PyLong_Check(py_long))
85 iscalar=(int)PyLong_AsLong(py_long);
[19896]86 else if (PyInt_Check(py_long))
87 iscalar=(int)PyInt_AsLong(py_long);
[14097]88 else if (PyFloat_Check(py_long))
[14093]89 iscalar=(int)PyFloat_AsDouble(py_long);
[14097]90 else if (PyBool_Check(py_long))
[14093]91 iscalar=(int)PyLong_AsLong(py_long);
[14097]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));
[14093]96 else
97 _error_("unrecognized long type in input!");
[12011]98
99 /*output: */
[14093]100 *pscalar=iscalar;
[12011]101}
102/*}}}*/
[14093]103/*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
104void FetchData(bool* pscalar,PyObject* py_boolean){
[12011]105
[14093]106 bool bscalar;
[13622]107
[14097]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);
[19895]113 else if (PyLong_Check(py_boolean))
114 bscalar=(bool)PyLong_AsLong(py_boolean);
[19896]115 else if (PyInt_Check(py_boolean))
116 bscalar=(bool)PyInt_AsLong(py_boolean);
[14097]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!");
[12011]123
[14097]124 /*output: */
[14093]125 *pscalar=bscalar;
[12011]126}
127/*}}}*/
[12365]128/*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
[12112]129void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
[12073]130
[12112]131 /*output: */
[13372]132 double* dmatrix=NULL;
[12112]133 double* matrix=NULL;
134 int M,N;
[13373]135 int ndim;
[12112]136 npy_intp* dims=NULL;
137
[13990]138 /*intermediary:*/
139 long* lmatrix=NULL;
140 bool* bmatrix=NULL;
[14317]141 int i;
142 PyObject* py_matrix2=NULL;
[13990]143
[14093]144 if (PyArray_Check((PyArrayObject*)py_matrix)) {
145 /*retrieve dimensions: */
146 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
[14098]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!");
[13622]157
[14317]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 }
[14316]163
[14093]164 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
165 /*retrieve internal value: */
166 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
[12112]167
[14093]168 /*copy matrix: */
169 matrix=xNew<double>(M*N);
[14317]170// if (PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
[14316]171 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
[14317]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// }
[19683]190
[14093]191 }
[13990]192
[14316]193 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
[14093]194 /*retrieve internal value: */
195 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
[13990]196
[14093]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 }
[13990]201
[14093]202 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
203 /*retrieve internal value: */
204 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
[13990]205
[14093]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
[14234]212 _error_("unrecognized double pyarray type in input!");
[14317]213
[19683]214 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
[13990]215 }
216 else
[14093]217 matrix=NULL;
[13484]218 }
[14093]219
[14097]220 else {
[14093]221 M=1;
222 N=1;
223 matrix=xNew<double>(M*N);
224 FetchData(&(matrix[0]),py_matrix);
225 }
226
[12112]227 /*output: */
228 if(pM)*pM=M;
229 if(pN)*pN=N;
230 if(pmatrix)*pmatrix=matrix;
231}
232/*}}}*/
[12776]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;
[13990]239 int ndim;
240 npy_intp* dims=NULL;
[12776]241
242 /*intermediary:*/
[13990]243 double* dmatrix=NULL;
244 long* lmatrix=NULL;
245 bool* bmatrix=NULL;
[12776]246 int i;
[14317]247 PyObject* py_matrix2=NULL;
[12776]248
[14093]249 if (PyArray_Check((PyArrayObject*)py_matrix)) {
250 /*retrieve dimensions: */
251 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
[14098]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!");
[13622]262
[14093]263 if (M && N) {
[14317]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
[14093]269 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
270 /*retrieve internal value: */
271 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
[12776]272
[14093]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 }
[13990]277
[14316]278 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
[14093]279 /*retrieve internal value: */
280 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
[13990]281
[14093]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 }
[13990]286
[14093]287 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
288 /*retrieve internal value: */
289 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
[13990]290
[14093]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
[14234]297 _error_("unrecognized int pyarray type in input!");
[14317]298
[21201]299 /* These lines are causing a segfault
[14317]300 if (py_matrix2)
301 delete(py_matrix2);
[21201]302 */
[13990]303 }
304 else
[14093]305 matrix=NULL;
[13484]306 }
[14093]307
[14097]308 else {
[14093]309 M=1;
310 N=1;
311 matrix=xNew<int>(M*N);
312 FetchData(&(matrix[0]),py_matrix);
313 }
314
[12776]315 /*output: */
316 if(pM)*pM=M;
317 if(pN)*pN=N;
318 if(pmatrix)*pmatrix=matrix;
319}
320/*}}}*/
[14234]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;
[14317]335 PyObject* py_matrix2=NULL;
[14234]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) {
[14317]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
[14234]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
[14316]366 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
[14234]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!");
[14317]386
387 if (py_matrix2)
388 delete(py_matrix2);
[14234]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/*}}}*/
[12365]407/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
[12120]408void FetchData(double** pvector,int* pM,PyObject* py_vector){
[12112]409
[12120]410 /*output: */
[13372]411 double* dvector=NULL;
[12120]412 double* vector=NULL;
413 int M;
[13373]414 int ndim;
[12120]415 npy_intp* dims=NULL;
416
[13990]417 /*intermediary:*/
418 long* lvector=NULL;
419 bool* bvector=NULL;
420 int i;
[14317]421 PyObject* py_vector2=NULL;
[13990]422
[14236]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!");
[13622]439
[14236]440 if (M) {
[14317]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
[14236]446 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
447 /*retrieve internal value: */
448 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
[12120]449
[14236]450 /*copy vector: */
451 vector=xNew<double>(M);
452 memcpy(vector,dvector,(M)*sizeof(double));
453 }
[13990]454
[14316]455 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
[14236]456 /*retrieve internal value: */
457 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
[13990]458
[14236]459 /*transform into double vector: */
460 vector=xNew<double>(M);
461 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
462 }
[13990]463
[14236]464 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
465 /*retrieve internal value: */
466 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
[13990]467
[14236]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!");
[14317]475
[21201]476 /* Causing a seg fault.
[14317]477 if (py_vector2)
478 delete(py_vector2);
[21201]479 */
[13990]480 }
481 else
[14236]482 vector=NULL;
[13484]483 }
[13372]484
[14236]485 else {
486 M=1;
487 vector=xNew<double>(M);
488 FetchData(&(vector[0]),py_vector);
489 }
490
[12120]491 /*output: */
492 if(pM)*pM=M;
493 if(pvector)*pvector=vector;
494}
495/*}}}*/
[22674]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/*}}}*/
[14221]581/*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
582void FetchData(int** pvector,int* pM,PyObject* py_vector){
[13310]583
[14221]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;
[14317]595 PyObject* py_vector2=NULL;
[14221]596
[14236]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!");
[14221]613
[14236]614 if (M) {
[14317]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
[14236]620 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
621 /*retrieve internal value: */
622 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
[14221]623
[14236]624 /*transform into int vector: */
625 vector=xNew<int>(M);
626 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
627 }
[14221]628
[14316]629 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
[14236]630 /*retrieve internal value: */
631 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
[14221]632
[14236]633 /*transform into int vector: */
634 vector=xNew<int>(M);
635 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
636 }
[14221]637
[14236]638 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
639 /*retrieve internal value: */
640 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
[14221]641
[14236]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!");
[14317]649
650 if (py_vector2)
651 delete(py_vector2);
[14221]652 }
653 else
[14236]654 vector=NULL;
[14221]655 }
656
[14236]657 else {
658 M=1;
659 vector=xNew<int>(M);
660 FetchData(&(vector[0]),py_vector);
661 }
662
[14221]663 /*output: */
664 if(pM)*pM=M;
665 if(pvector)*pvector=vector;
666}
667/*}}}*/
[14234]668/*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
669void FetchData(bool** pvector,int* pM,PyObject* py_vector){
[14221]670
[14234]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;
[14317]682 PyObject* py_vector2=NULL;
[14234]683
[14236]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!");
[14234]700
[14236]701 if (M) {
[14317]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
[14236]707 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
708 /*retrieve internal value: */
709 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
[14234]710
[14236]711 /*transform into bool vector: */
712 vector=xNew<bool>(M);
713 for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
714 }
[14234]715
[14316]716 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
[14236]717 /*retrieve internal value: */
718 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
[14234]719
[14236]720 /*transform into bool vector: */
721 vector=xNew<bool>(M);
722 for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
723 }
[14234]724
[14236]725 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
726 /*retrieve internal value: */
727 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
[14234]728
[14236]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!");
[14317]736
737 if (py_vector2)
738 delete(py_vector2);
[14234]739 }
740 else
[14236]741 vector=NULL;
[14234]742 }
743
[14236]744 else {
745 M=1;
746 vector=xNew<bool>(M);
747 FetchData(&(vector[0]),py_vector);
748 }
749
[14234]750 /*output: */
751 if(pM)*pM=M;
752 if(pvector)*pvector=vector;
753}
754/*}}}*/
755
[13310]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;
[13287]774}
775/*}}}*/
[13310]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;
[13287]794}
795/*}}}*/
[13310]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;
[13287]836}
837/*}}}*/
[13374]838/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
[13373]839void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
[12120]840
[13373]841 char *name = NULL;
842 Option *option = NULL;
843
[12776]844 /*Initialize output*/
845 Options* options=new Options();
846
[13373]847 /*Fetch all options*/
848 for (int i=istart; i<nrhs; i=i+2){
[19896]849 #if _PYTHON_MAJOR_ >= 3
[19895]850 if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
[19896]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
[12776]854
[13373]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
[15105]858 _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
[13373]859
860// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
861// options->AddOption(option);
862// option=NULL;
863 }
864
[12776]865 /*Assign output pointers:*/
866 *poptions=options;
867}
868/*}}}*/
[15335]869/*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
870void FetchData(Contours** pcontours,PyObject* py_list){
[12776]871
[13368]872 int numcontours,test1,test2;
873 char *contourname = NULL;
[15335]874 Contours *contours = NULL;
[13368]875 Contour<double> *contouri = NULL;
876 PyObject *py_dicti = NULL;
877 PyObject *py_item = NULL;
878
[19896]879 #if _PYTHON_MAJOR_ >= 3
[19895]880 if (PyUnicode_Check(py_list)){
[13368]881 FetchData(&contourname,py_list);
[14377]882 contours=ExpRead<double>(contourname);
[13368]883 }
[19896]884 #else
885 if (PyString_Check(py_list)){
886 FetchData(&contourname,py_list);
887 contours=ExpRead<double>(contourname);
888 }
889 #endif
[13368]890 else if(PyList_Check(py_list)){
891
[15335]892 contours=new Contours();
[13368]893 numcontours=(int)PyList_Size(py_list);
894
895 for(int i=0;i<numcontours;i++){
896
[15335]897 contouri=new Contour<double>();
[13368]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*/
[13372]922 xDelete<char>(contourname);
[13368]923 *pcontours=contours;
[13353]924}
925/*}}}*/
[22674]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}
[13353]929
[12073]930/*Python version dependent: */
931#if _PYTHON_MAJOR_ >= 3
[12365]932/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
[12073]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);
[13622]943
[19893]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));
[12073]947}
948/*}}}*/
949#else
[12365]950/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
[12073]951void FetchData(char** pstring,PyObject* py_string){
952
953 char* string=NULL;
954
955 /*extract internal string: */
956 string=PyString_AsString(py_string);
[13372]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));
[12073]961}
962/*}}}*/
963#endif
Note: See TracBrowser for help on using the repository browser.