source: sasview/DataLoader/extensions/smearer_module.cpp @ e6093db

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e6093db was 65883cf, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

dataloader: fixed get_bin_range method for smearer objects

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[a3f8d58]1/**
2 *  Smearer module to perform point and slit smearing calculations
3
4        This software was developed by the University of Tennessee as part of the
5        Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
6        project funded by the US National Science Foundation.
7
8        See the license text in license.txt
9
10        copyright 2009, University of Tennessee
11 */
12#include <Python.h>
13#include <stdio.h>
14#include <smearer.hh>
15
16// Utilities
17#define INVECTOR(obj,buf,len)                                                                           \
18    do { \
19        int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \
20        if (err < 0) return NULL; \
21        len /= sizeof(*buf); \
22    } while (0)
23
24#define OUTVECTOR(obj,buf,len) \
25    do { \
26        int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \
27        if (err < 0) return NULL; \
28        len /= sizeof(*buf); \
29    } while (0)
30
31/**
32 * Delete a SlitSmearer object
33 */
34void del_slit_smearer(void *ptr){
35        SlitSmearer* smearer = static_cast<SlitSmearer *>(ptr);
36        delete smearer;
37        return;
38}
39
40/**
41 * Create a SlitSmearer as a python object
42 */
43PyObject * new_slit_smearer(PyObject *, PyObject *args) {
44        double width;
45        double height;
46        double qmin;
47        double qmax;
48        int nbins;
49
50        if (!PyArg_ParseTuple(args, "ddddi", &width, &height, &qmin, &qmax, &nbins)) return NULL;
51        SlitSmearer* smearer = new SlitSmearer(width, height, qmin, qmax, nbins);
52        return PyCObject_FromVoidPtr(smearer, del_slit_smearer);
53}
54
55/**
[5859862]56 * Create a SlitSmearer as a python object by supplying a q array
57 */
58PyObject * new_slit_smearer_with_q(PyObject *, PyObject *args) {
59        PyObject *qvalues_obj;
60        Py_ssize_t n_q;
61        double* qvalues;
62        double width;
63        double height;
64
65        if (!PyArg_ParseTuple(args, "ddO", &width, &height, &qvalues_obj)) return NULL;
66        OUTVECTOR(qvalues_obj, qvalues, n_q);
67        SlitSmearer* smearer = new SlitSmearer(width, height, qvalues, n_q);
68        return PyCObject_FromVoidPtr(smearer, del_slit_smearer);
69}
70
71/**
[a3f8d58]72 * Delete a QSmearer object
73 */
74void del_q_smearer(void *ptr){
75        QSmearer* smearer = static_cast<QSmearer *>(ptr);
76        delete smearer;
77        return;
78}
79
80/**
81 * Create a QSmearer as a python object
82 */
83PyObject * new_q_smearer(PyObject *, PyObject *args) {
84        PyObject *width_obj;
85        Py_ssize_t n_w;
86        double* width;
87        double qmin;
88        double qmax;
89        int nbins;
90
91        if (!PyArg_ParseTuple(args, "Oddi", &width_obj, &qmin, &qmax, &nbins)) return NULL;
92        OUTVECTOR(width_obj, width, n_w);
93        QSmearer* smearer = new QSmearer(width, qmin, qmax, nbins);
94        return PyCObject_FromVoidPtr(smearer, del_q_smearer);
95}
96
97/**
[5859862]98 * Create a QSmearer as a python object by supplying a q array
99 */
100PyObject * new_q_smearer_with_q(PyObject *, PyObject *args) {
101        PyObject *width_obj;
102        Py_ssize_t n_w;
103        double* width;
104        PyObject *qvalues_obj;
105        Py_ssize_t n_q;
106        double* qvalues;
107
108        if (!PyArg_ParseTuple(args, "OO", &width_obj, &qvalues_obj)) return NULL;
109        OUTVECTOR(width_obj, width, n_w);
110        OUTVECTOR(qvalues_obj, qvalues, n_q);
111        QSmearer* smearer = new QSmearer(width, qvalues, n_q);
112        return PyCObject_FromVoidPtr(smearer, del_q_smearer);
113}
114
115/**
[a3f8d58]116 * Smear the given input according to a given smearer object
117 */
118PyObject * smear_input(PyObject *, PyObject *args) {
119        PyObject *input_iq_obj;
120        PyObject *output_iq_obj;
121        PyObject *smear_obj;
122        Py_ssize_t n_in;
123        Py_ssize_t n_out;
124        double *input_iq;
125        double *output_iq;
126        int first_bin;
127        int last_bin;
128
129
130        if (!PyArg_ParseTuple(args, "OOOii", &smear_obj, &input_iq_obj, &output_iq_obj, &first_bin, &last_bin)) return NULL;
131        OUTVECTOR(input_iq_obj, input_iq, n_in);
132        OUTVECTOR(output_iq_obj, output_iq, n_out);
133
134        // Sanity check
135        if(n_in!=n_out) return Py_BuildValue("i",-1);
136
137        // Set the array pointers
138        void *temp = PyCObject_AsVoidPtr(smear_obj);
139        BaseSmearer* s = static_cast<BaseSmearer *>(temp);
140
141        if(n_in!=s->get_nbins()) {
142                return Py_BuildValue("i",-2);
143        }
144
145        s->smear(input_iq, output_iq, first_bin, last_bin);
146
147        return Py_BuildValue("i",1);
148}
149
[5859862]150/**
151 * Get the q-value of a given bin
152 */
153PyObject * get_q(PyObject *, PyObject *args) {
154        PyObject *smear_obj;
155        int bin;
156
157        if (!PyArg_ParseTuple(args, "Oi", &smear_obj, &bin)) return NULL;
158
159        // Set the array pointers
160        void *temp = PyCObject_AsVoidPtr(smear_obj);
161        BaseSmearer* s = static_cast<BaseSmearer *>(temp);
162
[65883cf]163        if(s->get_nbins()<=0 || s->get_nbins()<=bin) {
164                return NULL;
165        }
[5859862]166
[65883cf]167        double q, q_min, q_max;
168        if (s->get_bin_range(bin, &q, &q_min, &q_max)<0) {
169                return NULL;
170        }
[5859862]171        return Py_BuildValue("d", q);
172}
173
[a3f8d58]174
175/**
176 * Define module methods
177 */
178static PyMethodDef module_methods[] = {
179        {"new_slit_smearer", (PyCFunction)new_slit_smearer, METH_VARARGS,
180                  "Create a new Slit Smearer object"},
[5859862]181        {"new_slit_smearer_with_q", (PyCFunction)new_slit_smearer_with_q, METH_VARARGS,
182                  "Create a new Slit Smearer object by supplying an array of Q values"},
[a3f8d58]183        {"new_q_smearer", (PyCFunction)new_q_smearer, METH_VARARGS,
184                  "Create a new Q Smearer object"},
[5859862]185        {"new_q_smearer_with_q", (PyCFunction)new_q_smearer_with_q, METH_VARARGS,
186                  "Create a new Q Smearer object by supplying an array of Q values"},
[a3f8d58]187        {"smear",(PyCFunction)smear_input, METH_VARARGS,
188                  "Smear the given input array"},
[5859862]189        {"get_q",(PyCFunction)get_q, METH_VARARGS,
190                  "Get the q-value of a given bin"},
[a3f8d58]191    {NULL}
192};
193
194
195#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
196#define PyMODINIT_FUNC void
197#endif
198PyMODINIT_FUNC
199initsmearer(void)
200{
201    PyObject* m;
202
203    m = Py_InitModule3("smearer", module_methods,
204                       "Q Smearing module");
205}
Note: See TracBrowser for help on using the repository browser.