1 /*
2  # This file is part of the Astrometry.net suite.
3  # Licensed under a 3-clause BSD style license - see LICENSE
4  */
5 #include <math.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 
11 #include "cutest.h"
12 #include "hd.h"
13 #include "starutil.h"
14 #include "ioutils.h"
15 
test_hd_1(CuTest * tc)16 void test_hd_1(CuTest* tc) {
17     hd_catalog_t* hdcat;
18     int* invperm;
19     int* present;
20     int ind, i, N;
21     double xyz[3];
22     double ra, dec;
23     int strangehds[] = { 40142, 40441, 40672, 40746, 40763, 40764,
24                          104176, 104193, 163635, 224698, 224699,
25                          129371 };
26 
27     if (!file_readable("hd.fits")) {
28         printf("File \"hd.fits\" does not exist; test skipped.\n");
29         return;
30     }
31 
32     hdcat = henry_draper_open("hd.fits");
33     CuAssertPtrNotNull(tc, hdcat);
34 
35     N = hdcat->kd->ndata;
36     invperm = calloc(N, sizeof(int));
37     CuAssertPtrNotNull(tc, invperm);
38 
39     CuAssertIntEquals(tc, 0, kdtree_check(hdcat->kd));
40 
41     kdtree_inverse_permutation(hdcat->kd, invperm);
42 
43     present = calloc(N, sizeof(int));
44     for (i=0; i<N; i++) {
45         CuAssert(tc, "invperm in range", invperm[i] < N);
46         present[invperm[i]]++;
47     }
48 
49     for (i=0; i<N; i++) {
50         CuAssertIntEquals(tc, 1, present[i]);
51     }
52     free(present);
53 
54     // Where is "HD n" ?
55     for (i=0; i<10; i++) {
56         bl* res;
57         int j;
58 
59         ind = invperm[i];
60         kdtree_copy_data_double(hdcat->kd, ind, 1, xyz);
61         xyzarr2radecdeg(xyz, &ra, &dec);
62         printf("HD %i: RA,Dec %g, %g\n", i+1, ra, dec);
63 
64         res = henry_draper_get(hdcat, ra, dec, 10.0);
65         CuAssertPtrNotNull(tc, res);
66         for (j=0; j<bl_size(res); j++) {
67             hd_entry_t* hd = bl_access(res, j);
68             printf("res %i: HD %i, RA, Dec %g, %g\n", j, hd->hd, hd->ra, hd->dec);
69         }
70         bl_free(res);
71     }
72 
73     for (i=0; i<sizeof(strangehds)/sizeof(int); i++) {
74         ind = invperm[strangehds[i]-1];
75         kdtree_copy_data_double(hdcat->kd, ind, 1, xyz);
76         xyzarr2radecdeg(xyz, &ra, &dec);
77         printf("HD %i: RA,Dec %g, %g\n", strangehds[i], ra, dec);
78     }
79     free(invperm);
80 
81     henry_draper_close(hdcat);
82 }
83