source: sasview/src/sas/sascalc/file_converter/c_ext/bsl_loader.c @ c3f0114

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.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c3f0114 was c3f0114, checked in by lewis, 8 years ago

Remove debug statements and tidy up

  • Property mode set to 100644
File size: 8.2 KB
Line 
1#include <Python.h>
2#include <numpy/arrayobject.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include "structmember.h"
6#include "bsl_loader.h"
7
8typedef struct {
9    PyObject_HEAD
10    CLoader_params params;
11} CLoader;
12
13static PyObject *CLoader_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
14    CLoader *self;
15
16    self = (CLoader *)type->tp_alloc(type, 0);
17
18    return (PyObject *)self;
19}
20
21static PyObject *CLoader_init(CLoader *self, PyObject *args, PyObject *kwds) {
22    const char *filename;
23    const int frame;
24    const int n_pixels;
25    const int n_rasters;
26    const int swap_bytes;
27
28    if (self != NULL) {
29        if (!PyArg_ParseTuple(args, "siiii", &filename, &frame, &n_pixels, &n_rasters, &swap_bytes))
30            Py_RETURN_NONE;
31        if (!(self->params.filename = malloc(strlen(filename) + 1)))
32            Py_RETURN_NONE;
33        strcpy(self->params.filename, filename);
34        self->params.frame = frame;
35        self->params.n_pixels = n_pixels;
36        self->params.n_rasters = n_rasters;
37        self->params.swap_bytes = swap_bytes;
38    }
39
40    return 0;
41}
42
43static void CLoader_dealloc(CLoader *self) {
44    free(self->params.filename);
45    self->ob_type->tp_free((PyObject *)self);
46}
47
48static PyObject *to_string(CLoader *self, PyObject *params) {
49    char str[100];
50    sprintf(str,
51        "Filename: %s\nframe: %d\nn_pixels: %d\nn_rasters: %d\nswap_bytes: %d",
52        self->params.filename,
53        self->params.frame,
54        self->params.n_pixels,
55        self->params.n_rasters,
56        self->params.swap_bytes);
57    return Py_BuildValue("s", str);
58}
59
60/*                    ----- Setters and Getters -----                        */
61
62static PyObject *get_filename(CLoader *self, PyObject *args) {
63    return Py_BuildValue("s", self->params.filename);
64}
65
66static PyObject *set_filename(CLoader *self, PyObject *args) {
67    const char *new_filename;
68    if (!PyArg_ParseTuple(args, "s", &new_filename))
69        return NULL;
70    strcpy(self->params.filename, new_filename);
71
72    return Py_BuildValue("s", self->params.filename);
73}
74
75static PyObject *get_frame(CLoader *self, PyObject *args) {
76    return Py_BuildValue("i", self->params.frame);
77}
78
79static PyObject *set_frame(CLoader *self, PyObject *args) {
80    int new_frame;
81    if (!PyArg_ParseTuple(args, "i", &new_frame))
82        return NULL;
83    self->params.frame = new_frame;
84
85    return Py_BuildValue("i", self->params.frame);
86}
87
88static PyObject *get_n_pixels(CLoader *self, PyObject *args) {
89    return Py_BuildValue("i", self->params.n_pixels);
90}
91
92static PyObject *set_n_pixels(CLoader *self, PyObject *args) {
93    int new_pixels;
94    if (!PyArg_ParseTuple(args, "i", &new_pixels))
95        return NULL;
96    self->params.n_pixels = new_pixels;
97
98    return Py_BuildValue("i", self->params.n_pixels);
99}
100
101static PyObject *get_n_rasters(CLoader *self, PyObject *args) {
102    return Py_BuildValue("i", self->params.n_rasters);
103}
104
105static PyObject *set_n_rasters(CLoader *self, PyObject *args) {
106    int new_rasters;
107    if (!PyArg_ParseTuple(args, "i", &new_rasters))
108        return NULL;
109    self->params.n_rasters = new_rasters;
110
111    return Py_BuildValue("i", self->params.n_rasters);
112}
113
114static PyObject *get_swap_bytes(CLoader *self, PyObject *args) {
115    return Py_BuildValue("i", self->params.swap_bytes);
116}
117
118static PyObject *set_swap_bytes(CLoader *self, PyObject *args) {
119    int new_swap;
120    if (!PyArg_ParseTuple(args, "i", &new_swap))
121        return NULL;
122    self->params.swap_bytes = new_swap;
123
124    return Py_BuildValue("i", self->params.swap_bytes);
125}
126
127/*                        ----- Instance Methods -----                       */
128
129float reverse_float(const float in_float){
130    float retval;
131    char *to_convert = (char *)&in_float;
132    char *return_float = (char *)&retval;
133
134    return_float[0] = to_convert[3];
135    return_float[1] = to_convert[2];
136    return_float[2] = to_convert[1];
137    return_float[3] = to_convert[0];
138
139    return retval;
140}
141
142static PyObject *load_data(CLoader *self, PyObject *args) {
143    int raster;
144    int pixel;
145    int frame_pos;
146    int size[2] = {self->params.n_rasters, self->params.n_pixels};
147    float cur_val;
148    FILE *input_file;
149    PyArrayObject *data;
150
151    if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &data)) {
152        return NULL;
153    }
154
155    input_file = fopen(self->params.filename, "rb");
156    if (!input_file) {
157        return NULL;
158    }
159
160    frame_pos = self->params.n_pixels * self->params.n_rasters * self->params.frame;
161    fseek(input_file, frame_pos*sizeof(float), SEEK_SET);
162
163    for (raster = 0; raster < self->params.n_rasters; raster++) {
164        for (pixel = 0; pixel < self->params.n_pixels; pixel++) {
165            fread(&cur_val, sizeof(float), 1, input_file);
166            if (self->params.swap_bytes == 0)
167                cur_val = reverse_float(cur_val);
168            PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val));
169        }
170    }
171
172    fclose(input_file);
173    Py_DECREF(data);
174
175    return Py_BuildValue("O", data);
176}
177
178/*                           ----- Class Registration -----                  */
179
180static PyMethodDef CLoader_methods[] = {
181    { "to_string", (PyCFunction)to_string, METH_VARARGS, "Print the objects params" },
182    { "get_filename", (PyCFunction)get_filename, METH_VARARGS, "Get the filename" },
183    { "set_filename", (PyCFunction)set_filename, METH_VARARGS, "Set the filename" },
184    { "get_frame", (PyCFunction)get_frame, METH_VARARGS, "Get the frame that will be loaded" },
185    { "set_frame", (PyCFunction)set_frame, METH_VARARGS, "Set the frame that will be loaded" },
186    { "get_n_pixels", (PyCFunction)get_n_pixels, METH_VARARGS, "Get n_pixels" },
187    { "set_n_pixels", (PyCFunction)set_n_pixels, METH_VARARGS, "Set n_pixels" },
188    { "get_n_rasters", (PyCFunction)get_n_rasters, METH_VARARGS, "Get n_rasters" },
189    { "set_n_rasters", (PyCFunction)set_n_rasters, METH_VARARGS, "Set n_rasters" },
190    { "get_swap_bytes", (PyCFunction)get_swap_bytes, METH_VARARGS, "Get swap_bytes" },
191    { "set_swap_bytes", (PyCFunction)set_swap_bytes, METH_VARARGS, "Set swap_bytes" },
192    { "load_data", (PyCFunction)load_data, METH_VARARGS, "Load the data into a numpy array" },
193    {NULL}
194};
195
196static PyMemberDef CLoader_members[] = {
197    {NULL}
198};
199
200static PyTypeObject CLoaderType = {
201    PyObject_HEAD_INIT(NULL)
202    0,                         /*ob_size*/
203    "CLoader",             /*tp_name*/
204    sizeof(CLoader),             /*tp_basicsize*/
205    0,                         /*tp_itemsize*/
206    (destructor)CLoader_dealloc, /*tp_dealloc*/
207    0,                         /*tp_print*/
208    0,                         /*tp_getattr*/
209    0,                         /*tp_setattr*/
210    0,                         /*tp_compare*/
211    0,                         /*tp_repr*/
212    0,                         /*tp_as_number*/
213    0,                         /*tp_as_sequence*/
214    0,                         /*tp_as_mapping*/
215    0,                         /*tp_hash */
216    0,                         /*tp_call*/
217    0,                         /*tp_str*/
218    0,                         /*tp_getattro*/
219    0,                         /*tp_setattro*/
220    0,                         /*tp_as_buffer*/
221    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
222    "CLoader objects",           /* tp_doc */
223    0,                         /* tp_traverse */
224    0,                         /* tp_clear */
225    0,                         /* tp_richcompare */
226    0,                         /* tp_weaklistoffset */
227    0,                         /* tp_iter */
228    0,                         /* tp_iternext */
229    CLoader_methods,             /* tp_methods */
230    CLoader_members,             /* tp_members */
231    0,                         /* tp_getset */
232    0,                         /* tp_base */
233    0,                         /* tp_dict */
234    0,                         /* tp_descr_get */
235    0,                         /* tp_descr_set */
236    0,                         /* tp_dictoffset */
237    (initproc)CLoader_init,      /* tp_init */
238    0,                         /* tp_alloc */
239    CLoader_new,                 /* tp_new */
240};
241
242PyMODINIT_FUNC
243initbsl_loader(void)
244{
245    PyObject *module;
246    module = Py_InitModule("bsl_loader", NULL);
247    import_array();
248
249    if (PyType_Ready(&CLoaderType) < 0)
250        return;
251
252    Py_INCREF(&CLoaderType);
253    PyModule_AddObject(module, "CLoader", (PyObject *)&CLoaderType);
254}
Note: See TracBrowser for help on using the repository browser.