X-shooter Pipeline Reference Manual 3.8.15
xsh_star_index.c
Go to the documentation of this file.
1/* $Id: xsh_star_index.c,v 1.3 2011-12-02 14:15:28 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: 2011-12-02 14:15:28 $
23 * $Revision: 1.3 $
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 "xsh_pro_save.h"
39//#include "xsh_pfits.h"
40//#include "xsh_utilities_scired.h"
41//#include "xsh_hidden.h"
42//#include "xsh_functions.h"
43#include "xsh_error.h"
44#include "xsh_utils_wrappers.h"
45//#include "xsh_globals.h"
46#include "xsh_star_index.h"
47
49{
50 cpl_table* index_table;
53 cpl_table** cache;
56};
57
58//typedef struct _star_index_ star_index;
59static const char* COL_NAME_EXTID = "ext_id";
60static const char* COL_NAME_NAME = "name";
61static const char* COL_NAME_RA = "ra";
62static const char* COL_NAME_DEC = "dec";
63
64static star_index* star_index_construct(const char* fits_file);
65static void star_index_destruct(star_index* pindex);
66// private functions
67
68static star_index* star_index_construct(const char* fits_file)
69{
70 star_index* pret = cpl_malloc(sizeof(star_index));
71 pret->index_size = 0;
72 pret->index_table = 0;
73 pret->cache_size = 0;
74 pret->cache = 0;
75 pret->cache_index = 0;
76 if (fits_file)
77 {
78 size_t bt = strlen(fits_file) * sizeof(*fits_file)+1;
79 pret->fits_file_name = cpl_malloc(bt);
80 strcpy(pret->fits_file_name, fits_file);
81 }
82 else
83 {
84 pret->fits_file_name = 0;
85 }
86 return pret;
87}
88
89static void star_index_destruct(star_index* pindex)
90{
91 if(pindex)
92 {
93 if (pindex->cache)
94 {
95 int i = 0;
96 for ( i = 0; i < pindex->cache_size; i++)
97 {
98 cpl_table_delete(pindex->cache[i]);
99 }
100 cpl_free(pindex->cache);
101 pindex->cache = 0;
102 pindex->cache_size = 0;
103 }
104 cpl_table_delete(pindex->index_table);
105 if(pindex->fits_file_name)
106 {
107 cpl_free(pindex->fits_file_name);
108 }
109 cpl_free(pindex->cache_index);
110 cpl_free(pindex);
111 }
112
113}
115{
117 // initialize table
118 pret->index_table = cpl_table_new(pret->index_size);
119 // create columns ext_id, name, ra, dec
120 check(cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT));
121 check(cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING));
122 check(cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE));
123 check(cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE));
124 return pret;
125 cleanup:
127 return 0;
128}
129star_index* star_index_load(const char* fits_file)
130{
131 star_index* pret = star_index_construct(fits_file);
132 // load index table from the file
133 cpl_table* pindex = 0;
134 check(pindex = cpl_table_load(fits_file,1,0));
135 // TODO check the structure of the table
136 pret->index_table = pindex;
137 check(pret->index_size = cpl_table_get_nrow(pindex));
138 return pret;
139 cleanup:
141 cpl_error_reset();
142 return 0;
143}
145{
146 star_index_destruct(pindex);
147}
148int star_index_add(star_index* pindex, double RA, double DEC, const char* star_name, cpl_table* ptable)
149{
150 int retval = 0;
151 if (pindex)
152 {
153 // expand the index table
154
155 check(cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1));
156 if (!pindex->cache)
157 {
158 pindex->cache_size = 1;
159 pindex->cache = cpl_malloc(sizeof(cpl_table*) * pindex->cache_size);
160 pindex->cache_index = cpl_malloc(sizeof(pindex->cache_index[0]) * pindex->cache_size);
161 }
162 else
163 {
164 // add new entry
165 pindex->cache_size++;
166 pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
167 }
168 check(pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable));
169 // fill the index table with values
170 check(cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name));
171 check(cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA));
172 check(cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC));
173 check(cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1));
174 retval = pindex->index_size;
175 }
176 return retval;
177
178 cleanup:
179 return 0;
180}
181
183{
184 return pindex ? pindex->index_size : 0;
185}
186
187int star_index_remove_by_name(star_index* pindex, const char* starname)
188{
189 int i = 0;
190 int index_pos = -1;
191 for (i = 0; i < pindex->index_size; i++)
192 {
193 const char* curr_star_name = 0;
194 check(curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
195 if (strcmp(curr_star_name, starname) == 0)
196 {
197 index_pos = i;
198 break;
199 }
200 }
201 if (index_pos >= 0)
202 {
203 // star is found
204 // clear only the index table, real data would be cleaned during save operation
205 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
206 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
207 {
208 // clear cache
209 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
210 cpl_table_delete(pindex->cache[cache_index]);
211 pindex->cache[cache_index] = 0;
212 }
213 }
214 cleanup:
215 return index_pos;
216}
217
218int star_index_save(star_index* pindex, const char* fits_file)
219{
220 int i = 0;
221 int inull = 0;
222 cpl_table* pnew_index = 0;
223 int nrows = 0;
224 // firstly save the index table - deleted entries should be removed firstly
225 check(cpl_table_unselect_all(pindex->index_table));
226 check(cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1));
227 // inverse selection
228 check(cpl_table_not_selected(pindex->index_table));
229 check(pnew_index = cpl_table_extract_selected(pindex->index_table));
230
231 nrows = cpl_table_get_nrow(pnew_index);
232 for (i = 0; i < nrows; i++)
233 {
234 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
235 }
236 check(cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE));
237 cpl_table_delete(pnew_index);
238 pnew_index = 0;
239 // save the data
240 for (i = 0;i < pindex->index_size; i++)
241 {
242 // 2. save cache
243 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
244 if (saved_ext > 0) // check that was not removed
245 {
246 cpl_table* ptable = 0;
247 if (i < pindex->index_size - pindex->cache_size)
248 {
249 check(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
250 }
251 else
252 {
253 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
254 }
255 check(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
256 cpl_table_delete(ptable);
257 }
258 }
259 cleanup:
260 return nrows;
261}
262cpl_table* star_index_get(star_index* pindex, double RA, double DEC, double RA_EPS, double DEC_EPS, const char** pstar_name)
263{
264 int i = 0;
265 cpl_table* pret = 0;
266 int inull = 0;
267
268 for (i = 0; i < pindex->index_size; i++)
269 {
270 double curr_ra = 0;
271 double curr_dec = 0;
272 int ext_id = 0;
273
274 check(ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull));
275 check(curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull));
276 check(curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull));
277 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
278 {
279 // found
280 // retrieve the data
281 if (i - pindex->index_size + pindex->cache_size >= 0)
282 {
283 // data is in cache
284 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
285 }
286 else
287 {
288 // data is on disk
289 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
290 }
291 if (pret && pstar_name)
292 {
293 check(*pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
294 }
295 break;
296 }
297 }
298 cleanup:
299 return pret;
300}
301
302void star_index_dump(star_index* pindex, FILE* pfile)
303{
304 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
305}
#define check(COMMAND)
Definition: xsh_error.h:71
cpl_table * index_table
char * fits_file_name
cpl_table ** cache
static const char * COL_NAME_DEC
int star_index_add(star_index *pindex, double RA, double DEC, const char *star_name, cpl_table *ptable)
void star_index_dump(star_index *pindex, FILE *pfile)
void star_index_delete(star_index *pindex)
static const char * COL_NAME_RA
static const char * COL_NAME_EXTID
star_index * star_index_create(void)
static star_index * star_index_construct(const char *fits_file)
star_index * star_index_load(const char *fits_file)
int star_index_remove_by_name(star_index *pindex, const char *starname)
int star_index_save(star_index *pindex, const char *fits_file)
static void star_index_destruct(star_index *pindex)
cpl_table * star_index_get(star_index *pindex, double RA, double DEC, double RA_EPS, double DEC_EPS, const char **pstar_name)
static const char * COL_NAME_NAME
int start_index_get_size(star_index *pindex)