36#include "eris_ifu_star_index.h"
37#include "eris_utils.h"
40 cpl_table* index_table;
49static const char* COL_NAME_EXTID =
"ext_id";
50static const char* COL_NAME_NAME =
"name";
51static const char* COL_NAME_RA =
"ra";
52static const char* COL_NAME_DEC =
"dec";
54static star_index* star_index_construct(
const char* fits_file);
55static void star_index_destruct(star_index* pindex);
64static star_index* star_index_construct(
const char* fits_file)
66 star_index* pret = cpl_malloc(
sizeof(star_index));
68 pret->index_table = 0;
71 pret->cache_index = 0;
74 size_t bt = strlen(fits_file) *
sizeof(*fits_file)+1;
75 pret->fits_file_name = cpl_malloc(bt);
76 strcpy(pret->fits_file_name, fits_file);
80 pret->fits_file_name = 0;
90static 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);
123star_index* star_index_create(
void)
125 star_index* pret = star_index_construct(0);
127 pret->index_table = cpl_table_new(pret->index_size);
129 cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT);
130 cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING);
131 cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE);
132 cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE);
134 if(cpl_error_get_code() == CPL_ERROR_NONE) {
137 star_index_destruct(pret);
148star_index* star_index_load(
const char* fits_file)
151 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, NULL);
152 if (access(fits_file, F_OK)) {
153 cpl_msg_error(cpl_func,
"File %s was not found",
155 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
156 cpl_ensure(CPL_FALSE, CPL_ERROR_FILE_NOT_FOUND, NULL);
158 star_index* pret = star_index_construct(fits_file);
160 cpl_table* pindex = 0;
161 pindex = cpl_table_load(fits_file,1,0);
163 pret->index_table = pindex;
164 pret->index_size = cpl_table_get_nrow(pindex);
166 if(cpl_error_get_code() == CPL_ERROR_NONE) {
169 star_index_destruct(pret);
180void star_index_delete(star_index* pindex)
182 star_index_destruct(pindex);
195int star_index_add(star_index* pindex,
double RA,
double DEC,
196 const char* star_name, cpl_table* ptable)
198 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
199 cpl_ensure(star_name, CPL_ERROR_NULL_INPUT, 0);
200 cpl_ensure(ptable, CPL_ERROR_NULL_INPUT, 0);
206 cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1);
209 pindex->cache_size = 1;
210 pindex->cache = cpl_malloc(
sizeof(cpl_table*) * pindex->cache_size);
211 pindex->cache_index = cpl_malloc(
sizeof(pindex->cache_index[0]) * pindex->cache_size);
216 pindex->cache_size++;
217 pindex->cache = cpl_realloc(pindex->cache,
sizeof(cpl_table*) * pindex->cache_size);
219 pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable);
221 cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name);
222 cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA);
223 cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC);
224 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1);
225 retval = pindex->index_size;
228 if(cpl_error_get_code() == CPL_ERROR_NONE) {
231 eris_print_rec_status(0);
244int star_index_remove_by_name(star_index* pindex,
const char* starname)
246 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
247 cpl_ensure(starname, CPL_ERROR_NULL_INPUT, 0);
250 for (i = 0; i < pindex->index_size; i++)
252 const char* curr_star_name = 0;
253 curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
254 if (strcmp(curr_star_name, starname) == 0)
264 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
265 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
268 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
269 cpl_table_delete(pindex->cache[cache_index]);
270 pindex->cache[cache_index] = 0;
284int star_index_save(star_index* pindex,
const char* fits_file)
287 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
288 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, 0);
292 cpl_table* pnew_index = 0;
295 cpl_table_unselect_all(pindex->index_table);
296 cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1);
298 cpl_table_not_selected(pindex->index_table);
299 pnew_index = cpl_table_extract_selected(pindex->index_table);
301 nrows = cpl_table_get_nrow(pnew_index);
303 for (i = 0; i < nrows; i++)
305 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2);
308 cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE);
309 cpl_table_delete(pnew_index);
312 for (i = 0;i < pindex->index_size; i++)
316 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
320 cpl_table* ptable = 0;
322 if (i < pindex->index_size - pindex->cache_size)
325 ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0);
330 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
333 cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND);
335 cpl_table_delete(ptable);
342 if(cpl_error_get_code() == CPL_ERROR_NONE) {
345 eris_print_rec_status(0);
363cpl_table* star_index_get(star_index* pindex,
double RA,
double DEC,
364 double RA_EPS,
double DEC_EPS,
const char** pstar_name)
367 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, NULL);
368 cpl_ensure(pstar_name, CPL_ERROR_NULL_INPUT, NULL);
374 for (i = 0; i < pindex->index_size; i++)
380 ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull);
381 curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull);
382 curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull);
383 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
387 if (i - pindex->index_size + pindex->cache_size >= 0)
390 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
395 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
397 if (pret && pstar_name)
399 *pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
404 if(cpl_error_get_code() == CPL_ERROR_NONE) {
407 cpl_table_delete(pret);
426eris_parse_catalog_std_stars(cpl_frame* cat,
433 cpl_ensure(cat, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
434 cpl_ensure(pptable, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
437 const char* name = cpl_frame_get_filename(cat);
440 star_index* pstarindex = star_index_load(name);
442 const char* star_name = 0;
443 cpl_msg_info(cpl_func,
444 "The catalog is loaded, looking for star in RA[%f] DEC[%f] tolerance[%f]",
446 *pptable = star_index_get(pstarindex, dRA, dDEC, EPSILON,
447 EPSILON, &star_name);
448 if (*pptable && star_name) {
449 cpl_msg_info(cpl_func,
450 "REF table is found in the catalog, star name is [%s]",
454 cpl_msg_info(cpl_func,
455 "ERROR - REF table could not be found in the catalog");
459 cpl_msg_info(cpl_func,
"ERROR - could not load the catalog");
461 star_index_delete(pstarindex);
467 return cpl_error_get_code();
477void star_index_dump(star_index* pindex, FILE* pfile)
479 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);