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
00118 star_index* star_index_create(void)
00119 {
00120 star_index* pret = star_index_construct(0);
00121
00122 check_nomsg(pret->index_table = cpl_table_new(pret->index_size));
00123
00124 cpl_table_new_column(pret->index_table, COL_NAME_EXTID, CPL_TYPE_INT);
00125 cpl_table_new_column(pret->index_table, COL_NAME_NAME, CPL_TYPE_STRING);
00126 cpl_table_new_column(pret->index_table, COL_NAME_RA, CPL_TYPE_DOUBLE);
00127 cpl_table_new_column(pret->index_table, COL_NAME_DEC, CPL_TYPE_DOUBLE);
00128
00129 return pret;
00130 cleanup:
00131 star_index_destruct(pret);
00132 return 0;
00133 }
00134 star_index* star_index_load(const char* fits_file)
00135 {
00136 star_index* pret = star_index_construct(fits_file);
00137
00138 cpl_table* pindex = 0;
00139 check_nomsg(pindex = cpl_table_load(fits_file,1,0));
00140
00141 pret->index_table = pindex;
00142 check_nomsg(pret->index_size = cpl_table_get_nrow(pindex));
00143 return pret;
00144 cleanup:
00145 star_index_destruct(pret);
00146 cpl_error_reset();
00147 return 0;
00148 }
00149 void star_index_delete(star_index* pindex)
00150 {
00151 star_index_destruct(pindex);
00152 }
00153 int star_index_add(star_index* pindex, double RA, double DEC, const char* star_name, cpl_table* ptable)
00154 {
00155 int retval = 0;
00156 if (pindex)
00157 {
00158
00159 check_nomsg(cpl_table_insert_window(pindex->index_table, pindex->index_size++, 1));
00160 if (!pindex->cache)
00161 {
00162 pindex->cache_size = 1;
00163 pindex->cache = cpl_malloc(sizeof(cpl_table*) * pindex->cache_size);
00164 pindex->cache_index = cpl_malloc(sizeof(pindex->cache_index[0]) * pindex->cache_size);
00165 }
00166 else
00167 {
00168
00169 pindex->cache_size++;
00170 pindex->cache = cpl_realloc(pindex->cache, sizeof(cpl_table*) * pindex->cache_size);
00171 }
00172 check_nomsg(pindex->cache[pindex->cache_size - 1] = cpl_table_duplicate(ptable));
00173
00174 check_nomsg(cpl_table_set_string(pindex->index_table, COL_NAME_NAME, pindex->index_size - 1 ,star_name));
00175 check_nomsg(cpl_table_set(pindex->index_table, COL_NAME_RA, pindex->index_size - 1 ,RA));
00176 check_nomsg(cpl_table_set(pindex->index_table, COL_NAME_DEC, pindex->index_size - 1,DEC));
00177 check_nomsg(cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, pindex->index_size - 1 ,pindex->index_size + 1));
00178 retval = pindex->index_size;
00179 }
00180 return retval;
00181
00182 cleanup:
00183
00184 return 0;
00185 }
00186
00187 int start_index_get_size(star_index* pindex)
00188 {
00189 return pindex ? pindex->index_size : 0;
00190 }
00191
00192 int star_index_remove_by_name(star_index* pindex, const char* starname)
00193 {
00194 int i = 0;
00195 int index_pos = -1;
00196 for (i = 0; i < pindex->index_size; i++)
00197 {
00198 const char* curr_star_name = 0;
00199 check_nomsg(curr_star_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
00200 if (strcmp(curr_star_name, starname) == 0)
00201 {
00202 index_pos = i;
00203 break;
00204 }
00205 }
00206 if (index_pos >= 0)
00207 {
00208
00209
00210 cpl_table_set_int(pindex->index_table, COL_NAME_EXTID, index_pos, -1);
00211 if (index_pos - pindex->index_size + pindex->cache_size >= 0)
00212 {
00213
00214 int cache_index = index_pos - pindex->index_size + pindex->cache_size;
00215 cpl_table_delete(pindex->cache[cache_index]);
00216 pindex->cache[cache_index] = 0;
00217 }
00218 }
00219 cleanup:
00220 return index_pos;
00221 }
00222
00223 int star_index_save(star_index* pindex, const char* fits_file)
00224 {
00225 int i = 0;
00226 int inull = 0;
00227 cpl_table* pnew_index = 0;
00228 int nrows = 0;
00229
00230 check_nomsg(cpl_table_unselect_all(pindex->index_table));
00231 check_nomsg(cpl_table_or_selected_int(pindex->index_table, COL_NAME_EXTID, CPL_EQUAL_TO, -1));
00232
00233 check_nomsg(cpl_table_not_selected(pindex->index_table));
00234 check_nomsg(pnew_index = cpl_table_extract_selected(pindex->index_table));
00235
00236 nrows = cpl_table_get_nrow(pnew_index);
00237
00238 for (i = 0; i < nrows; i++)
00239 {
00240 cpl_table_set_int(pnew_index, COL_NAME_EXTID, i, i+2);
00241 }
00242
00243 check_nomsg(cpl_table_save(pnew_index, NULL, NULL, fits_file, CPL_IO_CREATE));
00244 cpl_table_delete(pnew_index);
00245 pnew_index = 0;
00246
00247 for (i = 0;i < pindex->index_size; i++)
00248 {
00249
00250
00251 int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
00252
00253 if (saved_ext > 0)
00254 {
00255 cpl_table* ptable = 0;
00256
00257 if (i < pindex->index_size - pindex->cache_size)
00258 {
00259
00260 check_nomsg(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
00261 }
00262 else
00263 {
00264
00265 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00266 }
00267
00268 check_nomsg(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
00269
00270 cpl_table_delete(ptable);
00271
00272 }
00273
00274 }
00275
00276 return nrows;
00277 cleanup:
00278
00279 return 0;
00280 }
00281 cpl_table* star_index_get(star_index* pindex, double RA, double DEC, double RA_EPS, double DEC_EPS, const char** pstar_name)
00282 {
00283 int i = 0;
00284 cpl_table* pret = 0;
00285 int inull = 0;
00286
00287 for (i = 0; i < pindex->index_size; i++)
00288 {
00289 double curr_ra = 0;
00290 double curr_dec = 0;
00291 int ext_id = 0;
00292
00293 check_nomsg(ext_id = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i ,&inull));
00294 check_nomsg(curr_ra = cpl_table_get(pindex->index_table, COL_NAME_RA, i,&inull));
00295 check_nomsg(curr_dec = cpl_table_get(pindex->index_table, COL_NAME_DEC, i,&inull));
00296 if ((ext_id > 0) && (fabs(curr_ra - RA) < RA_EPS) && (fabs(curr_dec - DEC) < DEC_EPS))
00297 {
00298
00299
00300 if (i - pindex->index_size + pindex->cache_size >= 0)
00301 {
00302
00303 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00304 }
00305 else
00306 {
00307
00308 pret = cpl_table_load(pindex->fits_file_name, ext_id, 0);
00309 }
00310 if (pret && pstar_name)
00311 {
00312 check_nomsg(*pstar_name = cpl_table_get_string(pindex->index_table, COL_NAME_NAME, i));
00313 }
00314 break;
00315 }
00316 }
00317 cleanup:
00318 return pret;
00319 }
00320
00321 void star_index_dump(star_index* pindex, FILE* pfile)
00322 {
00323 cpl_table_dump(pindex->index_table, 0, cpl_table_get_nrow(pindex->index_table), pfile);
00324 }