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

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 535e181 was 535e181, checked in by lewis, 8 years ago

Begin implementing 2D BSL loader into GUI

  • Property mode set to 100644
File size: 8.7 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 n_frames;
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, &n_frames, &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.n_frames = n_frames;
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\nn_frames: %d\nframe: %d\nn_pixels: %d\nn_rasters: %d\nswap_bytes: %d",
52        self->params.filename,
53        self->params.n_frames,
54        self->params.frame,
55        self->params.n_pixels,
56        self->params.n_rasters,
57        self->params.swap_bytes);
58    return Py_BuildValue("s", str);
59}
60
61/*                    ----- Setters and Getters -----                        */
62
63static PyObject *get_filename(CLoader *self, PyObject *args) {
64    return Py_BuildValue("s", self->params.filename);
65}
66
67static PyObject *set_filename(CLoader *self, PyObject *args) {
68    const char *new_filename;
69    if (!PyArg_ParseTuple(args, "s", &new_filename))
70        return NULL;
71    strcpy(self->params.filename, new_filename);
72
73    return Py_BuildValue("s", self->params.filename);
74}
75
76static PyObject *get_n_frames(CLoader *self, PyObject *args) {
77    return Py_BuildValue("i", self->params.n_frames);
78}
79
80static PyObject *set_n_frames(CLoader *self, PyObject *args) {
81    int new_frames;
82    if (!PyArg_ParseTuple(args, "i", &new_frames))
83        return NULL;
84    self->params.n_frames = new_frames;
85
86    return Py_BuildValue("i", self->params.n_frames);
87}
88
89static PyObject *get_frame(CLoader *self, PyObject *args) {
90    return Py_BuildValue("i", self->params.frame);
91}
92
93static PyObject *set_frame(CLoader *self, PyObject *args) {
94    int new_frame;
95    if (!PyArg_ParseTuple(args, "i", &new_frame))
96        return NULL;
97    self->params.frame = new_frame;
98
99    return Py_BuildValue("i", self->params.frame);
100}
101
102static PyObject *get_n_pixels(CLoader *self, PyObject *args) {
103    return Py_BuildValue("i", self->params.n_pixels);
104}
105
106static PyObject *set_n_pixels(CLoader *self, PyObject *args) {
107    int new_pixels;
108    if (!PyArg_ParseTuple(args, "i", &new_pixels))
109        return NULL;
110    self->params.n_pixels = new_pixels;
111
112    return Py_BuildValue("i", self->params.n_pixels);
113}
114
115static PyObject *get_n_rasters(CLoader *self, PyObject *args) {
116    return Py_BuildValue("i", self->params.n_rasters);
117}
118
119static PyObject *set_n_rasters(CLoader *self, PyObject *args) {
120    int new_rasters;
121    if (!PyArg_ParseTuple(args, "i", &new_rasters))
122        return NULL;
123    self->params.n_rasters = new_rasters;
124
125    return Py_BuildValue("i", self->params.n_rasters);
126}
127
128static PyObject *get_swap_bytes(CLoader *self, PyObject *args) {
129    return Py_BuildValue("i", self->params.swap_bytes);
130}
131
132static PyObject *set_swap_bytes(CLoader *self, PyObject *args) {
133    int new_swap;
134    if (!PyArg_ParseTuple(args, "i", &new_swap))
135        return NULL;
136    self->params.swap_bytes = new_swap;
137
138    return Py_BuildValue("i", self->params.swap_bytes);
139}
140
141/*                        ----- Instance Methods -----                       */
142
143float reverse_float(const float in_float){
144    float retval;
145    char *to_convert = (char *)&in_float;
146    char *return_float = (char *)&retval;
147
148    return_float[0] = to_convert[3];
149    return_float[1] = to_convert[2];
150    return_float[2] = to_convert[1];
151    return_float[3] = to_convert[0];
152
153    return retval;
154}
155
156static PyObject *load_data(CLoader *self, PyObject *args) {
157    int raster;
158    int pixel;
159    int frame_pos;
160    npy_intp size[2] = {self->params.n_rasters, self->params.n_pixels};
161    float cur_val;
162    FILE *input_file;
163    PyArrayObject *data;
164
165    data = (PyArrayObject *)PyArray_SimpleNew(2, size, NPY_FLOAT);
166
167    input_file = fopen(self->params.filename, "rb");
168    if (!input_file) {
169        return NULL;
170    }
171
172    frame_pos = self->params.n_pixels * self->params.n_rasters * self->params.frame;
173    fseek(input_file, frame_pos*sizeof(float), SEEK_SET);
174
175    for (raster = 0; raster < self->params.n_rasters; raster++) {
176        for (pixel = 0; pixel < self->params.n_pixels; pixel++) {
177            fread(&cur_val, sizeof(float), 1, input_file);
178            if (self->params.swap_bytes == 0)
179                cur_val = reverse_float(cur_val);
180            PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val));
181        }
182    }
183
184    fclose(input_file);
185
186    return Py_BuildValue("N", data);
187}
188
189/*                           ----- Class Registration -----                  */
190
191static PyMethodDef CLoader_methods[] = {
192    { "to_string", (PyCFunction)to_string, METH_VARARGS, "Print the objects params" },
193    { "get_filename", (PyCFunction)get_filename, METH_VARARGS, "Get the filename" },
194    { "set_filename", (PyCFunction)set_filename, METH_VARARGS, "Set the filename" },
195    { "get_n_frames", (PyCFunction)get_n_frames, METH_VARARGS, "Get n_frames" },
196    { "set_n_frames", (PyCFunction)set_n_frames, METH_VARARGS, "Set n_frames" },
197    { "get_frame", (PyCFunction)get_frame, METH_VARARGS, "Get the frame that will be loaded" },
198    { "set_frame", (PyCFunction)set_frame, METH_VARARGS, "Set the frame that will be loaded" },
199    { "get_n_pixels", (PyCFunction)get_n_pixels, METH_VARARGS, "Get n_pixels" },
200    { "set_n_pixels", (PyCFunction)set_n_pixels, METH_VARARGS, "Set n_pixels" },
201    { "get_n_rasters", (PyCFunction)get_n_rasters, METH_VARARGS, "Get n_rasters" },
202    { "set_n_rasters", (PyCFunction)set_n_rasters, METH_VARARGS, "Set n_rasters" },
203    { "get_swap_bytes", (PyCFunction)get_swap_bytes, METH_VARARGS, "Get swap_bytes" },
204    { "set_swap_bytes", (PyCFunction)set_swap_bytes, METH_VARARGS, "Set swap_bytes" },
205    { "load_data", (PyCFunction)load_data, METH_VARARGS, "Load the data into a numpy array" },
206    {NULL}
207};
208
209static PyMemberDef CLoader_members[] = {
210    {NULL}
211};
212
213static PyTypeObject CLoaderType = {
214    PyObject_HEAD_INIT(NULL)
215    0,                         /*ob_size*/
216    "CLoader",             /*tp_name*/
217    sizeof(CLoader),             /*tp_basicsize*/
218    0,                         /*tp_itemsize*/
219    (destructor)CLoader_dealloc, /*tp_dealloc*/
220    0,                         /*tp_print*/
221    0,                         /*tp_getattr*/
222    0,                         /*tp_setattr*/
223    0,                         /*tp_compare*/
224    0,                         /*tp_repr*/
225    0,                         /*tp_as_number*/
226    0,                         /*tp_as_sequence*/
227    0,                         /*tp_as_mapping*/
228    0,                         /*tp_hash */
229    0,                         /*tp_call*/
230    0,                         /*tp_str*/
231    0,                         /*tp_getattro*/
232    0,                         /*tp_setattro*/
233    0,                         /*tp_as_buffer*/
234    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
235    "CLoader objects",           /* tp_doc */
236    0,                         /* tp_traverse */
237    0,                         /* tp_clear */
238    0,                         /* tp_richcompare */
239    0,                         /* tp_weaklistoffset */
240    0,                         /* tp_iter */
241    0,                         /* tp_iternext */
242    CLoader_methods,             /* tp_methods */
243    CLoader_members,             /* tp_members */
244    0,                         /* tp_getset */
245    0,                         /* tp_base */
246    0,                         /* tp_dict */
247    0,                         /* tp_descr_get */
248    0,                         /* tp_descr_set */
249    0,                         /* tp_dictoffset */
250    (initproc)CLoader_init,      /* tp_init */
251    0,                         /* tp_alloc */
252    CLoader_new,                 /* tp_new */
253};
254
255PyMODINIT_FUNC
256initbsl_loader(void)
257{
258    PyObject *module;
259    module = Py_InitModule("bsl_loader", NULL);
260    import_array();
261
262    if (PyType_Ready(&CLoaderType) < 0)
263        return;
264
265    Py_INCREF(&CLoaderType);
266    PyModule_AddObject(module, "CLoader", (PyObject *)&CLoaderType);
267}
Note: See TracBrowser for help on using the repository browser.