Changeset 14093


Ignore:
Timestamp:
12/04/12 10:49:52 (12 years ago)
Author:
jschierm
Message:

NEW: Allow python scalars to be input rather than numpy 1x1 arrays.

Location:
issm/trunk-jpl/src/wrappers/python/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp

    r13990 r14093  
    2020void FetchData(double* pscalar,PyObject* py_float){
    2121
    22         double scalar;
     22        double dscalar;
    2323
    2424        /*return internal value: */
    25         scalar=PyFloat_AsDouble(py_float);
    26 
    27         /*output: */
    28         *pscalar=scalar;
    29 }
    30 /*}}}*/
    31 /*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{*/
    32 void FetchData(int* pinteger, PyObject* py_long){
    33 
    34         int integer;
     25        if      (PyFloat_Check((PyArrayObject*)py_float))
     26                dscalar=PyFloat_AsDouble(py_float);
     27        else if (PyInt_Check((PyArrayObject*)py_float))
     28                dscalar=(double)PyInt_AsLong(py_float);
     29        else if (PyLong_Check((PyArrayObject*)py_float))
     30                dscalar=PyLong_AsDouble(py_float);
     31        else if (PyBool_Check((PyArrayObject*)py_float))
     32                dscalar=(double)PyLong_AsLong(py_float);
     33        else
     34                _error_("unrecognized float type in input!");
     35
     36        /*output: */
     37        *pscalar=dscalar;
     38}
     39/*}}}*/
     40/*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
     41void FetchData(int* pscalar, PyObject* py_long){
     42
     43        int iscalar;
    3544
    3645        /*return internal value: */
    37         integer=(int)PyLong_AsLong(py_long);
    38 
    39         /*output: */
    40         *pinteger=integer;
    41 }
    42 /*}}}*/
    43 /*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{*/
    44 void FetchData(bool* pboolean,PyObject* py_boolean){
    45 
    46         bool boolean;
     46        if      (PyInt_Check((PyArrayObject*)py_long))
     47                iscalar=(int)PyInt_AsLong(py_long);
     48        else if (PyLong_Check((PyArrayObject*)py_long))
     49                iscalar=(int)PyLong_AsLong(py_long);
     50        else if (PyFloat_Check((PyArrayObject*)py_long))
     51                iscalar=(int)PyFloat_AsDouble(py_long);
     52        else if (PyBool_Check((PyArrayObject*)py_long))
     53                iscalar=(int)PyLong_AsLong(py_long);
     54        else
     55                _error_("unrecognized long type in input!");
     56
     57        /*output: */
     58        *pscalar=iscalar;
     59}
     60/*}}}*/
     61/*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
     62void FetchData(bool* pscalar,PyObject* py_boolean){
     63
     64        bool bscalar;
    4765
    4866        /*check this is indeed a subtype of long type: */
     
    5068
    5169        /*extract boolean: */
    52         boolean=(bool)PyLong_AsLong(py_boolean);
     70        bscalar=(bool)PyLong_AsLong(py_boolean);
    5371
    5472        /*simple copy: */
    55         *pboolean=boolean;
     73        *pscalar=bscalar;
    5674
    5775}
     
    7290        int i;
    7391
    74         /*retrieve dimensions: */
    75         ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
    76         if(ndim!=2)_error_("expecting an MxN matrix in input!");
    77         dims=PyArray_DIMS((PyArrayObject*)py_matrix);
    78         M=dims[0]; N=dims[1];
    79 
    80         if (M && N) {
    81                 if      (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
    82                         /*retrieve internal value: */
    83                         dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
    84 
    85                         /*copy matrix: */
    86                         matrix=xNew<double>(M*N);
    87                         memcpy(matrix,dmatrix,(M*N)*sizeof(double));
    88                 }
    89 
    90                 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
    91                         /*retrieve internal value: */
    92                         lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
    93 
    94                         /*transform into double matrix: */
    95                         matrix=xNew<double>(M*N);
    96                         for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
    97                 }
    98 
    99                 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
    100                         /*retrieve internal value: */
    101                         bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
    102 
    103                         /*transform into double matrix: */
    104                         matrix=xNew<double>(M*N);
    105                         for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
    106                 }
    107 
     92        if     (PyArray_Check((PyArrayObject*)py_matrix)) {
     93                /*retrieve dimensions: */
     94                ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
     95                if(ndim!=2)_error_("expecting an MxN matrix in input!");
     96                dims=PyArray_DIMS((PyArrayObject*)py_matrix);
     97                M=dims[0]; N=dims[1];
     98
     99                if (M && N) {
     100                        if      (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
     101                                /*retrieve internal value: */
     102                                dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
     103
     104                                /*copy matrix: */
     105                                matrix=xNew<double>(M*N);
     106                                memcpy(matrix,dmatrix,(M*N)*sizeof(double));
     107                        }
     108
     109                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
     110                                /*retrieve internal value: */
     111                                lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
     112
     113                                /*transform into double matrix: */
     114                                matrix=xNew<double>(M*N);
     115                                for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
     116                        }
     117
     118                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
     119                                /*retrieve internal value: */
     120                                bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
     121
     122                                /*transform into double matrix: */
     123                                matrix=xNew<double>(M*N);
     124                                for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
     125                        }
     126
     127                        else
     128                                _error_("unrecognized pyarray type in input!");
     129                }
    108130                else
    109                         _error_("unrecognized matrix type in input!");
    110         }
     131                        matrix=NULL;
     132        }
     133
     134        else if (PyFloat_Check((PyArrayObject*)py_matrix) ||
     135                         PyInt_Check((PyArrayObject*)py_matrix) ||
     136                         PyLong_Check((PyArrayObject*)py_matrix) ||
     137                         PyBool_Check((PyArrayObject*)py_matrix)) {
     138                M=1;
     139                N=1;
     140                matrix=xNew<double>(M*N);
     141                FetchData(&(matrix[0]),py_matrix);
     142        }
     143
    111144        else
    112                 matrix=NULL;
     145                _error_("unrecognized array type in input!");
    113146
    114147        /*output: */
     
    133166        int i;
    134167
    135         /*retrieve dimensions: */
    136         ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
    137         if(ndim!=2)_error_("expecting an MxN matrix in input!");
    138         dims=PyArray_DIMS((PyArrayObject*)py_matrix);
    139         M=dims[0]; N=dims[1];
    140 
    141         if (M && N) {
    142                 if      (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
    143                         /*retrieve internal value: */
    144                         dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
    145 
    146                         /*transform into integer matrix: */
    147                         matrix=xNew<int>(M*N);
    148                         for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
    149                 }
    150 
    151                 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
    152                         /*retrieve internal value: */
    153                         lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
    154 
    155                         /*transform into integer matrix: */
    156                         matrix=xNew<int>(M*N);
    157                         for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
    158                 }
    159 
    160                 else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
    161                         /*retrieve internal value: */
    162                         bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
    163 
    164                         /*transform into integer matrix: */
    165                         matrix=xNew<int>(M*N);
    166                         for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
    167                 }
    168 
     168        if     (PyArray_Check((PyArrayObject*)py_matrix)) {
     169                /*retrieve dimensions: */
     170                ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
     171                if(ndim!=2)_error_("expecting an MxN matrix in input!");
     172                dims=PyArray_DIMS((PyArrayObject*)py_matrix);
     173                M=dims[0]; N=dims[1];
     174
     175                if (M && N) {
     176                        if      (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
     177                                /*retrieve internal value: */
     178                                dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
     179
     180                                /*transform into integer matrix: */
     181                                matrix=xNew<int>(M*N);
     182                                for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
     183                        }
     184
     185                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT64) {
     186                                /*retrieve internal value: */
     187                                lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
     188
     189                                /*transform into integer matrix: */
     190                                matrix=xNew<int>(M*N);
     191                                for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
     192                        }
     193
     194                        else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
     195                                /*retrieve internal value: */
     196                                bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
     197
     198                                /*transform into integer matrix: */
     199                                matrix=xNew<int>(M*N);
     200                                for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
     201                        }
     202
     203                        else
     204                                _error_("unrecognized pyarray type in input!");
     205                }
    169206                else
    170                         _error_("unrecognized matrix type in input!");
    171         }
     207                        matrix=NULL;
     208        }
     209
     210        else if (PyInt_Check((PyArrayObject*)py_matrix) ||
     211                         PyLong_Check((PyArrayObject*)py_matrix) ||
     212                         PyFloat_Check((PyArrayObject*)py_matrix) ||
     213                         PyBool_Check((PyArrayObject*)py_matrix)) {
     214                M=1;
     215                N=1;
     216                matrix=xNew<int>(M*N);
     217                FetchData(&(matrix[0]),py_matrix);
     218        }
     219
    172220        else
    173                 matrix=NULL;
     221                _error_("unrecognized array type in input!");
    174222
    175223        /*output: */
  • issm/trunk-jpl/src/wrappers/python/io/pythonio.h

    r14010 r14093  
    3434void FetchData(char** pstring,PyObject* py_unicode);
    3535void FetchData(double* pscalar,PyObject* py_float);
    36 void FetchData(int* pinteger,PyObject* py_long);
     36void FetchData(int* pscalar,PyObject* py_long);
    3737void FetchData(bool* pbool,PyObject* py_boolean);
    3838void FetchData(BamgGeom** bamggeom,PyObject* py_dict);
Note: See TracChangeset for help on using the changeset viewer.