core: table API rework Phase II.
[libastrodb.git] / examples / vii237.c
blob92398bb682cfe6cdda5bbc62fd3566fdf816ebfa
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) 2005 Liam Girdwood
19 #define _GNU_SOURCE /* for strtof and NAN */
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <math.h>
27 #include <libastrodb/astrodb.h>
29 struct hyperleda_object {
30 struct astrodb_object d;
31 float position_angle;
32 float axis_ratio;
33 char MType[4];
34 char OType[1];
35 char other_name[15];
38 static int pa_insert(void *dest, void *src)
40 char *ptr;
42 *(float *) dest = strtof(src, &ptr);
43 if (src == ptr) {
44 *(float*) dest = FP_NAN;
45 return -1;
47 if (*(float *) dest == (float) 999.0)
48 *(float*) dest = FP_NAN;
50 return 0;
53 static int size_insert(void *dest, void *src)
55 char *ptr;
57 *(float *) dest = strtof(src, &ptr);
58 if (src == ptr) {
59 *(float*) dest = FP_NAN;
60 return -1;
63 if (*(float *) dest == (float) 9.99)
64 *(float*) dest = FP_NAN;
65 return 0;
68 static int otype_insert(void *dest, void *src)
70 char *type = src;
72 if (*type == 'M')
73 *(char*)dest = 'M';
74 else if (*type == 'G') {
75 if (*(type + 1) == 'M')
76 *(char*)dest = 'X';
77 else
78 *(char*)dest = 'G';
81 return 0;
84 static struct astrodb_schema_object hyperleda_fields[] = {
85 astrodb_member("Name", "ANames", struct hyperleda_object, other_name,
86 CT_STRING, "", 0, NULL),
87 astrodb_member("ID", "PGC", struct hyperleda_object, d.id,
88 CT_INT, "", 0, NULL),
89 astrodb_gmember("RA Hours", "RAh", struct hyperleda_object, d.posn_size.ra,
90 CT_DOUBLE_HMS_HRS, "hours", 2, NULL),
91 astrodb_gmember("RA Minutes", "RAm", struct hyperleda_object, d.posn_size.ra,
92 CT_DOUBLE_HMS_MINS, "minutes", 1, NULL),
93 astrodb_gmember("RA Seconds", "RAs", struct hyperleda_object, d.posn_size.ra,
94 CT_DOUBLE_HMS_SECS, "seconds", 0, NULL),
95 astrodb_gmember("DEC Degrees", "DEd", struct hyperleda_object, d.posn_size.dec,
96 CT_DOUBLE_DMS_DEGS, "degrees", 3, NULL),
97 astrodb_gmember("DEC Minutes", "DEm", struct hyperleda_object, d.posn_size.dec,
98 CT_DOUBLE_DMS_MINS, "minutes", 2, NULL),
99 astrodb_gmember("DEC Seconds", "DEs", struct hyperleda_object, d.posn_size.dec,
100 CT_DOUBLE_DMS_SECS, "seconds", 1, NULL),
101 astrodb_gmember("DEC sign", "DE-", struct hyperleda_object, d.posn_size.dec,
102 CT_SIGN, "", 0, NULL),
103 astrodb_member("Type", "MType", struct hyperleda_object, MType,
104 CT_STRING, "", 0, NULL),
105 astrodb_member("OType", "OType", struct hyperleda_object, OType,
106 CT_STRING, "", 0, otype_insert),
107 astrodb_member("Diameter", "logD25", struct hyperleda_object, d.posn_size.size,
108 CT_FLOAT, "0.1amin", 0, size_insert),
109 astrodb_member("Axis Ratio", "logR25", struct hyperleda_object, axis_ratio,
110 CT_FLOAT, "0.1amin", 0, size_insert),
111 astrodb_member("Position Angle", "PA", struct hyperleda_object, position_angle,
112 CT_FLOAT, "deg", 0, pa_insert),
115 int main (int argc, char* argv[])
117 struct astrodb_library *lib;
118 struct astrodb_db *db;
119 struct astrodb_table *table;
121 /* set the remote db and initialise local repository/cache */
122 lib = astrodb_open_library("ftp://cdsarc.u-strasbg.fr/pub/cats", "lnc-test");
123 if (lib == NULL) {
124 printf("failed to open library\n");
125 return -1;
128 /* create a dbalog */
129 db = astrodb_create_db(lib, "VII", "237",
130 0.0, 360.0, -90.0, 90.0, 15.0, -2.0, 0);
131 if (db == NULL) {
132 printf("failed to create db\n");
133 return -1;
136 table = astrodb_table_create(db, "pgc", ADB_MEM | ADB_FILE );
137 if (table == NULL) {
138 printf("failed to create table\n");
139 return -1;
142 if (astrodb_table_register_schema(table, hyperleda_fields,
143 astrodb_size(hyperleda_fields), sizeof(struct hyperleda_object)) < 0)
144 printf("%s: failed to register object type\n", __func__);
146 /* We want to quickly search the dataset based on object ID and HD number */
147 if (astrodb_table_hash_key(table, "PGC"))
148 printf("%s: failed to hash on ID\n", __func__);
150 /* Import the dataset from remote/local repo into memory/disk cache */
151 if (astrodb_table_open(table, 20, 10, 1) < 0) {
152 printf("failed to open table\n");
153 return -1;
156 /* were done with the dataset */
157 astrodb_table_close(table);
159 /* were now done with dbalog */
160 astrodb_db_free(db);
162 astrodb_close_library(lib);
163 return 0;