Debian Codegraph @

cextern/astrometry.net/test_healpix.c

raw
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 #include <math.h>
7 #include <stdio.h>
8 #include <assert.h>
9
10 #include "os-features.h"
11 #include "CuTest.h"
12 #include "starutil.h"
13 #include "healpix.h"
14 #include "bl.h"
15
16 static double square(double x) {
17 return x*x;
18 }
19
20 void test_side_length(CuTest* ct) {
21 double hp;
22 double len = healpix_side_length_arcmin(1);
23 CuAssertDblEquals(ct, 3517.9, len, 0.1);
24 hp = healpix_nside_for_side_length_arcmin(len);
25 CuAssertDblEquals(ct, 1.0, hp, 0.001);
26
27 len = healpix_side_length_arcmin(2);
28 CuAssertDblEquals(ct, 1758.969, len, 0.001);
29 hp = healpix_nside_for_side_length_arcmin(len);
30 CuAssertDblEquals(ct, 2.0, hp, 0.001);
31 }
32
33 static void add_plot_xyz_point(double* xyz) {
34 double ra,dec;
35 xyzarr2radecdeg(xyz, &ra, &dec);
36 fprintf(stderr, "xp.append(%g)\n", ra);
37 fprintf(stderr, "yp.append(%g)\n", dec);
38 }
39
40 static void add_plot_point(int hp, int nside, double dx, double dy) {
41 double xyz[3];
42 healpixl_to_xyzarr(hp, nside, dx, dy, xyz);
43 add_plot_xyz_point(xyz);
44 }
45
46 void plot_point(int hp, int nside, double dx, double dy, char* style) {
47 fprintf(stderr, "xp=[]\n");
48 fprintf(stderr, "yp=[]\n");
49 add_plot_point(hp, nside, dx, dy);
50 fprintf(stderr, "plot(xp, yp, '%s')\n", style);
51 }
52
53 void plot_xyz_point(double* xyz, char* style) {
54 fprintf(stderr, "xp=[]\n");
55 fprintf(stderr, "yp=[]\n");
56 add_plot_xyz_point(xyz);
57 fprintf(stderr, "plot(xp, yp, '%s')\n", style);
58 }
59
60 static void plot_hp_boundary(int hp, int nside, double start, double step, char* style) {
61 double dx, dy;
62 fprintf(stderr, "xp=[]\n");
63 fprintf(stderr, "yp=[]\n");
64 dy = 0.0;
65 for (dx=start; dx<=1.0; dx+=step)
66 add_plot_point(hp, nside, dx, dy);
67 dx = 1.0;
68 for (dy=start; dy<=1.0; dy+=step)
69 add_plot_point(hp, nside, dx, dy);
70 dy = 1.0;
71 for (dx=1.0-start; dx>=0.0; dx-=step)
72 add_plot_point(hp, nside, dx, dy);
73 dx = 0.0;
74 for (dy=1.0-start; dy>=0.0; dy-=step)
75 add_plot_point(hp, nside, dx, dy);
76 dy = 0.0;
77 add_plot_point(hp, nside, dx, dy);
78 fprintf(stderr, "xp,yp = wrapxy(xp,yp)\nplot(xp, yp, '%s')\n", style);
79 }
80
81 static void hpmap(int nside, const char* fn) {
82 #if 0
83 int nhp;
84 #endif
85 double xyz[3];
86 double range;
87 int64_t hps[9];
88 int i;
89 int64_t hp;
90 double dx, dy;
91
92 // pick a point on the edge.
93 //hp = 8;
94 hp = 9;
95 dx = 0.95;
96 dy = 0.0;
97 range = 0.1;
98 /*
99 hp = 6;
100 dx = 0.05;
101 dy = 0.95;
102 range = 0.1;
103 */
104 healpixl_to_xyzarr(hp, nside, dx, dy, xyz);
105
106 for (i=0; i<12*nside*nside; i++) {
107 plot_hp_boundary(i, nside, 0.005, 0.01, "b-");
108 }
109
110 #if 0
111 nhp = healpix_get_neighbours_within_range(xyz, range, hps, nside);
112 assert(nhp >= 1);
113 assert(nhp <= 9);
114 #else
115 (void)healpix_get_neighbours_within_range(xyz, range, hps, nside);
116 #endif
117
118 /*
119 for (i=0; i<nhp; i++) {
120 printf("in range: %i\n", hps[i]);
121 plot_hp_boundary(hps[i], nside, 0.005, 0.01, "b-");
122 }
123 plot_hp_boundary(hp, nside, 0.005, 0.01, "k-");
124 plot_point(hp, nside, dx, dy, "r.");
125 */
126
127 for (i=0; i<12*nside*nside; i++) {
128 fprintf(stderr, "xp=[]; yp=[]\n");
129 add_plot_point(i, nside, 0.5, 0.5);
130 //fprintf(stderr, "text(xp[0], yp[0], '%i', color='b', horizontalalignment='center', verticalalignment='center', bbox=dict(facecolor='w', edgecolor='w'))\n", i);
131 fprintf(stderr, "text(xp[0], yp[0], '%i', color='b', horizontalalignment='center', verticalalignment='center')\n", i);
132 }
133
134 fprintf(stderr, "axis((360, 0, -90, 90))\n");
135 fprintf(stderr, "xlabel('RA (deg)')\n");
136 fprintf(stderr, "ylabel('Dec (deg)')\n");
137 fprintf(stderr, "title('healpixels: nside=%i')\n", nside);
138 fprintf(stderr, "savefig('%s')\n", fn);
139 fprintf(stderr, "clf()\n");
140 }
141
142 void test_make_map(CuTest* ct) {
143 fprintf(stderr, "%s", "from pylab import *\n");
144 fprintf(stderr, "clf()\n"
145 "def wrapxy(x,y):\n"
146 " lastx = x[0]\n"
147 " lasty = y[0]\n"
148 " outx = [lastx]\n"
149 " outy = [lasty]\n"
150 " for xx,yy in zip(x[1:],y[1:]):\n"
151 " if (xx-lastx)**2 + (yy - lasty)**2 > 1.:\n"
152 " if xx < 180:\n"
153 " xx += 360\n"
154 " else:\n"
155 " xx -= 360\n"
156 " outx.append(xx)\n"
157 " outy.append(yy)\n"
158 " lastx = xx\n"
159 " lasty = yy\n"
160 " return (array(outx),array(outy))\n"
161 );
162
163 hpmap(1, "hp.png");
164 hpmap(2, "hp2.png");
165 }
166
167
168 int tst_xyztohpf(CuTest* ct,
169 int hp, int nside, double dx, double dy) {
170 double x,y,z;
171 double outdx, outdy;
172 int outhp;
173 double outx,outy,outz;
174 double dist;
175 healpixl_to_xyz(hp, nside, dx, dy, &x, &y, &z);
176 outhp = xyztohealpixlf(x, y, z, nside, &outdx, &outdy);
177 healpixl_to_xyz(outhp, nside, outdx, outdy, &outx, &outy, &outz);
178 dist = sqrt(MAX(0, square(x-outx) + square(y-outy) + square(z-outz)));
179 printf("true/computed:\n"
180 "hp: %d / %d\n"
181 "dx: %.20g / %.20g\n"
182 "dy: %.20g / %.20g\n"
183 "x: %g / %g\n"
184 "y: %g / %g\n"
185 "z: %g / %g\n"
186 "dist: %g\n\n",
187 (int)hp, (int)outhp, dx, outdx, dy, outdy,
188 x, outx, y, outy, z, outz, dist);
189
190 if (dist > 1e-6) {
191 double a, b;
192 double outa, outb;
193 a = xy2ra(x,y) / (2.0 * M_PI);
194 b = z2dec(z) / (M_PI);
195 outa = xy2ra(outx, outy) / (2.0 * M_PI);
196 outb = z2dec(outz) / (M_PI);
197 fprintf(stderr,
198 "plot([%g, %g],[%g, %g],'r.-')\n", a, outa, b, outb);
199 fprintf(stderr,
200 "text(%g, %g, \"(%g,%g)\")\n",
201 a, b, dx, dy);
202 }
203
204 CuAssertIntEquals(ct, 1, (dist < 1e-6)?1:0);
205 return (dist > 1e-6);
206 }
207
208 void tEst_xyztohpf(CuTest* ct) {
209 double dx, dy;
210 int hp;
211 int nside;
212 double step = 0.1;
213 double a, b;
214 nside = 1;
215
216 fprintf(stderr, "%s", "from pylab import plot,text,savefig,clf\n");
217 fprintf(stderr, "clf()\n");
218
219 /*
220 Plot the grid of healpixes with dx,dy=step steps.
221 */
222 step = 0.25;
223 //for (hp=0; hp<12*nside*nside; hp++) {
224 for (hp=0; hp<1*nside*nside; hp++) {
225 double x,y,z;
226 for (dx=0.0; dx<=1.05; dx+=step) {
227 fprintf(stderr, "xp=[]\n");
228 fprintf(stderr, "yp=[]\n");
229 for (dy=0.0; dy<=1.05; dy+=step) {
230 healpixl_to_xyz(hp, nside, dx, dy, &x, &y, &z);
231 a = xy2ra(x,y) / (2.0 * M_PI);
232 b = z2dec(z) / (M_PI);
233 fprintf(stderr, "xp.append(%g)\n", a);
234 fprintf(stderr, "yp.append(%g)\n", b);
235 }
236 fprintf(stderr, "plot(xp, yp, 'k-')\n");
237 }
238 for (dy=0.0; dy<=1.05; dy+=step) {
239 fprintf(stderr, "xp=[]\n");
240 fprintf(stderr, "yp=[]\n");
241 for (dx=0.0; dx<=1.0; dx+=step) {
242 healpixl_to_xyz(hp, nside, dx, dy, &x, &y, &z);
243 a = xy2ra(x,y) / (2.0 * M_PI);
244 b = z2dec(z) / (M_PI);
245 fprintf(stderr, "xp.append(%g)\n", a);
246 fprintf(stderr, "yp.append(%g)\n", b);
247 }
248 fprintf(stderr, "plot(xp, yp, 'k-')\n");
249 }
250 }
251
252 step = 0.5;
253 /*
254 Plot places where the conversion screws up.
255 */
256 for (hp=0; hp<12*nside*nside; hp++) {
257 for (dx=0.0; dx<=1.01; dx+=step) {
258 for (dy=0.0; dy<=1.01; dy+=step) {
259 tst_xyztohpf(ct, hp, nside, dx, dy);
260 }
261 }
262 }
263 fprintf(stderr, "savefig('plot.png')\n");
264
265 }
266
267 static void tst_neighbours(CuTest* ct, int pix, int* true_neigh, int true_nn,
268 int Nside) {
269 int neigh[8];
270 int nn;
271 int i;
272 for (i=0; i<8; i++)
273 neigh[i] = -1;
274 healpixl_get_neighbours(pix, neigh, Nside);
275 /*
276 printf("true(%i) : [ ", pix);
277 for (i=0; i<true_nn; i++)
278 printf("%u, ", true_neigh[i]);
279 printf("]\n");
280 printf("got (%i) : [ ", pix);
281 for (i=0; i<nn; i++)
282 printf("%u, ", neigh[i]);
283 printf("]\n");
284 */
285
286 for (i=0; i<true_nn; i++)
287 CuAssertIntEquals(ct, true_neigh[i], neigh[i]);
288 }
289
290 static void tst_nested(CuTest* ct, int pix, int* true_neigh, int true_nn,
291 int Nside) {
292 int i;
293 int truexy[8];
294 int xypix;
295
296 /*
297 printf("nested true(%i) : [ ", pix);
298 for (i=0; i<true_nn; i++)
299 printf("%u ", true_neigh[i]);
300 printf("]\n");
301 */
302
303 CuAssert(ct, "true_nn <= 8", true_nn <= 8);
304 for (i=0; i<true_nn; i++) {
305 truexy[i] = healpixl_nested_to_xy(true_neigh[i], Nside);
306 CuAssertIntEquals(ct, true_neigh[i], healpixl_xy_to_nested(truexy[i], Nside));
307 }
308 xypix = healpixl_nested_to_xy(pix, Nside);
309 CuAssertIntEquals(ct, pix, healpixl_xy_to_nested(xypix, Nside));
310
311 tst_neighbours(ct, xypix, truexy, true_nn, Nside);
312 }
313
314 void print_node(double z, double phi, int Nside) {
315 double ra, dec;
316 int hp;
317 int neigh[8];
318 int k;
319
320 double scale = 10.0;
321
322 ra = phi;
323 dec = asin(z);
324 while (ra < 0.0)
325 ra += 2.0 * M_PI;
326 while (ra > 2.0 * M_PI)
327 ra -= 2.0 * M_PI;
328
329 // find its healpix.
330 hp = radec_to_healpixl(ra, dec, Nside);
331 // find its neighbourhood.
332 healpixl_get_neighbours(hp, neigh, Nside);
333 fprintf(stderr, " N%i [ label=\"%i\", pos=\"%g,%g!\" ];\n", hp, hp,
334 scale * ra/M_PI, scale * z);
335 for (k=0; k<8; k++) {
336 fprintf(stderr, " N%i -- N%i\n", hp, neigh[k]);
337 }
338 }
339
340 void test_healpix_distance_to_radec(CuTest *ct) {
341 double d;
342 double rd[2];
343
344 d = healpix_distance_to_radec(4, 1, 0, 0, NULL);
345 CuAssertDblEquals(ct, 0, d, 0);
346 d = healpix_distance_to_radec(4, 1, 45, 0, NULL);
347 CuAssertDblEquals(ct, 0, d, 0);
348 d = healpix_distance_to_radec(4, 1, 45+1, 0, NULL);
349 CuAssertDblEquals(ct, 1, d, 1e-9);
350 d = healpix_distance_to_radec(4, 1, 45+1, 0+1, NULL);
351 CuAssertDblEquals(ct, 1.414, d, 1e-3);
352
353 d = healpix_distance_to_radec(4, 1, 45+10, 0, NULL);
354 CuAssertDblEquals(ct, 10, d, 1e-9);
355
356 // top corner
357 d = healpix_distance_to_radec(4, 1, 0, rad2deg(asin(2.0/3.0)), NULL);
358 CuAssertDblEquals(ct, 0, d, 1e-9);
359
360 d = healpix_distance_to_radec(4, 1, 0, 1 + rad2deg(asin(2.0/3.0)), NULL);
361 CuAssertDblEquals(ct, 1, d, 1e-9);
362
363 d = healpix_distance_to_radec(4, 1, -45-10, -10, NULL);
364 CuAssertDblEquals(ct, 14.106044, d, 1e-6);
365
366 d = healpix_distance_to_radec(10, 1, 225, 5, NULL);
367 CuAssertDblEquals(ct, 5, d, 1e-6);
368
369 d = healpix_distance_to_radec(44, 2, 300, -50, NULL);
370 CuAssertDblEquals(ct, 3.007643, d, 1e-6);
371
372 d = healpix_distance_to_radec(45, 2, 310, -50, NULL);
373 CuAssertDblEquals(ct, 1.873942, d, 1e-6);
374
375 // south-polar hp, north pole.
376 d = healpix_distance_to_radec(36, 2, 180, 90, NULL);
377 // The hp corner is -41.8 deg; add 90.
378 CuAssertDblEquals(ct, 131.810, d, 1e-3);
379
380 // just south of equator to nearly across the sphere
381 d = healpix_distance_to_radec(35, 2, 225, 20, NULL);
382 // this one actually has the midpoint further than A and B.
383 CuAssertDblEquals(ct, 158.189685, d, 1e-6);
384
385 /*
386 xyz[0] = -100.0;
387 ra = dec = -1.0;
388 d = healpix_distance_to_xyz(4, 1, 0, 0, xyz);
389 xyzarr2radecdeg(xyz, &ra, &dec);
390 CuAssertDblEquals(ct, 0, ra, 0);
391 CuAssertDblEquals(ct, 0, dec, 0);
392 */
393 rd[0] = rd[1] = -1.0;
394 d = healpix_distance_to_radec(4, 1, 0, 0, rd);
395 CuAssertDblEquals(ct, 0, rd[0], 0);
396 CuAssertDblEquals(ct, 0, rd[1], 0);
397
398
399 /*
400 xyz[0] = -100.0;
401 ra = dec = -1.0;
402 d = healpix_distance_to_xyz(4, 1, 45, 0, xyz);
403 xyzarr2radecdeg(xyz, &ra, &dec);
404 CuAssertDblEquals(ct, 45, ra, 0);
405 CuAssertDblEquals(ct, 0, dec, 0);
406 */
407
408 rd[0] = rd[1] = -1.0;
409 d = healpix_distance_to_radec(4, 1, 45+1, 0, rd);
410 //CuAssertDblEquals(ct, 45, rd[0], 0);
411 CuAssertDblEquals(ct, 45, rd[0], 1e-8);
412 CuAssertDblEquals(ct, 0, rd[1], 1e-8);
413
414 d = healpix_distance_to_radec(4, 1, 45+1, 0+1, rd);
415 //CuAssertDblEquals(ct, 45, rd[0], 0);
416 CuAssertDblEquals(ct, 45, rd[0], 1e-8);
417 CuAssertDblEquals(ct, 0, rd[1], 0);
418 // really??
419
420 d = healpix_distance_to_radec(4, 1, 20, 25, rd);
421 CuAssertDblEquals(ct, d, 2.297298, 1e-6);
422 CuAssertDblEquals(ct, 18.200995, rd[0], 1e-6);
423 CuAssertDblEquals(ct, 23.392159, rd[1], 1e-6);
424
425 }
426
427 void test_healpix_neighbours(CuTest *ct) {
428 int n0[] = { 1,3,2,71,69,143,90,91 };
429 int n5[] = { 26,27,7,6,4,94,95 };
430 int n13[] = { 30,31,15,14,12,6,7,27 };
431 int n15[] = { 31,47,63,61,14,12,13,30 };
432 int n30[] = { 31,15,13,7,27,25,28,29 };
433 int n101[] = { 32,34,103,102,100,174,175,122 };
434 int n127[] = { 58,37,36,126,124,125,56 };
435 int n64[] = { 65,67,66,183,181,138,139 };
436 int n133[] = { 80,82,135,134,132,152,154 };
437 int n148[] = { 149,151,150,147,145,162,168,170 };
438 int n160[] = { 161,163,162,145,144,128,176,178 };
439 int n24[] = { 25,27,26,95,93,87,18,19 };
440 int n42[] = { 43,23,21,111,109,40,41 };
441 int n59[] = { 62,45,39,37,58,56,57,60 };
442 int n191[] = { 74,48,117,116,190,188,189,72 };
443 int n190[] = { 191,117,116,113,187,185,188,189 };
444 int n186[] = { 187,113,112,165,164,184,185 };
445 int n184[] = { 185,187,186,165,164,161,178,179 };
446
447 // These were taken (IIRC) from the Healpix paper, so the healpix
448 // numbers are all in the NESTED scheme.
449
450 tst_nested(ct, 0, n0, sizeof(n0) /sizeof(int), 4);
451 tst_nested(ct, 5, n5, sizeof(n5) /sizeof(int), 4);
452 tst_nested(ct, 13, n13, sizeof(n13) /sizeof(int), 4);
453 tst_nested(ct, 15, n15, sizeof(n15) /sizeof(int), 4);
454 tst_nested(ct, 30, n30, sizeof(n30) /sizeof(int), 4);
455 tst_nested(ct, 101, n101, sizeof(n101)/sizeof(int), 4);
456 tst_nested(ct, 127, n127, sizeof(n127)/sizeof(int), 4);
457 tst_nested(ct, 64, n64, sizeof(n64) /sizeof(int), 4);
458 tst_nested(ct, 133, n133, sizeof(n133)/sizeof(int), 4);
459 tst_nested(ct, 148, n148, sizeof(n148)/sizeof(int), 4);
460 tst_nested(ct, 160, n160, sizeof(n160)/sizeof(int), 4);
461 tst_nested(ct, 24, n24, sizeof(n24) /sizeof(int), 4);
462 tst_nested(ct, 42, n42, sizeof(n42) /sizeof(int), 4);
463 tst_nested(ct, 59, n59, sizeof(n59) /sizeof(int), 4);
464 tst_nested(ct, 191, n191, sizeof(n191)/sizeof(int), 4);
465 tst_nested(ct, 190, n190, sizeof(n190)/sizeof(int), 4);
466 tst_nested(ct, 186, n186, sizeof(n186)/sizeof(int), 4);
467 tst_nested(ct, 184, n184, sizeof(n184)/sizeof(int), 4);
468 }
469
470 /*
471 void pnprime_to_xy(int, int*, int*, int);
472 int xy_to_pnprime(int, int, int);
473
474 void tst_healpix_pnprime_to_xy(CuTest *ct) {
475 int px,py;
476 pnprime_to_xy(6, &px, &py, 3);
477 CuAssertIntEquals(ct, px, 2);
478 CuAssertIntEquals(ct, py, 0);
479 pnprime_to_xy(8, &px, &py, 3);
480 CuAssertIntEquals(ct, px, 2);
481 CuAssertIntEquals(ct, py, 2);
482 pnprime_to_xy(0, &px, &py, 3);
483 CuAssertIntEquals(ct, px, 0);
484 CuAssertIntEquals(ct, py, 0);
485 pnprime_to_xy(2, &px, &py, 3);
486 CuAssertIntEquals(ct, px, 0);
487 CuAssertIntEquals(ct, py, 2);
488 pnprime_to_xy(4, &px, &py, 3);
489 CuAssertIntEquals(ct, px, 1);
490 CuAssertIntEquals(ct, py, 1);
491 }
492
493 void tst_healpix_xy_to_pnprime(CuTest *ct) {
494 CuAssertIntEquals(ct, xy_to_pnprime(0,0,3), 0);
495 CuAssertIntEquals(ct, xy_to_pnprime(1,0,3), 3);
496 CuAssertIntEquals(ct, xy_to_pnprime(2,0,3), 6);
497 CuAssertIntEquals(ct, xy_to_pnprime(0,1,3), 1);
498 CuAssertIntEquals(ct, xy_to_pnprime(1,1,3), 4);
499 CuAssertIntEquals(ct, xy_to_pnprime(2,1,3), 7);
500 CuAssertIntEquals(ct, xy_to_pnprime(0,2,3), 2);
501 CuAssertIntEquals(ct, xy_to_pnprime(1,2,3), 5);
502 CuAssertIntEquals(ct, xy_to_pnprime(2,2,3), 8);
503 }
504 */
505 void print_test_healpix_output(int Nside) {
506
507 int i, j;
508 double z;
509 double phi;
510 fprintf(stderr, "graph Nside4 {\n");
511
512 // north polar
513 for (i=1; i<=Nside; i++) {
514 for (j=1; j<=(4*i); j++) {
515 // find the center of the pixel in ring i
516 // and longitude j.
517 z = 1.0 - square((double)i / (double)Nside)/3.0;
518 phi = M_PI / (2.0 * i) * ((double)j - 0.5);
519 fprintf(stderr, " // North polar, i=%i, j=%i. z=%g, phi=%g\n", i, j, z, phi);
520 print_node(z, phi, Nside);
521 }
522 }
523 // south polar
524 for (i=1; i<=Nside; i++) {
525 for (j=1; j<=(4*i); j++) {
526 z = 1.0 - square((double)i / (double)Nside)/3.0;
527 z *= -1.0;
528 phi = M_PI / (2.0 * i) * ((double)j - 0.5);
529 fprintf(stderr, " // South polar, i=%i, j=%i. z=%g, phi=%g\n", i, j, z, phi);
530 print_node(z, phi, Nside);
531 }
532 }
533 // north equatorial
534 for (i=Nside+1; i<=2*Nside; i++) {
535 for (j=1; j<=(4*Nside); j++) {
536 int s;
537 z = 4.0/3.0 - 2.0 * i / (3.0 * Nside);
538 s = (i - Nside + 1) % 2;
539 s = (s + 2) % 2;
540 phi = M_PI / (2.0 * Nside) * ((double)j - (double)s / 2.0);
541 fprintf(stderr, " // North equatorial, i=%i, j=%i. z=%g, phi=%g, s=%i\n", i, j, z, phi, s);
542 print_node(z, phi, Nside);
543 }
544 }
545 // south equatorial
546 for (i=Nside+1; i<2*Nside; i++) {
547 for (j=1; j<=(4*Nside); j++) {
548 int s;
549 z = 4.0/3.0 - 2.0 * i / (3.0 * Nside);
550 z *= -1.0;
551 s = (i - Nside + 1) % 2;
552 s = (s + 2) % 2;
553 phi = M_PI / (2.0 * Nside) * ((double)j - s / 2.0);
554 fprintf(stderr, " // South equatorial, i=%i, j=%i. z=%g, phi=%g, s=%i\n", i, j, z, phi, s);
555 print_node(z, phi, Nside);
556 }
557 }
558
559 fprintf(stderr, " node [ shape=point ]\n");
560 fprintf(stderr, " C0 [ pos=\"0,-10!\" ];\n");
561 fprintf(stderr, " C1 [ pos=\"20,-10!\" ];\n");
562 fprintf(stderr, " C2 [ pos=\"20,10!\" ];\n");
563 fprintf(stderr, " C3 [ pos=\"0,10!\" ];\n");
564 fprintf(stderr, " C0 -- C1 -- C2 -- C3 -- C0\n");
565 fprintf(stderr, "}\n");
566 }
567
568 void print_healpix_grid(int Nside) {
569 int i;
570 int j;
571 int N = 500;
572
573 fprintf(stderr, "x%i=[", Nside);
574 for (i=0; i<N; i++) {
575 for (j=0; j<N; j++) {
576 fprintf(stderr, "%i ", radec_to_healpixl(i*2*M_PI/N, M_PI*(j-N/2)/N, Nside));
577 }
578 fprintf(stderr, ";");
579 }
580 fprintf(stderr, "];\n\n");
581 fflush(stderr);
582 }
583
584 void print_healpix_borders(int Nside) {
585 int i;
586 int j;
587 int N = 1;
588
589 fprintf(stderr, "x%i=[", Nside);
590 for (i=0; i<N; i++) {
591 for (j=0; j<N; j++) {
592 fprintf(stderr, "%i ", radec_to_healpixl(i*2*M_PI/N, M_PI*(j-N/2)/N, Nside));
593 }
594 fprintf(stderr, ";");
595 }
596 fprintf(stderr, "];\n\n");
597 fflush(stderr);
598 }
599
600 void test_big_nside(CuTest* ct) {
601 double ra1, dec1, ra2, dec2;
602 int Nside = 2097152;
603 int64_t hp;
604 double dx, dy;
605
606 // just a random val...
607 ra1 = 43.7;
608 dec1 = -38.4;
609
610 hp = radecdegtohealpixl(ra1, dec1, Nside);
611 healpixl_to_radecdeg(hp, Nside, 0, 0, &ra2, &dec2);
612
613 CuAssertDblEquals(ct, ra1, ra2, arcsec2deg(0.1));
614 CuAssertDblEquals(ct, dec1, dec2, arcsec2deg(0.1));
615
616 // another random val...
617 ra1 = 0.0003;
618 dec1 = 75.3;
619
620 dx = dy = -1.0;
621 hp = radecdegtohealpixlf(ra1, dec1, Nside, &dx, &dy);
622 CuAssert(ct, "dx", dx >= 0.0);
623 CuAssert(ct, "dx", dx <= 1.0);
624 CuAssert(ct, "dy", dy >= 0.0);
625 CuAssert(ct, "dy", dy <= 1.0);
626 healpixl_to_radecdeg(hp, Nside, dx, dy, &ra2, &dec2);
627
628 CuAssertDblEquals(ct, ra1, ra2, arcsec2deg(1e-10));
629 CuAssertDblEquals(ct, dec1, dec2, arcsec2deg(1e-10));
630
631 printf("RA,Dec difference: %g, %g arcsec\n", deg2arcsec(ra2-ra1), deg2arcsec(dec2-dec1));
632 }
633
634 void test_distortion_at_pole(CuTest* ct) {
635 // not really a test of the code, more of healpix itself...
636 double ra1, dec1, ra2, dec2, ra3, dec3, ra4, dec4;
637 int Nside = 2097152;
638 int64_t hp;
639 double d1, d2, d3, d4, d5, d6;
640 double testras [] = { 0.0, 45.0, 0.0, 0.0 };
641 double testdecs[] = { 90.0, 50.0, 40.0, 0.0 };
642 char* testnames[] = { "north pole", "mid-polar", "mid-equatorial",
643 "equator" };
644 double ra, dec;
645 int i;
646
647 for (i=0; i<sizeof(testras)/sizeof(double); i++) {
648 ra = testras[i];
649 dec = testdecs[i];
650
651 hp = radecdegtohealpixl(ra, dec, Nside);
652
653 healpixl_to_radecdeg(hp, Nside, 0, 0, &ra1, &dec1);
654 healpixl_to_radecdeg(hp, Nside, 0, 1, &ra2, &dec2);
655 healpixl_to_radecdeg(hp, Nside, 1, 1, &ra3, &dec3);
656 healpixl_to_radecdeg(hp, Nside, 1, 0, &ra4, &dec4);
657
658 // FIXME: implement or remove
659 // At the moment `arcsec_between_radecdeg` is not included here.
660 // // sides
661 // d1 = arcsec_between_radecdeg(ra1, dec1, ra2, dec2);
662 // d2 = arcsec_between_radecdeg(ra2, dec2, ra3, dec3);
663 // d3 = arcsec_between_radecdeg(ra3, dec3, ra4, dec4);
664 // d4 = arcsec_between_radecdeg(ra4, dec4, ra1, dec1);
665 // // diagonals
666 // d5 = arcsec_between_radecdeg(ra1, dec1, ra3, dec3);
667 // d6 = arcsec_between_radecdeg(ra2, dec2, ra4, dec4);
668
669 // printf("%-15s (%4.1f, %4.1f): %-5.3f, %-5.3f, %-5.3f, %-5.3f / %-5.3f, %-5.3f\n", testnames[i], ra, dec, d1, d2, d3, d4, d5, d6);
670 }
671 }
672
673
674 #if defined(TEST_HEALPIX_MAIN)
675 int main(int argc, char** args) {
676
677 /* Run all tests */
678 CuString *output = CuStringNew();
679 CuSuite* suite = CuSuiteNew();
680
681 /* Add new tests here */
682 SUITE_ADD_TEST(suite, test_healpix_neighbours);
683 SUITE_ADD_TEST(suite, test_healpix_distance_to_radec);
684
685 /* Run the suite, collect results and display */
686 CuSuiteRun(suite);
687 CuSuiteSummary(suite, output);
688 CuSuiteDetails(suite, output);
689 printf("%s\n", output->buffer);
690
691 /*
692 print_healpix_grid(1);
693 print_healpix_grid(2);
694 print_healpix_grid(3);
695 print_healpix_grid(4);
696 print_healpix_grid(5);
697 */
698
699 //print_test_healpix_output();
700
701 /*
702 int rastep, decstep;
703 int Nra = 100;
704 int Ndec = 100;
705 double ra, dec;
706 int healpix;
707 printf("radechealpix=zeros(%i,3);\n", Nra*Ndec);
708 for (rastep=0; rastep<Nra; rastep++) {
709 ra = ((double)rastep / (double)(Nra-1)) * 2.0 * M_PI;
710 for (decstep=0; decstep<Ndec; decstep++) {
711 dec = (((double)decstep / (double)(Ndec-1)) * M_PI) - M_PI/2.0;
712 healpix = radec_to_healpixl(ra, dec);
713 printf("radechealpix(%i,:)=[%g,%g,%i];\n",
714 (rastep*Ndec) + decstep + 1, ra, dec, healpix);
715 }
716 }
717 printf("ra=radechealpix(:,1);\n");
718 printf("dec=radechealpix(:,2);\n");
719 printf("healpix=radechealpix(:,3);\n");
720 return 0;
721 */
722 return 0;
723 }
724 #endif