File: gribapi.c

package info (click to toggle)
eccodes 2.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 480,184 kB
  • sloc: ansic: 163,815; makefile: 21,266; sh: 8,507; python: 6,026; f90: 5,762; perl: 2,891; yacc: 818; lex: 356; cpp: 305; fortran: 116; awk: 66
file content (196 lines) | stat: -rw-r--r-- 4,366 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright 2005-2018 ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
 */

#include "Python.h"
#include "grib_api.h"

static PyObject* Error;

PyDoc_STRVAR(gribapi__doc__,
		"gribapi point evalutation kernel");

PyDoc_STRVAR(read__doc__,
		"gribapi point evalutation kernel");

static PyObject *
py_new_handle_from_file(PyObject *self, PyObject *args)
{
	/* TODO: Call PyFile_Check or PyFile_CheckExact */
	int err = 0;
	grib_handle *h;
	PyObject *file;

	if(!PyArg_ParseTuple(args,"O",&file))
		return NULL;

	h = grib_handle_new_from_file(NULL,PyFile_AsFile(file),&err);

	if(err)  {
		/* http://www.ragestorm.net/tutorial?id=21 */
		PyErr_Format(Error,"grib_api: %s",grib_get_error_message(err));
		return NULL;
	}

	
	/* TODO: Do an OO version */

	if(h)
	return PyLong_FromVoidPtr(h);

	Py_RETURN_NONE;
}

static PyObject *
py_handle_delete(PyObject *self, PyObject *args)
{
	PyObject* h;
	if(!PyArg_ParseTuple(args,"O",&h))	
		return NULL;
	grib_handle_delete(PyLong_AsVoidPtr(h));
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
py_keys_iterator_delete(PyObject *self, PyObject *args)
{
	PyObject* h;
	if(!PyArg_ParseTuple(args,"O",&h))	
		return NULL;
	grib_keys_iterator_delete(PyLong_AsVoidPtr(h));
	Py_INCREF(Py_None);
	return Py_None;
}


static PyObject *
py_get_string(PyObject *self, PyObject *args)
{
	PyObject* h;
	char *s;
	size_t size;
	char tmp[1024];
	int err;
	
	if(!PyArg_ParseTuple(args,"Os",&h,&s))	
		return NULL;

	size = sizeof(tmp);
	err  = grib_get_string(PyLong_AsVoidPtr(h),s,tmp,&size);

	if(err)  {
		/* http://www.ragestorm.net/tutorial?id=21 */
		PyErr_Format(Error,"grib_api: %s %s",s,grib_get_error_message(err));
		return NULL;
	}

	return PyString_InternFromString(tmp);

}

static PyObject *
py_keys_iterator_next(PyObject *self, PyObject *args)
{
    PyObject* h;
	int more;

    if(!PyArg_ParseTuple(args,"O",&h))
        return NULL;

	more = grib_keys_iterator_next(PyLong_AsVoidPtr(h));
	if(more)
	{
		Py_RETURN_TRUE;
	}
	else
	{
		Py_RETURN_FALSE;
	}
}


static PyObject *
py_keys_iterator_get_string(PyObject *self, PyObject *args)
{
	PyObject* h;
	size_t size;
	char tmp[1024];
	int err;
	
	if(!PyArg_ParseTuple(args,"O",&h))	
		return NULL;

	size = sizeof(tmp);
	err  = grib_keys_iterator_get_string(PyLong_AsVoidPtr(h),tmp,&size);

	if(err)  {
		/* http://www.ragestorm.net/tutorial?id=21 */
		PyErr_Format(Error,"grib_api: %s",grib_get_error_message(err));
		return NULL;
	}

	return PyString_InternFromString(tmp);

}

static PyObject *
py_keys_iterator_get_name(PyObject *self, PyObject *args)
{
	PyObject* h;
	
	if(!PyArg_ParseTuple(args,"O",&h))	
		return NULL;


	return PyString_InternFromString(grib_keys_iterator_get_name(PyLong_AsVoidPtr(h)));

}

static PyObject *
py_keys_iterator_new(PyObject *self, PyObject *args)
{
	PyObject* h;
	char *s;

	if(!PyArg_ParseTuple(args,"Os",&h,&s))	
		return NULL;

	return PyLong_FromVoidPtr(grib_keys_iterator_new(PyLong_AsVoidPtr(h),GRIB_KEYS_ITERATOR_ALL_KEYS,s));
}

static PyMethodDef gribapi_methods[] = {

	{"new_handle_from_file",  py_new_handle_from_file, METH_VARARGS, read__doc__},
	{"get_string",            py_get_string, METH_VARARGS, read__doc__},
	{"keys_iterator_new",     py_keys_iterator_new, METH_VARARGS, read__doc__},
	{"keys_iterator_delete",  py_keys_iterator_delete, METH_VARARGS, read__doc__},
	{"handle_delete",         py_handle_delete, METH_VARARGS, read__doc__},
	{"keys_iterator_get_string",         py_keys_iterator_get_string, METH_VARARGS, read__doc__},
	{"keys_iterator_get_name",         py_keys_iterator_get_name, METH_VARARGS, read__doc__},
	{"keys_iterator_next",         py_keys_iterator_next, METH_VARARGS, read__doc__},


	{NULL, NULL}      /* sentinel */
};

PyMODINIT_FUNC
initgribapi(void)
{
	PyObject* m;

	/* There have been several InitModule functions over time */
	m = Py_InitModule3("gribapi", gribapi_methods, gribapi__doc__); 

	/* TODO: Create our own exception */
	Error = PyErr_NewException("grib.apierror",NULL,NULL);
	Py_INCREF(Error);
	PyModule_AddObject(m, "error", Error);
}