table: astro_object - added union for char designation.
[libastrodb.git] / examples / ngc.c
blobd583ade8427c2d113ac47aa826dbdf0aad8983df
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
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <sys/time.h>
23 #include <stdio.h>
25 #include <libastrodb/astrodb.h>
27 static struct timeval start, end;
29 inline static void start_timer(void)
31 gettimeofday(&start, NULL);
34 static void end_timer(int objects, int bytes)
36 double usecs;
38 gettimeofday(&end, NULL);
39 usecs = (end.tv_sec * 1000000 + end.tv_usec) -
40 (start.tv_sec * 1000000 + start.tv_usec);
42 if (bytes)
43 printf ("Time(secs) %f : %9.0f objects per sec %9.0f bytes per"
44 " secs\n", usecs / 1000000.0,
45 (1000000.0 / usecs) * objects,
46 (1000000.0 / usecs) * bytes);
47 else
48 printf("Time(secs) %f : %9.0f objects per sec\n",
49 usecs / 1000000.0, (1000000.0 / usecs) * objects);
52 /*
53 * Get all the objects in the dataset.
55 static int get_all(struct astrodb_table * table)
57 int count = 0;
58 struct astrodb_slist *res = NULL;
60 printf("Get all dataset objects\n");
61 astrodb_table_unclip(table);
62 count = astrodb_table_get_objects(table, &res, ADB_SMEM);
63 printf(" Got %d objects\n", count);
64 astrodb_table_put_objects(res);
65 return 0;
68 int main(int argc, char *argv[])
70 struct astrodb_db *db = NULL;
71 struct astrodb_library *lib = NULL;
72 struct astrodb_table *table = NULL;
73 int table_size, object_size;
75 printf("%s using libastrodb %s\n", argv[0], astrodb_get_version());
77 /* set the remote db and initialise local repository/cache */
78 lib = astrodb_open_library("ftp://cdsarc.u-strasbg.fr/pub/cats", "lnc-test");
79 if (lib == NULL) {
80 printf("failed to open library\n");
81 return -1;
84 /* create a dbalog, using class VII and dbalog 118 (NGC2000) */
85 /* ra,dec,mag bounds are set here along with the 3d tile array size */
86 db = astrodb_create_db(lib, "VII", "118", 0.0, 360.0, -90.0, 90.0,
87 15.0, -2.0, 0);
88 if (db == NULL) {
89 printf("failed to create db\n");
90 return -1;
93 /* use the first dataset in this example */
94 table = astrodb_table_create(db, "ngc2000.dat", ADB_MEM | ADB_FILE );
95 if (table == NULL) {
96 printf("failed to create table\n");
97 return -1;
100 /* Import every field in the dataset. */
101 // if (astrodb_table_add_custom_field(table, "*"))
102 printf("failed to add *\n");
104 /* Import the dataset from remote/local repo into memory/disk cache */
105 start_timer();
107 if (astrodb_table_open(table, 0, 0, 0) < 0) {
108 printf("failed to open table\n");
109 return -1;
112 table_size = astrodb_table_get_size(table);
113 object_size = astrodb_table_get_row_size(table);
114 end_timer(table_size, table_size * object_size);
116 /* we can now perform operations on the dbalog data !!! */
117 get_all(table);
119 /* were done with the table */
120 astrodb_table_close(table);
122 /* were now done with db */
123 astrodb_db_free(db);
124 astrodb_close_library(lib);
125 return 0;