From 0ef791f9f681450aaaadf544921b927c41d77dd7 2017-04-17 22:28:52 From: Juan C. Valdez Date: 2017-04-17 22:28:52 Subject: [PATCH] Add C extension for hildebrand_sekhon noise estimation and update requirements versions in setup --- diff --git a/schainpy/model/data/jrodata.py b/schainpy/model/data/jrodata.py index bc94ee7..6cd9c1d 100644 --- a/schainpy/model/data/jrodata.py +++ b/schainpy/model/data/jrodata.py @@ -9,6 +9,8 @@ import numpy import datetime from jroheaderIO import SystemHeader, RadarControllerHeader +from schainpy import cSchain + def getNumpyDtype(dataTypeCode): @@ -64,41 +66,45 @@ def hildebrand_sekhon(data, navg): """ sortdata = numpy.sort(data,axis=None) - lenOfData = len(sortdata) - nums_min = lenOfData*0.2 - - if nums_min <= 5: - nums_min = 5 +# lenOfData = len(sortdata) +# nums_min = lenOfData*0.2 +# +# if nums_min <= 5: +# nums_min = 5 +# +# sump = 0. +# +# sumq = 0. +# +# j = 0 +# +# cont = 1 +# +# while((cont==1)and(j nums_min: +# rtest = float(j)/(j-1) + 1.0/navg +# if ((sumq*j) > (rtest*sump**2)): +# j = j - 1 +# sump = sump - sortdata[j] +# sumq = sumq - sortdata[j]**2 +# cont = 0 +# +# j += 1 +# +# lnoise = sump /j +# +# return lnoise + + return cSchain.hildebrand_sekhon(sortdata, navg) - sump = 0. - - sumq = 0. - - j = 0 - - cont = 1 - - while((cont==1)and(j nums_min: - rtest = float(j)/(j-1) + 1.0/navg - if ((sumq*j) > (rtest*sump**2)): - j = j - 1 - sump = sump - sortdata[j] - sumq = sumq - sortdata[j]**2 - cont = 0 - - j += 1 - - lnoise = sump /j -# stdv = numpy.sqrt((sumq - lnoise**2)/(j - 1)) - return lnoise class Beam: + def __init__(self): self.codeList = [] self.azimuthList = [] diff --git a/schainpy/model/proc/extensions.c b/schainpy/model/proc/extensions.c new file mode 100644 index 0000000..8117e99 --- /dev/null +++ b/schainpy/model/proc/extensions.c @@ -0,0 +1,55 @@ +#include +#include +#include + +static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args); + +static PyMethodDef extensionsMethods[] = { + { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with" }, + { NULL, NULL, 0, NULL } +}; + +PyMODINIT_FUNC initcSchain() { + Py_InitModule("cSchain", extensionsMethods); + import_array(); +} + +static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) { + /* Do your stuff here. */ + double navg; + PyObject *data_obj, *data_array; + + if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL; + data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_IN_ARRAY); + if (data_array == NULL) { + Py_XDECREF(data_array); + Py_XDECREF(data_obj); + return NULL; + } + double *sortdata = (double*)PyArray_DATA(data_array); + int lenOfData = (int)PyArray_SIZE(data_array) ; + double nums_min = lenOfData*0.2; + if (nums_min <= 5) nums_min = 5; + double sump = 0; + double sumq = 0; + int j = 0; + int cont = 1; + double rtest = 0; + while ((cont == 1) && (j < lenOfData)) { + sump = sump + sortdata[j]; + sumq = sumq + pow(sortdata[j], 2); + if (j > nums_min) { + rtest = (double)j/(j-1) + 1/navg; + if ((sumq*j) > (rtest*pow(sump, 2))) { + j = j - 1; + sump = sump - sortdata[j]; + sumq = sumq - pow(sortdata[j],2); + cont = 0; + } + } + j = j + 1; + } + + double lnoise = sump / j; + return Py_BuildValue("d", lnoise); +} diff --git a/setup.py b/setup.py index a2e379b..a0f14e8 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ ''' Created on Jul 16, 2014 -@author: @author: Miguel Urco +@author: Miguel Urco ''' from schainpy import __version__ @@ -26,6 +26,7 @@ setup(name="schainpy", 'schainpy.gui.viewcontroller', 'schainpy.gui.viewer', 'schainpy.gui.viewer.windows'}, + ext_package='schainpy', py_modules=[''], package_data={'': ['schain.conf.template'], 'schainpy.gui.figures': ['*.png','*.jpg'], @@ -33,13 +34,15 @@ setup(name="schainpy", include_package_data=False, scripts =['schainpy/gui/schainGUI', 'schainpy/scripts/schain'], + ext_modules=[Extension("cSchain", ["schainpy/model/proc/extensions.c"])], install_requires=[ - "scipy >= 0.9.0", - "h5py >= 2.0.1", - "matplotlib >= 1.0.0", - "pyfits >= 2.0.0", - "numpy >= 1.6.0", - "paramiko", - "paho-mqtt" + "scipy >= 0.14.0", + "h5py >= 2.2.1", + "matplotlib >= 1.4.2", + "pyfits >= 3.4", + "numpy >= 1.11.2", + "paramiko >= 2.1.2", + "paho-mqtt >= 1.2", + "zmq", ], ) \ No newline at end of file