36#include "eris_ifu_star_index.h"
37#include "eris_utils.h"
59 cpl_table* index_table;
68static const char* COL_NAME_EXTID =
"ext_id";
69static const char* COL_NAME_NAME =
"name";
70static const char* COL_NAME_RA =
"ra";
71static const char* COL_NAME_DEC =
"dec";
73static star_index* star_index_construct(
const char* fits_file);
74static void star_index_destruct(star_index* pindex);
90static star_index* star_index_construct(
const char* fits_file)
92 star_index* pret = cpl_malloc(
sizeof(star_index));
94 pret->index_table = 0;
97 pret->cache_index = 0;
100 size_t bt = strlen(fits_file) *
sizeof(*fits_file)+1;
101 pret->fits_file_name = cpl_malloc(bt);
102 strcpy(pret->fits_file_name, fits_file);
106 pret->fits_file_name = 0;
122static void star_index_destruct(star_index* pindex)
129 for ( i = 0; i < pindex->cache_size; i++)
131 cpl_table_delete(pindex->cache[i]);
133 cpl_free(pindex->cache);
135 pindex->cache_size = 0;
137 cpl_table_delete(pindex->index_table);
138 if(pindex->fits_file_name)
140 cpl_free(pindex->fits_file_name);
142 cpl_free(pindex->cache_index);
160star_index* star_index_create(
void)
162 star_index* pret = star_index_construct(0);
164 pret->index_table = cpl_table_new(pret->index_size);
166 cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT);
167 cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING);
168 cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE);
169 cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE);
171 if(cpl_error_get_code() == CPL_ERROR_NONE) {
174 star_index_destruct(pret);
193star_index* star_index_load(
const char* fits_file)
196 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, NULL);
197 if (access(fits_file, F_OK)) {
198 cpl_msg_error(cpl_func,
"File %s was not found",
200 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
201 cpl_ensure(CPL_FALSE, CPL_ERROR_FILE_NOT_FOUND, NULL);
203 star_index* pret = star_index_construct(fits_file);
205 cpl_table* pindex = 0;
206 pindex = cpl_table_load(fits_file,1,0);
208 pret->index_table = pindex;
209 pret->index_size = cpl_table_get_nrow(pindex);
211 if(cpl_error_get_code() == CPL_ERROR_NONE) {
214 star_index_destruct(pret);
228void star_index_delete(star_index* pindex)
230 star_index_destruct(pindex);
250int star_index_add(star_index* pindex,
double RA,
double DEC,
251 const char* star_name, cpl_table* ptable)
253 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
254 cpl_ensure(star_name, CPL_ERROR_NULL_INPUT, 0);
255 cpl_ensure(ptable, CPL_ERROR_NULL_INPUT, 0);
261 cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1);
264 pindex->cache_size = 1;
265 pindex->cache = cpl_malloc(
sizeof(cpl_table*) * pindex->cache_size);
266 pindex->cache_index = cpl_malloc(
sizeof(pindex->cache_index[0]) * pindex->cache_size);
271 pindex->cache_size++;
272 pindex->cache = cpl_realloc(pindex->cache,
sizeof(cpl_table*) * pindex->cache_size);
274 pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable);
276 cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name);
277 cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA);
278 cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC);
279 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1);
280 retval = pindex->index_size;
283 if(cpl_error_get_code() == CPL_ERROR_NONE) {
286 eris_print_rec_status(0);
306int star_index_remove_by_name(star_index* pindex,
const char* starname)
308 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
309 cpl_ensure(starname, CPL_ERROR_NULL_INPUT, 0);
312 for (i = 0; i < pindex->index_size; i++)
314 const char* curr_star_name = 0;
315 curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
316 if (strcmp(curr_star_name, starname) == 0)
326 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
327 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
330 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
331 cpl_table_delete(pindex->cache[cache_index]);
332 pindex->cache[cache_index] = 0;
355int star_index_save(star_index* pindex,
const char* fits_file)
358 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
359 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, 0);
363 cpl_table* pnew_index = 0;
366 cpl_table_unselect_all(pindex->index_table);
367 cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1);
369 cpl_table_not_selected(pindex->index_table);
370 pnew_index = cpl_table_extract_selected(pindex->index_table);
372 nrows = cpl_table_get_nrow(pnew_index);
374 for (i = 0; i < nrows; i++)
376 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2);
379 cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE);
380 cpl_table_delete(pnew_index);
383 for (i = 0;i < pindex->index_size; i++)
387 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
391 cpl_table* ptable = 0;
393 if (i < pindex->index_size - pindex->cache_size)
396 ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0);
401 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
404 cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND);
406 cpl_table_delete(ptable);
413 if(cpl_error_get_code() == CPL_ERROR_NONE) {
416 eris_print_rec_status(0);
442cpl_table* star_index_get(star_index* pindex,
double RA,
double DEC,
443 double RA_EPS,
double DEC_EPS,
const char** pstar_name)
446 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, NULL);
447 cpl_ensure(pstar_name, CPL_ERROR_NULL_INPUT, NULL);
453 for (i = 0; i < pindex->index_size; i++)
459 ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull);
460 curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull);
461 curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull);
462 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
466 if (i - pindex->index_size + pindex->cache_size >= 0)
469 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
474 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
476 if (pret && pstar_name)
478 *pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
483 if(cpl_error_get_code() == CPL_ERROR_NONE) {
486 cpl_table_delete(pret);
510eris_parse_catalog_std_stars(cpl_frame* cat,
517 cpl_ensure(cat, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
518 cpl_ensure(pptable, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
521 const char* name = cpl_frame_get_filename(cat);
524 star_index* pstarindex = star_index_load(name);
526 const char* star_name = 0;
527 cpl_msg_info(cpl_func,
528 "The catalog is loaded, looking for star in RA[%f] DEC[%f] tolerance[%f]",
530 *pptable = star_index_get(pstarindex, dRA, dDEC, EPSILON,
531 EPSILON, &star_name);
532 if (*pptable && star_name) {
533 cpl_msg_info(cpl_func,
534 "REF table is found in the catalog, star name is [%s]",
538 cpl_msg_info(cpl_func,
539 "ERROR - REF table could not be found in the catalog");
543 cpl_msg_info(cpl_func,
"ERROR - could not load the catalog");
545 star_index_delete(pstarindex);
551 return cpl_error_get_code();
564void star_index_dump(star_index* pindex, FILE* pfile)
566 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);