ERIS Pipeline Reference Manual 1.8.14
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"
38struct _star_index_
39{
40 cpl_table* index_table;
41 char* fits_file_name;
42 int index_size;
43 cpl_table** cache;
44 int cache_size;
45 int* cache_index;
46};
47
48//typedef struct _star_index_ star_index;
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";
53
54static star_index* star_index_construct(const char* fits_file);
55static void star_index_destruct(star_index* pindex);
56// private functions
57/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64static star_index* star_index_construct(const char* fits_file)
65{
66 star_index* pret = cpl_malloc(sizeof(star_index));
67 pret->index_size = 0;
68 pret->index_table = 0;
69 pret->cache_size = 0;
70 pret->cache = 0;
71 pret->cache_index = 0;
72 if (fits_file)
73 {
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);
77 }
78 else
79 {
80 pret->fits_file_name = 0;
81 }
82 return pret;
83}
84/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90static void star_index_destruct(star_index* pindex)
91{
92 if(pindex)
93 {
94 if (pindex->cache)
95 {
96 int i = 0;
97 for ( i = 0; i < pindex->cache_size; i++)
98 {
99 cpl_table_delete(pindex->cache[i]);
100 }
101 cpl_free(pindex->cache);
102 pindex->cache = 0;
103 pindex->cache_size = 0;
104 }
105 cpl_table_delete(pindex->index_table);
106 if(pindex->fits_file_name)
107 {
108 cpl_free(pindex->fits_file_name);
109 }
110 cpl_free(pindex->cache_index);
111 cpl_free(pindex);
112 }
113
114}
115
117/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123star_index* star_index_create(void)
124{
125 star_index* pret = star_index_construct(0);
126 // initialize table
127 pret->index_table = cpl_table_new(pret->index_size);
128 // create columns ext_id, name, ra, dec
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);
133
134 if(cpl_error_get_code() == CPL_ERROR_NONE) {
135 return pret;
136 } else {
137 star_index_destruct(pret);
138 return 0;
139 }
140
141}
142/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
148star_index* star_index_load(const char* fits_file)
149{
150
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",
154 fits_file);
155 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
156 cpl_ensure(CPL_FALSE, CPL_ERROR_FILE_NOT_FOUND, NULL);
157 }
158 star_index* pret = star_index_construct(fits_file);
159 // load index table from the file
160 cpl_table* pindex = 0;
161 pindex = cpl_table_load(fits_file,1,0);
162 // TODO check_nomsg the structure of the table
163 pret->index_table = pindex;
164 pret->index_size = cpl_table_get_nrow(pindex);
165
166 if(cpl_error_get_code() == CPL_ERROR_NONE) {
167 return pret;
168 } else {
169 star_index_destruct(pret);
170 //cpl_error_reset();
171 return 0;
172 }
173}
174/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180void star_index_delete(star_index* pindex)
181{
182 star_index_destruct(pindex);
183}
184
185/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
195int star_index_add(star_index* pindex, double RA, double DEC,
196 const char* star_name, cpl_table* ptable)
197{
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);
201
202 int retval = 0;
203 if (pindex)
204 {
205 // expand the index table
206 cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1);
207 if (!pindex->cache)
208 {
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);
212 }
213 else
214 {
215 // add new entry
216 pindex->cache_size++;
217 pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
218 }
219 pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable);
220 // fill the index table with values
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;
226 }
227
228 if(cpl_error_get_code() == CPL_ERROR_NONE) {
229 return retval;
230 } else {
231 eris_print_rec_status(0);
232 return 0;
233 }
234}
235/*---------------------------------------------------------------------------*/
241/*---------------------------------------------------------------------------*/
242
243/* not used */
244int star_index_remove_by_name(star_index* pindex, const char* starname)
245{
246 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
247 cpl_ensure(starname, CPL_ERROR_NULL_INPUT, 0);
248 int i = 0;
249 int index_pos = -1;
250 for (i = 0; i < pindex->index_size; i++)
251 {
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)
255 {
256 index_pos = i;
257 break;
258 }
259 }
260 if (index_pos >= 0)
261 {
262 // star is found
263 // clear only the index table, real data would be cleaned during save operation
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)
266 {
267 // clear cache
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;
271 }
272 }
273
274 return index_pos;
275}
276/*---------------------------------------------------------------------------*/
282/*---------------------------------------------------------------------------*/
283
284int star_index_save(star_index* pindex, const char* fits_file)
285{
286
287 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, 0);
288 cpl_ensure(fits_file, CPL_ERROR_NULL_INPUT, 0);
289
290 int i = 0;
291 int inull = 0;
292 cpl_table* pnew_index = 0;
293 int nrows = 0;
294 // firstly save the index table - deleted entries should be removed firstly
295 cpl_table_unselect_all(pindex->index_table);
296 cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1);
297 // inverse selection
298 cpl_table_not_selected(pindex->index_table);
299 pnew_index = cpl_table_extract_selected(pindex->index_table);
300
301 nrows = cpl_table_get_nrow(pnew_index);
302 // printf("rows to save[%d]\n", nrows);
303 for (i = 0; i < nrows; i++)
304 {
305 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
306 }
307 // printf("writing index [%s]\n", fits_file);
308 cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE);
309 cpl_table_delete(pnew_index);
310 pnew_index = 0;
311 // save the data
312 for (i = 0;i < pindex->index_size; i++)
313 {
314 // printf("saving ext [%d]\n", i);
315 // 2. save cache
316 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
317 // printf("saving 1\n");
318 if (saved_ext > 0) // check_nomsg that was not removed
319 {
320 cpl_table* ptable = 0;
321 // printf("saving 2\n");
322 if (i < pindex->index_size - pindex->cache_size)
323 {
324 // printf("saving 3\n");
325 ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0);
326 }
327 else
328 {
329 // printf("saving 4\n");
330 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
331 }
332 // printf("saving 5\n");
333 cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND);
334 // printf("saving 6\n");
335 cpl_table_delete(ptable);
336 // printf("saving 7\n");
337 }
338 // printf("saving 8\n");
339 }
340 // printf("saving exit\n");
341
342 if(cpl_error_get_code() == CPL_ERROR_NONE) {
343 return nrows;
344 } else {
345 eris_print_rec_status(0);
346 return 0;
347 }
348}
349/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
361
362
363cpl_table* star_index_get(star_index* pindex, double RA, double DEC,
364 double RA_EPS, double DEC_EPS, const char** pstar_name)
365{
366
367 cpl_ensure(pindex, CPL_ERROR_NULL_INPUT, NULL);
368 cpl_ensure(pstar_name, CPL_ERROR_NULL_INPUT, NULL);
369
370 int i = 0;
371 cpl_table* pret = 0;
372 int inull = 0;
373
374 for (i = 0; i < pindex->index_size; i++)
375 {
376 double curr_ra = 0;
377 double curr_dec = 0;
378 int ext_id = 0;
379
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))
384 {
385 // found
386 // retrieve the data
387 if (i - pindex->index_size + pindex->cache_size >= 0)
388 {
389 // data is in cache TODO: this generates a leak as there is a loop and the created table is not deleated
390 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
391 }
392 else
393 {
394 // data is on disk
395 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
396 }
397 if (pret && pstar_name)
398 {
399 *pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i);
400 }
401 break;
402 }
403 }
404 if(cpl_error_get_code() == CPL_ERROR_NONE) {
405 return pret;
406 } else {
407 cpl_table_delete(pret);
408 return NULL;
409 }
410}
411
412/*---------------------------------------------------------------------------*/
423/*---------------------------------------------------------------------------*/
424
425cpl_error_code
426eris_parse_catalog_std_stars(cpl_frame* cat,
427 double dRA,
428 double dDEC,
429 double EPSILON,
430 cpl_table** pptable)
431{
432
433 cpl_ensure(cat, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
434 cpl_ensure(pptable, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
435 if (cat != NULL) {
436
437 const char* name = cpl_frame_get_filename(cat);
438
439 if (name) {
440 star_index* pstarindex = star_index_load(name);
441 if (pstarindex) {
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]",
445 dRA, dDEC, EPSILON);
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]",
451 star_name);
452 }
453 else {
454 cpl_msg_info(cpl_func,
455 "ERROR - REF table could not be found in the catalog");
456 }
457 }
458 else {
459 cpl_msg_info(cpl_func, "ERROR - could not load the catalog");
460 }
461 star_index_delete(pstarindex);
462
463 }
464 }
465
466
467 return cpl_error_get_code();
468}
469/*---------------------------------------------------------------------------*/
475/*---------------------------------------------------------------------------*/
476
477void star_index_dump(star_index* pindex, FILE* pfile)
478{
479 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
480}