43 #include "sinfo_error.h"
44 #include "sinfo_msg.h"
45 #include "sinfo_utils_wrappers.h"
47 #include "sinfo_star_index.h"
51 cpl_table* index_table;
60 static const char* COL_NAME_EXTID =
"ext_id";
61 static const char* COL_NAME_NAME =
"name";
62 static const char* COL_NAME_RA =
"ra";
63 static const char* COL_NAME_DEC =
"dec";
65 static star_index* star_index_construct(
const char* fits_file);
66 static void star_index_destruct(star_index* pindex);
69 static star_index* star_index_construct(
const char* fits_file)
71 star_index* pret = cpl_malloc(
sizeof(star_index));
73 pret->index_table = 0;
76 pret->cache_index = 0;
79 size_t bt = strlen(fits_file) *
sizeof(*fits_file)+1;
80 pret->fits_file_name = cpl_malloc(bt);
81 strcpy(pret->fits_file_name, fits_file);
85 pret->fits_file_name = 0;
90 static void star_index_destruct(star_index* pindex)
97 for ( i = 0; i < pindex->cache_size; i++)
99 cpl_table_delete(pindex->cache[i]);
101 cpl_free(pindex->cache);
103 pindex->cache_size = 0;
105 cpl_table_delete(pindex->index_table);
106 if(pindex->fits_file_name)
108 cpl_free(pindex->fits_file_name);
110 cpl_free(pindex->cache_index);
118 star_index* star_index_create(
void)
120 star_index* pret = star_index_construct(0);
122 check_nomsg(pret->index_table = cpl_table_new(pret->index_size));
124 cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT);
125 cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING);
126 cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE);
127 cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE);
131 star_index_destruct(pret);
134 star_index* star_index_load(
const char* fits_file)
136 star_index* pret = star_index_construct(fits_file);
138 cpl_table* pindex = 0;
139 check_nomsg(pindex = cpl_table_load(fits_file,1,0));
141 pret->index_table = pindex;
142 check_nomsg(pret->index_size = cpl_table_get_nrow(pindex));
145 star_index_destruct(pret);
149 void star_index_delete(star_index* pindex)
151 star_index_destruct(pindex);
153 int star_index_add(star_index* pindex,
double RA,
double DEC,
const char* star_name, cpl_table* ptable)
159 check_nomsg(cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1));
162 pindex->cache_size = 1;
163 pindex->cache = cpl_malloc(
sizeof(cpl_table*) * pindex->cache_size);
164 pindex->cache_index = cpl_malloc(
sizeof(pindex->cache_index[0]) * pindex->cache_size);
169 pindex->cache_size++;
170 pindex->cache = cpl_realloc(pindex->cache,
sizeof(cpl_table*) * pindex->cache_size);
172 check_nomsg(pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable));
174 check_nomsg(cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name));
175 check_nomsg(cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA));
176 check_nomsg(cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC));
177 check_nomsg(cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1));
178 retval = pindex->index_size;
187 int start_index_get_size(star_index* pindex)
189 return pindex ? pindex->index_size : 0;
192 int star_index_remove_by_name(star_index* pindex,
const char* starname)
196 for (i = 0; i < pindex->index_size; i++)
198 const char* curr_star_name = 0;
199 check_nomsg(curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
200 if (strcmp(curr_star_name, starname) == 0)
210 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
211 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
214 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
215 cpl_table_delete(pindex->cache[cache_index]);
216 pindex->cache[cache_index] = 0;
223 int star_index_save(star_index* pindex,
const char* fits_file)
227 cpl_table* pnew_index = 0;
230 check_nomsg(cpl_table_unselect_all(pindex->index_table));
231 check_nomsg(cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1));
233 check_nomsg(cpl_table_not_selected(pindex->index_table));
234 check_nomsg(pnew_index = cpl_table_extract_selected(pindex->index_table));
236 nrows = cpl_table_get_nrow(pnew_index);
238 for (i = 0; i < nrows; i++)
240 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2);
243 check_nomsg(cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE));
244 cpl_table_delete(pnew_index);
247 for (i = 0;i < pindex->index_size; i++)
251 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
255 cpl_table* ptable = 0;
257 if (i < pindex->index_size - pindex->cache_size)
260 check_nomsg(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
265 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
268 check_nomsg(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
270 cpl_table_delete(ptable);
281 cpl_table* star_index_get(star_index* pindex,
double RA,
double DEC,
double RA_EPS,
double DEC_EPS,
const char** pstar_name)
287 for (i = 0; i < pindex->index_size; i++)
293 check_nomsg(ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull));
294 check_nomsg(curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull));
295 check_nomsg(curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull));
296 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
300 if (i - pindex->index_size + pindex->cache_size >= 0)
303 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
308 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
310 if (pret && pstar_name)
312 check_nomsg(*pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
321 void star_index_dump(star_index* pindex, FILE* pfile)
323 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);