HAWKI Pipeline Reference Manual 1.8.12
hawki_util_stdstars.c
00001 /* $Id: hawki_util_stdstars.c,v 1.16 2011/07/06 08:21:36 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI 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 /*
00022  * $Author: cgarcia $
00023  * $Date: 2011/07/06 08:21:36 $
00024  * $Revision: 1.16 $
00025  * $Name: hawki-1_8_12 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_utils.h"
00040 #include "irplib_stdstar.h"
00041 
00042 #include "hawki_utils.h"
00043 #include "hawki_load.h"
00044 #include "hawki_pfits.h"
00045 #include "hawki_dfs.h"
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 
00051 static int hawki_util_stdstars_create(cpl_plugin *) ;
00052 static int hawki_util_stdstars_exec(cpl_plugin *) ;
00053 static int hawki_util_stdstars_destroy(cpl_plugin *) ;
00054 static int hawki_util_stdstars(cpl_frameset *) ;
00055 static cpl_table * hawki_util_stdstars_convert(const char *) ; 
00056 
00057 /*-----------------------------------------------------------------------------
00058                             Static variables
00059  -----------------------------------------------------------------------------*/
00060 
00061 static char hawki_util_stdstars_description[] = 
00062 "hawki_util_stdstars -- HAWK-I standard stars catalog creation.\n"
00063 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00064 "raw-file.txt "HAWKI_UTIL_STDSTARS_RAW"\n" ;
00065 
00066 /*-----------------------------------------------------------------------------
00067                                 Functions code
00068  -----------------------------------------------------------------------------*/
00069 
00070 /*----------------------------------------------------------------------------*/
00078 /*----------------------------------------------------------------------------*/
00079 int cpl_plugin_get_info(cpl_pluginlist * list)
00080 {
00081     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00082     cpl_plugin  *   plugin = &recipe->interface ;
00083 
00084     cpl_plugin_init(plugin,
00085                     CPL_PLUGIN_API,
00086                     HAWKI_BINARY_VERSION,
00087                     CPL_PLUGIN_TYPE_RECIPE,
00088                     "hawki_util_stdstars",
00089                     "Standard stars catalog creation",
00090                     hawki_util_stdstars_description,
00091                     "Cesar Enrique Garcia Dabo",
00092                     PACKAGE_BUGREPORT,  
00093                     hawki_get_license(),
00094                     hawki_util_stdstars_create,
00095                     hawki_util_stdstars_exec,
00096                     hawki_util_stdstars_destroy) ;
00097 
00098     cpl_pluginlist_append(list, plugin) ;
00099     
00100     return 0;
00101 }
00102 
00103 /*----------------------------------------------------------------------------*/
00112 /*----------------------------------------------------------------------------*/
00113 static int hawki_util_stdstars_create(cpl_plugin * plugin)
00114 {
00115     cpl_recipe      * recipe ;
00116 
00117     /* Get the recipe out of the plugin */
00118     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00119         recipe = (cpl_recipe *)plugin ;
00120     else return -1 ;
00121 
00122     /* Create the parameters list in the cpl_recipe object */
00123     recipe->parameters = cpl_parameterlist_new() ;
00124 
00125     /* Return */
00126     return 0;
00127 }
00128 
00129 /*----------------------------------------------------------------------------*/
00135 /*----------------------------------------------------------------------------*/
00136 static int hawki_util_stdstars_exec(cpl_plugin * plugin)
00137 {
00138     cpl_recipe  *   recipe ;
00139 
00140     /* Get the recipe out of the plugin */
00141     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00142         recipe = (cpl_recipe *)plugin ;
00143     else return -1 ;
00144 
00145     /* Issue a banner */
00146     hawki_print_banner();
00147 
00148     return hawki_util_stdstars(recipe->frames) ;
00149 }
00150 
00151 /*----------------------------------------------------------------------------*/
00157 /*----------------------------------------------------------------------------*/
00158 static int hawki_util_stdstars_destroy(cpl_plugin * plugin)
00159 {
00160     cpl_recipe  *   recipe ;
00161 
00162     /* Get the recipe out of the plugin */
00163     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00164         recipe = (cpl_recipe *)plugin ;
00165     else return -1 ;
00166 
00167     cpl_parameterlist_delete(recipe->parameters) ;
00168     return 0 ;
00169 }
00170 
00171 /*----------------------------------------------------------------------------*/
00177 /*----------------------------------------------------------------------------*/
00178 static int hawki_util_stdstars(
00179         cpl_frameset        *   framelist)
00180 {
00181     cpl_frameset        *   rawframes ;
00182     const char          *   recipe_name = "hawki_util_stdstars" ;
00183 
00184     
00185     /* Identify the RAW and CALIB frames in the input frameset */
00186     if (hawki_dfs_set_groups(framelist)) {
00187         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00188         return -1 ;
00189     }
00190 
00191     /* Retrieve raw frames */
00192     if ((rawframes = hawki_extract_frameset(framelist,
00193                     HAWKI_UTIL_STDSTARS_RAW)) == NULL) {
00194         cpl_msg_error(__func__, "Cannot find raw frames in the input list") ;
00195         return -1 ;
00196     }
00197 
00198     /* Write the catalog */
00199     if (irplib_stdstar_write_catalogs(framelist,
00200                 rawframes, 
00201                 recipe_name,
00202                 HAWKI_CALPRO_STDSTARS, 
00203                 HAWKI_PROTYPE_STDSTARS,
00204                 PACKAGE "/" PACKAGE_VERSION,
00205                 "HAWKI",
00206                 hawki_util_stdstars_convert) == -1) {
00207         cpl_msg_error(__func__, "Cannot write the catalogs") ;
00208         cpl_frameset_delete(rawframes) ;
00209         return -1 ;
00210     }
00211     cpl_frameset_delete(rawframes) ;
00212 
00213     /* Return */
00214     if (cpl_error_get_code())
00215     {
00216         cpl_msg_error(__func__,
00217                       "HAWK-I pipeline could not recover from previous errors");
00218         return -1 ;
00219     }
00220     else return 0 ;
00221 }
00222 
00223 /*----------------------------------------------------------------------------*/
00244 /*----------------------------------------------------------------------------*/
00245 static cpl_table * hawki_util_stdstars_convert(const char * filename) 
00246 {
00247     cpl_table   *   out ;
00248     int             nfilters ;
00249     const char  *   filters[4];
00250     double          mags[4] ;
00251     int             nbentries ;
00252     FILE        *   in ;
00253     char            line[1024];
00254     double          ra, dec ;
00255     char            sname[512];
00256     char            stype[512];
00257     int             i ;
00258     
00259     /* Check entries */
00260     if (filename == NULL) return NULL ;
00261 
00262     /* Initialise */
00263     nfilters = 4 ;
00264     filters[0] = hawki_std_band_name(HAWKI_BAND_J) ;
00265     filters[1] = hawki_std_band_name(HAWKI_BAND_H) ;
00266     filters[2] = hawki_std_band_name(HAWKI_BAND_K) ;
00267     filters[3] = hawki_std_band_name(HAWKI_BAND_Y) ;
00268    
00269     /* Get the number of lines */
00270     nbentries = 0 ;
00271     if ((in = fopen(filename, "r")) == NULL) {
00272         return NULL ;
00273     }
00274     while (fgets(line, 1024, in) != NULL) {
00275         if (line[0] != '#') nbentries ++ ;
00276     }
00277     fclose(in) ;
00278     
00279    /* Create the table */
00280     out = cpl_table_new(nbentries);
00281     cpl_table_new_column(out, IRPLIB_STDSTAR_STAR_COL, CPL_TYPE_STRING);
00282     cpl_table_new_column(out, IRPLIB_STDSTAR_TYPE_COL, CPL_TYPE_STRING);
00283     cpl_table_new_column(out, IRPLIB_STDSTAR_RA_COL, CPL_TYPE_DOUBLE);
00284     cpl_table_new_column(out, IRPLIB_STDSTAR_DEC_COL, CPL_TYPE_DOUBLE);
00285     for (i=0 ; i<nfilters ; i++)
00286         cpl_table_new_column(out, filters[i], CPL_TYPE_DOUBLE);
00287 
00288     /* Parse the file */
00289     if ((in = fopen(filename, "r")) == NULL) {
00290         cpl_table_delete(out) ;
00291         return NULL ;
00292     }
00293     nbentries = 0 ;
00294     while (fgets(line, 1024, in) != NULL) {
00295         if (line[0] != '#') {
00296             if (sscanf(line, "%s %lg %lg %s %lg %lg %lg %lg", 
00297                         sname, &ra, &dec, stype, &(mags[0]), &(mags[1]),
00298                         &(mags[2]), &(mags[3])) != 8) {
00299                 cpl_table_delete(out) ;
00300                 return NULL ;
00301             }
00302             cpl_table_set_string(out, IRPLIB_STDSTAR_STAR_COL,nbentries, sname);
00303             cpl_table_set_string(out, IRPLIB_STDSTAR_TYPE_COL,nbentries, stype);
00304             cpl_table_set_double(out, IRPLIB_STDSTAR_RA_COL, nbentries, ra);
00305             cpl_table_set_double(out, IRPLIB_STDSTAR_DEC_COL, nbentries, dec);
00306             for (i=0 ; i<nfilters ; i++)
00307                 cpl_table_set_double(out, filters[i], nbentries, mags[i]);
00308             nbentries ++ ;
00309         }
00310     }
00311     fclose(in) ;
00312                 
00313     return out ;
00314 }
00315