25#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
32#define PYFMIIMAGE_MODULE
36#include <arrayobject.h>
37#include "pypolarscan.h"
38#include "pypolarvolume.h"
39#include "pyravefield.h"
40#include "pyrave_debug.h"
41#include "rave_alloc.h"
51#define raiseException_gotoTag(tag, type, msg) \
52{PyErr_SetString(type, msg); goto tag;}
57#define raiseException_returnNULL(type, msg) \
58{PyErr_SetString(type, msg); return NULL;}
63static PyObject *ErrorObject;
77 RAVE_ASSERT((pyfmiimage != NULL),
"pyfmiimage == NULL");
78 return RAVE_OBJECT_COPY(pyfmiimage->
image);
98 RAVE_CRITICAL0(
"Failed to allocate memory for fmi image.");
101 if (width > 0 && height > 0) {
103 RAVE_CRITICAL0(
"Failed to initialize fmi image");
108 cp = RAVE_OBJECT_COPY(p);
109 result = RAVE_OBJECT_GETBINDING(p);
110 if (result != NULL) {
115 if (result == NULL) {
117 if (result != NULL) {
118 PYRAVE_DEBUG_OBJECT_CREATED;
119 result->
image = RAVE_OBJECT_COPY(cp);
120 RAVE_OBJECT_BIND(result->
image, result);
122 RAVE_CRITICAL0(
"Failed to create PyFmiImage instance");
128 RAVE_OBJECT_RELEASE(cp);
136static void _pyfmiimage_dealloc(
PyFmiImage* obj)
142 PYRAVE_DEBUG_OBJECT_DESTROYED;
143 RAVE_OBJECT_UNBIND(obj->
image, obj);
144 RAVE_OBJECT_RELEASE(obj->
image);
154static PyObject* _pyfmiimage_new(PyObject* self, PyObject* args)
156 int width = 0, height = 0;
157 if (!PyArg_ParseTuple(args,
"|ii", &width, &height)) {
163static PyObject* _pyfmiimage_fromRave(PyObject* self, PyObject* args)
165 PyObject* inptr = NULL;
166 RaveCoreObject*
object = NULL;
167 char* quantity = NULL;
172 if (!PyArg_ParseTuple(args,
"O|s", &inptr, &quantity)) {
176 if (PyPolarVolume_Check(inptr)) {
177 object = (RaveCoreObject*)PyPolarVolume_GetNative((PyPolarVolume*)inptr);
179 }
else if (PyPolarScan_Check(inptr)) {
180 object = (RaveCoreObject*)PyPolarScan_GetNative((PyPolarScan*)inptr);
182 }
else if (PyRaveField_Check(inptr)) {
183 object = (RaveCoreObject*)PyRaveField_GetNative((PyRaveField*)inptr);
189 if (fmiimage != NULL) {
192 PyErr_SetString(PyExc_RuntimeError,
"Could not convert rave object into fmiimage");
195 RAVE_OBJECT_RELEASE(fmiimage);
196 RAVE_OBJECT_RELEASE(
object);
198 return (PyObject*)result;
201static PyObject* _pyfmiimage_fromRaveVolume(PyObject* self, PyObject* args)
203 PyObject* inptr = NULL;
204 RaveCoreObject*
object = NULL;
205 char* quantity = NULL;
211 if (!PyArg_ParseTuple(args,
"Oi|s", &inptr, &scannr, &quantity)) {
215 if (PyPolarVolume_Check(inptr)) {
216 object = (RaveCoreObject*)PyPolarVolume_GetNative((PyPolarVolume*)inptr);
222 if (fmiimage != NULL) {
225 PyErr_SetString(PyExc_RuntimeError,
"Could not convert rave object into fmiimage");
228 RAVE_OBJECT_RELEASE(fmiimage);
229 RAVE_OBJECT_RELEASE(
object);
231 return (PyObject*)result;
242static PyObject* _pyfmiimage_addAttribute(
PyFmiImage* self, PyObject* args)
244 RaveAttribute_t* attr = NULL;
246 PyObject* obj = NULL;
247 PyObject* result = NULL;
249 if (!PyArg_ParseTuple(args,
"sO", &name, &obj)) {
253 attr = RAVE_OBJECT_NEW(&RaveAttribute_TYPE);
258 if (!RaveAttribute_setName(attr, name)) {
262 if (PyLong_Check(obj) || PyInt_Check(obj)) {
263 long value = PyLong_AsLong(obj);
264 RaveAttribute_setLong(attr, value);
265 }
else if (PyFloat_Check(obj)) {
266 double value = PyFloat_AsDouble(obj);
267 RaveAttribute_setDouble(attr, value);
268 }
else if (PyString_Check(obj)) {
269 char* value = (
char*)PyString_AsString(obj);
270 if (!RaveAttribute_setString(attr, value)) {
273 }
else if (PyArray_Check(obj)) {
274 PyArrayObject* arraydata = (PyArrayObject*)obj;
275 if (PyArray_NDIM(arraydata) != 1) {
278 if (!RaveAttribute_setArrayFromData(attr, PyArray_DATA(arraydata), PyArray_DIM(arraydata, 0), translate_pyarraytype_to_ravetype(PyArray_TYPE(arraydata)))) {
289 result = PyBool_FromLong(1);
291 RAVE_OBJECT_RELEASE(attr);
301static PyObject* _pyfmiimage_getAttribute(
PyFmiImage* self, PyObject* args)
303 RaveAttribute_t* attribute = NULL;
305 PyObject* result = NULL;
306 if (!PyArg_ParseTuple(args,
"s", &name)) {
311 if (attribute != NULL) {
312 RaveAttribute_Format format = RaveAttribute_getFormat(attribute);
313 if (format == RaveAttribute_Format_Long) {
315 RaveAttribute_getLong(attribute, &value);
316 result = PyLong_FromLong(value);
317 }
else if (format == RaveAttribute_Format_Double) {
319 RaveAttribute_getDouble(attribute, &value);
320 result = PyFloat_FromDouble(value);
321 }
else if (format == RaveAttribute_Format_String) {
323 RaveAttribute_getString(attribute, &value);
324 result = PyString_FromString(value);
325 }
else if (format == RaveAttribute_Format_LongArray) {
330 RaveAttribute_getLongArray(attribute, &value, &len);
332 result = PyArray_SimpleNew(1, dims, NPY_LONG);
333 for (i = 0; i < len; i++) {
334 *((
long*) PyArray_GETPTR1((PyArrayObject*)result, i)) = value[i];
336 }
else if (format == RaveAttribute_Format_DoubleArray) {
337 double* value = NULL;
341 RaveAttribute_getDoubleArray(attribute, &value, &len);
343 result = PyArray_SimpleNew(1, dims, NPY_DOUBLE);
344 for (i = 0; i < len; i++) {
345 *((
double*) PyArray_GETPTR1((PyArrayObject*)result, i)) = value[i];
348 RAVE_CRITICAL1(
"Undefined format on requested attribute %s", name);
355 RAVE_OBJECT_RELEASE(attribute);
365static PyObject* _pyfmiimage_getAttributeNames(
PyFmiImage* self, PyObject* args)
367 RaveList_t* list = NULL;
368 PyObject* result = NULL;
376 n = RaveList_size(list);
377 result = PyList_New(0);
378 for (i = 0; result != NULL && i < n; i++) {
379 char* name = RaveList_get(list, i);
381 PyObject* pynamestr = PyString_FromString(name);
382 if (pynamestr == NULL) {
385 if (PyList_Append(result, pynamestr) != 0) {
386 Py_DECREF(pynamestr);
389 Py_DECREF(pynamestr);
392 RaveList_freeAndDestroy(&list);
395 RaveList_freeAndDestroy(&list);
406static PyObject* _pyfmiimage_toPolarScan(
PyFmiImage* self, PyObject* args)
408 char* quantity = NULL;
410 PolarScan_t* scan = NULL;
411 PyObject* result = NULL;
413 if (!PyArg_ParseTuple(args,
"|si", &quantity, &datatype)) {
418 result = (PyObject*)PyPolarScan_New(scan);
420 RAVE_OBJECT_RELEASE(scan);
430static PyObject* _pyfmiimage_toRaveField(
PyFmiImage* self, PyObject* args)
432 RaveField_t* field = NULL;
433 PyObject* result = NULL;
436 if (!PyArg_ParseTuple(args,
"|i", &datatype)) {
441 result = (PyObject*)PyRaveField_New(field);
443 RAVE_OBJECT_RELEASE(field);
453static PyObject* _pyfmiimage_setValue(
PyFmiImage* self, PyObject* args)
457 if (!PyArg_ParseTuple(args,
"lll", &x, &y, &v)) {
472static PyObject* _pyfmiimage_getValue(
PyFmiImage* self, PyObject* args)
476 if (!PyArg_ParseTuple(args,
"ll", &x, &y)) {
482 return PyLong_FromLong(v);
491static PyObject* _pyfmiimage_setOriginalValue(
PyFmiImage* self, PyObject* args)
496 if (!PyArg_ParseTuple(args,
"lld", &x, &y, &v)) {
511static PyObject* _pyfmiimage_getOriginalValue(
PyFmiImage* self, PyObject* args)
515 if (!PyArg_ParseTuple(args,
"ll", &x, &y)) {
521 return PyFloat_FromDouble(v);
529static struct PyMethodDef _pyfmiimage_methods[] =
531 {
"offset", NULL, METH_VARARGS},
532 {
"gain", NULL, METH_VARARGS},
533 {
"undetect", NULL, METH_VARARGS},
534 {
"nodata", NULL, METH_VARARGS},
535 {
"addAttribute", (PyCFunction)_pyfmiimage_addAttribute, 1},
536 {
"getAttribute", (PyCFunction)_pyfmiimage_getAttribute, 1},
537 {
"getAttributeNames", (PyCFunction)_pyfmiimage_getAttributeNames, 1},
538 {
"toPolarScan", (PyCFunction)_pyfmiimage_toPolarScan, 1},
539 {
"toRaveField", (PyCFunction)_pyfmiimage_toRaveField, 1},
540 {
"setValue", (PyCFunction) _pyfmiimage_setValue, 1},
541 {
"getValue", (PyCFunction) _pyfmiimage_getValue, 1},
542 {
"setOriginalValue", (PyCFunction) _pyfmiimage_setOriginalValue, 1},
543 {
"getOriginalValue", (PyCFunction) _pyfmiimage_getOriginalValue, 1},
550static PyObject* _pyfmiimage_getattro(
PyFmiImage* self, PyObject* name)
552 if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"offset", name) == 0) {
554 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"gain", name) == 0) {
556 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"nodata", name) == 0) {
558 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"undetect", name) == 0) {
561 return PyObject_GenericGetAttr((PyObject*)self, name);
567static int _pyfmiimage_setattro(
PyFmiImage* self, PyObject* name, PyObject* val)
574 if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"offset", name) == 0) {
576 if (PyFloat_Check(val)) {
577 v = PyFloat_AsDouble(val);
578 }
else if (PyLong_Check(val)) {
579 v = PyLong_AsDouble(val);
584 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"gain", name) == 0) {
586 if (PyFloat_Check(val)) {
587 v = PyFloat_AsDouble(val);
588 }
else if (PyLong_Check(val)) {
589 v = PyLong_AsDouble(val);
594 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"nodata", name) == 0) {
596 if (PyFloat_Check(val)) {
597 v = PyFloat_AsDouble(val);
598 }
else if (PyLong_Check(val)) {
599 v = PyLong_AsDouble(val);
604 }
else if (PY_COMPARE_STRING_WITH_ATTRO_NAME(
"undetect", name) == 0) {
606 if (PyFloat_Check(val)) {
607 v = PyFloat_AsDouble(val);
608 }
else if (PyLong_Check(val)) {
609 v = PyLong_AsDouble(val);
631 PyVarObject_HEAD_INIT(NULL, 0)
636 (destructor)_pyfmiimage_dealloc,
648 (getattrofunc)_pyfmiimage_getattro,
649 (setattrofunc)_pyfmiimage_setattro,
680static PyMethodDef functions[] = {
681 {
"new", (PyCFunction)_pyfmiimage_new, 1},
682 {
"fromRave", (PyCFunction)_pyfmiimage_fromRave, 1},
683 {
"fromRaveVolume", (PyCFunction)_pyfmiimage_fromRaveVolume, 1},
692 PyObject *module=NULL,*dictionary=NULL;
694 PyObject *c_api_object = NULL;
700 MOD_INIT_DEF(module,
"_fmiimage", NULL, functions);
701 if (module == NULL) {
702 return MOD_INIT_ERROR;
709 c_api_object = PyCapsule_New(PyFmiImage_API, PyFmiImage_CAPSULE_NAME, NULL);
710 dictionary = PyModule_GetDict(module);
711 PyDict_SetItemString(dictionary,
"_C_API", c_api_object);
713 ErrorObject = PyErr_NewException(
"_fmiimage.error", NULL, NULL);
714 if (ErrorObject == NULL || PyDict_SetItemString(dictionary,
"error", ErrorObject) != 0) {
715 Py_FatalError(
"Can't define _fmiimage.error");
716 return MOD_INIT_ERROR;
720 import_pypolarscan();
721 import_pypolarvolume();
722 import_pyravefield();
723 PYRAVE_DEBUG_INITIALIZE;
724 return MOD_INIT_SUCCESS(module);
#define raiseException_gotoTag(tag, type, msg)
PyTypeObject PyFmiImage_Type
PYRAVE_DEBUG_MODULE("_pyfmiimage")
#define raiseException_returnNULL(type, msg)
#define PyFmiImage_GetNative
#define PyFmiImage_GetNative_NUM
#define PyFmiImage_API_pointers
#define PyFmiImage_Type_NUM
#define PyFmiImage_New_NUM
FmiImage * RaveFmiImage_getImage(RaveFmiImage_t *self)
double RaveFmiImage_getOffset(RaveFmiImage_t *self)
int RaveFmiImage_addAttribute(RaveFmiImage_t *self, RaveAttribute_t *attribute)
RaveAttribute_t * RaveFmiImage_getAttribute(RaveFmiImage_t *self, const char *name)
void RaveFmiImage_setGain(RaveFmiImage_t *self, double gain)
RaveField_t * RaveFmiImage_toRaveField(RaveFmiImage_t *self, int datatype)
RaveCoreObjectType RaveFmiImage_TYPE
PolarScan_t * RaveFmiImage_toPolarScan(RaveFmiImage_t *self, const char *quantity, int datatype)
void RaveFmiImage_setNodata(RaveFmiImage_t *self, double v)
double RaveFmiImage_getUndetect(RaveFmiImage_t *self)
double RaveFmiImage_getGain(RaveFmiImage_t *self)
RaveList_t * RaveFmiImage_getAttributeNames(RaveFmiImage_t *self)
RaveFmiImage_t * RaveFmiImage_fromPolarVolume(PolarVolume_t *volume, int scannr, const char *quantity)
int RaveFmiImage_initialize(RaveFmiImage_t *self, int width, int height)
double RaveFmiImage_getNodata(RaveFmiImage_t *self)
void RaveFmiImage_setUndetect(RaveFmiImage_t *self, double v)
RaveFmiImage_t * RaveFmiImage_fromRaveField(RaveField_t *field)
RaveFmiImage_t * RaveFmiImage_fromPolarScan(PolarScan_t *scan, const char *quantity)
void RaveFmiImage_setOffset(RaveFmiImage_t *self, double offset)
PyObject_HEAD RaveFmiImage_t * image