Codebase list astropy-healpix / HEAD astropy_healpix / _core.c
HEAD

Tree @HEAD (Download .tar.gz)

_core.c @HEADraw · history · blame

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <Python.h>
#include <numpy/arrayobject.h>
#include <numpy/ufuncobject.h>
#include "healpix.h"
#include "healpix-utils.h"
#include "interpolation.h"

/* FIXME: We need npy_set_floatstatus_invalid(), but unlike most of the Numpy
 * C API it is only available on some platforms if you explicitly link against
 * Numpy, which is not typically done for building C extensions. This bundled
 * header file contains a static definition of _npy_set_floatstatus_invalid().
 */
#include "ieee754.h"


#define INVALID_INDEX (-1)

/* Data structure for storing function pointers for routines that are specific
 * to the HEALPix ordering scheme. When we create the ufuncs using
 * PyUFunc_FromFuncAndData, we will set them up to pass a pointer to this
 * data structure through as the void *data parameter for the loop functions
 * defined below. */
typedef struct {
    int64_t (*order_to_xy)(int64_t, int);
    int64_t (*xy_to_order)(int64_t, int);
} order_funcs;

static order_funcs
    nested_order_funcs = {healpixl_nested_to_xy, healpixl_xy_to_nested},
    ring_order_funcs   = {healpixl_ring_to_xy, healpixl_xy_to_ring};

static void *no_ufunc_data[]     = {NULL},
            *nested_ufunc_data[] = {&nested_order_funcs},
            *ring_ufunc_data[]   = {&ring_order_funcs};


static int64_t nside2npix(int nside)
{
    return 12 * ((int64_t) nside) * ((int64_t) nside);
}


static int pixel_nside_valid(int64_t pixel, int nside)
{
    return pixel >= 0 && pixel < nside2npix(nside);
}


static void healpix_to_lonlat_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    order_funcs *funcs = data;
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
        int     nside = *(int *)     &args[1][i * steps[1]];
        double  dx    = *(double *)  &args[2][i * steps[2]];
        double  dy    = *(double *)  &args[3][i * steps[3]];
        double  *lon  =  (double *)  &args[4][i * steps[4]];
        double  *lat  =  (double *)  &args[5][i * steps[5]];
        int64_t xy = INVALID_INDEX;

        if (pixel_nside_valid(pixel, nside))
            xy = funcs->order_to_xy(pixel, nside);

        if (xy >= 0)
            healpixl_to_radec(xy, nside, dx, dy, lon, lat);
        else
        {
            *lon = *lat = NPY_NAN;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void lonlat_to_healpix_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    order_funcs *funcs = data;
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        double  lon    = *(double *)  &args[0][i * steps[0]];
        double  lat    = *(double *)  &args[1][i * steps[1]];
        int     nside  = *(int *)     &args[2][i * steps[2]];
        int64_t *pixel =  (int64_t *) &args[3][i * steps[3]];
        double  *dx    =  (double *)  &args[4][i * steps[4]];
        double  *dy    =  (double *)  &args[5][i * steps[5]];
        int64_t xy = INVALID_INDEX;

        xy = radec_to_healpixlf(lon, lat, nside, dx, dy);
        if (xy >= 0)
            *pixel = funcs->xy_to_order(xy, nside);
        else {
            *pixel = INVALID_INDEX;
            *dx = *dy = NPY_NAN;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void healpix_to_xyz_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    order_funcs *funcs = data;
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
        int     nside = *(int *)     &args[1][i * steps[1]];
        double  dx    = *(double *)  &args[2][i * steps[2]];
        double  dy    = *(double *)  &args[3][i * steps[3]];
        double  *x    =  (double *)  &args[4][i * steps[4]];
        double  *y    =  (double *)  &args[5][i * steps[5]];
        double  *z    =  (double *)  &args[6][i * steps[6]];
        int64_t xy = INVALID_INDEX;

        if (pixel_nside_valid(pixel, nside))
            xy = funcs->order_to_xy(pixel, nside);

        if (xy >= 0)
            healpixl_to_xyz(xy, nside, dx, dy, x, y, z);
        else
        {
            *x = *y = *z = NPY_NAN;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void xyz_to_healpix_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    order_funcs *funcs = data;
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        double  x      = *(double *)  &args[0][i * steps[0]];
        double  y      = *(double *)  &args[1][i * steps[1]];
        double  z      = *(double *)  &args[2][i * steps[2]];
        int     nside  = *(int *)     &args[3][i * steps[3]];
        int64_t *pixel =  (int64_t *) &args[4][i * steps[4]];
        double  *dx    =  (double *)  &args[5][i * steps[5]];
        double  *dy    =  (double *)  &args[6][i * steps[6]];
        int64_t xy = INVALID_INDEX;

        /* xyztohealpixlf expects a unit vector */
        double norm = sqrt(x*x + y*y + z*z);
        x /= norm;
        y /= norm;
        z /= norm;

        xy = xyztohealpixlf(x, y, z, nside, dx, dy);
        if (xy >= 0)
            *pixel = funcs->xy_to_order(xy, nside);
        else {
            *pixel = INVALID_INDEX;
            *dx = *dy = NPY_NAN;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void nested_to_ring_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        int64_t nested = *(int64_t *) &args[0][i * steps[0]];
        int     nside  = *(int *)     &args[1][i * steps[1]];
        int64_t *ring  =  (int64_t *) &args[2][i * steps[2]];
        int64_t xy = INVALID_INDEX;

        if (pixel_nside_valid(nested, nside))
            xy = healpixl_nested_to_xy(nested, nside);
        if (xy >= 0)
            *ring = healpixl_xy_to_ring(xy, nside);
        else {
            *ring = INVALID_INDEX;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void ring_to_nested_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        int64_t ring   = *(int64_t *) &args[0][i * steps[0]];
        int     nside  = *(int *)     &args[1][i * steps[1]];
        int64_t *nested = (int64_t *) &args[2][i * steps[2]];
        int64_t xy = INVALID_INDEX;

        if (pixel_nside_valid(ring, nside))
            xy = healpixl_ring_to_xy(ring, nside);
        if (xy >= 0)
            *nested = healpixl_xy_to_nested(xy, nside);
        else {
            *nested = INVALID_INDEX;
            _npy_set_floatstatus_invalid();
        }
    }
}


static void bilinear_interpolation_weights_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        double  lon    = *(double *)  &args[0][i * steps[0]];
        double  lat    = *(double *)  &args[1][i * steps[1]];
        int     nside  = *(int *)     &args[2][i * steps[2]];
        int64_t indices[4];
        double weights[4];
        int j;

        interpolate_weights(lon, lat, indices, weights, nside);
        for (j = 0; j < 4; j ++)
        {
            *(int64_t *) &args[3 + j][i * steps[3 + j]] = indices[j];
            *(double *)  &args[7 + j][i * steps[7 + j]] = weights[j];
        }
    }
}


static void neighbours_loop(
    char **args, npy_intp *dimensions, npy_intp *steps, void *data)
{
    order_funcs *funcs = data;
    npy_intp i, n = dimensions[0];

    for (i = 0; i < n; i ++)
    {
        int64_t pixel = *(int64_t *) &args[0][i * steps[0]];
        int     nside = *(int *)     &args[1][i * steps[1]];
        int64_t neighbours[] = {
            INVALID_INDEX, INVALID_INDEX, INVALID_INDEX, INVALID_INDEX,
            INVALID_INDEX, INVALID_INDEX, INVALID_INDEX, INVALID_INDEX};
        int j;
        int64_t xy = INVALID_INDEX;

        if (pixel_nside_valid(pixel, nside))
            xy = funcs->order_to_xy(pixel, nside);
        if (xy >= 0)
            healpixl_get_neighbours(xy, neighbours, nside);

        for (j = 0; j < 8; j ++)
        {
            int k = 4 - j;
            if (k < 0)
                k += 8;
            xy = neighbours[k];
            if (xy >= 0)
                pixel = funcs->xy_to_order(xy, nside);
            else {
                pixel = INVALID_INDEX;
                _npy_set_floatstatus_invalid();
            }
            *(int64_t *) &args[2 + j][i * steps[2 + j]] = pixel;
        }
    }
}


static PyObject *healpix_cone_search(
    PyObject *self, PyObject *args, PyObject *kwargs)
{
    PyObject *result;
    static char *kws[] = {"lon", "lat", "radius", "nside", "order", NULL};
    double lon, lat, radius;
    int nside;
    char *order;
    int64_t *indices, n_indices;
    int64_t *result_data;
    npy_intp dims[1];

    if (!PyArg_ParseTupleAndKeywords(
            args, kwargs, "dddis", kws, &lon, &lat, &radius, &nside, &order))
        return NULL;

    n_indices = healpix_rangesearch_radec_simple(
        lon, lat, radius, nside, 0, &indices);
    if (!indices)
    {
        PyErr_SetString(
            PyExc_RuntimeError, "healpix_rangesearch_radec_simple failed");
        return NULL;
    }

    dims[0] = n_indices;
    result = PyArray_SimpleNew(1, dims, NPY_INT64);
    if (result)
    {
        result_data = PyArray_DATA((PyArrayObject *) result);

        if (strcmp(order, "nested") == 0)
        {
            int i;
            for (i = 0; i < n_indices; i ++)
                result_data[i] = healpixl_xy_to_nested(indices[i], nside);
        } else {
            int i;
            for (i = 0; i < n_indices; i ++)
                result_data[i] = healpixl_xy_to_ring(indices[i], nside);
        }
    }

    free(indices);
    return result;
}


static PyMethodDef methods[] = {
    {"healpix_cone_search", (PyCFunction) healpix_cone_search, METH_VARARGS | METH_KEYWORDS, NULL},
    {NULL, NULL, 0, NULL}
};


static PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "_core", NULL, -1, methods
};

static PyUFuncGenericFunction
    healpix_to_lonlat_loops             [] = {healpix_to_lonlat_loop},
    lonlat_to_healpix_loops             [] = {lonlat_to_healpix_loop},
    healpix_to_xyz_loops                [] = {healpix_to_xyz_loop},
    xyz_to_healpix_loops                [] = {xyz_to_healpix_loop},
    nested_to_ring_loops                [] = {nested_to_ring_loop},
    ring_to_nested_loops                [] = {ring_to_nested_loop},
    bilinear_interpolation_weights_loops[] = {bilinear_interpolation_weights_loop},
    neighbours_loops                    [] = {neighbours_loop};

static char
    healpix_to_lonlat_types[] = {
        NPY_INT64, NPY_INT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
    lonlat_to_healpix_types[] = {
        NPY_DOUBLE, NPY_DOUBLE, NPY_INT, NPY_INT64, NPY_DOUBLE, NPY_DOUBLE},
    healpix_to_xyz_types[] = {
        NPY_INT64, NPY_INT, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
    xyz_to_healpix_types[] = {
        NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_INT, NPY_INT64, NPY_DOUBLE, NPY_DOUBLE},
    healpix_to_healpix_types[] = {
        NPY_INT64, NPY_INT, NPY_INT64},
    bilinear_interpolation_weights_types[] = {
        NPY_DOUBLE, NPY_DOUBLE, NPY_INT,
        NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64,
        NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE},
    neighbours_types[] = {
        NPY_INT64, NPY_INT,
        NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64,
        NPY_INT64, NPY_INT64, NPY_INT64, NPY_INT64};


PyMODINIT_FUNC PyInit__core(void)
{
    PyObject *module;

    import_array();
    import_umath();

    module = PyModule_Create(&moduledef);

    PyModule_AddObject(
        module, "healpix_nested_to_lonlat", PyUFunc_FromFuncAndData(
            healpix_to_lonlat_loops, nested_ufunc_data,
            healpix_to_lonlat_types, 1, 4, 2, PyUFunc_None,
            "healpix_nested_to_lonlat", NULL, 0));

    PyModule_AddObject(
        module, "healpix_ring_to_lonlat", PyUFunc_FromFuncAndData(
            healpix_to_lonlat_loops, ring_ufunc_data,
            healpix_to_lonlat_types, 1, 4, 2, PyUFunc_None,
            "healpix_ring_to_lonlat", NULL, 0));

    PyModule_AddObject(
        module, "lonlat_to_healpix_nested", PyUFunc_FromFuncAndData(
            lonlat_to_healpix_loops, nested_ufunc_data,
            lonlat_to_healpix_types, 1, 3, 3, PyUFunc_None,
            "lonlat_to_healpix_nested", NULL, 0));

    PyModule_AddObject(
        module, "lonlat_to_healpix_ring", PyUFunc_FromFuncAndData(
            lonlat_to_healpix_loops, ring_ufunc_data,
            lonlat_to_healpix_types, 1, 3, 3, PyUFunc_None,
            "lonlat_to_healpix_ring", NULL, 0));

    PyModule_AddObject(
        module, "healpix_nested_to_xyz", PyUFunc_FromFuncAndData(
            healpix_to_xyz_loops, nested_ufunc_data,
            healpix_to_xyz_types, 1, 4, 3, PyUFunc_None,
            "healpix_nested_to_xyz", NULL, 0));

    PyModule_AddObject(
        module, "healpix_ring_to_xyz", PyUFunc_FromFuncAndData(
            healpix_to_xyz_loops, ring_ufunc_data,
            healpix_to_xyz_types, 1, 4, 3, PyUFunc_None,
            "healpix_ring_to_xyz", NULL, 0));

    PyModule_AddObject(
        module, "xyz_to_healpix_nested", PyUFunc_FromFuncAndData(
            xyz_to_healpix_loops, nested_ufunc_data,
            xyz_to_healpix_types, 1, 4, 3, PyUFunc_None,
            "xyz_to_healpix_nested", NULL, 0));

    PyModule_AddObject(
        module, "xyz_to_healpix_ring", PyUFunc_FromFuncAndData(
            xyz_to_healpix_loops, ring_ufunc_data,
            xyz_to_healpix_types, 1, 4, 3, PyUFunc_None,
            "xyz_to_healpix_ring", NULL, 0));

    PyModule_AddObject(
        module, "nested_to_ring", PyUFunc_FromFuncAndData(
            nested_to_ring_loops, no_ufunc_data,
            healpix_to_healpix_types, 1, 2, 1, PyUFunc_None,
            "nested_to_ring", NULL, 0));

    PyModule_AddObject(
        module, "ring_to_nested", PyUFunc_FromFuncAndData(
            ring_to_nested_loops, no_ufunc_data,
            healpix_to_healpix_types, 1, 2, 1, PyUFunc_None,
            "ring_to_nested", NULL, 0));

    PyModule_AddObject(
        module, "bilinear_interpolation_weights", PyUFunc_FromFuncAndData(
            bilinear_interpolation_weights_loops, no_ufunc_data,
            bilinear_interpolation_weights_types, 1, 3, 8, PyUFunc_None,
            "bilinear_interpolation_weights", NULL, 0));

    PyModule_AddObject(
        module, "neighbours_nested", PyUFunc_FromFuncAndData(
            neighbours_loops, nested_ufunc_data,
            neighbours_types, 1, 2, 8, PyUFunc_None,
            "neighbours_nested", NULL, 0));

    PyModule_AddObject(
        module, "neighbours_ring", PyUFunc_FromFuncAndData(
            neighbours_loops, ring_ufunc_data,
            neighbours_types, 1, 2, 8, PyUFunc_None,
            "neighbours_ring", NULL, 0));

    return module;
}