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