ICON Community Interface 0.4.0
Loading...
Searching...
No Matches
descrdata_properties.h
Go to the documentation of this file.
1/* @authors 11/2023 :: ICON Community Interface <comin@icon-model.org>
2
3 SPDX-License-Identifier: BSD-3-Clause
4
5 Please see the file LICENSE in the root of the source tree for this code.
6 Where software is supplied by third parties, it is indicated in the
7 headers of the routines. */
8
9#ifndef PY_COMIN_DESCRDATA_PROPERTIES
10#define PY_COMIN_DESCRDATA_PROPERTIES
11
12#include "comin.h"
13
14static PyObject* BuildValue_FromPtr(std::string ctype, void* data) {
15 if (ctype == "double")
16 return Py_BuildValue("f", *(double*)data);
17 if (ctype == "int")
18 return Py_BuildValue("i", *(int*)data);
19 return PyErr_Format(PyExc_RuntimeError, "Unknown data type: %s",
20 ctype.data());
21}
22
23// helper function to generate dict from properties array
25 const comin_descrdata_property_t* properties) {
26 PyObject* dict = PyDict_New();
27 const comin_descrdata_property_t* prop = properties;
28 while (prop->name != NULL) {
29 PyDict_SetItemString(
30 dict, prop->name,
31 PyCapsule_New((void*)prop, "descrdata_property", NULL));
32 prop++;
33 }
34 return dict;
35}
36
37static PyObject* py_comin_descrdata_eval_property(PyObject* self,
38 PyObject* args,
39 PyObject* kwargs) {
40 PyObject* prop_cap;
41 int jg = 0;
42 static char const* kwlist[] = {"property", "jg", NULL};
43 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", (char**)&kwlist,
44 &prop_cap, &jg))
45 return NULL;
46 comin_descrdata_property_t* prop =
47 (comin_descrdata_property_t*)PyCapsule_GetPointer(prop_cap,
48 "descrdata_property");
49 if (prop == NULL)
50 return NULL;
51 void* dataptr;
52 if (std::string(prop->datatype) ==
53 "void") { // return the dict of subproperties
54 return py_comin_descrdata_properties_to_dict(prop->subtypes);
55 } else if (prop->ndims == 0) { // return scalar
56 if (std::string(prop->datatype) == "int") {
57 int val;
58 if (prop->has_jg)
59 val = ((int (*)(int))prop->get_function)(jg);
60 else
61 val = ((int (*)())prop->get_function)();
62 return PyLong_FromLong(val);
63 }
64 if (std::string(prop->datatype) == "double") {
65 double val;
66 if (prop->has_jg)
67 val = ((double (*)(int))prop->get_function)(jg);
68 else
69 val = ((double (*)())prop->get_function)();
70 return PyFloat_FromDouble(val);
71 }
72 if (std::string(prop->datatype) == "bool") {
73 bool val;
74 if (prop->has_jg)
75 val = ((bool (*)(int))prop->get_function)(jg);
76 else
77 val = ((bool (*)())prop->get_function)();
78 if (val)
79 Py_RETURN_TRUE;
80 else
81 Py_RETURN_FALSE;
82 }
83 return PyErr_Format(PyExc_RuntimeError,
84 "datatype %s for scalar value %s not implemented",
85 prop->datatype, prop->name);
86 } else {
87 std::vector<int> arr_size(prop->ndims);
88 if (prop->has_jg)
89 ((void (*)(int, void**, int*))prop->get_function)(jg, &dataptr,
90 arr_size.data());
91 else
92 ((void (*)(void**, int*))prop->get_function)(&dataptr, arr_size.data());
93 if (dataptr == NULL)
94 return PyErr_Format(PyExc_RuntimeError, "%s get function returned NULL",
95 prop->name);
96 if (prop->ndims == 0) { // return the value directly
97 return BuildValue_FromPtr(prop->datatype, dataptr);
98 }
99 if (std::string(prop->datatype) == "char" && prop->ndims == 1)
100 return Py_BuildValue("s#", dataptr, (Py_ssize_t)arr_size[0]);
101 // return MemoryView
102 Py_buffer buffer;
103 if (std::string(prop->datatype) == "double") {
104 fill_buffer<double>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
105 } else if (std::string(prop->datatype) == "int") {
106 fill_buffer<int>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
107 } else if (std::string(prop->datatype) == "int8_t") {
108 fill_buffer<int8_t>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
109 } else
110 return PyErr_Format(PyExc_RuntimeError,
111 "datatype %s for property %s not implemented",
112 prop->datatype, prop->name);
113 buffer.obj = self;
114 return PyMemoryView_FromBuffer(&buffer);
115 }
116}
117
118#endif
C interface for the ICON Community Interface.
static PyObject * py_comin_descrdata_properties_to_dict(const comin_descrdata_property_t *properties)
static PyObject * py_comin_descrdata_eval_property(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * BuildValue_FromPtr(std::string ctype, void *data)
void fill_buffer(Py_buffer *buffer, void *mem, int *shape, int ndims, int readonly=1)
Definition util.h:26
const int jg
double * buffer