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 STAR_KD_H
7 #define STAR_KD_H
8 
9 #include <stdint.h>
10 #include <stdio.h>
11 
12 #include "astrometry/kdtree.h"
13 #include "astrometry/kdtree_fits_io.h"
14 #include "astrometry/fitstable.h"
15 #include "astrometry/keywords.h"
16 #include "astrometry/anqfits.h"
17 
18 #ifdef SWIG
19 // this keyword (from keywords.h) confuses swig
20 #define Malloc
21 #endif
22 
23 #define AN_FILETYPE_STARTREE "SKDT"
24 
25 #define AN_FILETYPE_TAGALONG "TAGALONG"
26 
27 #define STARTREE_NAME "stars"
28 
29 typedef struct {
30     kdtree_t* tree;
31     qfits_header* header;
32     int* inverse_perm;
33     uint8_t* sweep;
34 
35     // reading or writing?
36     int writing;
37 
38     // reading: tagged-along data (a FITS BINTABLE with one row per star,
39     // in the same order); access this via startree_get_tagalong() ONLY!
40     fitstable_t* tagalong;
41 } startree_t;
42 
43 startree_t* startree_open(const char* fn);
44 
45 startree_t* startree_open_fits(anqfits_t* fits);
46 
47 /**
48  Searches for stars within a radius of a point.
49 
50  xyzcenter: double[3]: unit-sphere coordinates of point; see
51  starutil.h : radecdeg2xyzarr() to convert RA,Decs to this form.
52 
53  radius2: radius-squared on the unit sphere; see starutil.h :
54  deg2distsq() or arcsec2distsq().
55 
56  xyzresults: if non-NULL, returns the xyz positions of the stars that
57  are found, in a newly-allocated array.
58 
59  radecresults: if non-NULL, returns the RA,Dec positions (in degrees)
60  of the stars within range.
61 
62  starinds: if non-NULL, returns the indices of stars within range.
63  This can be used to retrieve extra information about the stars, using
64  the 'startree_get_data_column()' function.
65 
66  */
67 void startree_search_for(const startree_t* s, const double* xyzcenter, double radius2,
68                          double** xyzresults, double** radecresults,
69                          int** starinds, int* nresults);
70 
71 /**
72  RA, Dec, and radius in degrees.  Otherwise same as startree_search_for().
73  */
74 void startree_search_for_radec(const startree_t* s, double ra, double dec, double radius,
75                                double** xyzresults, double** radecresults,
76                                int** starinds, int* nresults);
77 
78 void startree_search(const startree_t* s, const double* xyzcenter, double radius2,
79                      double** xyzresults, double** radecresults, int* nresults);
80 
81 /**
82  Reads a column of data from the "tag-along" table.
83 
84  Get the "inds" and "N" from "startree_search" or "startree_search_for".
85 
86  To get all entries, set "inds" = NULL and N = startree_N().
87 
88  The return value is a newly-allocated array of size N.  It should be
89  freed using "startree_free_data_column"
90  */
91 Malloc
92 double* startree_get_data_column(startree_t* s, const char* colname, const int* indices, int N);
93 
94 /**
95  Same as startree_get_data_column but for int64_t.  Don't you love C templating?
96  */
97 Malloc
98 int64_t* startree_get_data_column_int64(startree_t* s, const char* colname, const int* indices, int N);
99 
100 /**
101  Reads a column of data from the "tag-along" table.
102 
103  The column may be an array (that is, each row contains multiple
104  entries); the array size is placed in "arraysize".
105 
106  The array entries
107  */
108 Malloc
109 double* startree_get_data_column_array(startree_t* s, const char* colname, const int* indices, int N, int* arraysize);
110 
111 void startree_free_data_column(startree_t* s, double* d);
112 
113 
114 
115 
116 anbool startree_has_tagalong(startree_t* s);
117 
118 fitstable_t* startree_get_tagalong(startree_t* s);
119 
120 /*
121  Returns a string-list of the names of the columns in the "tagalong" table of this star kdtree.
122  If you pass in a non-NULL "lst", the names will be added to that list; otherwise, a new sl*
123  will be allocated (free it with sl_free2()).
124 
125  If you want to avoid "sl*", see:
126  -- startree_get_tagalong_N_columns(s)
127  -- startree_get_tagalong_column_name(s, i)
128  */
129 sl* startree_get_tagalong_column_names(startree_t* s, sl* lst);
130 
131 /**
132  Returns the number of columns in the tagalong table.
133  */
134 int startree_get_tagalong_N_columns(startree_t* s);
135 
136 /**
137  Returns the name of the 'i'th column in the tagalong table.
138  The lifetime of the returned string is the lifetime of this starkd.
139  */
140 const char* startree_get_tagalong_column_name(startree_t* s, int i);
141 
142 /**
143  Returns the FITS type of the 'i'th column in the tagalong table.
144  */
145 tfits_type startree_get_tagalong_column_fits_type(startree_t* s, int i);
146 
147 /**
148  Returns the array size of the 'i'th column in the tagalong table.
149  For scalar columns, this is 1.
150  */
151 int startree_get_tagalong_column_array_size(startree_t* s, int i);
152 
153 
154 /*
155  Retrieve parameters of the cut-an process, if they are available.
156  Older index files may not have these header cards.
157  */
158 // healpix nside, or -1
159 int startree_get_cut_nside(const startree_t* s);
160 int startree_get_cut_nsweeps(const startree_t* s);
161 // in arcsec; 0 if none.
162 double startree_get_cut_dedup(const startree_t* s);
163 // band (one of several static strings), or NULL
164 char* startree_get_cut_band(const startree_t* s);
165 // margin, in healpix, or -1
166 int startree_get_cut_margin(const startree_t* s);
167 
168 double startree_get_jitter(const startree_t* s);
169 
170 void startree_set_jitter(startree_t* s, double jitter_arcsec);
171 
172 //uint64_t startree_get_starid(const startree_t* s, int ind);
173 
174 // returns the sweep number of star 'ind', or -1 if the index is out of bounds
175 // or the tree has no sweep numbers.
176 int startree_get_sweep(const startree_t* s, int ind);
177 
178 int startree_N(const startree_t* s);
179 
180 int startree_nodes(const startree_t* s);
181 
182 int startree_D(const startree_t* s);
183 
184 qfits_header* startree_header(const startree_t* s);
185 
186 int startree_get(startree_t* s, int starid, double* posn);
187 
188 int startree_get_radec(startree_t* s, int starid, double* ra, double* dec);
189 
190 int startree_close(startree_t* s);
191 
192 void startree_compute_inverse_perm(startree_t* s);
193 
194 int startree_check_inverse_perm(startree_t* s);
195 
196 // for writing
197 startree_t* startree_new(void);
198 
199 int startree_write_to_file(startree_t* s, const char* fn);
200 
201 int startree_write_to_file_flipped(startree_t* s, const char* fn);
202 
203 int startree_append_to(startree_t* s, FILE* fid);
204 
205 #endif
206