sinfo_star_index.c

00001 /* $Id: sinfo_star_index.c,v 1.8 2010/08/20 08:58:44 kmirny Exp $
00002  *
00003  * This file is part of the X-Shooter Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 /*
00021  * $Author: kmirny $
00022  * $Date: 2010/08/20 08:58:44 $
00023  * $Revision: 1.8 $
00024  * $Name: sinfo-2_2_5 $
00025  */
00026 
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #include <config.h>          /* allows the program compilation */
00031 #endif
00032 
00033 #include <cpl.h>
00034 #include <string.h>
00035 #include <math.h>
00036 
00037 
00038 //#include "sinfo_pro_save.h"
00039 //#include "sinfo_pfits.h"
00040 //#include "sinfo_utilities_scired.h"
00041 //#include "sinfo_hidden.h"
00042 //#include "sinfo_functions.h"
00043 #include "sinfo_error.h"
00044 #include "sinfo_msg.h"
00045 #include "sinfo_utils_wrappers.h"
00046 //#include "sinfo_globals.h"
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 //typedef struct _star_index_ star_index;
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 // private functions
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     // initialize table
00119     check_nomsg(pret->index_table = cpl_table_new(pret->index_size));
00120     // create columns ext_id, name, ra, dec
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     // load index table from the file
00135     cpl_table* pindex = 0;
00136     check_nomsg(pindex = cpl_table_load(fits_file,1,0));
00137     // TODO check_nomsg the structure of the table
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         // expand the index table
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             // add new entry
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         // fill the index table with values
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     //printf ("error: %s\n", cpl_error_get_message());
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         // star is found
00206         // clear only the index table, real data would be cleaned during save operation
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             // clear cache
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     // firstly save the index table - deleted entries should be removed firstly
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     // inverse selection
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 //  printf("rows to save[%d]\n", nrows);
00235     for (i = 0; i < nrows; i++)
00236     {
00237         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
00238     }
00239 //  printf("writing index [%s]\n", fits_file);
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     // save the data
00244     for (i = 0;i < pindex->index_size; i++)
00245     {
00246 //      printf("saving ext [%d]\n", i);
00247         // 2. save cache
00248         int saved_ext = cpl_table_get_int(pindex->index_table, COL_NAME_EXTID, i, &inull);
00249 //      printf("saving 1\n");
00250         if (saved_ext > 0) // check_nomsg that was not removed
00251         {
00252             cpl_table* ptable = 0;
00253 //          printf("saving 2\n");
00254             if (i < pindex->index_size - pindex->cache_size)
00255             {
00256 //              printf("saving 3\n");
00257                 check_nomsg(ptable = cpl_table_load(pindex->fits_file_name, saved_ext, 0));
00258             }
00259             else
00260             {
00261 //              printf("saving 4\n");
00262                 ptable = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00263             }
00264 //          printf("saving 5\n");
00265             check_nomsg(cpl_table_save(ptable, NULL, NULL, fits_file, CPL_IO_EXTEND));
00266 //          printf("saving 6\n");
00267             cpl_table_delete(ptable);
00268 //          printf("saving 7\n");
00269         }
00270 //      printf("saving 8\n");
00271     }
00272 //  printf("saving exit\n");
00273     return nrows;
00274     cleanup:
00275 //  printf("error during save\n");
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             // found
00296             // retrieve the data
00297             if (i - pindex->index_size + pindex->cache_size  >= 0)
00298             {
00299                 // data is in cache
00300                 pret = cpl_table_duplicate(pindex->cache[i - pindex->index_size + pindex->cache_size ]);
00301             }
00302             else
00303             {
00304                 // data is on disk
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 }

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1