table: astro_object - added union for char designation.
[libastrodb.git] / examples / sky2k.c
blob3505b205ab9941ed9168b917dd8a8ff2efbcf143
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2008 Liam Girdwood
19 #define _GNU_SOURCE /* for NAN */
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <sys/time.h>
24 #include <stdio.h>
25 #include <math.h>
27 #include <libastrodb/astrodb.h>
29 static struct timeval start, end;
31 inline static void start_timer(void)
33 gettimeofday(&start, NULL);
36 static void end_timer(int objects, int bytes)
38 double usecs;
40 gettimeofday(&end, NULL);
41 usecs = (end.tv_sec * 1000000 + end.tv_usec) -
42 (start.tv_sec * 1000000 + start.tv_usec);
44 if (bytes)
45 printf ("Time(secs) %f : %9.0f objects per sec %9.0f bytes"
46 " per secs\n", usecs / 1000000.0,
47 (1000000.0 / usecs) * objects, (1000000.0 / usecs) * bytes);
48 else
49 printf("Time(secs) %f : %9.0f objects per sec\n",
50 usecs / 1000000.0, (1000000.0 / usecs) * objects);
54 * Search for all stars with:-
56 * ((pmRA < 0.2 && pmRA > 0.05) ||
57 * (pmDE < 0.2 && pmDE > 0.05)) ||
58 * (RV < 40 && RV > 25)
60 * Uses Reverse Polish Notation to define search parameters
62 static int search1(struct astrodb_table * table)
64 struct astrodb_slist *res = NULL;
65 struct astrodb_search *srch;
66 int err;
68 printf("Searching for high pm or RV objects\n");
70 srch = astrodb_search_create(table);
71 if (astrodb_search_add_comparator(srch, "pmRA", ADB_COMP_LT, "0.2"))
72 printf("failed to add comp pmRA\n");
73 if (astrodb_search_add_comparator(srch, "pmRA", ADB_COMP_GT, "0.05"))
74 printf("failed to add comp pmRA\n");
75 if (astrodb_search_add_operator(srch, ADB_OP_AND))
76 printf("failed to add op and\n");
77 if (astrodb_search_add_comparator(srch, "pmDE", ADB_COMP_LT, "0.2"))
78 printf("failed to add comp pmDE\n");
79 if (astrodb_search_add_comparator(srch, "pmDE", ADB_COMP_GT, "0.05"))
80 printf("failed to add comp pmDE\n");
81 if (astrodb_search_add_operator(srch, ADB_OP_AND))
82 printf("failed to add op and\n");
83 if (astrodb_search_add_comparator(srch, "RV", ADB_COMP_LT, "40"))
84 printf("failed to add comp RV\n");
85 if (astrodb_search_add_comparator(srch, "RV", ADB_COMP_GT, "25"))
86 printf("failed to add comp RV\n");
87 if (astrodb_search_add_operator(srch, ADB_OP_AND))
88 printf("failed to add op and\n");
89 if (astrodb_search_add_operator(srch, ADB_OP_OR))
90 printf("failed to add op or\n");
92 start_timer();
93 if ((err = astrodb_search_get_results(srch, &res, ADB_SMEM)) < 0) {
94 printf("Search init failed %d\n", err);
95 astrodb_search_free(srch);
96 return err;
98 end_timer(astrodb_search_get_tests(srch), 0);
100 printf(" Search got %d objects out of %d tests\n",
101 astrodb_search_get_hits(srch), astrodb_search_get_tests(srch));
102 astrodb_search_put_results(res);
103 astrodb_search_free(srch);
104 return 0;
109 * Search for all G type stars
110 * Uses Wildcard "G5*" to match with Sp
112 static int search2(struct astrodb_table * table)
114 struct astrodb_slist *res = NULL;
115 struct astrodb_search *srch;
116 int err;
118 srch = astrodb_search_create(table);
119 if (astrodb_search_add_comparator(srch, "Sp", ADB_COMP_LT, "G5*"))
120 printf("failed to add comp G5*\n");
121 if (astrodb_search_add_operator(srch, ADB_OP_OR))
122 printf("failed to add op or\n");
124 printf("Searching for G5 class objects\n");
125 start_timer();
126 err = astrodb_search_get_results(srch, &res, ADB_SMEM);
127 if (err < 0) {
128 printf("Search init failed %d\n", err);
129 astrodb_search_free(srch);
130 return err;
133 end_timer(astrodb_search_get_tests(srch), 0);
134 printf(" Search got %d objects out of %d tests\n",
135 astrodb_search_get_hits(srch),
136 astrodb_search_get_tests(srch));
137 astrodb_search_put_results(res);
138 astrodb_search_free(srch);
139 return 0;
142 struct s_data {
143 int id_offset;
144 int mag_offset;
145 int ra_offset;
146 int dec_offset;
149 void search3_print(void *object, void *data)
151 struct s_data *s = (struct s_data *) data;
153 printf("Obj: %s RA: %f DEC: %f Mag %f\n",
154 (char *) (object + s->id_offset),
155 *((double *) (object + s->ra_offset)),
156 *((double *) (object + s->dec_offset)),
157 *((float *) (object + s->mag_offset)));
161 * Search for all M1 type stars
162 * Uses Wildcard "M1" to match with Sp
164 static int search3(struct astrodb_table * table)
166 struct astrodb_slist *res = NULL;
167 struct astrodb_search *srch;
168 int err;
169 struct s_data s;
171 srch = astrodb_search_create(table);
172 if (astrodb_search_add_comparator(srch, "Sp", ADB_COMP_LT, "M1*"))
173 printf("failed to add comp M1*\n");
174 if (astrodb_search_add_operator(srch, ADB_OP_OR))
175 printf("failed to add op or\n");
177 printf("Searching for M1 class objects\n");
178 start_timer();
179 err = astrodb_search_get_results(srch, &res, ADB_SMEM);
180 if (err < 0) {
181 printf("Search init failed %d\n", err);
182 astrodb_search_free(srch);
183 return err;
185 end_timer(astrodb_search_get_tests(srch), 0);
186 printf(" Search got %d objects out of %d tests\n",
187 astrodb_search_get_hits(srch),
188 astrodb_search_get_tests(srch));
190 s.id_offset = astrodb_table_get_column_offset(table, "ID");
191 s.mag_offset = astrodb_table_get_column_offset(table, "Vmag");
192 s.ra_offset = astrodb_table_get_column_offset(table, "RA");
193 s.dec_offset = astrodb_table_get_column_offset(table, "DEC");
195 astrodb_table_for_search_results_do(res, search3_print, &s);
196 astrodb_search_put_results(res);
197 astrodb_search_free(srch);
198 return 0;
202 * Get all the objects in the dataset.
204 static int get1(struct astrodb_table * table)
206 int count = 0;
207 struct astrodb_slist *res = NULL;
209 printf("Get all dataset objects\n");
210 astrodb_table_unclip(table);
211 count = astrodb_table_get_objects(table, &res, ADB_SMEM);
212 printf(" Got %d objects\n", count);
213 astrodb_table_put_objects(res);
214 return 0;
218 * Get all the objects brighter than mag 3 in the dataset.
220 static int get2(struct astrodb_table * table)
222 int count = 0;
223 struct astrodb_slist *res = NULL;
225 printf("Get all objects < mag 3\n");
227 /* we clip the dbalog in terms of RA, DEC and mag, this is much faster
228 * than searching, but suffers from boundary overlaps i.e.
229 * we may get some objects fainter than mag 3 depending on the mag clip
230 * boundary. This is not a problem for rendering sky views.
232 astrodb_table_clip_on_position(table, 0, -90, 360, 90, 3, -2);
233 count = astrodb_table_get_objects(table, &res, ADB_SMEM);
234 printf(" Got %d objects\n", count);
235 astrodb_table_put_objects(res);
236 astrodb_table_unclip(table);
237 return 0;
241 * Get all the objects brighter than mag 3 in the dataset.
243 static int get3(struct astrodb_table * table)
245 int count = 0;
246 struct astrodb_slist *res = NULL;
247 struct s_data s;
249 printf("Get all objects < mag 3, in radius 30 deg around 0,0\n");
251 /* we clip the dbalog in terms of RA, DEC and mag, this is much faster
252 * than searching, but suffers from boundary overlaps i.e.
253 * we may get some objects fainter than mag 3 depending on the mag clip
254 * boundary. This is not a problem for rendering sky views.
256 astrodb_table_clip_on_fov(table, 60, 0, 10, 5, -2);
257 count = astrodb_table_get_objects(table, &res, ADB_SMEM);
258 printf(" Got %d objects\n", count);
260 s.id_offset = astrodb_table_get_column_offset(table, "ID");
261 s.mag_offset = astrodb_table_get_column_offset(table, "Vmag");
262 s.ra_offset = astrodb_table_get_column_offset(table, "RA");
263 s.dec_offset = astrodb_table_get_column_offset(table, "DEC");
264 astrodb_table_for_search_results_do(res, search3_print, &s);
266 astrodb_table_put_objects(res);
267 astrodb_table_unclip(table);
268 return 0;
271 int main(int argc, char *argv[])
273 struct astrodb_db *db = NULL;
274 struct astrodb_library *lib = NULL;
275 struct astrodb_table *table = NULL;
276 int table_size, object_size;
278 printf("%s using libastrodb %s\n", argv[0], astrodb_get_version());
280 /* set the remote db and initialise local repository/cache */
281 lib = astrodb_open_library("ftp://cdsarc.u-strasbg.fr/pub/cats",
282 "lnc-test");
283 if (lib == NULL) {
284 printf("failed to open library\n");
285 return -1;
288 /* create a dbalog, using class V and dbalog 109 (Skymap2000) */
289 /* ra,dec,mag bounds are set here along with the 3d tile array size */
290 db = astrodb_create_db(lib, "V", "109", 0.0, 360.0, -90.0, 90.0,
291 15.0, -2.0, 0);
292 if (db == NULL) {
293 printf("failed to create db\n");
294 return -1;
297 /* use the first dataset in this example */
298 table = astrodb_table_create(db, "sky2kv4", ADB_MEM | ADB_FILE );
299 if (table == NULL) {
300 printf("failed to create table\n");
301 return -1;
303 #if 0
304 /* RA, DEC, Vmag and ID/Name are imported by default
305 * Add a couple of custom fields that we are interested in
307 if (astrodb_table_add_custom_field(table, "pmRA"))
308 printf("failed to add pmRA\n");
309 if (astrodb_table_add_custom_field(table, "pmDE"))
310 printf("failed to add pmDE\n");
311 if (astrodb_table_add_custom_field(table, "RV"))
312 printf("failed to add RV\n");
313 if (astrodb_table_add_custom_field(table, "Sp"))
314 printf("failed to add Sp\n");
315 if (astrodb_table_add_custom_field(table, "Vder"))
316 printf("failed to add Vder\n");
318 /* Vmag is blank in some records in the dataset, so we use can Vder
319 * as an alternate field.
321 if (astrodb_table_alt_column(table, "Vmag", "Vder", 0))
322 printf("failed to add alt index\n");
324 /* Import every field in the dataset. */
325 if (astrodb_table_add_custom_field(table, "*"))
326 printf("failed to add *\n");
327 if (astrodb_table_alt_column(table, "Vmag", "Vder", 0))
328 printf("failed to add alt index\n");
329 #endif
331 /* Import the dataset from remote/local repo into memory/disk cache */
332 start_timer();
333 if (astrodb_table_open(table, 0, 0, 0) < 0) {
334 printf("failed to open table\n");
335 return -1;
338 table_size = astrodb_table_get_size(table);
339 object_size = astrodb_table_get_row_size(table);
340 end_timer(table_size, table_size * object_size);
342 /* we can now perform operations on the dbalog data !!! */
343 get1(table);
344 get2(table);
345 search1(table);
346 search2(table);
347 search3(table);
348 get3(table);
350 /* were done with the dataset */
351 astrodb_table_close(table);
353 /* were now done with dbalog */
354 astrodb_db_free(db);
355 astrodb_close_library(lib);
356 return 0;