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