Johann Hibschman johann@physics.berkeley.edu
Thu, 6 Mar 1997 18:58:47 -0800 (PST)

On Fri, 7 Mar 1997, Michael Bell wrote:

> It seems to me that it _ought_ to be trivial to extend the SWIGed code
> to import array and matrix data (1d and 2d) from python objects to
> pointers for the C/fortran library, but it's beyond my naive skill.

Well, I can move arrays back and forth.  You would have to transpose
the NumPy arrays before sending them in, since PGPLOT expects Fortran 
array element order, and you would have to remember that fortran indices 
start at 1, not 0, but a little bit of python wrapper code could fix 

A more serious problem is the use of output variables, passed by 
reference in the argument lists of many PGPLOT functions.  Python just 
won't do this.  The only options I see are to send lists (or other 
mutable types) to the functions, or to have the pythonized versions 
return tuples of the output vars.  Not terribly nice.  I've asked on 
c.l.p if anyone has an elegant solution to this.

As for the arrays, the following code should work:

  Given a Python array object, produce a Python string representing a
   SWIG pointer to the data of that object (as a contiguous block)

static PyObject *
PyArray_ToSwigRef( PyObject *self, PyObject *args ) {
  PyObject *obj;
  char  *data;
  int   dims;
  char  *ptrstring;

  if(!PyArg_ParseTuple(args,"O",&obj))  /* get the object */
    return NULL;

  /* attempt to interpret the object as a 1D array of floats */
  if( !(PyArray_As1D(&obj, &data, &dims, PyArray_FLOAT))
    return NULL;  /* set error flag? */

  /* make a SWIG pointer (represented as a python string) */
  _swig_make_hex(ptrstring, data, "_float_p");

  return Py_BuildValue("s", ptrstring);

I'm not sure if this code is right, so don't blame me if your computer 

- Johann

Johann Hibschman            | Grad student in Physics, lurking in Astronomy,
johann@physics.berkeley.edu | Probing Pulsar Pair Plasma Production Processes

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org