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

Last change on this file since 26565 was 26565, checked in by bdef, 3 years ago

NEW: adding some single precision import

File size: 37.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 _PYTHON_MAJOR_ == 3
25 if (PyFloat_Check(py_float))
26 dscalar=PyFloat_AsDouble(py_float);
27 else if (PyLong_Check(py_float))
28 dscalar=(double)PyLong_AsLong(py_float);
29 else if (PyBool_Check(py_float))
30 dscalar=(double)PyLong_AsLong(py_float);
31 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
32 FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
33 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
34 FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
35 else
36 _error_("unrecognized float type py3 in input!");
37
38 #else
39 if (PyFloat_Check(py_float))
40 dscalar=PyFloat_AsDouble(py_float);
41 else if (PyLong_Check(py_float))
42 dscalar=PyLong_AsDouble(py_float);
43 else if (PyInt_Check(py_float))
44 dscalar=(double)PyInt_AsLong(py_float);
45 else if (PyBool_Check(py_float))
46 dscalar=(double)PyLong_AsLong(py_float);
47 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
48 FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
49 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
50 FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
51 else
52 _error_("unrecognized float type in py2 input!");
53 #endif
54 /*output: */
55 *pscalar=dscalar;
56}
57/*}}}*/
58/*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
59void FetchData(float* pscalar,PyObject* py_float){
60
61 float fscalar;
62
63 /*return internal value: */
64 #if _PYTHON_MAJOR_ == 3
65 if (PyFloat_Check(py_float))
66 fscalar=PyFloat_AsDouble(py_float);
67 else if (PyLong_Check(py_float))
68 fscalar=(float)PyLong_AsLong(py_float);
69 else if (PyBool_Check(py_float))
70 fscalar=(float)PyLong_AsLong(py_float);
71 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
72 FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
73 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
74 FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
75 else
76 _error_("unrecognized float type in input!");
77 #else
78 if (PyFloat_Check(py_float))
79 fscalar=PyFloat_AsDouble(py_float);
80 else if (PyLong_Check(py_float))
81 fscalar=(float)PyLong_AsDouble(py_float);
82 else if (PyInt_Check(py_float))
83 fscalar=(float)PyInt_AsLong(py_float);
84 else if (PyBool_Check(py_float))
85 fscalar=(float)PyLong_AsLong(py_float);
86 else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
87 FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
88 else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
89 FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
90 else
91 _error_("unrecognized float type in input!");
92 #endif
93 /*output: */
94 *pscalar=fscalar;
95}
96/*}}}*/
97/*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
98void FetchData(int* pscalar, PyObject* py_long){
99
100 int iscalar;
101
102 /*return internal value: */
103 #if _PYTHON_MAJOR_ == 3
104 if (PyLong_Check(py_long))
105 iscalar=(int)PyLong_AsLong(py_long);
106 else if (PyFloat_Check(py_long))
107 iscalar=(int)PyFloat_AsDouble(py_long);
108 else if (PyBool_Check(py_long))
109 iscalar=(int)PyLong_AsLong(py_long);
110 else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
111 FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
112 else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
113 FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
114 else
115 _error_("unrecognized long type in input!");
116
117 #else
118 if (PyLong_Check(py_long))
119 iscalar=(int)PyLong_AsLong(py_long);
120 else if (PyInt_Check(py_long))
121 iscalar=(int)PyInt_AsLong(py_long);
122 else if (PyFloat_Check(py_long))
123 iscalar=(int)PyFloat_AsDouble(py_long);
124 else if (PyBool_Check(py_long))
125 iscalar=(int)PyLong_AsLong(py_long);
126 else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
127 FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
128 else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
129 FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
130 else
131 _error_("unrecognized long type in input!");
132 #endif
133 /*output: */
134 *pscalar=iscalar;
135}
136/*}}}*/
137/*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
138void FetchData(bool* pscalar,PyObject* py_boolean){
139
140 bool bscalar;
141
142 /*return internal value: */
143 #if _PYTHON_MAJOR_ == 3
144 if (PyBool_Check(py_boolean))
145 bscalar=(bool)PyLong_AsLong(py_boolean);
146 else if (PyLong_Check(py_boolean))
147 bscalar=(bool)PyLong_AsLong(py_boolean);
148 else if (PyLong_Check(py_boolean))
149 bscalar=(bool)PyLong_AsLong(py_boolean);
150 else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
151 FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
152 else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
153 FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
154 else
155 _error_("unrecognized boolean type in input!");
156
157 #else
158 if (PyBool_Check(py_boolean))
159 bscalar=(bool)PyLong_AsLong(py_boolean);
160 else if (PyLong_Check(py_boolean))
161 bscalar=(bool)PyLong_AsLong(py_boolean);
162 else if (PyLong_Check(py_boolean))
163 bscalar=(bool)PyLong_AsLong(py_boolean);
164 else if (PyInt_Check(py_boolean))
165 bscalar=(bool)PyInt_AsLong(py_boolean);
166 else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
167 FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
168 else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
169 FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
170 else
171 _error_("unrecognized boolean type in input!");
172 #endif
173 /*output: */
174 *pscalar=bscalar;
175}
176/*}}}*/
177/*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
178void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
179
180 /*output: */
181 double* dmatrix=NULL;
182 double* matrix=NULL;
183 int M=0;
184 int N=0;
185 int ndim;
186 npy_intp* dims=NULL;
187
188 /*intermediary:*/
189 long* lmatrix=NULL;
190 bool* bmatrix=NULL;
191 float* smatrix=NULL;
192 int i;
193 PyObject* py_matrix2=NULL;
194
195 if (PyArray_Check((PyArrayObject*)py_matrix)) {
196 /*retrieve dimensions: */
197 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
198 if (ndim==2) {
199 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
200 M=dims[0]; N=dims[1];
201 }
202 else if (ndim==1) {
203 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
204 M=dims[0]; N=1;
205 }
206 else
207 _error_("expecting an MxN matrix or M vector in input!");
208
209 if (M && N) {
210 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
211 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
212 py_matrix=py_matrix2;
213 }
214
215 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_FLOAT) {
216 /*retrieve internal value: */
217 smatrix=(float*)PyArray_DATA((PyArrayObject*)py_matrix);
218
219 /*transform into double matrix: */
220 matrix=xNew<double>(M*N);
221 for(i=0;i<M*N;i++)matrix[i]=(double)smatrix[i];
222 }
223
224 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
225 /*retrieve internal value: */
226 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
227
228 /*copy matrix: */
229 matrix=xNew<double>(M*N);
230 memcpy(matrix,dmatrix,(M*N)*sizeof(double));
231 }
232
233 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
234 /*retrieve internal value: */
235 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
236
237 /*transform into double matrix: */
238 matrix=xNew<double>(M*N);
239 for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
240 }
241
242 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
243 /*retrieve internal value: */
244 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
245
246 /*transform into double matrix: */
247 matrix=xNew<double>(M*N);
248 for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
249 }
250
251 else
252 _error_("unrecognized double pyarray type in input!");
253
254 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
255 }
256 else
257 matrix=NULL;
258 }
259 else if (PyList_Check(py_matrix)) {
260 /*retrieve dimensions: */
261 M=(int)PyList_Size(py_matrix);
262 N=1;
263 if (M) {
264 matrix=xNew<double>(M);
265 for (int index = 0; index < M; index++) {
266 PyObject *item;
267 item = PyList_GetItem(py_matrix, index);
268 if ((int)PyList_Size(item)>1)
269 _error_("2D lists are not suported");
270 FetchData(&(matrix[index]),item);
271 }
272 }
273 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
274 else
275 matrix=NULL;
276 }
277 else if (PyTuple_Check(py_matrix)) {
278 /*retrieve dimensions: */
279 M=(int)PyTuple_Size(py_matrix);
280 N=1;
281 if (M) {
282 matrix=xNew<double>(M);
283 for (int index = 0; index < M; index++) {
284 PyObject *item;
285 item = PyTuple_GetItem(py_matrix, index);
286 if ((int)PyTuple_Size(item)>1)
287 _error_("2D tuple are not suported");
288 FetchData(&(matrix[index]),item);
289 }
290 }
291 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
292 else
293 matrix=NULL;
294 }
295
296 else {
297 M=1;
298 N=1;
299 matrix=xNew<double>(M*N);
300 FetchData(&(matrix[0]),py_matrix);
301 }
302
303 /*output: */
304 if(pM)*pM=M;
305 if(pN)*pN=N;
306 if(pmatrix)*pmatrix=matrix;
307}
308/*}}}*/
309/*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
310void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
311
312 /*output: */
313 int* matrix=NULL;
314 int M=0;
315 int N=0;
316 int ndim;
317 npy_intp* dims=NULL;
318
319 /*intermediary:*/
320 double* dmatrix=NULL;
321 long* lmatrix=NULL;
322 bool* bmatrix=NULL;
323 int i;
324 PyObject* py_matrix2=NULL;
325
326 if (PyArray_Check((PyArrayObject*)py_matrix)) {
327 /*retrieve dimensions: */
328 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
329 if (ndim==2) {
330 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
331 M=dims[0]; N=dims[1];
332 }
333 else if (ndim==1) {
334 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
335 M=dims[0]; N=1;
336 }
337 else
338 _error_("expecting an MxN matrix or M vector in input!");
339
340 if (M && N) {
341 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
342 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
343 py_matrix=py_matrix2;
344 }
345
346 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
347 /*retrieve internal value: */
348 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
349
350 /*transform into integer matrix: */
351 matrix=xNew<int>(M*N);
352 for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
353 }
354
355 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
356 /*retrieve internal value: */
357 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
358
359 /*transform into integer matrix: */
360 matrix=xNew<int>(M*N);
361 for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
362 }
363
364 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
365 /*retrieve internal value: */
366 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
367
368 /*transform into integer matrix: */
369 matrix=xNew<int>(M*N);
370 for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
371 }
372
373 else
374 _error_("unrecognized int pyarray type in input!");
375
376 /* These lines are causing a segfault
377 if (py_matrix2)
378 delete(py_matrix2);
379 */
380 }
381 else
382 matrix=NULL;
383 }
384 else if (PyList_Check(py_matrix)) {
385 /*retrieve dimensions: */
386 M=(int)PyList_Size(py_matrix);
387 N=1;
388 if (M) {
389 matrix=xNew<int>(M);
390 for (int index = 0; index < M; index++) {
391 PyObject *item;
392 item = PyList_GetItem(py_matrix, index);
393 if ((int)PyList_Size(item)>1)
394 _error_("2D lists are not suported");
395 FetchData(&(matrix[index]),item);
396 }
397 }
398 else
399 matrix=NULL;
400 }
401 else if (PyTuple_Check(py_matrix)) {
402 /*retrieve dimensions: */
403 M=(int)PyTuple_Size(py_matrix);
404 N=1;
405 if (M) {
406 matrix=xNew<int>(M);
407 for (int index = 0; index < M; index++) {
408 PyObject *item;
409 item = PyTuple_GetItem(py_matrix, index);
410 if ((int)PyTuple_Size(item)>1)
411 _error_("2D tuple are not suported");
412 FetchData(&(matrix[index]),item);
413 }
414 }
415 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
416 else
417 matrix=NULL;
418 }
419
420 else {
421 M=1;
422 N=1;
423 matrix=xNew<int>(M*N);
424 FetchData(&(matrix[0]),py_matrix);
425 }
426
427 /*output: */
428 if(pM)*pM=M;
429 if(pN)*pN=N;
430 if(pmatrix)*pmatrix=matrix;
431}
432/*}}}*/
433/*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
434void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
435
436 /*output: */
437 bool* bmatrix=NULL;
438 bool* matrix=NULL;
439 int M=0;
440 int N=0;
441 int ndim;
442 npy_intp* dims=NULL;
443
444 /*intermediary:*/
445 double* dmatrix=NULL;
446 long* lmatrix=NULL;
447 int i;
448 PyObject* py_matrix2=NULL;
449
450 if (PyArray_Check((PyArrayObject*)py_matrix)) {
451 /*retrieve dimensions: */
452 ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
453 if (ndim==2) {
454 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
455 M=dims[0]; N=dims[1];
456 }
457 else if (ndim==1) {
458 dims=PyArray_DIMS((PyArrayObject*)py_matrix);
459 M=dims[0]; N=1;
460 }
461 else
462 _error_("expecting an MxN matrix or M vector in input!");
463
464 if (M && N) {
465 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
466 py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
467 py_matrix=py_matrix2;
468 }
469
470 if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
471 /*retrieve internal value: */
472 dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
473
474 /*transform into bool matrix: */
475 matrix=xNew<bool>(M*N);
476 for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
477 }
478
479 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
480 /*retrieve internal value: */
481 lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
482
483 /*transform into bool matrix: */
484 matrix=xNew<bool>(M*N);
485 for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
486 }
487
488 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
489 /*retrieve internal value: */
490 bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
491
492 /*copy matrix: */
493 matrix=xNew<bool>(M*N);
494 memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
495 }
496
497 else
498 _error_("unrecognized bool pyarray type in input!");
499
500 if (py_matrix2)
501 delete(py_matrix2);
502 }
503 else
504 matrix=NULL;
505 }
506 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
507
508 else if (PyList_Check(py_matrix)) {
509 /*retrieve dimensions: */
510 M=(int)PyList_Size(py_matrix);
511 N=1;
512 if (M) {
513 matrix=xNew<bool>(M);
514 for (int index = 0; index < M; index++) {
515 PyObject *item;
516 item = PyList_GetItem(py_matrix, index);
517 if ((int)PyList_Size(item)>1)
518 _error_("2D lists are not suported");
519 FetchData(&(matrix[index]),item);
520 }
521 }
522 else
523 matrix=NULL;
524 }
525 else if (PyTuple_Check(py_matrix)) {
526 /*retrieve dimensions: */
527 M=(int)PyTuple_Size(py_matrix);
528 N=1;
529 if (M) {
530 matrix=xNew<bool>(M);
531 for (int index = 0; index < M; index++) {
532 PyObject *item;
533 item = PyTuple_GetItem(py_matrix, index);
534 if ((int)PyTuple_Size(item)>1)
535 _error_("2D tuples are not suported");
536 FetchData(&(matrix[index]),item);
537 }
538 }
539 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
540 else
541 matrix=NULL;
542 }
543
544 else {
545 M=1;
546 N=1;
547 matrix=xNew<bool>(M*N);
548 FetchData(&(matrix[0]),py_matrix);
549 }
550
551 /*output: */
552 if(pM)*pM=M;
553 if(pN)*pN=N;
554 if(pmatrix)*pmatrix=matrix;
555}
556/*}}}*/
557/*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
558void FetchData(double** pvector,int* pM,PyObject* py_vector){
559
560 /*output: */
561 double* dvector=NULL;
562 double* vector=NULL;
563 int M=0;
564 int ndim;
565 npy_intp* dims=NULL;
566
567 /*intermediary:*/
568 long* lvector=NULL;
569 bool* bvector=NULL;
570 float* svector=NULL;
571 int i;
572 PyObject* py_vector2=NULL;
573
574 if (PyArray_Check((PyArrayObject*)py_vector)) {
575 /*retrieve dimensions: */
576 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
577 if (ndim==1) {
578 dims=PyArray_DIMS((PyArrayObject*)py_vector);
579 M=dims[0];
580 }
581 else if (ndim==2) {
582 dims=PyArray_DIMS((PyArrayObject*)py_vector);
583 if (dims[1]==1)
584 M=dims[0];
585 else
586 _error_("expecting an Mx1 matrix or M vector in input!");
587 }
588 else
589 _error_("expecting an Mx1 matrix or M vector in input!");
590
591 if (M) {
592 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
593 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
594 py_vector=py_vector2;
595 }
596
597 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_FLOAT) {
598 /*retrieve internal value: */
599 svector=(float*)PyArray_DATA((PyArrayObject*)py_vector);
600
601 /*transform into double matrix: */
602 vector=xNew<double>(M);
603 for(i=0;i<M;i++)vector[i]=(double)svector[i];
604 }
605 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
606 /*retrieve internal value: */
607 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
608
609 /*copy vector: */
610 vector=xNew<double>(M);
611 memcpy(vector,dvector,(M)*sizeof(double));
612 }
613
614 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
615 /*retrieve internal value: */
616 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
617
618 /*transform into double vector: */
619 vector=xNew<double>(M);
620 for(i=0;i<M;i++)vector[i]=(double)lvector[i];
621 }
622
623 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
624 /*retrieve internal value: */
625 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
626
627 /*transform into double vector: */
628 vector=xNew<double>(M);
629 for(i=0;i<M;i++)vector[i]=(double)bvector[i];
630 }
631
632 else
633 _error_("unrecognized double pyarray type in input!");
634
635 /* Causing a seg fault.
636 if (py_vector2)
637 delete(py_vector2);
638 */
639 }
640 else
641 vector=NULL;
642 }
643 else if (PyList_Check(py_vector)) {
644 /*retrieve dimensions: */
645 M=(int)PyList_Size(py_vector);
646
647 if (M) {
648 vector=xNew<double>(M);
649 for (int index = 0; index < M; index++) {
650 PyObject *item;
651 item = PyList_GetItem(py_vector, index);
652 FetchData(&(vector[index]),item);
653 }
654 }
655 else
656 vector=NULL;
657 }
658 else if (PyTuple_Check(py_vector)) {
659 /*retrieve dimensions: */
660 M=(int)PyTuple_Size(py_vector);
661
662 if (M) {
663 vector=xNew<double>(M);
664 for (int index = 0; index < M; index++) {
665 PyObject *item;
666 item = PyTuple_GetItem(py_vector, index);
667 FetchData(&(vector[index]),item);
668 }
669 }
670 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
671 else
672 vector=NULL;
673 }
674
675 else {
676 M=1;
677 vector=xNew<double>(M);
678 FetchData(&(vector[0]),py_vector);
679 }
680
681 /*output: */
682 if(pM)*pM=M;
683 if(pvector)*pvector=vector;
684}
685/*}}}*/
686/*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
687void FetchData(float** pvector,int* pM,PyObject* py_vector){
688
689 /*output: */
690 float* vector=NULL;
691 int M=0;
692 int ndim;
693 npy_intp* dims=NULL;
694
695 /*intermediary:*/
696 long* lvector=NULL;
697 bool* bvector=NULL;
698 double* dvector=NULL;
699 int i;
700 PyObject* py_vector2=NULL;
701
702 if (PyArray_Check((PyArrayObject*)py_vector)) {
703 /*retrieve dimensions: */
704 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
705 if (ndim==1) {
706 dims=PyArray_DIMS((PyArrayObject*)py_vector);
707 M=dims[0];
708 }
709 else if (ndim==2) {
710 dims=PyArray_DIMS((PyArrayObject*)py_vector);
711 if (dims[1]==1)
712 M=dims[0];
713 else
714 _error_("expecting an Mx1 matrix or M vector in input!");
715 }
716 else
717 _error_("expecting an Mx1 matrix or M vector in input!");
718
719 if (M) {
720 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
721 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
722 py_vector=py_vector2;
723 }
724
725 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
726 /*retrieve internal value: */
727 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
728
729 /*transform into int vector: */
730 vector=xNew<float>(M);
731 for(i=0;i<M;i++)vector[i]=(float)dvector[i];
732 }
733
734 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
735 /*retrieve internal value: */
736 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
737
738 /*transform into int vector: */
739 vector=xNew<float>(M);
740 for(i=0;i<M;i++)vector[i]=(float)lvector[i];
741 }
742
743 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
744 /*retrieve internal value: */
745 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
746
747 /*transform into int vector: */
748 vector=xNew<float>(M);
749 for(i=0;i<M;i++)vector[i]=(float)bvector[i];
750 }
751
752 else
753 _error_("unrecognized int pyarray type in input!");
754
755 if(py_vector2) delete(py_vector2);
756 }
757 else
758 vector=NULL;
759 }
760 else if (PyList_Check(py_vector)) {
761 /*retrieve dimensions: */
762 M=(int)PyList_Size(py_vector);
763
764 if (M) {
765 vector=xNew<float>(M);
766 for (int index = 0; index < M; index++) {
767 PyObject *item;
768 item = PyList_GetItem(py_vector, index);
769 FetchData(&(vector[index]),item);
770 }
771 }
772 else
773 vector=NULL;
774 }
775
776 else if (PyTuple_Check(py_vector)) {
777 /*retrieve dimensions: */
778 M=(int)PyTuple_Size(py_vector);
779
780 if (M) {
781 vector=xNew<float>(M);
782 for (int index = 0; index < M; index++) {
783 PyObject *item;
784 item = PyTuple_GetItem(py_vector, index);
785 FetchData(&(vector[index]),item);
786 }
787 }
788 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
789 else
790 vector=NULL;
791 }
792
793 else{
794 M=1;
795 vector=xNew<float>(M);
796 FetchData(&(vector[0]),py_vector);
797 }
798
799 /*output: */
800 if(pM)*pM=M;
801 if(pvector)*pvector=vector;
802}
803/*}}}*/
804/*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
805void FetchData(int** pvector,int* pM,PyObject* py_vector){
806
807 /*output: */
808 int* vector=NULL;
809 int M=0;
810 int ndim;
811 npy_intp* dims=NULL;
812
813 /*intermediary:*/
814 long* lvector=NULL;
815 bool* bvector=NULL;
816 double* dvector=NULL;
817 int i;
818 PyObject* py_vector2=NULL;
819
820 if (PyArray_Check((PyArrayObject*)py_vector)) {
821 /*retrieve dimensions: */
822 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
823 if (ndim==1) {
824 dims=PyArray_DIMS((PyArrayObject*)py_vector);
825 M=dims[0];
826 }
827 else if (ndim==2) {
828 dims=PyArray_DIMS((PyArrayObject*)py_vector);
829 if (dims[1]==1)
830 M=dims[0];
831 else
832 _error_("expecting an Mx1 matrix or M vector in input!");
833 }
834 else
835 _error_("expecting an Mx1 matrix or M vector in input!");
836
837 if (M) {
838 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
839 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
840 py_vector=py_vector2;
841 }
842
843 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
844 /*retrieve internal value: */
845 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
846
847 /*transform into int vector: */
848 vector=xNew<int>(M);
849 for(i=0;i<M;i++)vector[i]=(int)dvector[i];
850 }
851
852 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
853 /*retrieve internal value: */
854 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
855
856 /*transform into int vector: */
857 vector=xNew<int>(M);
858 for(i=0;i<M;i++)vector[i]=(int)lvector[i];
859 }
860
861 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
862 /*retrieve internal value: */
863 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
864
865 /*transform into int vector: */
866 vector=xNew<int>(M);
867 for(i=0;i<M;i++)vector[i]=(int)bvector[i];
868 }
869
870 else
871 _error_("unrecognized int pyarray type in input!");
872
873 if (py_vector2)
874 delete(py_vector2);
875 }
876 else
877 vector=NULL;
878 }
879
880 else if (PyList_Check(py_vector)) {
881 /*retrieve dimensions: */
882 M=(int)PyList_Size(py_vector);
883
884 if (M) {
885 vector=xNew<int>(M);
886 for (int index = 0; index < M; index++) {
887 PyObject *item;
888 item = PyList_GetItem(py_vector, index);
889 FetchData(&(vector[index]),item);
890 }
891 }
892 else
893 vector=NULL;
894 }
895
896 else if (PyTuple_Check(py_vector)) {
897 /*retrieve dimensions: */
898 M=(int)PyTuple_Size(py_vector);
899
900 if (M) {
901 vector=xNew<int>(M);
902 for (int index = 0; index < M; index++) {
903 PyObject *item;
904 item = PyTuple_GetItem(py_vector, index);
905 FetchData(&(vector[index]),item);
906 }
907 }
908 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
909 else
910 vector=NULL;
911 }
912
913 else {
914 M=1;
915 vector=xNew<int>(M);
916 FetchData(&(vector[0]),py_vector);
917 }
918
919 /*output: */
920 if(pM)*pM=M;
921 if(pvector)*pvector=vector;
922}
923/*}}}*/
924/*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
925void FetchData(bool** pvector,int* pM,PyObject* py_vector){
926
927 /*output: */
928 bool* bvector=NULL;
929 bool* vector=NULL;
930 int M=0;
931 int ndim;
932 npy_intp* dims=NULL;
933
934 /*intermediary:*/
935 double* dvector=NULL;
936 long* lvector=NULL;
937 int i;
938 PyObject* py_vector2=NULL;
939
940 if (PyArray_Check((PyArrayObject*)py_vector)) {
941 /*retrieve dimensions: */
942 ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
943 if (ndim==1) {
944 dims=PyArray_DIMS((PyArrayObject*)py_vector);
945 M=dims[0];
946 }
947 else if (ndim==2) {
948 dims=PyArray_DIMS((PyArrayObject*)py_vector);
949 if (dims[1]==1)
950 M=dims[0];
951 else
952 _error_("expecting an Mx1 matrix or M vector in input!");
953 }
954 else
955 _error_("expecting an Mx1 matrix or M vector in input!");
956
957 if (M) {
958 if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
959 py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
960 py_vector=py_vector2;
961 }
962
963 if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
964 /*retrieve internal value: */
965 dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
966
967 /*transform into bool vector: */
968 vector=xNew<bool>(M);
969 for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
970 }
971
972 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
973 /*retrieve internal value: */
974 lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
975
976 /*transform into bool vector: */
977 vector=xNew<bool>(M);
978 for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
979 }
980
981 else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
982 /*retrieve internal value: */
983 bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
984
985 /*copy vector: */
986 vector=xNew<bool>(M);
987 memcpy(vector,bvector,(M)*sizeof(bool));
988 }
989
990 else
991 _error_("unrecognized bool pyarray type in input!");
992
993 if (py_vector2)
994 delete(py_vector2);
995 }
996 else
997 vector=NULL;
998 }
999 else if (PyList_Check(py_vector)) {
1000 /*retrieve dimensions: */
1001 M=(int)PyList_Size(py_vector);
1002 if (M) {
1003 vector=xNew<bool>(M);
1004 for (int index = 0; index < M; index++) {
1005 PyObject *item;
1006 item = PyList_GetItem(py_vector, index);
1007 FetchData(&(vector[index]),item);
1008 }
1009 }
1010 else
1011 vector=NULL;
1012 }
1013
1014 else if (PyTuple_Check(py_vector)) {
1015 /*retrieve dimensions: */
1016 M=(int)PyTuple_Size(py_vector);
1017
1018 if (M) {
1019 vector=xNew<bool>(M);
1020 for (int index = 0; index < M; index++) {
1021 PyObject *item;
1022 item = PyTuple_GetItem(py_vector, index);
1023 FetchData(&(vector[index]),item);
1024 }
1025 }
1026 //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
1027 else
1028 vector=NULL;
1029 }
1030
1031 else {
1032 M=1;
1033 vector=xNew<bool>(M);
1034 FetchData(&(vector[0]),py_vector);
1035 }
1036
1037 /*output: */
1038 if(pM)*pM=M;
1039 if(pvector)*pvector=vector;
1040}
1041/*}}}*/
1042
1043/*ISSM objects*/
1044/*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
1045void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
1046
1047 /*Initialize output*/
1048 BamgGeom* bamggeom=new BamgGeom();
1049
1050 /*Fetch all fields*/
1051 FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
1052 FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
1053 FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
1054 FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
1055 FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
1056 FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
1057 FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
1058
1059 /*Assign output pointers:*/
1060 *pbamggeom=bamggeom;
1061}
1062/*}}}*/
1063/*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
1064void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
1065
1066 /*Initialize output*/
1067 BamgMesh* bamgmesh=new BamgMesh();
1068
1069 /*Fetch all fields*/
1070 FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
1071 FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
1072 FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
1073 FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
1074 FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
1075 FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
1076 FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
1077 FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
1078
1079 /*Assign output pointers:*/
1080 *pbamgmesh=bamgmesh;
1081}
1082/*}}}*/
1083/*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
1084void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
1085
1086 /*Initialize output*/
1087 BamgOpts* bamgopts=new BamgOpts();
1088
1089 /*Fetch all fields*/
1090 FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
1091 FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
1092 FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
1093 FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
1094 FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
1095 FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
1096 FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
1097 FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
1098 FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
1099 FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
1100 FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
1101 FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
1102 FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
1103 FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
1104
1105 FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
1106 FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
1107 FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
1108
1109 FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
1110 FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
1111 FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
1112 FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
1113 FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,PyDict_GetItemString(py_dict,"hVertices"));
1114 FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
1115 FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
1116 FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
1117
1118 /*Additional checks*/
1119 bamgopts->Check();
1120
1121 /*Assign output pointers:*/
1122 *pbamgopts=bamgopts;
1123}
1124/*}}}*/
1125/*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
1126void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
1127
1128 char *name = NULL;
1129 Option *option = NULL;
1130
1131 /*Initialize output*/
1132 Options* options=new Options();
1133
1134 /*Fetch all options*/
1135 for (int i=istart; i<nrhs; i=i+2){
1136 #if _PYTHON_MAJOR_ >= 3
1137 if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
1138 #else
1139 if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
1140 #endif
1141
1142 FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
1143 if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
1144
1145 _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
1146
1147// option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
1148// options->AddOption(option);
1149// option=NULL;
1150 }
1151
1152 /*Assign output pointers:*/
1153 *poptions=options;
1154}
1155/*}}}*/
1156/*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
1157void FetchData(Contours** pcontours,PyObject* py_list){
1158
1159 int numcontours,test1,test2;
1160 char *contourname = NULL;
1161 Contours *contours = NULL;
1162 Contour<double> *contouri = NULL;
1163 PyObject *py_dicti = NULL;
1164 PyObject *py_item = NULL;
1165
1166 #if _PYTHON_MAJOR_ >= 3
1167 if (PyUnicode_Check(py_list)){
1168 #else
1169 if (PyString_Check(py_list)){
1170 #endif
1171 FetchData(&contourname,py_list);
1172 contours=ExpRead<double>(contourname);
1173 }
1174 else if(PyList_Check(py_list)){
1175
1176 contours=new Contours();
1177 numcontours=(int)PyList_Size(py_list);
1178
1179 for(int i=0;i<numcontours;i++){
1180
1181 contouri=new Contour<double>();
1182 py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
1183
1184 py_item = PyDict_GetItemString(py_dicti,"nods");
1185 if(!py_item) _error_("input structure does not have a 'nods' field");
1186 FetchData(&contouri->nods,py_item);
1187
1188 py_item = PyDict_GetItemString(py_dicti,"x");
1189 if(!py_item) _error_("input structure does not have a 'x' field");
1190 FetchData(&contouri->x,&test1,&test2,py_item);
1191 if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
1192
1193 py_item = PyDict_GetItemString(py_dicti,"y");
1194 if(!py_item) _error_("input structure does not have a 'y' field");
1195 FetchData(&contouri->y,&test1,&test2,py_item);
1196 if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
1197
1198 contours->AddObject(contouri);
1199 }
1200 }
1201 else{
1202 _error_("Contour is neither a string nor a structure and cannot be loaded");
1203 }
1204
1205 /*clean-up and assign output pointer*/
1206 xDelete<char>(contourname);
1207 *pcontours=contours;
1208}
1209
1210void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
1211
1212 double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
1213 int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
1214 int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
1215
1216 /*Fetch adjacency matrix:*/
1217 int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
1218
1219 int* mwstart = xNewZeroInit<int>(nvtxs+1);
1220 pyGetJc(a,nvtxs,mwstart);
1221
1222 int* mwadjacency = xNewZeroInit<int>(nzmax);
1223 pyGetIr(a,nvtxs,nzmax,mwadjacency);
1224
1225 int* start = xNew<int>(nvtxs+1);
1226 for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
1227 int* adjacency = xNew<int>(nzmax);
1228 for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
1229
1230 /*Get edges weights*/
1231 int nedges = start[nvtxs];
1232 float* ewgts = NULL;
1233 int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
1234 //size may be 1 and still be empty;
1235 //presumably size of edge_weights input will never be exactly 1
1236 if(size != 1 && size != 0){
1237 ewgts = xNewZeroInit<float>(nedges);
1238 for(int i = 0; i<nedges;i++){
1239 ewgts[i] = (float)a[i];
1240 }
1241 }
1242
1243 /*Assign output pointers*/
1244 *pnvtxs = nvtxs;
1245 *padjacency = adjacency;
1246 *pstart = start;
1247 *pewgts = ewgts;
1248}
1249/*}}}*/
1250
1251void pyGetJc(double* a, int nvtxs, int* Jc){
1252/*{{{*/
1253 //get the number of non-zero elements in each row, adding to the prior number;
1254 //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
1255 // eg: 0, 1, 3, 6, 8, 13, ...
1256 // for: 1 in the first row, 2 in the second, 3 in the third, etc.
1257 int c = 0;
1258 Jc[c++] = 0;
1259
1260 for(int i=0;i<nvtxs;i++){
1261 for(int j=0;j<nvtxs;j++){
1262 if ((int)a[i+(j*nvtxs)] != 0){
1263 Jc[c] += 1;
1264 }
1265 }
1266 c++;
1267 Jc[c] = Jc[c-1];
1268 }
1269 return;
1270}
1271/*}}}*/
1272
1273void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
1274/*{{{*/
1275 //get the row-wise position of each non-zero element;
1276 //has length: number_of_non_zero_elements_in(a) == nzmax
1277 // eg: 10, 24, 25, 4, ...
1278 // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
1279 // using pyGetJc to determine which indices correspond to which row
1280 int r = 0;
1281
1282 for(int i=0;i<nvtxs;i++){
1283 for(int j=0;j<nvtxs;j++){
1284 if ((int)a[i+(j*nvtxs)] != 0){
1285 Ir[r] = j;
1286 r++;
1287 }
1288 }
1289 }
1290 return;
1291}
1292/*}}}*/
1293
1294/*Python version dependent: */
1295/* #if _PYTHON_MAJOR_ >= 3 */
1296/* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
1297/* void FetchData(char** pstring,PyObject* py_unicode){ */
1298
1299/* PyObject* py_bytes; */
1300/* char* string=NULL; */
1301
1302/* /\*convert to bytes format: *\/ */
1303/* PyUnicode_FSConverter(py_unicode,&py_bytes); */
1304
1305/* /\*convert from bytes to string: *\/ */
1306/* string=PyBytes_AsUTF8(py_bytes); */
1307
1308/* /\*copy string (note strlen does not include trailing NULL): *\/ */
1309/* *pstring=xNew<char>(strlen(string)+1); */
1310/* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
1311/* } */
1312/* /\*}}}*\/ */
1313/* #else */
1314/*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
1315void FetchData(char** pstring,PyObject* py_string){
1316
1317 /*extract internal string: */
1318 #if _PYTHON_MAJOR_ == 3
1319 const char* string=PyUnicode_AsUTF8(py_string);
1320 #else
1321 char* string=PyString_AsString(py_string);
1322 #endif
1323 /*copy string (note strlen does not include trailing NULL): */
1324 *pstring=xNew<char>(strlen(string)+1);
1325 memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
1326}
1327/*}}}*/
1328//#endif
Note: See TracBrowser for help on using the repository browser.