SINFONI Pipeline Reference Manual  2.5.2
sinfo_star_index.c
1 /* $Id: sinfo_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 
37 
38 //#include "sinfo_pro_save.h"
39 //#include "sinfo_pfits.h"
40 //#include "sinfo_utilities_scired.h"
41 //#include "sinfo_hidden.h"
42 //#include "sinfo_functions.h"
43 #include "sinfo_error.h"
44 #include "sinfo_msg.h"
45 #include "sinfo_utils_wrappers.h"
46 //#include "sinfo_globals.h"
47 #include "sinfo_star_index.h"
48 
49 struct _star_index_
50 {
51  cpl_table* index_table;
52  char* fits_file_name;
53  int index_size;
54  cpl_table** cache;
55  int cache_size;
56  int* cache_index;
57 };
58 
59 //typedef struct _star_index_ star_index;
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";
64 
65 static star_index* star_index_construct(const char* fits_file);
66 static void star_index_destruct(star_index* pindex);
67 // private functions
68 
69 static star_index* star_index_construct(const char* fits_file)
70 {
71  star_index* pret = cpl_malloc(sizeof(star_index));
72  pret->index_size = 0;
73  pret->index_table = 0;
74  pret->cache_size = 0;
75  pret->cache = 0;
76  pret->cache_index = 0;
77  if (fits_file)
78  {
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);
82  }
83  else
84  {
85  pret->fits_file_name = 0;
86  }
87  return pret;
88 }
89 
90 static 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 
118 star_index* star_index_create(void)
119 {
120  star_index* pret = star_index_construct(0);
121  // initialize table
122  check_nomsg(pret->index_table = cpl_table_new(pret->index_size));
123  // create columns ext_id, name, ra, dec
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);
128 
129  return pret;
130  cleanup:
131  star_index_destruct(pret);
132  return 0;
133 }
134 star_index* star_index_load(const char* fits_file)
135 {
136  star_index* pret = star_index_construct(fits_file);
137  // load index table from the file
138  cpl_table* pindex = 0;
139  check_nomsg(pindex = cpl_table_load(fits_file,1,0));
140  // TODO check_nomsg the structure of the table
141  pret->index_table = pindex;
142  check_nomsg(pret->index_size = cpl_table_get_nrow(pindex));
143  return pret;
144  cleanup:
145  star_index_destruct(pret);
146  cpl_error_reset();
147  return 0;
148 }
149 void star_index_delete(star_index* pindex)
150 {
151  star_index_destruct(pindex);
152 }
153 int star_index_add(star_index* pindex, double RA, double DEC, const char* star_name, cpl_table* ptable)
154 {
155  int retval = 0;
156  if (pindex)
157  {
158  // expand the index table
159  check_nomsg(cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1));
160  if (!pindex->cache)
161  {
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);
165  }
166  else
167  {
168  // add new entry
169  pindex->cache_size++;
170  pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
171  }
172  check_nomsg(pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable));
173  // fill the index table with values
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;
179  }
180  return retval;
181 
182  cleanup:
183  //printf ("error: %s\n", cpl_error_get_message());
184  return 0;
185 }
186 
187 int start_index_get_size(star_index* pindex)
188 {
189  return pindex ? pindex->index_size : 0;
190 }
191 
192 int star_index_remove_by_name(star_index* pindex, const char* starname)
193 {
194  int i = 0;
195  int index_pos = -1;
196  for (i = 0; i < pindex->index_size; i++)
197  {
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)
201  {
202  index_pos = i;
203  break;
204  }
205  }
206  if (index_pos >= 0)
207  {
208  // star is found
209  // clear only the index table, real data would be cleaned during save operation
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)
212  {
213  // clear cache
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;
217  }
218  }
219  cleanup:
220  return index_pos;
221 }
222 
223 int star_index_save(star_index* pindex, const char* fits_file)
224 {
225  int i = 0;
226  int inull = 0;
227  cpl_table* pnew_index = 0;
228  int nrows = 0;
229  // firstly save the index table - deleted entries should be removed firstly
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));
232  // inverse selection
233  check_nomsg(cpl_table_not_selected(pindex->index_table));
234  check_nomsg(pnew_index = cpl_table_extract_selected(pindex->index_table));
235 
236  nrows = cpl_table_get_nrow(pnew_index);
237  // printf("rows to save[%d]\n", nrows);
238  for (i = 0; i < nrows; i++)
239  {
240  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
241  }
242  // printf("writing index [%s]\n", fits_file);
243  check_nomsg(cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE));
244  cpl_table_delete(pnew_index);
245  pnew_index = 0;
246  // save the data
247  for (i = 0;i < pindex->index_size; i++)
248  {
249  // printf("saving ext [%d]\n", i);
250  // 2. save cache
251  int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
252  // printf("saving 1\n");
253  if (saved_ext > 0) // check_nomsg that was not removed
254  {
255  cpl_table* ptable = 0;
256  // printf("saving 2\n");
257  if (i < pindex->index_size - pindex->cache_size)
258  {
259  // printf("saving 3\n");
260  check_nomsg(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
261  }
262  else
263  {
264  // printf("saving 4\n");
265  ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
266  }
267  // printf("saving 5\n");
268  check_nomsg(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
269  // printf("saving 6\n");
270  cpl_table_delete(ptable);
271  // printf("saving 7\n");
272  }
273  // printf("saving 8\n");
274  }
275  // printf("saving exit\n");
276  return nrows;
277  cleanup:
278  // printf("error during save\n");
279  return 0;
280 }
281 cpl_table* star_index_get(star_index* pindex, double RA, double DEC, double RA_EPS, double DEC_EPS, const char** pstar_name)
282 {
283  int i = 0;
284  cpl_table* pret = 0;
285  int inull = 0;
286 
287  for (i = 0; i < pindex->index_size; i++)
288  {
289  double curr_ra = 0;
290  double curr_dec = 0;
291  int ext_id = 0;
292 
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))
297  {
298  // found
299  // retrieve the data
300  if (i - pindex->index_size + pindex->cache_size >= 0)
301  {
302  // data is in cache
303  pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
304  }
305  else
306  {
307  // data is on disk
308  pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
309  }
310  if (pret && pstar_name)
311  {
312  check_nomsg(*pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
313  }
314  break;
315  }
316  }
317  cleanup:
318  return pret;
319 }
320 
321 void star_index_dump(star_index* pindex, FILE* pfile)
322 {
323  cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
324 }