ERIS Pipeline Reference Manual 1.8.15
eris_ifu_star_index.c
1/* $Id: eris_star_index.c,v 1.9 2012-03-03 10:18:26 amodigli Exp $
2 *
3 * This file is part of the X-Shooter Pipeline
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20/*
21 * $Author: amodigli $
22 * $Date: 2012-03-03 10:18:26 $
23 * $Revision: 1.9 $
24 * $Name: not supported by cvs2svn $
25 */
26
27
28
29#ifdef HAVE_CONFIG_H
30#include <config.h> /* allows the program compilation */
31#endif
32
33#include <cpl.h>
34#include <string.h>
35#include <math.h>
36#include "eris_ifu_star_index.h"
37#include "eris_utils.h"
38
39/*----------------------------------------------------------------------------*/
55/*----------------------------------------------------------------------------*/
56
57struct _star_index_
58{
59 cpl_table* index_table;
60 char* fits_file_name;
61 int index_size;
62 cpl_table** cache;
63 int cache_size;
64 int* cache_index;
65};
66
67//typedef struct _star_index_ star_index;
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";
72
73static star_index* star_index_construct(const char* fits_file);
74static void star_index_destruct(star_index* pindex);
75// private functions
76/*----------------------------------------------------------------------------*/
89/*----------------------------------------------------------------------------*/
90static star_index* star_index_construct(const char* fits_file)
91{
92 star_index* pret = cpl_malloc(sizeof(star_index));
93 pret->index_size = 0;
94 pret->index_table = 0;
95 pret->cache_size = 0;
96 pret->cache = 0;
97 pret->cache_index = 0;
98 if (fits_file)
99 {
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);
103 }
104 else
105 {
106 pret->fits_file_name = 0;
107 }
108 return pret;
109}
110/*----------------------------------------------------------------------------*/
121/*----------------------------------------------------------------------------*/
122static void star_index_destruct(star_index* pindex)
123{
124 if(pindex)
125 {
126 if (pindex->cache)
127 {
128 int i = 0;
129 for ( i = 0; i < pindex->cache_size; i++)
130 {
131 cpl_table_delete(pindex->cache[i]);
132 }
133 cpl_free(pindex->cache);
134 pindex->cache = 0;
135 pindex->cache_size = 0;
136 }
137 cpl_table_delete(pindex->index_table);
138 if(pindex->fits_file_name)
139 {
140 cpl_free(pindex->fits_file_name);
141 }
142 cpl_free(pindex->cache_index);
143 cpl_free(pindex);
144 }
145
146}
147
149/*----------------------------------------------------------------------------*/
159/*----------------------------------------------------------------------------*/
160star_index* star_index_create(void)
161{
162 star_index* pret = star_index_construct(0);
163 // initialize table
164 pret->index_table = cpl_table_new(pret->index_size);
165 // create columns ext_id, name, ra, dec
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);
170
171 if(cpl_error_get_code() == CPL_ERROR_NONE) {
172 return pret;
173 } else {
174 star_index_destruct(pret);
175 return 0;
176 }
177
178}
179/*----------------------------------------------------------------------------*/
192/*----------------------------------------------------------------------------*/
193star_index* star_index_load(const char* fits_file)
194{
195
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",
199 fits_file);
200 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
201 cpl_ensure(CPL_FALSE, CPL_ERROR_FILE_NOT_FOUND, NULL);
202 }
203 star_index* pret = star_index_construct(fits_file);
204 // load index table from the file
205 cpl_table* pindex = 0;
206 pindex = cpl_table_load(fits_file,1,0);
207 // TODO check_nomsg the structure of the table
208 pret->index_table = pindex;
209 pret->index_size = cpl_table_get_nrow(pindex);
210
211 if(cpl_error_get_code() == CPL_ERROR_NONE) {
212 return pret;
213 } else {
214 star_index_destruct(pret);
215 //cpl_error_reset();
216 return 0;
217 }
218}
219/*----------------------------------------------------------------------------*/
227/*----------------------------------------------------------------------------*/
228void star_index_delete(star_index* pindex)
229{
230 star_index_destruct(pindex);
231}
232
233/*----------------------------------------------------------------------------*/
249/*----------------------------------------------------------------------------*/
250int star_index_add(star_index* pindex, double RA, double DEC,
251 const char* star_name, cpl_table* ptable)
252{
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);
256
257 int retval = 0;
258 if (pindex)
259 {
260 // expand the index table
261 cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1);
262 if (!pindex->cache)
263 {
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);
267 }
268 else
269 {
270 // add new entry
271 pindex->cache_size++;
272 pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
273 }
274 pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable);
275 // fill the index table with values
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;
281 }
282
283 if(cpl_error_get_code() == CPL_ERROR_NONE) {
284 return retval;
285 } else {
286 eris_print_rec_status(0);
287 return 0;
288 }
289}
290/*----------------------------------------------------------------------------*/
303/*----------------------------------------------------------------------------*/
304
305/* not used */
306int star_index_remove_by_name(star_index* pindex, const char* starname)
307{
308 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
309 cpl_ensure(starname, CPL_ERROR_NULL_INPUT, 0);
310 int i = 0;
311 int index_pos = -1;
312 for (i = 0; i < pindex->index_size; i++)
313 {
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)
317 {
318 index_pos = i;
319 break;
320 }
321 }
322 if (index_pos >= 0)
323 {
324 // star is found
325 // clear only the index table, real data would be cleaned during save operation
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)
328 {
329 // clear cache
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;
333 }
334 }
335
336 return index_pos;
337}
338/*----------------------------------------------------------------------------*/
353/*----------------------------------------------------------------------------*/
354
355int star_index_save(star_index* pindex, const char* fits_file)
356{
357
358 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
359 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, 0);
360
361 int i = 0;
362 int inull = 0;
363 cpl_table* pnew_index = 0;
364 int nrows = 0;
365 // firstly save the index table - deleted entries should be removed firstly
366 cpl_table_unselect_all(pindex->index_table);
367 cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1);
368 // inverse selection
369 cpl_table_not_selected(pindex->index_table);
370 pnew_index = cpl_table_extract_selected(pindex->index_table);
371
372 nrows = cpl_table_get_nrow(pnew_index);
373 // printf("rows to save[%d]\n", nrows);
374 for (i = 0; i < nrows; i++)
375 {
376 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2); // ext in fits starts from 1, and another 1 is used by index_table
377 }
378 // printf("writing index [%s]\n", fits_file);
379 cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE);
380 cpl_table_delete(pnew_index);
381 pnew_index = 0;
382 // save the data
383 for (i = 0;i < pindex->index_size; i++)
384 {
385 // printf("saving ext [%d]\n", i);
386 // 2. save cache
387 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
388 // printf("saving 1\n");
389 if (saved_ext > 0) // check_nomsg that was not removed
390 {
391 cpl_table* ptable = 0;
392 // printf("saving 2\n");
393 if (i < pindex->index_size - pindex->cache_size)
394 {
395 // printf("saving 3\n");
396 ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0);
397 }
398 else
399 {
400 // printf("saving 4\n");
401 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
402 }
403 // printf("saving 5\n");
404 cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND);
405 // printf("saving 6\n");
406 cpl_table_delete(ptable);
407 // printf("saving 7\n");
408 }
409 // printf("saving 8\n");
410 }
411 // printf("saving exit\n");
412
413 if(cpl_error_get_code() == CPL_ERROR_NONE) {
414 return nrows;
415 } else {
416 eris_print_rec_status(0);
417 return 0;
418 }
419}
420/*----------------------------------------------------------------------------*/
439/*----------------------------------------------------------------------------*/
440
441
442cpl_table* star_index_get(star_index* pindex, double RA, double DEC,
443 double RA_EPS, double DEC_EPS, const char** pstar_name)
444{
445
446 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, NULL);
447 cpl_ensure(pstar_name, CPL_ERROR_NULL_INPUT, NULL);
448
449 int i = 0;
450 cpl_table* pret = 0;
451 int inull = 0;
452
453 for (i = 0; i < pindex->index_size; i++)
454 {
455 double curr_ra = 0;
456 double curr_dec = 0;
457 int ext_id = 0;
458
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))
463 {
464 // found
465 // retrieve the data
466 if (i - pindex->index_size + pindex->cache_size >= 0)
467 {
468 // data is in cache TODO: this generates a leak as there is a loop and the created table is not deleated
469 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
470 }
471 else
472 {
473 // data is on disk
474 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
475 }
476 if (pret && pstar_name)
477 {
478 *pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
479 }
480 break;
481 }
482 }
483 if(cpl_error_get_code() == CPL_ERROR_NONE) {
484 return pret;
485 } else {
486 cpl_table_delete(pret);
487 return NULL;
488 }
489}
490
491/*----------------------------------------------------------------------------*/
507/*----------------------------------------------------------------------------*/
508
509cpl_error_code
510eris_parse_catalog_std_stars(cpl_frame* cat,
511 double dRA,
512 double dDEC,
513 double EPSILON,
514 cpl_table** pptable)
515{
516
517 cpl_ensure(cat, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
518 cpl_ensure(pptable, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
519 if (cat != NULL) {
520
521 const char* name = cpl_frame_get_filename(cat);
522
523 if (name) {
524 star_index* pstarindex = star_index_load(name);
525 if (pstarindex) {
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]",
529 dRA, dDEC, EPSILON);
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]",
535 star_name);
536 }
537 else {
538 cpl_msg_info(cpl_func,
539 "ERROR - REF table could not be found in the catalog");
540 }
541 }
542 else {
543 cpl_msg_info(cpl_func, "ERROR - could not load the catalog");
544 }
545 star_index_delete(pstarindex);
546
547 }
548 }
549
550
551 return cpl_error_get_code();
552}
553/*----------------------------------------------------------------------------*/
562/*----------------------------------------------------------------------------*/
563
564void star_index_dump(star_index* pindex, FILE* pfile)
565{
566 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
567}