// the data should be FLOAT32 and should be ensured in the wrapper 
static PyObject *interp3(PyObject *self, PyObject *args)
{
	PyArrayObject *volume, *result, *C, *R, *S;
	float *pr, *pc, *ps;
        float *pvol, *pvc;
        int xdim, ydim, zdim;

	// We expect 4 arguments of the PyArray_Type
	if(!PyArg_ParseTuple(args, "O!O!O!O!", 
				&PyArray_Type, &volume,
				&PyArray_Type, &R,
				&PyArray_Type, &C,
				&PyArray_Type, &S)) return NULL;

	if ( NULL == volume ) return NULL;
	if ( NULL == C ) return NULL;
	if ( NULL == R ) return NULL;
	if ( NULL == S ) return NULL;

	// result matrix is the same size as C and is float
	result = (PyArrayObject*) PyArray_ZEROS(PyArray_NDIM(C), C->dimensions, NPY_FLOAT, 0); 
	// This is for reference counting ( I think )
	PyArray_FLAGS(result) |= NPY_OWNDATA; 

	// massive use of iterators to progress through the data
	PyArrayIterObject *itr_v, *itr_r, *itr_c, *itr_s;
    itr_v = (PyArrayIterObject *) PyArray_IterNew(result);
    itr_r = (PyArrayIterObject *) PyArray_IterNew(R);
    itr_c = (PyArrayIterObject *) PyArray_IterNew(C);
    itr_s = (PyArrayIterObject *) PyArray_IterNew(S);
    pvol = (float *)PyArray_DATA(volume);
    xdim = PyArray_DIM(volume, 0);
    ydim = PyArray_DIM(volume, 1);
    zdim = PyArray_DIM(volume, 2);
    //printf("%f\n", pvol[4*20*30 + 11*30 + 15]);
    while(PyArray_ITER_NOTDONE(itr_v)) 
    {
		pvc = (float *) PyArray_ITER_DATA(itr_v);
		pr = (float *) PyArray_ITER_DATA(itr_r);
		pc = (float *) PyArray_ITER_DATA(itr_c);
		ps = (float *) PyArray_ITER_DATA(itr_s);
        // The order is weird because the tricubic code below is 
        // for Fortran ordering. Note that the xdim changes fast in
        // the code, whereas the rightmost dim should change fast
        // in C multidimensional arrays.
		*pvc = TriCubic(*ps, *pc, *pr, pvol, zdim, ydim, xdim); 
		PyArray_ITER_NEXT(itr_v);
		PyArray_ITER_NEXT(itr_r);
		PyArray_ITER_NEXT(itr_c);
		PyArray_ITER_NEXT(itr_s);
    }

	return result;
}
void allstats_ubyte(PyObject *inputarray, stats *result) {
	PyObject *iter;
	npy_ubyte *ptr;

	iter = PyArray_IterNew(inputarray);

	while (PyArray_ITER_NOTDONE(iter)) {
		ptr = (npy_ubyte *)PyArray_ITER_DATA(iter);
		updateStats(result, (double) (*ptr));
		PyArray_ITER_NEXT(iter);
	}
	Py_XDECREF(iter);
}
Пример #3
0
static PyObject*
PyUnits_convert(
    PyUnits* self,
    PyObject* args,
    PyObject* kwds) {

  int            status       = 1;
  PyObject*      input        = NULL;
  PyArrayObject* input_arr    = NULL;
  PyArrayObject* output_arr   = NULL;
  PyObject*      input_iter   = NULL;
  PyObject*      output_iter  = NULL;
  double         input_val;
  double         output_val;

  if (!PyArg_ParseTuple(args, "O:UnitConverter.convert", &input)) {
    goto exit;
  }

  input_arr = (PyArrayObject*)PyArray_FromObject(
      input, NPY_DOUBLE, 0, NPY_MAXDIMS);
  if (input_arr == NULL) {
    goto exit;
  }

  output_arr = (PyArrayObject*)PyArray_SimpleNew(
      PyArray_NDIM(input_arr), PyArray_DIMS(input_arr), PyArray_DOUBLE);
  if (output_arr == NULL) {
    goto exit;
  }

  input_iter = PyArray_IterNew((PyObject*)input_arr);
  if (input_iter == NULL) {
    goto exit;
  }

  output_iter = PyArray_IterNew((PyObject*)output_arr);
  if (output_iter == NULL) {
    goto exit;
  }

  if (self->power != 1.0) {
    while (PyArray_ITER_NOTDONE(input_iter)) {
      input_val = *(double *)PyArray_ITER_DATA(input_iter);
      output_val = pow(self->scale*input_val + self->offset, self->power);
      if (errno) {
        PyErr_SetFromErrno(PyExc_ValueError);
        goto exit;
      }
      *(double *)PyArray_ITER_DATA(output_iter) = output_val;
      PyArray_ITER_NEXT(input_iter);
      PyArray_ITER_NEXT(output_iter);
    }
  } else {
    while (PyArray_ITER_NOTDONE(input_iter)) {
      input_val = *(double *)PyArray_ITER_DATA(input_iter);
      output_val = self->scale*input_val + self->offset;
      *(double *)PyArray_ITER_DATA(output_iter) = output_val;
      PyArray_ITER_NEXT(input_iter);
      PyArray_ITER_NEXT(output_iter);
    }
  }

  status = 0;

 exit:

  Py_XDECREF((PyObject*)input_arr);
  Py_XDECREF(input_iter);
  Py_XDECREF(output_iter);
  if (status) {
    Py_XDECREF((PyObject*)output_arr);
    return NULL;
  }
  return (PyObject*)output_arr;
}