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

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

FIX: Consider strides on module input from Python matrices (slow right now, but works).

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