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