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

Last change on this file since 15100 was 15100, checked in by Eric.Larour, 12 years ago

CHG: changed the names of _printString_ and _pprintString_
to _printf_ and _printf0_

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