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 <assert.h>
7 
8 #include "kdtree_fits_io.h"
9 #include "kdtree.h"
10 #include "errors.h"
11 #include "starutil.h"
12 #include "hd.h"
13 
henry_draper_open(const char * fn)14 hd_catalog_t* henry_draper_open(const char* fn) {
15     hd_catalog_t* hd = calloc(1, sizeof(hd_catalog_t));
16     hd->fn = strdup(fn);
17     hd->kd = kdtree_fits_read(hd->fn, NULL, NULL);
18     if (!hd->kd) {
19         ERROR("Failed to read a kdtree from file %s", hd->fn);
20         return NULL;
21     }
22     return hd;
23 }
24 
henry_draper_n(const hd_catalog_t * hd)25 int henry_draper_n(const hd_catalog_t* hd) {
26     assert(hd);
27     assert(hd->kd);
28     return kdtree_n(hd->kd);
29 }
30 
henry_draper_close(hd_catalog_t * hd)31 void henry_draper_close(hd_catalog_t* hd) {
32     if (!hd) return;
33     free(hd->fn);
34     kdtree_fits_close(hd->kd);
35     free(hd);
36 }
37 
henry_draper_get(hd_catalog_t * hdcat,double racenter,double deccenter,double r_arcsec)38 bl* henry_draper_get(hd_catalog_t* hdcat,
39                      double racenter, double deccenter,
40                      double r_arcsec) {
41     double r2;
42     double xyz[3];
43     kdtree_qres_t* q;
44     bl* res;
45     int i;
46     hd_entry_t hd;
47 
48     radecdeg2xyzarr(racenter, deccenter, xyz);
49     r2 = arcsec2distsq(r_arcsec);
50     q = kdtree_rangesearch(hdcat->kd, xyz, r2);
51     if (!q) {
52         return NULL;
53     }
54 
55     res = bl_new(256, sizeof(hd_entry_t));
56     for (i=0; i<q->nres; i++) {
57         double* pt = q->results.d + i*3;
58         xyzarr2radecdeg(pt, &(hd.ra), &(hd.dec));
59         hd.hd = q->inds[i] + 1;
60         bl_append(res, &hd);
61     }
62 
63     kdtree_free_query(q);
64 
65     return res;
66 }
67 
68