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

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

Implement getters in python class

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[18e7309]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    if (self != NULL) {
27        if (!PyArg_ParseTuple(args, "siii", &filename, &frame, &n_pixels, &n_rasters))
28            Py_RETURN_NONE;
29        if (!(self->params.filename = malloc(strlen(filename) + 1)))
30            Py_RETURN_NONE;
31        strcpy(self->params.filename, filename);
32        self->params.n_pixels = n_pixels;
33        self->params.n_rasters = n_rasters;
34    }
35
36    return 0;
37}
38
39static void CLoader_dealloc(CLoader *self) {
40    free(self->params.filename);
41    self->ob_type->tp_free((PyObject *)self);
42}
43
44static PyObject *to_string(CLoader *self, PyObject *params) {
45    char str[100];
46    sprintf(str,
47        "Filename: %s\nframe: %d\nn_pixels: %d\nn_rasters: %d\n",
48        self->params.filename,
49        self->params.frame,
50        self->params.n_pixels,
51        self->params.n_rasters);
52    return Py_BuildValue("s", str);
53}
54
[2760662]55static PyObject *get_filename(CLoader *self, PyObject *args) {
56    return Py_BuildValue("s", self->params.filename);
57}
58
59static PyObject *get_frame(CLoader *self, PyObject *args) {
60    return Py_BuildValue("i", self->params.frame);
61}
62
63static PyObject *get_n_pixels(CLoader *self, PyObject *args) {
64    return Py_BuildValue("i", self->params.n_pixels);
65}
66
67static PyObject *get_n_rasters(CLoader *self, PyObject *args) {
68    return Py_BuildValue("i", self->params.n_rasters);
69}
70
[18e7309]71static PyObject *load_data(CLoader *self, PyObject *args) {
72    int raster;
73    int pixel;
74    int frame_pos;
75    int size[2] = {self->params.n_rasters, self->params.n_pixels};
76    float cur_val;
77    PyObject *read_val;
78    FILE *input_file;
79    PyArrayObject *data;
80
81    printf("load_data called\n");
82
83    if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &data)) {
84        printf("Failed parsing PyArray object");
85        return NULL;
86    }
87
88    input_file = fopen(self->params.filename, "rb");
89    if (!input_file) {
90        printf("Failed opening file\n");
91        return NULL;
92    }
93
94    printf("Calculating frame_pos\n");
95    frame_pos = self->params.n_pixels * self->params.n_rasters * self->params.frame;
96    printf("frame_pos: %d\n", frame_pos);
97    fseek(input_file, frame_pos*sizeof(float), SEEK_SET);
98    printf("fseek completed\n");
99
100    for (raster = 0; raster < self->params.n_rasters; raster++) {
101        for (pixel = 0; pixel < self->params.n_pixels; pixel++) {
102            fread(&cur_val, sizeof(float), 1, input_file);
103            PyArray_SETITEM(data, PyArray_GETPTR2(data, raster, pixel), PyFloat_FromDouble(cur_val));
104            read_val = PyArray_GETITEM(data, PyArray_GETPTR2(data, raster, pixel));
105        }
106    }
107    printf("Data read.\n");
108
109    fclose(input_file);
110    printf("File closed\n");
111    Py_DECREF(data);
112    printf("Function completed\n");
113    return Py_BuildValue("O", data);
114}
115
116static PyMethodDef CLoader_methods[] = {
117    { "to_string", (PyCFunction)to_string, METH_VARARGS, "Print the objects params" },
[2760662]118    { "get_filename", (PyCFunction)get_filename, METH_VARARGS, "Get the filename" },
119    { "get_frame", (PyCFunction)get_frame, METH_VARARGS, "Get the frame that will be loaded" },
120    { "get_n_pixels", (PyCFunction)get_n_pixels, METH_VARARGS, "Get n_pixels" },
121    { "get_n_rasters", (PyCFunction)get_n_rasters, METH_VARARGS, "Get n_rasters" },
122    { "load_data", (PyCFunction)load_data, METH_VARARGS, "Load the data into a numpy array" },
[18e7309]123    {NULL}
124};
125
126static PyMemberDef CLoader_members[] = {
127    {NULL}
128};
129
130static PyTypeObject CLoaderType = {
131    PyObject_HEAD_INIT(NULL)
132    0,                         /*ob_size*/
133    "CLoader",             /*tp_name*/
134    sizeof(CLoader),             /*tp_basicsize*/
135    0,                         /*tp_itemsize*/
136    (destructor)CLoader_dealloc, /*tp_dealloc*/
137    0,                         /*tp_print*/
138    0,                         /*tp_getattr*/
139    0,                         /*tp_setattr*/
140    0,                         /*tp_compare*/
141    0,                         /*tp_repr*/
142    0,                         /*tp_as_number*/
143    0,                         /*tp_as_sequence*/
144    0,                         /*tp_as_mapping*/
145    0,                         /*tp_hash */
146    0,                         /*tp_call*/
147    0,                         /*tp_str*/
148    0,                         /*tp_getattro*/
149    0,                         /*tp_setattro*/
150    0,                         /*tp_as_buffer*/
151    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
152    "CLoader objects",           /* tp_doc */
153    0,                         /* tp_traverse */
154    0,                         /* tp_clear */
155    0,                         /* tp_richcompare */
156    0,                         /* tp_weaklistoffset */
157    0,                         /* tp_iter */
158    0,                         /* tp_iternext */
159    CLoader_methods,             /* tp_methods */
160    CLoader_members,             /* tp_members */
161    0,                         /* tp_getset */
162    0,                         /* tp_base */
163    0,                         /* tp_dict */
164    0,                         /* tp_descr_get */
165    0,                         /* tp_descr_set */
166    0,                         /* tp_dictoffset */
167    (initproc)CLoader_init,      /* tp_init */
168    0,                         /* tp_alloc */
169    CLoader_new,                 /* tp_new */
170};
171
172PyMODINIT_FUNC
173initbsl_loader(void)
174{
175    PyObject *module;
176    module = Py_InitModule("bsl_loader", NULL);
177    import_array();
178
179    if (PyType_Ready(&CLoaderType) < 0)
180        return;
181
182    Py_INCREF(&CLoaderType);
183    PyModule_AddObject(module, "CLoader", (PyObject *)&CLoaderType);
184}
Note: See TracBrowser for help on using the repository browser.