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

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

Implement reading BSL/OTOKO header file

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