#include "Python.h" #include #include #include #include #include "libnumarray.h" static PyObject *_convolveError; static void Convolve1d(long ksizex, Float64 *kernel, long dsizex, Float64 *data, Float64 *convolved) { long xc; long halfk = ksizex/2; for(xc=0; xcnd != 1) || (data->nd != 1)) { PyErr_Format(_convolveError, "Convolve1d: numarray must have 1 dimensions."); goto _fail; } if (!NA_ShapeEqual(data, convolved)) { PyErr_Format(_convolveError, "Convolve1d: data and output numarray need" "identitcal shapes."); goto _fail; } Convolve1d(kernel->dimensions[0], NA_OFFSETDATA(kernel), data->dimensions[0], NA_OFFSETDATA(data), NA_OFFSETDATA(convolved)); Py_XDECREF(kernel); Py_XDECREF(data); /* Align, Byteswap, Contiguous, Typeconvert */ return NA_ReturnOutput(oconvolved, convolved); _fail: Py_XDECREF(kernel); Py_XDECREF(data); Py_XDECREF(convolved); return NULL; } static PyObject * Py_Convolve2d(PyObject *obj, PyObject *args) { PyObject *okernel, *odata, *oconvolved=Py_None; PyArrayObject *kernel, *data, *convolved; if (!PyArg_ParseTuple(args, "OO|O", &okernel, &odata, &oconvolved)) return PyErr_Format(_convolveError, "Convolve2d: Invalid parameters."); /* Align, Byteswap, Contiguous, Typeconvert */ kernel = NA_InputArray(okernel, tFloat64, NUM_C_ARRAY); data = NA_InputArray(odata, tFloat64, NUM_C_ARRAY); convolved = NA_OptionalOutputArray(oconvolved, tFloat64, NUM_C_ARRAY, data); if (!kernel || !data || !convolved) { PyErr_Format( _convolveError, "Convolve2d: error converting array inputs."); goto _fail; } if ((kernel->nd != 2) || (data->nd != 2)) { PyErr_Format(_convolveError, "Convolve2d: numarray must have 2 dimensions."); goto _fail; } if (!NA_ShapeEqual(data, convolved)) { PyErr_Format(_convolveError, "Convolve2d: data and output numarray need identitcal shapes."); goto _fail; } Convolve2d(kernel->dimensions[0], kernel->dimensions[1], NA_OFFSETDATA(kernel), data->dimensions[0], data->dimensions[1], NA_OFFSETDATA(data), NA_OFFSETDATA(convolved)); Py_XDECREF(kernel); Py_XDECREF(data); /* Align, Byteswap, Contiguous, Typeconvert */ return NA_ReturnOutput(oconvolved, convolved); _fail: Py_XDECREF(kernel); Py_XDECREF(data); Py_XDECREF(convolved); return NULL; } static PyMethodDef _convolveMethods[] = { {"Convolve1d", Py_Convolve1d, METH_VARARGS}, {"Convolve2d", Py_Convolve2d, METH_VARARGS}, {NULL, NULL} /* Sentinel */ }; /* platform independent*/ #ifdef MS_WIN32 __declspec(dllexport) #endif void inithigh_level(void) { PyObject *m, *d; m = Py_InitModule("high_level", _convolveMethods); d = PyModule_GetDict(m); _convolveError = PyErr_NewException("_high_level.error", NULL, NULL); PyDict_SetItemString(d, "error", _convolveError); import_libnumarray(); } /* * Local Variables: * mode: C * c-file-style: "python" * End: */