| 1 |
/* |
| 2 |
# This file is part of the Astrometry.net suite. |
| 3 |
# Licensed under a 3-clause BSD style license - see LICENSE |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef HEALPIX_H |
| 7 |
#define HEALPIX_H |
| 8 |
|
| 9 |
#include <sys/types.h> |
| 10 |
|
| 11 |
#ifdef _MSC_VER |
| 12 |
#if _MSC_VER >= 1600 |
| 13 |
#include <stdint.h> |
| 14 |
#else |
| 15 |
#include <stdint_msc.h> |
| 16 |
#endif |
| 17 |
#else |
| 18 |
#include <stdint.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
#include "keywords.h" |
| 22 |
|
| 23 |
//#undef Const |
| 24 |
//#define Const |
| 25 |
|
| 26 |
/** |
| 27 |
The HEALPix paper is here: |
| 28 |
http://iopscience.iop.org/0004-637X/622/2/759/pdf/0004-637X_622_2_759.pdf |
| 29 |
See: |
| 30 |
http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=2005ApJ...622..759G&db_key=AST&high=41069202cf02947 |
| 31 |
*/ |
| 32 |
|
| 33 |
/** |
| 34 |
In this documentation we talk about "base healpixes": these are the big, |
| 35 |
top-level healpixes. There are 12 of these, with indices [0, 11]. |
| 36 |
|
| 37 |
We say "fine healpixes" or "healpixes" or "pixels" when we mean the fine- |
| 38 |
scale healpixes; there are Nside^2 of these in each base healpix, |
| 39 |
for a total of 12*Nside^2, indexed from zero. |
| 40 |
*/ |
| 41 |
|
| 42 |
/** |
| 43 |
Some notes about the different indexing schemes: |
| 44 |
|
| 45 |
The healpix paper discusses two different ways to number healpixes, and |
| 46 |
there is a third way, which we prefer, which is (in my opinion) more |
| 47 |
sensible and easy. |
| 48 |
|
| 49 |
|
| 50 |
-RING indexing. Healpixes are numbered first in order of decreasing DEC, |
| 51 |
then in order of increasing RA of the center of the pixel, ie: |
| 52 |
|
| 53 |
. 0 1 2 3 |
| 54 |
. 4 5 6 7 8 9 10 11 |
| 55 |
. 12 13 14 15 16 17 18 19 |
| 56 |
. 20 21 22 23 24 25 26 27 |
| 57 |
. 28 29 30 31 32 33 34 35 |
| 58 |
. 36 37 38 39 40 41 42 43 |
| 59 |
. 44 45 46 47 |
| 60 |
|
| 61 |
Note that 12, 20 and 28 are part of base healpix 4, as is 27; it "wraps |
| 62 |
around". |
| 63 |
|
| 64 |
The RING index can be decomposed into the "ring number" and the index |
| 65 |
within the ring (called "longitude index"). Note that different rings |
| 66 |
contain different numbers of healpixes. Also note that the ring number |
| 67 |
starts from 1, but the longitude index starts from zero. |
| 68 |
|
| 69 |
|
| 70 |
-NESTED indexing. This only works for Nside parameters that are powers of |
| 71 |
two. This scheme is hierarchical in the sense that each pair of bits of |
| 72 |
the index tells you where the pixel center is to finer and finer |
| 73 |
resolution. It doesn't really show with Nside=2, but here it is anyway: |
| 74 |
|
| 75 |
. 3 7 11 15 |
| 76 |
. 2 1 6 5 10 9 14 13 |
| 77 |
. 19 0 23 4 27 8 31 12 |
| 78 |
. 17 22 21 26 25 30 29 18 |
| 79 |
. 16 35 20 39 24 43 28 47 |
| 80 |
. 34 33 38 37 42 41 46 45 |
| 81 |
. 32 36 40 44 |
| 82 |
|
| 83 |
Note that all the base healpixes have the same pattern; they're just |
| 84 |
offset by factors of Nside^2. |
| 85 |
|
| 86 |
Here's a zoom-in of the first base healpix, turned 45 degrees to the |
| 87 |
right, for Nside=4: |
| 88 |
|
| 89 |
. 10 11 14 15 |
| 90 |
. 8 9 12 13 |
| 91 |
. 2 3 6 7 |
| 92 |
. 0 1 4 5 |
| 93 |
|
| 94 |
Note that the bottom-left block of 4 have the smallest values, and within |
| 95 |
that the bottom-left corner has the smallest value, followed by the |
| 96 |
bottom-right, top-left, then top-right. |
| 97 |
|
| 98 |
The NESTED index can't be decomposed into 'orthogonal' directions. |
| 99 |
|
| 100 |
|
| 101 |
-XY indexing. This is arguably the most natural, at least for the |
| 102 |
internal usage of the healpix code. Within each base healpix, the |
| 103 |
healpixes are numbered starting with 0 for the southmost pixel, then |
| 104 |
increasing first in the "y" (north-west), then in the "x" (north-east) |
| 105 |
direction. In other words, within each base healpix there is a grid |
| 106 |
and we number the pixels "lexicographically" (mod a 135 degree turn). |
| 107 |
|
| 108 |
. 3 7 11 15 |
| 109 |
. 1 2 5 6 9 10 13 14 |
| 110 |
. 19 0 23 4 27 8 31 12 |
| 111 |
. 18 21 22 25 26 29 30 17 |
| 112 |
. 16 35 20 39 24 43 28 47 |
| 113 |
. 33 34 37 38 41 42 45 46 |
| 114 |
. 32 36 40 44 |
| 115 |
|
| 116 |
Zooming in on the first base healpix, turning 45 degrees to the right, |
| 117 |
for Nside=4 we get: |
| 118 |
|
| 119 |
. 3 7 11 15 |
| 120 |
. 2 6 10 14 |
| 121 |
. 1 5 9 13 |
| 122 |
. 0 4 8 12 |
| 123 |
|
| 124 |
Notice that the numbers first increase from bottom to top (y), then left to |
| 125 |
right (x). |
| 126 |
|
| 127 |
The XY indexing can be decomposed into 'x' and 'y' coordinates |
| 128 |
(in case that wasn't obvious), where the above figure becomes (x,y): |
| 129 |
|
| 130 |
. (0,3) (1,3) (2,3) (3,3) |
| 131 |
. (0,2) (1,2) (2,2) (3,2) |
| 132 |
. (0,1) (1,1) (2,1) (3,1) |
| 133 |
. (0,0) (1,0) (2,0) (3,0) |
| 134 |
|
| 135 |
Note that "x" increases in the north-east direction, and "y" increases in |
| 136 |
the north-west direction. |
| 137 |
|
| 138 |
The major advantage to this indexing scheme is that it extends to |
| 139 |
fractional coordinates in a natural way: it is meaningful to talk about |
| 140 |
the position (x,y) = (0.25, 0.6) and you can compute its position. |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
In this code, all healpix indexing uses the XY scheme. If you want to |
| 146 |
use the other schemes you will have to use the conversion routines: |
| 147 |
. healpix_xy_to_ring |
| 148 |
. healpix_ring_to_xy |
| 149 |
. healpix_xy_to_nested |
| 150 |
. healpix_nested_to_xy |
| 151 |
*/ |
| 152 |
|
| 153 |
// The maximum healpix Nside that leads to int-sized healpix indices. |
| 154 |
// 12 * (13377+1)^2 > 2^31 (since we use signed ints) |
| 155 |
// This corresponds to about 16 arcsec side length. |
| 156 |
#define HP_MAX_INT_NSIDE 13377 |
| 157 |
|
| 158 |
/** |
| 159 |
Converts a healpix index from the XY scheme to the RING scheme. |
| 160 |
*/ |
| 161 |
Const int64_t healpixl_xy_to_ring(int64_t hp, int Nside); |
| 162 |
|
| 163 |
/** |
| 164 |
Converts a healpix index from the RING scheme to the XY scheme. |
| 165 |
*/ |
| 166 |
Const int64_t healpixl_ring_to_xy(int64_t ring_index, int Nside); |
| 167 |
|
| 168 |
/** |
| 169 |
Converts a healpix index from the XY scheme to the NESTED scheme. |
| 170 |
*/ |
| 171 |
Const int64_t healpixl_xy_to_nested(int64_t hp, int Nside); |
| 172 |
|
| 173 |
/** |
| 174 |
Converts a healpix index from the NESTED scheme to the XY scheme. |
| 175 |
*/ |
| 176 |
Const int64_t healpixl_nested_to_xy(int64_t nested_index, int Nside); |
| 177 |
|
| 178 |
/** |
| 179 |
Decomposes a RING index into the "ring number" (each ring contain |
| 180 |
healpixels of equal latitude) and "longitude index". Pixels within a |
| 181 |
ring have longitude index starting at zero for the first pixel with |
| 182 |
RA >= 0. Different rings contain different numbers of healpixels. |
| 183 |
*/ |
| 184 |
void healpixl_decompose_ring(int64_t ring_index, int Nside, |
| 185 |
int* p_ring_number, int* p_longitude_index); |
| 186 |
|
| 187 |
/** |
| 188 |
Composes a RING index given the "ring number" and "longitude index". |
| 189 |
|
| 190 |
Does NOT check that the values are legal! Garbage in, garbage out. |
| 191 |
*/ |
| 192 |
Const int64_t healpixl_compose_ring(int ring, int longind, int Nside); |
| 193 |
// Const int64_t healpixl_compose_ringl(int64_t ring, int64_t longind, int64_t Nside); |
| 194 |
|
| 195 |
/** |
| 196 |
Decomposes an XY index into the "base healpix" and "x" and "y" coordinates |
| 197 |
within that healpix. |
| 198 |
*/ |
| 199 |
void healpix_decompose_xy(int finehp, int* bighp, int* x, int* y, int Nside); |
| 200 |
|
| 201 |
void healpixl_decompose_xy(int64_t finehp, int* bighp, int* x, int* y, int Nside); |
| 202 |
|
| 203 |
/** |
| 204 |
Composes an XY index given the "base healpix" and "x" and "y" coordinates |
| 205 |
within that healpix. |
| 206 |
*/ |
| 207 |
|
| 208 |
Const int64_t healpixl_compose_xy(int bighp, int x, int y, int Nside); |
| 209 |
|
| 210 |
/** |
| 211 |
Given (x,y) coordinates of resolution "nside" within a base-level |
| 212 |
healpixel, and an output resolution "outnside", returns the output |
| 213 |
(x,y) coordinates at the output resolution. |
| 214 |
*/ |
| 215 |
void healpixl_convert_xy_nside(int x, int y, int nside, int outnside, |
| 216 |
int* outx, int* outy); |
| 217 |
|
| 218 |
/** |
| 219 |
Given a healpix index (in the XY scheme) of resolution "nside", and |
| 220 |
an output resolution "outnside", returns the healpix index at the |
| 221 |
output resolution. |
| 222 |
*/ |
| 223 |
void healpix_convert_nside(int64_t hp, int nside, int outnside, int* outhp); |
| 224 |
|
| 225 |
/** |
| 226 |
Converts (RA, DEC) coordinates (in radians) to healpix index. |
| 227 |
*/ |
| 228 |
Const int64_t radec_to_healpixl(double ra, double dec, int Nside); |
| 229 |
|
| 230 |
int64_t radec_to_healpixlf(double ra, double dec, int Nside, double* dx, double* dy); |
| 231 |
|
| 232 |
/** |
| 233 |
Converts (RA, DEC) coordinates (in degrees) to healpix index. |
| 234 |
*/ |
| 235 |
Const int radecdegtohealpix(double ra, double dec, int Nside); |
| 236 |
|
| 237 |
int radecdegtohealpixf(double ra, double dec, int Nside, double* dx, double* dy); |
| 238 |
|
| 239 |
Const int64_t radecdegtohealpixl(double ra, double dec, int Nside); |
| 240 |
|
| 241 |
int64_t radecdegtohealpixlf(double ra, double dec, int Nside, double* dx, double* dy); |
| 242 |
|
| 243 |
/** |
| 244 |
Converts (x,y,z) coordinates on the unit sphere into a healpix index. |
| 245 |
*/ |
| 246 |
Const int64_t xyztohealpixl(double x, double y, double z, int Nside); |
| 247 |
|
| 248 |
int64_t xyztohealpixlf(double x, double y, double z, int Nside, |
| 249 |
double* p_dx, double* p_dy); |
| 250 |
|
| 251 |
/** |
| 252 |
Converts (x,y,z) coordinates (stored in an array) on the unit sphere into |
| 253 |
a healpix index. |
| 254 |
*/ |
| 255 |
int xyzarrtohealpix(const double* xyz, int Nside); |
| 256 |
|
| 257 |
int64_t xyzarrtohealpixl(const double* xyz, int Nside); |
| 258 |
|
| 259 |
int xyzarrtohealpixf(const double* xyz,int Nside, double* p_dx, double* p_dy); |
| 260 |
|
| 261 |
/** |
| 262 |
Converts a healpix index, plus fractional offsets (dx,dy), into (x,y,z) |
| 263 |
coordinates on the unit sphere. (dx,dy) must be in [0, 1]. (0.5, 0.5) |
| 264 |
is the center of the healpix. (0,0) is the southernmost corner, (1,1) is |
| 265 |
the northernmost corner, (1,0) is the easternmost, and (0,1) the |
| 266 |
westernmost. |
| 267 |
*/ |
| 268 |
void healpixl_to_xyz(int64_t hp, int Nside, double dx, double dy, |
| 269 |
double* p_x, double *p_y, double *p_z); |
| 270 |
|
| 271 |
/** |
| 272 |
Same as healpix_to_xyz, but (x,y,z) are stored in an array. |
| 273 |
*/ |
| 274 |
void healpixl_to_xyzarr(int64_t hp, int Nside, double dx, double dy, |
| 275 |
double* xyz); |
| 276 |
|
| 277 |
/** |
| 278 |
Same as healpix_to_xyz, but returns (RA,DEC) in radians. |
| 279 |
*/ |
| 280 |
void healpixl_to_radec(int64_t hp, int Nside, double dx, double dy, |
| 281 |
double* ra, double* dec); |
| 282 |
|
| 283 |
void healpixl_to_radecdeg(int64_t hp, int Nside, double dx, double dy, |
| 284 |
double* ra, double* dec); |
| 285 |
|
| 286 |
/** |
| 287 |
Same as healpix_to_radec, but (RA,DEC) are stored in an array. |
| 288 |
*/ |
| 289 |
void healpix_to_radecarr(int64_t hp, int Nside, double dx, double dy, |
| 290 |
double* radec); |
| 291 |
|
| 292 |
void healpix_to_radecdegarr(int64_t hp, int Nside, double dx, double dy, |
| 293 |
double* radec); |
| 294 |
|
| 295 |
/** |
| 296 |
Computes the approximate side length of a healpix, in arcminutes. |
| 297 |
*/ |
| 298 |
Const double healpix_side_length_arcmin(int Nside); |
| 299 |
|
| 300 |
/** |
| 301 |
Computes the approximate Nside you need to get healpixes with side |
| 302 |
length about "arcmin" arcminutes. (inverse of |
| 303 |
healpix_side_length_arcmin) |
| 304 |
*/ |
| 305 |
double healpix_nside_for_side_length_arcmin(double arcmin); |
| 306 |
|
| 307 |
/** |
| 308 |
Finds the healpixes neighbouring the given healpix, placing them in the |
| 309 |
array "neighbour". Returns the number of neighbours. You must ensure |
| 310 |
that "neighbour" has 8 elements. |
| 311 |
|
| 312 |
Healpixes in the interior of a large healpix will have eight neighbours; |
| 313 |
pixels near the edges can have fewer. |
| 314 |
*/ |
| 315 |
void healpixl_get_neighbours(int64_t pix, int64_t* neighbour, int Nside); |
| 316 |
|
| 317 |
/** |
| 318 |
Finds the healpixes containing and neighbouring the given xyz |
| 319 |
position which are within distance 'range' (in units of distance of |
| 320 |
the unit sphere). Places the results in 'healpixes', which must have |
| 321 |
at least 9 elements. Returns the number of 'healpixes' set. |
| 322 |
|
| 323 |
Returns -1 if "Nside" < 0. |
| 324 |
*/ |
| 325 |
int healpix_get_neighbours_within_range(double* xyz, double range, int64_t* healpixes, |
| 326 |
int Nside); |
| 327 |
|
| 328 |
/** |
| 329 |
Same as above, but RA,Dec,radius in degrees. |
| 330 |
*/ |
| 331 |
int healpix_get_neighbours_within_range_radec(double ra, double dec, double radius, |
| 332 |
int64_t* healpixes, int Nside); |
| 333 |
|
| 334 |
/** |
| 335 |
Returns the minimum distance (in degrees) between the given healpix |
| 336 |
and the given RA,Dec (in degrees). |
| 337 |
*/ |
| 338 |
double healpix_distance_to_radec(int64_t hp, int Nside, double ra, double dec, |
| 339 |
double* closestradec); |
| 340 |
|
| 341 |
/** |
| 342 |
Returns the minimum distance (in degrees) between the given healpix |
| 343 |
and the given xyz (point on unit sphere). |
| 344 |
*/ |
| 345 |
double healpix_distance_to_xyz(int64_t hp, int Nside, const double* xyz, |
| 346 |
double* closestxyz); |
| 347 |
|
| 348 |
/** |
| 349 |
Returns true if the closest distance between the given healpix and |
| 350 |
the given RA,Dec (in degrees) is less than then given radius (in degrees). |
| 351 |
*/ |
| 352 |
int healpix_within_range_of_radec(int64_t hp, int Nside, double ra, double dec, |
| 353 |
double radius); |
| 354 |
int healpix_within_range_of_xyz(int64_t hp, int Nside, const double* xyz, |
| 355 |
double radius); |
| 356 |
int healpixl_within_range_of_xyz(int64_t hp, int Nside, const double* xyz, |
| 357 |
double radius); |
| 358 |
|
| 359 |
|
| 360 |
/** |
| 361 |
Computes the RA,Dec bounding-box of the given healpix. Results are |
| 362 |
in degrees. RA may be wacky for healpixes spanning RA=0. |
| 363 |
*/ |
| 364 |
void healpix_radec_bounds(int64_t hp, int nside, |
| 365 |
double* ralo, double* rahi, |
| 366 |
double* declo, double* dechi); |
| 367 |
|
| 368 |
#endif |