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