1 #define NO_IMPORT_ARRAY
2
3 #include "astropy_wcs/wcslib_wtbarr_wrap.h"
4
5 #include <wcs.h>
6 #include <wcsprintf.h>
7 #include <tab.h>
8 #include <wtbarr.h>
9
10 /*
11 It gets to be really tedious to type long docstrings in ANSI C syntax
12 (since multi-line strings literals are not valid). Therefore, the
13 docstrings are written in doc/docstrings.py, which are then converted
14 by setup.py into docstrings.h, which we include here.
15 */
16 #include "astropy_wcs/docstrings.h"
17
18
19 /***************************************************************************
20 * PyWtbarr methods *
21 ***************************************************************************/
22
23 static PyObject*
PyWtbarr_new(PyTypeObject * type,PyObject * args,PyObject * kwds)24 PyWtbarr_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
25 PyWtbarr* self;
26 self = (PyWtbarr*)type->tp_alloc(type, 0);
27 return (PyObject*)self;
28 }
29
30
31 static int
PyWtbarr_traverse(PyWtbarr * self,visitproc visit,void * arg)32 PyWtbarr_traverse(PyWtbarr* self, visitproc visit, void *arg) {
33 Py_VISIT(self->owner);
34 return 0;
35 }
36
37
38 static int
PyWtbarr_clear(PyWtbarr * self)39 PyWtbarr_clear(PyWtbarr* self) {
40 Py_CLEAR(self->owner);
41 return 0;
42 }
43
44
PyWtbarr_dealloc(PyWtbarr * self)45 static void PyWtbarr_dealloc(PyWtbarr* self) {
46 PyWtbarr_clear(self);
47 Py_TYPE(self)->tp_free((PyObject*)self);
48 }
49
50
PyWtbarr_cnew(PyObject * wcsprm,struct wtbarr * x)51 PyWtbarr* PyWtbarr_cnew(PyObject* wcsprm, struct wtbarr* x) {
52 PyWtbarr* self;
53 self = (PyWtbarr*)(&PyWtbarrType)->tp_alloc(&PyWtbarrType, 0);
54 if (self == NULL) return NULL;
55 self->x = x;
56 Py_INCREF(wcsprm);
57 self->owner = wcsprm;
58 return self;
59 }
60
61
wtbarrprt(const struct wtbarr * wtb)62 static void wtbarrprt(const struct wtbarr *wtb) {
63 int i, nd, ndim;
64
65 if (wtb == 0x0) return;
66
67 wcsprintf(" i: %d\n", wtb->i);
68 wcsprintf(" m: %d\n", wtb->m);
69 wcsprintf(" kind: %c\n", wtb->kind);
70 wcsprintf("extnam: %s\n", wtb->extnam);
71 wcsprintf("extver: %d\n", wtb->extver);
72 wcsprintf("extlev: %d\n", wtb->extlev);
73 wcsprintf(" ttype: %s\n", wtb->ttype);
74 wcsprintf(" row: %ld\n", wtb->row);
75 wcsprintf(" ndim: %d\n", wtb->ndim);
76 wcsprintf("dimlen: %p\n", (void *)wtb->dimlen);
77
78 ndim = wtb->ndim - (int)(wtb->kind == 'c');
79 nd = 1 + (int) log10(ndim ? ndim : 1);
80 for (i = 0; i < ndim; i++) {
81 wcsprintf(" %*d: %d\n", nd, i, wtb->dimlen[i]);
82 }
83 wcsprintf("arrayp: %p\n", (void *)wtb->arrayp);
84
85 return;
86 }
87
88
PyWtbarr_print_contents(PyWtbarr * self)89 static PyObject* PyWtbarr_print_contents(PyWtbarr* self) {
90 /* This is not thread-safe, but since we're holding onto the GIL,
91 we can assume we won't have thread conflicts */
92 wcsprintf_set(NULL);
93 wtbarrprt(self->x);
94 printf("%s", wcsprintf_buf());
95 fflush(stdout);
96 Py_RETURN_NONE;
97 }
98
99
PyWtbarr___str__(PyWtbarr * self)100 static PyObject* PyWtbarr___str__(PyWtbarr* self) {
101 /* This is not thread-safe, but since we're holding onto the GIL,
102 we can assume we won't have thread conflicts */
103 wcsprintf_set(NULL);
104 wtbarrprt(self->x);
105 return PyUnicode_FromString(wcsprintf_buf());
106 }
107
108
109 /***************************************************************************
110 * Member getters/setters (properties)
111 */
112
113
PyWtbarr_get_i(PyWtbarr * self,void * closure)114 static PyObject* PyWtbarr_get_i(PyWtbarr* self, void* closure) {
115 return get_int("i", self->x->i);
116 }
117
118
PyWtbarr_get_m(PyWtbarr * self,void * closure)119 static PyObject* PyWtbarr_get_m(PyWtbarr* self, void* closure) {
120 return get_int("m", self->x->m);
121 }
122
123
PyWtbarr_get_extver(PyWtbarr * self,void * closure)124 static PyObject* PyWtbarr_get_extver(PyWtbarr* self, void* closure) {
125 return get_int("extver", self->x->extver);
126 }
127
128
PyWtbarr_get_extlev(PyWtbarr * self,void * closure)129 static PyObject* PyWtbarr_get_extlev(PyWtbarr* self, void* closure) {
130 return get_int("extlev", self->x->extlev);
131 }
132
133
PyWtbarr_get_ndim(PyWtbarr * self,void * closure)134 static PyObject* PyWtbarr_get_ndim(PyWtbarr* self, void* closure) {
135 return get_int("ndim", self->x->ndim);
136 }
137
138
PyWtbarr_get_row(PyWtbarr * self,void * closure)139 static PyObject* PyWtbarr_get_row(PyWtbarr* self, void* closure) {
140 return get_int("row", self->x->row);
141 }
142
143
PyWtbarr_get_extnam(PyWtbarr * self,void * closure)144 static PyObject* PyWtbarr_get_extnam(PyWtbarr* self, void* closure) {
145 if (is_null(self->x->extnam)) return NULL;
146 return get_string("extnam", self->x->extnam);
147 }
148
149
PyWtbarr_get_ttype(PyWtbarr * self,void * closure)150 static PyObject* PyWtbarr_get_ttype(PyWtbarr* self, void* closure) {
151 if (is_null(self->x->ttype)) return NULL;
152 return get_string("ttype", self->x->ttype);
153 }
154
155
PyWtbarr_get_kind(PyWtbarr * self,void * closure)156 static PyObject* PyWtbarr_get_kind(PyWtbarr* self, void* closure) {
157 return PyUnicode_FromFormat("%c", self->x->kind);
158 }
159
160
161 /***************************************************************************
162 * PyWtbarr definition structures
163 */
164
165 static PyGetSetDef PyWtbarr_getset[] = {
166 {"i", (getter)PyWtbarr_get_i, NULL, (char *) doc_i},
167 {"m", (getter)PyWtbarr_get_m, NULL, (char *) doc_m},
168 {"kind", (getter)PyWtbarr_get_kind, NULL, (char *) doc_kind},
169 {"extnam", (getter)PyWtbarr_get_extnam, NULL, (char *) doc_extnam},
170 {"extver", (getter)PyWtbarr_get_extver, NULL, (char *) doc_extver},
171 {"extlev", (getter)PyWtbarr_get_extlev, NULL, (char *) doc_extlev},
172 {"ttype", (getter)PyWtbarr_get_ttype, NULL, (char *) doc_ttype},
173 {"row", (getter)PyWtbarr_get_row, NULL, (char *) doc_row},
174 {"ndim", (getter)PyWtbarr_get_ndim, NULL, (char *) doc_ndim},
175 /* {"dimlen", (getter)PyWtbarr_get_dimlen, NULL, (char *) NULL}, */
176 /* {"arrayp", (getter)PyWtbarr_get_arrayp, NULL, (char *) NULL}, */
177 {NULL}
178 };
179
180
181 static PyMethodDef PyWtbarr_methods[] = {
182 {"print_contents", (PyCFunction)PyWtbarr_print_contents, METH_NOARGS, doc_print_contents_wtbarr},
183 {NULL}
184 };
185
186 PyTypeObject PyWtbarrType = {
187 PyVarObject_HEAD_INIT(NULL, 0)
188 "astropy.wcs.Wtbarr", /*tp_name*/
189 sizeof(PyWtbarr), /*tp_basicsize*/
190 0, /*tp_itemsize*/
191 (destructor)PyWtbarr_dealloc, /*tp_dealloc*/
192 0, /*tp_print*/
193 0, /*tp_getattr*/
194 0, /*tp_setattr*/
195 0, /*tp_compare*/
196 0, /*tp_repr*/
197 0, /*tp_as_number*/
198 0, /*tp_as_sequence*/
199 0, /*tp_as_mapping*/
200 0, /*tp_hash */
201 0, /*tp_call*/
202 (reprfunc)PyWtbarr___str__, /*tp_str*/
203 0, /*tp_getattro*/
204 0, /*tp_setattro*/
205 0, /*tp_as_buffer*/
206 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
207 doc_Wtbarr, /* tp_doc */
208 PyWtbarr_traverse, /* tp_traverse */
209 PyWtbarr_clear, /* tp_clear */
210 0, /* tp_richcompare */
211 0, /* tp_weaklistoffset */
212 0, /* tp_iter */
213 0, /* tp_iternext */
214 PyWtbarr_methods, /* tp_methods */
215 0, /* tp_members */
216 PyWtbarr_getset, /* tp_getset */
217 0, /* tp_base */
218 0, /* tp_dict */
219 0, /* tp_descr_get */
220 0, /* tp_descr_set */
221 0, /* tp_dictoffset */
222 0, /* tp_init */
223 0, /* tp_alloc */
224 0, /* tp_new */
225 };
226
227
228 int
_setup_wtbarr_type(PyObject * m)229 _setup_wtbarr_type(PyObject* m) {
230 if (PyType_Ready(&PyWtbarrType) < 0) {
231 return -1;
232 }
233
234 Py_INCREF(&PyWtbarrType);
235
236 PyModule_AddObject(m, "Wtbarr", (PyObject *)&PyWtbarrType);
237
238 return 0;
239 }
240