Debian Codegraph @

astropy_healpix/_core.c

raw
1 #include <Python.h>
2 #include <math.h>
3 #include <numpy/arrayobject.h>
4 #include <numpy/ufuncobject.h>
5 #include "healpix.h"
6 #include "healpix-utils.h"
7 #include "interpolation.h"
8
9 /* FIXME: We need npy_set_floatstatus_invalid(), but unlike most of the Numpy
10 * C API it is only available on some platforms if you explicitly link against
11 * Numpy, which is not typically done for building C extensions. This bundled
12 * header file contains a static definition of _npy_set_floatstatus_invalid().
13 */
14 #include "ieee754.h"
15
16 /* FIXME:
17 * The Numpy C-API defines PyArrayDescr_Type as:
18 *
19 * #define PyArrayDescr_Type (*(PyTypeObject *)PyArray_API[3])
20 *
21 * and then in some places we need to take its address, &PyArrayDescr_Type.
22 * This is fine in GCC 10 and Clang, but earlier versions of GCC complain:
23 *
24 * error: dereferencing pointer to incomplete type 'PyTypeObject'
25 * {aka 'struct _typeobject'}
26 *
27 * As a workaround, provide a faux forward declaration for PyTypeObject.
28 * See https://github.com/numpy/numpy/issues/16970.
29 *
30 * Drop this when supporting gcc < 10 becomes irrelevant.
31 */
32 #ifndef PYPY_VERSION
33 struct _typeobject {
34 int _placeholder;
35 };
36 #endif
37
38
39 #define INVALID_INDEX (-1)
40
41 /* Data structure for storing function pointers for routines that are specific
42 * to the HEALPix ordering scheme. When we create the ufuncs using
43 * PyUFunc_FromFuncAndData, we will set them up to pass a pointer to this
44 * data structure through as the void *data parameter for the loop functions
45 * defined below. */
46 typedef struct {
47 int64_t (*order_to_xy)(int64_t, int);
48 int64_t (*xy_to_order)(int64_t, int);
49 } order_funcs;
50
51 static order_funcs
52 nested_order_funcs = {healpixl_nested_to_xy, healpixl_xy_to_nested},
53 ring_order_funcs = {healpixl_ring_to_xy, healpixl_xy_to_ring};
54
55 static void *no_ufunc_data[] = {NULL},
56 *nested_ufunc_data[] = {&nested_order_funcs},
57 *ring_ufunc_data[] = {&ring_order_funcs};
58
59
60 static int64_t nside2npix(int nside)
61 {
62 return 12 * ((int64_t) nside) * ((int64_t) nside);
63 }
64
65
66 static int pixel_nside_valid(int64_t pixel, int nside)
67 {
68 return pixel >= 0 && pixel < nside2npix(nside);
69 }
70
71
72 static void healpix_to_lonlat_loop(
73 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
74 {
75 order_funcs *funcs = data;
76 npy_intp i, n = dimensions[0];
77
78 for (i = 0; i < n; i ++)
79 {
80 int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
81 int nside = *(int *) &args[1][i * steps[1]];
82 double dx = *(double *) &args[2][i * steps[2]];
83 double dy = *(double *) &args[3][i * steps[3]];
84 double *lon = (double *) &args[4][i * steps[4]];
85 double *lat = (double *) &args[5][i * steps[5]];
86 int64_t xy = INVALID_INDEX;
87
88 if (pixel_nside_valid(pixel, nside))
89 xy = funcs->order_to_xy(pixel, nside);
90
91 if (xy >= 0)
92 healpixl_to_radec(xy, nside, dx, dy, lon, lat);
93 else
94 {
95 *lon = *lat = NPY_NAN;
96 _npy_set_floatstatus_invalid();
97 }
98 }
99 }
100
101
102 static void lonlat_to_healpix_loop(
103 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
104 {
105 order_funcs *funcs = data;
106 npy_intp i, n = dimensions[0];
107
108 for (i = 0; i < n; i ++)
109 {
110 double lon = *(double *) &args[0][i * steps[0]];
111 double lat = *(double *) &args[1][i * steps[1]];
112 int nside = *(int *) &args[2][i * steps[2]];
113 int64_t *pixel = (int64_t *) &args[3][i * steps[3]];
114 double *dx = (double *) &args[4][i * steps[4]];
115 double *dy = (double *) &args[5][i * steps[5]];
116 int64_t xy = INVALID_INDEX;
117
118 if (isfinite(lon) && isfinite(lat))
119 xy = radec_to_healpixlf(lon, lat, nside, dx, dy);
120 if (xy >= 0)
121 *pixel = funcs->xy_to_order(xy, nside);
122 else {
123 *pixel = INVALID_INDEX;
124 *dx = *dy = NPY_NAN;
125 _npy_set_floatstatus_invalid();
126 }
127 }
128 }
129
130
131 static void healpix_to_xyz_loop(
132 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
133 {
134 order_funcs *funcs = data;
135 npy_intp i, n = dimensions[0];
136
137 for (i = 0; i < n; i ++)
138 {
139 int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
140 int nside = *(int *) &args[1][i * steps[1]];
141 double dx = *(double *) &args[2][i * steps[2]];
142 double dy = *(double *) &args[3][i * steps[3]];
143 double *x = (double *) &args[4][i * steps[4]];
144 double *y = (double *) &args[5][i * steps[5]];
145 double *z = (double *) &args[6][i * steps[6]];
146 int64_t xy = INVALID_INDEX;
147
148 if (pixel_nside_valid(pixel, nside))
149 xy = funcs->order_to_xy(pixel, nside);
150
151 if (xy >= 0)
152 healpixl_to_xyz(xy, nside, dx, dy, x, y, z);
153 else
154 {
155 *x = *y = *z = NPY_NAN;
156 _npy_set_floatstatus_invalid();
157 }
158 }
159 }
160
161
162 static void xyz_to_healpix_loop(
163 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
164 {
165 order_funcs *funcs = data;
166 npy_intp i, n = dimensions[0];
167
168 for (i = 0; i < n; i ++)
169 {
170 double x = *(double *) &args[0][i * steps[0]];
171 double y = *(double *) &args[1][i * steps[1]];
172 double z = *(double *) &args[2][i * steps[2]];
173 int nside = *(int *) &args[3][i * steps[3]];
174 int64_t *pixel = (int64_t *) &args[4][i * steps[4]];
175 double *dx = (double *) &args[5][i * steps[5]];
176 double *dy = (double *) &args[6][i * steps[6]];
177 int64_t xy = INVALID_INDEX;
178
179 if (isfinite(x) && isfinite(y) && isfinite(z)) {
180 /* xyztohealpixlf expects a unit vector */
181 double norm = sqrt(x*x + y*y + z*z);
182 x /= norm;
183 y /= norm;
184 z /= norm;
185
186 xy = xyztohealpixlf(x, y, z, nside, dx, dy);
187 }
188 if (xy >= 0)
189 *pixel = funcs->xy_to_order(xy, nside);
190 else {
191 *pixel = INVALID_INDEX;
192 *dx = *dy = NPY_NAN;
193 _npy_set_floatstatus_invalid();
194 }
195 }
196 }
197
198
199 static void nested_to_ring_loop(
200 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
201 {
202 npy_intp i, n = dimensions[0];
203
204 for (i = 0; i < n; i ++)
205 {
206 int64_t nested = *(int64_t *) &args[0][i * steps[0]];
207 int nside = *(int *) &args[1][i * steps[1]];
208 int64_t *ring = (int64_t *) &args[2][i * steps[2]];
209 int64_t xy = INVALID_INDEX;
210
211 if (pixel_nside_valid(nested, nside))
212 xy = healpixl_nested_to_xy(nested, nside);
213 if (xy >= 0)
214 *ring = healpixl_xy_to_ring(xy, nside);
215 else {
216 *ring = INVALID_INDEX;
217 _npy_set_floatstatus_invalid();
218 }
219 }
220 }
221
222
223 static void ring_to_nested_loop(
224 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
225 {
226 npy_intp i, n = dimensions[0];
227
228 for (i = 0; i < n; i ++)
229 {
230 int64_t ring = *(int64_t *) &args[0][i * steps[0]];
231 int nside = *(int *) &args[1][i * steps[1]];
232 int64_t *nested = (int64_t *) &args[2][i * steps[2]];
233 int64_t xy = INVALID_INDEX;
234
235 if (pixel_nside_valid(ring, nside))
236 xy = healpixl_ring_to_xy(ring, nside);
237 if (xy >= 0)
238 *nested = healpixl_xy_to_nested(xy, nside);
239 else {
240 *nested = INVALID_INDEX;
241 _npy_set_floatstatus_invalid();
242 }
243 }
244 }
245
246
247 static void bilinear_interpolation_weights_loop(
248 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
249 {
250 npy_intp i, n = dimensions[0];
251
252 for (i = 0; i < n; i ++)
253 {
254 double lon = *(double *) &args[0][i * steps[0]];
255 double lat = *(double *) &args[1][i * steps[1]];
256 int nside = *(int *) &args[2][i * steps[2]];
257 int64_t indices[4];
258 double weights[4];
259 int j;
260
261 if (isfinite(lon) && isfinite(lat)) {
262 interpolate_weights(lon, lat, indices, weights, nside);
263 for (j = 0; j < 4; j ++)
264 {
265 *(int64_t *) &args[3 + j][i * steps[3 + j]] = indices[j];
266 *(double *) &args[7 + j][i * steps[7 + j]] = weights[j];
267 }
268 } else {
269 for (j = 0; j < 4; j ++)
270 {
271 *(int64_t *) &args[3 + j][i * steps[3 + j]] = INVALID_INDEX;
272 *(double *) &args[7 + j][i * steps[7 + j]] = NPY_NAN;
273 }
274 _npy_set_floatstatus_invalid();
275 }
276 }
277 }
278
279
280 static void neighbours_loop(
281 char **args, const npy_intp *dimensions, const npy_intp *steps, void *data)
282 {
283 order_funcs *funcs = data;
284 npy_intp i, n = dimensions[0];
285
286 for (i = 0; i < n; i ++)
287 {
288 int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
289 int nside = *(int *) &args[1][i * steps[1]];
290 int64_t neighbours[] = {
291 INVALID_INDEX, INVALID_INDEX, INVALID_INDEX, INVALID_INDEX,
292 INVALID_INDEX, INVALID_INDEX, INVALID_INDEX, INVALID_INDEX};
293 int j;
294 int64_t xy = INVALID_INDEX;
295
296 if (pixel_nside_valid(pixel, nside))
297 xy = funcs->order_to_xy(pixel, nside);
298 if (xy >= 0)
299 healpixl_get_neighbours(xy, neighbours, nside);
300
301 for (j = 0; j < 8; j ++)
302 {
303 int k = 4 - j;
304 if (k < 0)
305 k += 8;
306 xy = neighbours[k];
307 if (xy >= 0)
308 pixel = funcs->xy_to_order(xy, nside);
309 else {
310 pixel = INVALID_INDEX;
311 _npy_set_floatstatus_invalid();
312 }
313 *(int64_t *) &args[2 + j][i * steps[2 + j]] = pixel;
314 }
315 }
316 }
317
318
319 static PyObject *healpix_cone_search(
320 PyObject *self, PyObject *args, PyObject *kwargs)
321 {
322 PyObject *result;
323 static char *kws[] = {"lon", "lat", "radius", "nside", "order", NULL};
324 double lon, lat, radius;
325 int nside;
326 char *order;
327 int64_t *indices, n_indices;
328 int64_t *result_data;
329 npy_intp dims[1];
330
331 if (!PyArg_ParseTupleAndKeywords(
332 args, kwargs, "dddis", kws, &lon, &lat, &radius, &nside, &order))
333 return NULL;
334
335 n_indices = healpix_rangesearch_radec_simple(
336 lon, lat, radius, nside, 0, &indices);
337 if (!indices)
338 {
339 PyErr_SetString(
340 PyExc_RuntimeError, "healpix_rangesearch_radec_simple failed");
341 return NULL;
342 }
343
344 dims[0] = n_indices;
345 result = PyArray_SimpleNew(1, dims, NPY_INT64);
346 if (result)
347 {
348 result_data = PyArray_DATA((PyArrayObject *) result);
349
350 if (strcmp(order, "nested") == 0)
351 {
352 int i;
353 for (i = 0; i < n_indices; i ++)
354 result_data[i] = healpixl_xy_to_nested(indices[i], nside);
355 } else {
356 int i;
357 for (i = 0; i < n_indices; i ++)
358 result_data[i] = healpixl_xy_to_ring(indices[i], nside);
359 }
360 }
361
362 free(indices);
363 return result;
364 }
365
366
367 static PyMethodDef methods[] = {
368 {"healpix_cone_search", (PyCFunction) healpix_cone_search, METH_VARARGS | METH_KEYWORDS, NULL},
369 {NULL, NULL, 0, NULL}
370 };
371
372
373 static PyModuleDef moduledef = {
374 PyModuleDef_HEAD_INIT,
375 "_core", NULL, -1, methods
376 };
377
378 static PyUFuncGenericFunction
379 healpix_to_lonlat_loops [] = {healpix_to_lonlat_loop},
380 lonlat_to_healpix_loops [] = {lonlat_to_healpix_loop},
381 healpix_to_xyz_loops [] = {healpix_to_xyz_loop},
382 xyz_to_healpix_loops [] = {xyz_to_healpix_loop},
383 nested_to_ring_loops [] = {nested_to_ring_loop},
384 ring_to_nested_loops [] = {ring_to_nested_loop},
385 bilinear_interpolation_weights_loops[] = {bilinear_interpolation_weights_loop},
386 neighbours_loops [] = {neighbours_loop};
387
388 static char
389 healpix_to_lonlat_types[] = {
390 NPY_INT64, NPY_INT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
391 lonlat_to_healpix_types[] = {
392 NPY_DOUBLE, NPY_DOUBLE, NPY_INT, NPY_INT64, NPY_DOUBLE, NPY_DOUBLE},
393 healpix_to_xyz_types[] = {
394 NPY_INT64, NPY_INT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
395 xyz_to_healpix_types[] = {
396 NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_INT, NPY_INT64, NPY_DOUBLE, NPY_DOUBLE},
397 healpix_to_healpix_types[] = {
398 NPY_INT64, NPY_INT, NPY_INT64},
399 bilinear_interpolation_weights_types[] = {
400 NPY_DOUBLE, NPY_DOUBLE, NPY_INT,
401 NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64,
402 NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
403 neighbours_types[] = {
404 NPY_INT64, NPY_INT,
405 NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64,
406 NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64};
407
408
409 PyMODINIT_FUNC PyInit__core(void)
410 {
411 PyObject *module;
412
413 import_array();
414 import_umath();
415
416 module = PyModule_Create(&moduledef);
417
418 PyModule_AddObject(
419 module, "healpix_nested_to_lonlat", PyUFunc_FromFuncAndData(
420 healpix_to_lonlat_loops, nested_ufunc_data,
421 healpix_to_lonlat_types, 1, 4, 2, PyUFunc_None,
422 "healpix_nested_to_lonlat", NULL, 0));
423
424 PyModule_AddObject(
425 module, "healpix_ring_to_lonlat", PyUFunc_FromFuncAndData(
426 healpix_to_lonlat_loops, ring_ufunc_data,
427 healpix_to_lonlat_types, 1, 4, 2, PyUFunc_None,
428 "healpix_ring_to_lonlat", NULL, 0));
429
430 PyModule_AddObject(
431 module, "lonlat_to_healpix_nested", PyUFunc_FromFuncAndData(
432 lonlat_to_healpix_loops, nested_ufunc_data,
433 lonlat_to_healpix_types, 1, 3, 3, PyUFunc_None,
434 "lonlat_to_healpix_nested", NULL, 0));
435
436 PyModule_AddObject(
437 module, "lonlat_to_healpix_ring", PyUFunc_FromFuncAndData(
438 lonlat_to_healpix_loops, ring_ufunc_data,
439 lonlat_to_healpix_types, 1, 3, 3, PyUFunc_None,
440 "lonlat_to_healpix_ring", NULL, 0));
441
442 PyModule_AddObject(
443 module, "healpix_nested_to_xyz", PyUFunc_FromFuncAndData(
444 healpix_to_xyz_loops, nested_ufunc_data,
445 healpix_to_xyz_types, 1, 4, 3, PyUFunc_None,
446 "healpix_nested_to_xyz", NULL, 0));
447
448 PyModule_AddObject(
449 module, "healpix_ring_to_xyz", PyUFunc_FromFuncAndData(
450 healpix_to_xyz_loops, ring_ufunc_data,
451 healpix_to_xyz_types, 1, 4, 3, PyUFunc_None,
452 "healpix_ring_to_xyz", NULL, 0));
453
454 PyModule_AddObject(
455 module, "xyz_to_healpix_nested", PyUFunc_FromFuncAndData(
456 xyz_to_healpix_loops, nested_ufunc_data,
457 xyz_to_healpix_types, 1, 4, 3, PyUFunc_None,
458 "xyz_to_healpix_nested", NULL, 0));
459
460 PyModule_AddObject(
461 module, "xyz_to_healpix_ring", PyUFunc_FromFuncAndData(
462 xyz_to_healpix_loops, ring_ufunc_data,
463 xyz_to_healpix_types, 1, 4, 3, PyUFunc_None,
464 "xyz_to_healpix_ring", NULL, 0));
465
466 PyModule_AddObject(
467 module, "nested_to_ring", PyUFunc_FromFuncAndData(
468 nested_to_ring_loops, no_ufunc_data,
469 healpix_to_healpix_types, 1, 2, 1, PyUFunc_None,
470 "nested_to_ring", NULL, 0));
471
472 PyModule_AddObject(
473 module, "ring_to_nested", PyUFunc_FromFuncAndData(
474 ring_to_nested_loops, no_ufunc_data,
475 healpix_to_healpix_types, 1, 2, 1, PyUFunc_None,
476 "ring_to_nested", NULL, 0));
477
478 PyModule_AddObject(
479 module, "bilinear_interpolation_weights", PyUFunc_FromFuncAndData(
480 bilinear_interpolation_weights_loops, no_ufunc_data,
481 bilinear_interpolation_weights_types, 1, 3, 8, PyUFunc_None,
482 "bilinear_interpolation_weights", NULL, 0));
483
484 PyModule_AddObject(
485 module, "neighbours_nested", PyUFunc_FromFuncAndData(
486 neighbours_loops, nested_ufunc_data,
487 neighbours_types, 1, 2, 8, PyUFunc_None,
488 "neighbours_nested", NULL, 0));
489
490 PyModule_AddObject(
491 module, "neighbours_ring", PyUFunc_FromFuncAndData(
492 neighbours_loops, ring_ufunc_data,
493 neighbours_types, 1, 2, 8, PyUFunc_None,
494 "neighbours_ring", NULL, 0));
495
496 return module;
497 }