Changeset 14234
- Timestamp:
- 01/10/13 11:07:29 (12 years ago)
- Location:
- issm/trunk-jpl/src/wrappers/python/io
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp ¶
r14221 r14234 148 148 149 149 else 150 _error_("unrecognized floatpyarray type in input!");150 _error_("unrecognized double pyarray type in input!"); 151 151 } 152 152 else … … 225 225 226 226 else 227 _error_("unrecognized longpyarray type in input!");227 _error_("unrecognized int pyarray type in input!"); 228 228 } 229 229 else … … 244 244 } 245 245 /*}}}*/ 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 /*}}}*/ 246 323 /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/ 247 324 void FetchData(double** pvector,int* pM,PyObject* py_vector){ … … 294 371 295 372 else 296 _error_("unrecognized vectortype in input!");373 _error_("unrecognized double pyarray type in input!"); 297 374 } 298 375 else … … 354 431 355 432 else 356 _error_("unrecognized vectortype in input!");433 _error_("unrecognized int pyarray type in input!"); 357 434 } 358 435 else 359 436 vector=NULL; 437 438 /*output: */ 439 if(pM)*pM=M; 440 if(pvector)*pvector=vector; 441 } 442 /*}}}*/ 443 /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/ 444 void FetchData(bool** pvector,int* pM,PyObject* py_vector){ 445 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; 360 497 361 498 /*output: */ -
TabularUnified issm/trunk-jpl/src/wrappers/python/io/WritePythonData.cpp ¶
r14232 r14234 209 209 } 210 210 /*}}}*/ 211 /*FUNCTION WriteData(PyObject* py_tuple,int index,SeqMat<bool>* matrix){{{*/ 212 void WriteData(PyObject* py_tuple,int index,SeqMat<bool>* matrix){ 213 214 int M,N; 215 bool* buffer=NULL; 216 npy_intp dims[2]={0,0}; 217 PyObject* array=NULL; 218 219 matrix->GetSize(&M,&N); 220 buffer=matrix->ToSerial(); 221 222 dims[0]=(npy_intp)M; 223 dims[1]=(npy_intp)N; 224 array=PyArray_SimpleNewFromData(2,dims,NPY_BOOL,buffer); 225 226 PyTuple_SetItem(py_tuple, index, array); 227 228 }/*}}}*/ 229 /*FUNCTION WriteData(PyObject* py_tuple,int index,SeqVec<bool>* vector){{{*/ 230 void WriteData(PyObject* py_tuple,int index,SeqVec<bool>* vector){ 231 232 int M; 233 bool* buffer=NULL; 234 npy_intp dim=10; 235 PyObject* array=NULL; 236 237 vector->GetSize(&M); 238 buffer=vector->ToMPISerial(); 239 240 dim=(npy_intp)M; 241 array=PyArray_SimpleNewFromData(1,&dim,NPY_BOOL,buffer); 242 243 PyTuple_SetItem(py_tuple, index, array); 244 } 245 /*}}}*/ 211 246 /*FUNCTION WriteData(PyObject* py_tuple,int index,RiftStruct* riftstruct){{{*/ 212 247 void WriteData(PyObject* py_tuple,int index,RiftStruct* riftstruct){ … … 288 323 } 289 324 /*}}}*/ 325 /*FUNCTION PyArrayFromCopiedData(int dimi,int dimj,bool* data){{{*/ 326 PyObject* PyArrayFromCopiedData(int dimi,int dimj,bool* data){ 327 328 bool* pydata; 329 npy_intp pydims[2]={0,0}; 330 331 /* note that PyArray_SimpleNewFromData does not copy the data, so that when the original 332 object (e.g. bamggeom,bamgmesh) is deleted, the data is gone. */ 333 334 pydims[0]=(npy_intp)dimi; 335 pydims[1]=(npy_intp)dimj; 336 pydata=xNew<bool>(dimi*dimj); 337 memcpy(pydata,data,dimi*dimj*sizeof(bool)); 338 return PyArray_SimpleNewFromData(2,pydims,NPY_BOOL,pydata); 339 } 340 /*}}}*/ -
TabularUnified issm/trunk-jpl/src/wrappers/python/io/pythonio.h ¶
r14221 r14234 27 27 void WriteData(PyObject* py_tuple,int index, SeqMat<int>* matrix); 28 28 void WriteData(PyObject* py_tuple,int index, SeqVec<int>* vector); 29 void WriteData(PyObject* py_tuple,int index, SeqMat<bool>* matrix); 30 void WriteData(PyObject* py_tuple,int index, SeqVec<bool>* vector); 29 31 void WriteData(PyObject* py_tuple,int index, BamgGeom* bamggeom); 30 32 void WriteData(PyObject* py_tuple,int index, BamgMesh* bamgmesh); 31 33 void WriteData(PyObject* py_tuple,int index, RiftStruct* riftstruct); 32 34 33 void FetchData(int** pvector,int* pM,PyObject* py_ref);34 void FetchData(double** pvector,int* pM,PyObject* py_ref);35 35 void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_array); 36 36 void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix); 37 void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix); 38 void FetchData(double** pvector,int* pM,PyObject* py_ref); 39 void FetchData(int** pvector,int* pM,PyObject* py_ref); 40 void FetchData(bool** pvector,int* pM,PyObject* py_ref); 37 41 void FetchData(char** pstring,PyObject* py_unicode); 38 42 void FetchData(double* pscalar,PyObject* py_float); … … 51 55 PyObject* PyArrayFromCopiedData(int dimi,int dimj,double* data); 52 56 PyObject* PyArrayFromCopiedData(int dimi,int dimj,int* data); 57 PyObject* PyArrayFromCopiedData(int dimi,int dimj,bool* data); 53 58 54 59 #endif /* _IO_H_ */
Note:
See TracChangeset
for help on using the changeset viewer.