[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] | 20 | void 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){{{*/
|
---|
| 45 | void 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){{{*/
|
---|
| 70 | void 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] | 93 | void 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;
|
---|
| 105 | int i;
|
---|
| 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 |
|
---|
[14093] | 121 | if (M && N) {
|
---|
| 122 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 123 | /*retrieve internal value: */
|
---|
| 124 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12112] | 125 |
|
---|
[14093] | 126 | /*copy matrix: */
|
---|
| 127 | matrix=xNew<double>(M*N);
|
---|
| 128 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
| 129 | }
|
---|
[13990] | 130 |
|
---|
[14093] | 131 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
---|
| 132 | /*retrieve internal value: */
|
---|
| 133 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 134 |
|
---|
[14093] | 135 | /*transform into double matrix: */
|
---|
| 136 | matrix=xNew<double>(M*N);
|
---|
| 137 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
| 138 | }
|
---|
[13990] | 139 |
|
---|
[14093] | 140 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 141 | /*retrieve internal value: */
|
---|
| 142 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 143 |
|
---|
[14093] | 144 | /*transform into double matrix: */
|
---|
| 145 | matrix=xNew<double>(M*N);
|
---|
| 146 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | else
|
---|
[14234] | 150 | _error_("unrecognized double pyarray type in input!");
|
---|
[13990] | 151 | }
|
---|
| 152 | else
|
---|
[14093] | 153 | matrix=NULL;
|
---|
[13484] | 154 | }
|
---|
[14093] | 155 |
|
---|
[14097] | 156 | else {
|
---|
[14093] | 157 | M=1;
|
---|
| 158 | N=1;
|
---|
| 159 | matrix=xNew<double>(M*N);
|
---|
| 160 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[12112] | 163 | /*output: */
|
---|
| 164 | if(pM)*pM=M;
|
---|
| 165 | if(pN)*pN=N;
|
---|
| 166 | if(pmatrix)*pmatrix=matrix;
|
---|
| 167 | }
|
---|
| 168 | /*}}}*/
|
---|
[12776] | 169 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 170 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 171 |
|
---|
| 172 | /*output: */
|
---|
| 173 | int* matrix=NULL;
|
---|
| 174 | int M,N;
|
---|
[13990] | 175 | int ndim;
|
---|
| 176 | npy_intp* dims=NULL;
|
---|
[12776] | 177 |
|
---|
| 178 | /*intermediary:*/
|
---|
[13990] | 179 | double* dmatrix=NULL;
|
---|
| 180 | long* lmatrix=NULL;
|
---|
| 181 | bool* bmatrix=NULL;
|
---|
[12776] | 182 | int i;
|
---|
| 183 |
|
---|
[14093] | 184 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 185 | /*retrieve dimensions: */
|
---|
| 186 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[14098] | 187 | if (ndim==2) {
|
---|
| 188 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 189 | M=dims[0]; N=dims[1];
|
---|
| 190 | }
|
---|
| 191 | else if (ndim==1) {
|
---|
| 192 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 193 | M=dims[0]; N=1;
|
---|
| 194 | }
|
---|
| 195 | else
|
---|
| 196 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 197 |
|
---|
[14093] | 198 | if (M && N) {
|
---|
| 199 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 200 | /*retrieve internal value: */
|
---|
| 201 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12776] | 202 |
|
---|
[14093] | 203 | /*transform into integer matrix: */
|
---|
| 204 | matrix=xNew<int>(M*N);
|
---|
| 205 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
| 206 | }
|
---|
[13990] | 207 |
|
---|
[14093] | 208 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
---|
| 209 | /*retrieve internal value: */
|
---|
| 210 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 211 |
|
---|
[14093] | 212 | /*transform into integer matrix: */
|
---|
| 213 | matrix=xNew<int>(M*N);
|
---|
| 214 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
| 215 | }
|
---|
[13990] | 216 |
|
---|
[14093] | 217 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 218 | /*retrieve internal value: */
|
---|
| 219 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 220 |
|
---|
[14093] | 221 | /*transform into integer matrix: */
|
---|
| 222 | matrix=xNew<int>(M*N);
|
---|
| 223 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | else
|
---|
[14234] | 227 | _error_("unrecognized int pyarray type in input!");
|
---|
[13990] | 228 | }
|
---|
| 229 | else
|
---|
[14093] | 230 | matrix=NULL;
|
---|
[13484] | 231 | }
|
---|
[14093] | 232 |
|
---|
[14097] | 233 | else {
|
---|
[14093] | 234 | M=1;
|
---|
| 235 | N=1;
|
---|
| 236 | matrix=xNew<int>(M*N);
|
---|
| 237 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[12776] | 240 | /*output: */
|
---|
| 241 | if(pM)*pM=M;
|
---|
| 242 | if(pN)*pN=N;
|
---|
| 243 | if(pmatrix)*pmatrix=matrix;
|
---|
| 244 | }
|
---|
| 245 | /*}}}*/
|
---|
[14234] | 246 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 247 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 248 |
|
---|
| 249 | /*output: */
|
---|
| 250 | bool* bmatrix=NULL;
|
---|
| 251 | bool* matrix=NULL;
|
---|
| 252 | int M,N;
|
---|
| 253 | int ndim;
|
---|
| 254 | npy_intp* dims=NULL;
|
---|
| 255 |
|
---|
| 256 | /*intermediary:*/
|
---|
| 257 | double* dmatrix=NULL;
|
---|
| 258 | long* lmatrix=NULL;
|
---|
| 259 | int i;
|
---|
| 260 |
|
---|
| 261 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 262 | /*retrieve dimensions: */
|
---|
| 263 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
| 264 | if (ndim==2) {
|
---|
| 265 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 266 | M=dims[0]; N=dims[1];
|
---|
| 267 | }
|
---|
| 268 | else if (ndim==1) {
|
---|
| 269 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 270 | M=dims[0]; N=1;
|
---|
| 271 | }
|
---|
| 272 | else
|
---|
| 273 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
| 274 |
|
---|
| 275 | if (M && N) {
|
---|
| 276 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 277 | /*retrieve internal value: */
|
---|
| 278 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 279 |
|
---|
| 280 | /*transform into bool matrix: */
|
---|
| 281 | matrix=xNew<bool>(M*N);
|
---|
| 282 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
|
---|
| 286 | /*retrieve internal value: */
|
---|
| 287 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 288 |
|
---|
| 289 | /*transform into bool matrix: */
|
---|
| 290 | matrix=xNew<bool>(M*N);
|
---|
| 291 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 295 | /*retrieve internal value: */
|
---|
| 296 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 297 |
|
---|
| 298 | /*copy matrix: */
|
---|
| 299 | matrix=xNew<bool>(M*N);
|
---|
| 300 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | else
|
---|
| 304 | _error_("unrecognized bool pyarray type in input!");
|
---|
| 305 | }
|
---|
| 306 | else
|
---|
| 307 | matrix=NULL;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | else {
|
---|
| 311 | M=1;
|
---|
| 312 | N=1;
|
---|
| 313 | matrix=xNew<bool>(M*N);
|
---|
| 314 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /*output: */
|
---|
| 318 | if(pM)*pM=M;
|
---|
| 319 | if(pN)*pN=N;
|
---|
| 320 | if(pmatrix)*pmatrix=matrix;
|
---|
| 321 | }
|
---|
| 322 | /*}}}*/
|
---|
[12365] | 323 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
[12120] | 324 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
[12112] | 325 |
|
---|
[12120] | 326 | /*output: */
|
---|
[13372] | 327 | double* dvector=NULL;
|
---|
[12120] | 328 | double* vector=NULL;
|
---|
| 329 | int M;
|
---|
[13373] | 330 | int ndim;
|
---|
[12120] | 331 | npy_intp* dims=NULL;
|
---|
| 332 |
|
---|
[13990] | 333 | /*intermediary:*/
|
---|
| 334 | long* lvector=NULL;
|
---|
| 335 | bool* bvector=NULL;
|
---|
| 336 | int i;
|
---|
| 337 |
|
---|
| 338 | /*retrieve dimensions: */
|
---|
[12120] | 339 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
[13056] | 340 | if(ndim!=1)_error_("expecting an Mx1 vector in input!");
|
---|
[12120] | 341 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 342 | M=dims[0];
|
---|
[13622] | 343 |
|
---|
[13484] | 344 | if (M) {
|
---|
[13990] | 345 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 346 | /*retrieve internal value: */
|
---|
| 347 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[12120] | 348 |
|
---|
[13990] | 349 | /*copy vector: */
|
---|
| 350 | vector=xNew<double>(M);
|
---|
| 351 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
---|
| 355 | /*retrieve internal value: */
|
---|
| 356 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 357 |
|
---|
| 358 | /*transform into double vector: */
|
---|
| 359 | vector=xNew<double>(M);
|
---|
| 360 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 364 | /*retrieve internal value: */
|
---|
| 365 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 366 |
|
---|
| 367 | /*transform into double vector: */
|
---|
| 368 | vector=xNew<double>(M);
|
---|
| 369 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | else
|
---|
[14234] | 373 | _error_("unrecognized double pyarray type in input!");
|
---|
[13484] | 374 | }
|
---|
| 375 | else
|
---|
| 376 | vector=NULL;
|
---|
[13372] | 377 |
|
---|
[12120] | 378 | /*output: */
|
---|
| 379 | if(pM)*pM=M;
|
---|
| 380 | if(pvector)*pvector=vector;
|
---|
| 381 | }
|
---|
| 382 | /*}}}*/
|
---|
[14221] | 383 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 384 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
[13310] | 385 |
|
---|
[14221] | 386 | /*output: */
|
---|
| 387 | int* vector=NULL;
|
---|
| 388 | int M;
|
---|
| 389 | int ndim;
|
---|
| 390 | npy_intp* dims=NULL;
|
---|
| 391 |
|
---|
| 392 | /*intermediary:*/
|
---|
| 393 | long* lvector=NULL;
|
---|
| 394 | bool* bvector=NULL;
|
---|
| 395 | double* dvector=NULL;
|
---|
| 396 | int i;
|
---|
| 397 |
|
---|
| 398 | /*retrieve dimensions: */
|
---|
| 399 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 400 | if(ndim!=1)_error_("expecting an Mx1 vector in input!");
|
---|
| 401 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 402 | M=dims[0];
|
---|
| 403 |
|
---|
| 404 | if (M) {
|
---|
| 405 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 406 | /*retrieve internal value: */
|
---|
| 407 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 408 |
|
---|
| 409 | /*transform into int vector: */
|
---|
| 410 | vector=xNew<int>(M);
|
---|
| 411 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
---|
| 415 | /*retrieve internal value: */
|
---|
| 416 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 417 |
|
---|
| 418 | /*transform into int vector: */
|
---|
| 419 | vector=xNew<int>(M);
|
---|
| 420 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 424 | /*retrieve internal value: */
|
---|
| 425 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 426 |
|
---|
| 427 | /*transform into int vector: */
|
---|
| 428 | vector=xNew<int>(M);
|
---|
| 429 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | else
|
---|
[14234] | 433 | _error_("unrecognized int pyarray type in input!");
|
---|
[14221] | 434 | }
|
---|
| 435 | else
|
---|
| 436 | vector=NULL;
|
---|
| 437 |
|
---|
| 438 | /*output: */
|
---|
| 439 | if(pM)*pM=M;
|
---|
| 440 | if(pvector)*pvector=vector;
|
---|
| 441 | }
|
---|
| 442 | /*}}}*/
|
---|
[14234] | 443 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 444 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
[14221] | 445 |
|
---|
[14234] | 446 | /*output: */
|
---|
| 447 | bool* bvector=NULL;
|
---|
| 448 | bool* vector=NULL;
|
---|
| 449 | int M;
|
---|
| 450 | int ndim;
|
---|
| 451 | npy_intp* dims=NULL;
|
---|
| 452 |
|
---|
| 453 | /*intermediary:*/
|
---|
| 454 | double* dvector=NULL;
|
---|
| 455 | long* lvector=NULL;
|
---|
| 456 | int i;
|
---|
| 457 |
|
---|
| 458 | /*retrieve dimensions: */
|
---|
| 459 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 460 | if(ndim!=1)_error_("expecting an Mx1 vector in input!");
|
---|
| 461 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 462 | M=dims[0];
|
---|
| 463 |
|
---|
| 464 | if (M) {
|
---|
| 465 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 466 | /*retrieve internal value: */
|
---|
| 467 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 468 |
|
---|
| 469 | /*transform into bool vector: */
|
---|
| 470 | vector=xNew<bool>(M);
|
---|
| 471 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_INT64) {
|
---|
| 475 | /*retrieve internal value: */
|
---|
| 476 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 477 |
|
---|
| 478 | /*transform into bool vector: */
|
---|
| 479 | vector=xNew<bool>(M);
|
---|
| 480 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 484 | /*retrieve internal value: */
|
---|
| 485 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 486 |
|
---|
| 487 | /*copy vector: */
|
---|
| 488 | vector=xNew<bool>(M);
|
---|
| 489 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | else
|
---|
| 493 | _error_("unrecognized bool pyarray type in input!");
|
---|
| 494 | }
|
---|
| 495 | else
|
---|
| 496 | vector=NULL;
|
---|
| 497 |
|
---|
| 498 | /*output: */
|
---|
| 499 | if(pM)*pM=M;
|
---|
| 500 | if(pvector)*pvector=vector;
|
---|
| 501 | }
|
---|
| 502 | /*}}}*/
|
---|
| 503 |
|
---|
[13310] | 504 | /*ISSM objects*/
|
---|
| 505 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
| 506 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
| 507 |
|
---|
| 508 | /*Initialize output*/
|
---|
| 509 | BamgGeom* bamggeom=new BamgGeom();
|
---|
| 510 |
|
---|
| 511 | /*Fetch all fields*/
|
---|
| 512 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 513 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 514 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
| 515 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
| 516 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
| 517 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 518 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
| 519 |
|
---|
| 520 | /*Assign output pointers:*/
|
---|
| 521 | *pbamggeom=bamggeom;
|
---|
[13287] | 522 | }
|
---|
| 523 | /*}}}*/
|
---|
[13310] | 524 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
| 525 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
| 526 |
|
---|
| 527 | /*Initialize output*/
|
---|
| 528 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
| 529 |
|
---|
| 530 | /*Fetch all fields*/
|
---|
| 531 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 532 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 533 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
| 534 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 535 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
| 536 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
| 537 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
| 538 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
| 539 |
|
---|
| 540 | /*Assign output pointers:*/
|
---|
| 541 | *pbamgmesh=bamgmesh;
|
---|
[13287] | 542 | }
|
---|
| 543 | /*}}}*/
|
---|
[13310] | 544 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
| 545 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
| 546 |
|
---|
| 547 | /*Initialize output*/
|
---|
| 548 | BamgOpts* bamgopts=new BamgOpts();
|
---|
| 549 |
|
---|
| 550 | /*Fetch all fields*/
|
---|
| 551 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
| 552 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
| 553 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
| 554 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
| 555 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
| 556 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
| 557 | FetchData(&bamgopts->MaxCornerAngle,PyDict_GetItemString(py_dict,"MaxCornerAngle"));
|
---|
| 558 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
| 559 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
| 560 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
| 561 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
| 562 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
| 563 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
| 564 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
| 565 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
| 566 |
|
---|
| 567 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
| 568 | FetchData(&bamgopts->geometricalmetric,PyDict_GetItemString(py_dict,"geometricalmetric"));
|
---|
| 569 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
| 570 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
| 571 |
|
---|
| 572 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
| 573 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
| 574 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
| 575 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
| 576 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
|
---|
| 577 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
| 578 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
| 579 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
| 580 |
|
---|
| 581 | /*Additional checks*/
|
---|
| 582 | bamgopts->Check();
|
---|
| 583 |
|
---|
| 584 | /*Assign output pointers:*/
|
---|
| 585 | *pbamgopts=bamgopts;
|
---|
[13287] | 586 | }
|
---|
| 587 | /*}}}*/
|
---|
[13374] | 588 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
[13373] | 589 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
[12120] | 590 |
|
---|
[13373] | 591 | char *name = NULL;
|
---|
| 592 | Option *option = NULL;
|
---|
| 593 |
|
---|
[12776] | 594 | /*Initialize output*/
|
---|
| 595 | Options* options=new Options();
|
---|
| 596 |
|
---|
[13373] | 597 | /*Fetch all options*/
|
---|
| 598 | for (int i=istart; i<nrhs; i=i+2){
|
---|
| 599 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
[12776] | 600 |
|
---|
[13373] | 601 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
| 602 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
| 603 |
|
---|
[13374] | 604 | _pprintLine_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!");
|
---|
[13373] | 605 |
|
---|
| 606 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
| 607 | // options->AddOption(option);
|
---|
| 608 | // option=NULL;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[12776] | 611 | /*Assign output pointers:*/
|
---|
| 612 | *poptions=options;
|
---|
| 613 | }
|
---|
| 614 | /*}}}*/
|
---|
[13368] | 615 | /*FUNCTION FetchData(DataSet** pcontours,PyObject* py_list){{{*/
|
---|
| 616 | void FetchData(DataSet** pcontours,PyObject* py_list){
|
---|
[12776] | 617 |
|
---|
[13368] | 618 | int numcontours,test1,test2;
|
---|
| 619 | char *contourname = NULL;
|
---|
| 620 | DataSet *contours = NULL;
|
---|
| 621 | Contour<double> *contouri = NULL;
|
---|
| 622 | PyObject *py_dicti = NULL;
|
---|
| 623 | PyObject *py_item = NULL;
|
---|
| 624 |
|
---|
| 625 | if (PyString_Check(py_list)){
|
---|
| 626 | FetchData(&contourname,py_list);
|
---|
| 627 | contours=DomainOutlineRead<double>(contourname);
|
---|
| 628 | }
|
---|
| 629 | else if(PyList_Check(py_list)){
|
---|
| 630 |
|
---|
| 631 | contours=new DataSet(0);
|
---|
| 632 | numcontours=(int)PyList_Size(py_list);
|
---|
| 633 |
|
---|
| 634 | for(int i=0;i<numcontours;i++){
|
---|
| 635 |
|
---|
| 636 | contouri=xNew<Contour<double> >(1);
|
---|
| 637 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
| 638 |
|
---|
| 639 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
| 640 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
| 641 | FetchData(&contouri->nods,py_item);
|
---|
| 642 |
|
---|
| 643 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
| 644 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
| 645 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
| 646 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
| 647 |
|
---|
| 648 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
| 649 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
| 650 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
| 651 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
| 652 |
|
---|
| 653 | contours->AddObject(contouri);
|
---|
| 654 | }
|
---|
| 655 | }
|
---|
| 656 | else{
|
---|
| 657 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | /*clean-up and assign output pointer*/
|
---|
[13372] | 661 | xDelete<char>(contourname);
|
---|
[13368] | 662 | *pcontours=contours;
|
---|
[13353] | 663 | }
|
---|
| 664 | /*}}}*/
|
---|
| 665 |
|
---|
[12073] | 666 | /*Python version dependent: */
|
---|
| 667 | #if _PYTHON_MAJOR_ >= 3
|
---|
[12365] | 668 | /*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*/
|
---|
[12073] | 669 | void FetchData(char** pstring,PyObject* py_unicode){
|
---|
| 670 |
|
---|
| 671 | PyObject* py_bytes;
|
---|
| 672 | char* string=NULL;
|
---|
| 673 |
|
---|
| 674 | /*convert to bytes format: */
|
---|
| 675 | PyUnicode_FSConverter(py_unicode,&py_bytes);
|
---|
| 676 |
|
---|
| 677 | /*convert from bytes to string: */
|
---|
| 678 | string=PyBytes_AS_STRING(py_bytes);
|
---|
[13622] | 679 |
|
---|
[12073] | 680 | *pstring=string;
|
---|
| 681 | }
|
---|
| 682 | /*}}}*/
|
---|
| 683 | #else
|
---|
[12365] | 684 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
[12073] | 685 | void FetchData(char** pstring,PyObject* py_string){
|
---|
| 686 |
|
---|
| 687 | char* string=NULL;
|
---|
| 688 |
|
---|
| 689 | /*extract internal string: */
|
---|
| 690 | string=PyString_AsString(py_string);
|
---|
[13372] | 691 |
|
---|
| 692 | /*copy string (note strlen does not include trailing NULL): */
|
---|
| 693 | *pstring=xNew<char>(strlen(string)+1);
|
---|
| 694 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
[12073] | 695 | }
|
---|
| 696 | /*}}}*/
|
---|
| 697 | #endif
|
---|