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 | */ |
---|
34 | void 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 | */ |
---|
43 | PyObject * 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 | /** |
---|
56 | * Delete a QSmearer object |
---|
57 | */ |
---|
58 | void del_q_smearer(void *ptr){ |
---|
59 | QSmearer* smearer = static_cast<QSmearer *>(ptr); |
---|
60 | delete smearer; |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Create a QSmearer as a python object |
---|
66 | */ |
---|
67 | PyObject * new_q_smearer(PyObject *, PyObject *args) { |
---|
68 | PyObject *width_obj; |
---|
69 | Py_ssize_t n_w; |
---|
70 | double* width; |
---|
71 | double qmin; |
---|
72 | double qmax; |
---|
73 | int nbins; |
---|
74 | |
---|
75 | if (!PyArg_ParseTuple(args, "Oddi", &width_obj, &qmin, &qmax, &nbins)) return NULL; |
---|
76 | OUTVECTOR(width_obj, width, n_w); |
---|
77 | QSmearer* smearer = new QSmearer(width, qmin, qmax, nbins); |
---|
78 | return PyCObject_FromVoidPtr(smearer, del_q_smearer); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Smear the given input according to a given smearer object |
---|
83 | */ |
---|
84 | PyObject * smear_input(PyObject *, PyObject *args) { |
---|
85 | PyObject *input_iq_obj; |
---|
86 | PyObject *output_iq_obj; |
---|
87 | PyObject *smear_obj; |
---|
88 | Py_ssize_t n_in; |
---|
89 | Py_ssize_t n_out; |
---|
90 | double *input_iq; |
---|
91 | double *output_iq; |
---|
92 | int first_bin; |
---|
93 | int last_bin; |
---|
94 | |
---|
95 | |
---|
96 | if (!PyArg_ParseTuple(args, "OOOii", &smear_obj, &input_iq_obj, &output_iq_obj, &first_bin, &last_bin)) return NULL; |
---|
97 | OUTVECTOR(input_iq_obj, input_iq, n_in); |
---|
98 | OUTVECTOR(output_iq_obj, output_iq, n_out); |
---|
99 | |
---|
100 | // Sanity check |
---|
101 | if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
102 | |
---|
103 | // Set the array pointers |
---|
104 | void *temp = PyCObject_AsVoidPtr(smear_obj); |
---|
105 | BaseSmearer* s = static_cast<BaseSmearer *>(temp); |
---|
106 | |
---|
107 | if(n_in!=s->get_nbins()) { |
---|
108 | return Py_BuildValue("i",-2); |
---|
109 | } |
---|
110 | |
---|
111 | s->smear(input_iq, output_iq, first_bin, last_bin); |
---|
112 | |
---|
113 | return Py_BuildValue("i",1); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * Define module methods |
---|
119 | */ |
---|
120 | static PyMethodDef module_methods[] = { |
---|
121 | {"new_slit_smearer", (PyCFunction)new_slit_smearer, METH_VARARGS, |
---|
122 | "Create a new Slit Smearer object"}, |
---|
123 | {"new_q_smearer", (PyCFunction)new_q_smearer, METH_VARARGS, |
---|
124 | "Create a new Q Smearer object"}, |
---|
125 | {"smear",(PyCFunction)smear_input, METH_VARARGS, |
---|
126 | "Smear the given input array"}, |
---|
127 | {NULL} |
---|
128 | }; |
---|
129 | |
---|
130 | |
---|
131 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
132 | #define PyMODINIT_FUNC void |
---|
133 | #endif |
---|
134 | PyMODINIT_FUNC |
---|
135 | initsmearer(void) |
---|
136 | { |
---|
137 | PyObject* m; |
---|
138 | |
---|
139 | m = Py_InitModule3("smearer", module_methods, |
---|
140 | "Q Smearing module"); |
---|
141 | } |
---|