00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032
00033 #include <cpl.h>
00034 #include <string.h>
00035 #include <math.h>
00036
00037
00038
00039
00040
00041
00042
00043 #include "xsh_error.h"
00044 #include "xsh_utils_wrappers.h"
00045
00046 #include "xsh_star_index.h"
00047
00048 struct _star_index_
00049 {
00050 cpl_table* index_table;
00051 char* fits_file_name;
00052 int index_size;
00053 cpl_table** cache;
00054 int cache_size;
00055 int* cache_index;
00056 };
00057
00058
00059 static const char* COL_NAME_EXTID = "ext_id";
00060 static const char* COL_NAME_NAME = "name";
00061 static const char* COL_NAME_RA = "ra";
00062 static const char* COL_NAME_DEC = "dec";
00063
00064 static star_index* star_index_construct(const char* fits_file);
00065 static void star_index_destruct(star_index* pindex);
00066
00067
00068 static star_index* star_index_construct(const char* fits_file)
00069 {
00070 star_index* pret = cpl_malloc(sizeof(star_index));
00071 pret->index_size = 0;
00072 pret->index_table = 0;
00073 pret->cache_size = 0;
00074 pret->cache = 0;
00075 pret->cache_index = 0;
00076 if (fits_file)
00077 {
00078 size_t bt = strlen(fits_file) * sizeof(*fits_file)+1;
00079 pret->fits_file_name = cpl_malloc(bt);
00080 strcpy(pret->fits_file_name, fits_file);
00081 }
00082 else
00083 {
00084 pret->fits_file_name = 0;
00085 }
00086 return pret;
00087 }
00088
00089 static void star_index_destruct(star_index* pindex)
00090 {
00091 if(pindex)
00092 {
00093 if (pindex->cache)
00094 {
00095 int i = 0;
00096 for ( i = 0; i < pindex->cache_size; i++)
00097 {
00098 cpl_table_delete(pindex->cache[i]);
00099 }
00100 cpl_free(pindex->cache);
00101 pindex->cache = 0;
00102 pindex->cache_size = 0;
00103 }
00104 cpl_table_delete(pindex->index_table);
00105 if(pindex->fits_file_name)
00106 {
00107 cpl_free(pindex->fits_file_name);
00108 }
00109 cpl_free(pindex->cache_index);
00110 cpl_free(pindex);
00111 }
00112
00113 }
00114 star_index* star_index_create(void)
00115 {
00116 star_index* pret = star_index_construct(0);
00117
00118 pret->index_table = cpl_table_new(pret->index_size);
00119
00120 check(cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT));
00121 check(cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING));
00122 check(cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE));
00123 check(cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE));
00124 return pret;
00125 cleanup:
00126 star_index_destruct(pret);
00127 return 0;
00128 }
00129 star_index* star_index_load(const char* fits_file)
00130 {
00131 star_index* pret = star_index_construct(fits_file);
00132
00133 cpl_table* pindex = 0;
00134 check(pindex = cpl_table_load(fits_file,1,0));
00135
00136 pret->index_table = pindex;
00137 check(pret->index_size = cpl_table_get_nrow(pindex));
00138 return pret;
00139 cleanup:
00140 star_index_destruct(pret);
00141 cpl_error_reset();
00142 return 0;
00143 }
00144 void star_index_delete(star_index* pindex)
00145 {
00146 star_index_destruct(pindex);
00147 }
00148 int star_index_add(star_index* pindex, double RA, double DEC, const char* star_name, cpl_table* ptable)
00149 {
00150 int retval = 0;
00151 if (pindex)
00152 {
00153
00154
00155 check(cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1));
00156 if (!pindex->cache)
00157 {
00158 pindex->cache_size = 1;
00159 pindex->cache = cpl_malloc(sizeof(cpl_table*) * pindex->cache_size);
00160 pindex->cache_index = cpl_malloc(sizeof(pindex->cache_index[0]) * pindex->cache_size);
00161 }
00162 else
00163 {
00164
00165 pindex->cache_size++;
00166 pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
00167 }
00168 check(pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable));
00169
00170 check(cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name));
00171 check(cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA));
00172 check(cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC));
00173 check(cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1));
00174 retval = pindex->index_size;
00175 }
00176 return retval;
00177
00178 cleanup:
00179 return 0;
00180 }
00181
00182 int start_index_get_size(star_index* pindex)
00183 {
00184 return pindex ? pindex->index_size : 0;
00185 }
00186
00187 int star_index_remove_by_name(star_index* pindex, const char* starname)
00188 {
00189 int i = 0;
00190 int index_pos = -1;
00191 for (i = 0; i < pindex->index_size; i++)
00192 {
00193 const char* curr_star_name = 0;
00194 check(curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
00195 if (strcmp(curr_star_name, starname) == 0)
00196 {
00197 index_pos = i;
00198 break;
00199 }
00200 }
00201 if (index_pos >= 0)
00202 {
00203
00204
00205 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
00206 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
00207 {
00208
00209 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
00210 cpl_table_delete(pindex->cache[cache_index]);
00211 pindex->cache[cache_index] = 0;
00212 }
00213 }
00214 cleanup:
00215 return index_pos;
00216 }
00217
00218 int star_index_save(star_index* pindex, const char* fits_file)
00219 {
00220 int i = 0;
00221 int inull = 0;
00222 cpl_table* pnew_index = 0;
00223 int nrows = 0;
00224
00225 check(cpl_table_unselect_all(pindex->index_table));
00226 check(cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1));
00227
00228 check(cpl_table_not_selected(pindex->index_table));
00229 check(pnew_index = cpl_table_extract_selected(pindex->index_table));
00230
00231 nrows = cpl_table_get_nrow(pnew_index);
00232 for (i = 0; i < nrows; i++)
00233 {
00234 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2);
00235 }
00236 check(cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE));
00237 cpl_table_delete(pnew_index);
00238 pnew_index = 0;
00239
00240 for (i = 0;i < pindex->index_size; i++)
00241 {
00242
00243 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
00244 if (saved_ext > 0)
00245 {
00246 cpl_table* ptable = 0;
00247 if (i < pindex->index_size - pindex->cache_size)
00248 {
00249 check(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
00250 }
00251 else
00252 {
00253 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00254 }
00255 check(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
00256 cpl_table_delete(ptable);
00257 }
00258 }
00259 cleanup:
00260 return nrows;
00261 }
00262 cpl_table* star_index_get(star_index* pindex, double RA, double DEC, double RA_EPS, double DEC_EPS, const char** pstar_name)
00263 {
00264 int i = 0;
00265 cpl_table* pret = 0;
00266 int inull = 0;
00267
00268 for (i = 0; i < pindex->index_size; i++)
00269 {
00270 double curr_ra = 0;
00271 double curr_dec = 0;
00272 int ext_id = 0;
00273
00274 check(ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull));
00275 check(curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull));
00276 check(curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull));
00277 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
00278 {
00279
00280
00281 if (i - pindex->index_size + pindex->cache_size >= 0)
00282 {
00283
00284 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00285 }
00286 else
00287 {
00288
00289 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
00290 }
00291 if (pret && pstar_name)
00292 {
00293 check(*pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
00294 }
00295 break;
00296 }
00297 }
00298 cleanup:
00299 return pret;
00300 }
00301
00302 void star_index_dump(star_index* pindex, FILE* pfile)
00303 {
00304 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
00305 }