HAWKI Pipeline Reference Manual 1.8.6
hawki_tec_filtchk.c
00001 /* $Id: hawki_tec_filtchk.c,v 1.15 2011/10/24 10:41:32 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/10/24 10:41:32 $
00024  * $Revision: 1.15 $
00025  * $Name: hawki-1_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include <hawki_dfs.h>
00041 #include <hawki_load.h>
00042 #include <hawki_save.h>
00043 #include <hawki_pfits.h>
00044 #include <hawki_image_stats.h>
00045 #include <hawki_utils.h>
00046 
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Functions prototypes
00050  -----------------------------------------------------------------------------*/
00051 
00052 static int hawki_tec_filtchk_create(cpl_plugin *) ;
00053 static int hawki_tec_filtchk_exec(cpl_plugin *) ;
00054 static int hawki_tec_filtchk_destroy(cpl_plugin *) ;
00055 static int hawki_tec_filtchk(cpl_parameterlist *, cpl_frameset *) ;
00056 
00057 static int hawki_tec_filtchk_frameset_stats
00058 (cpl_table       **  target_stats,
00059  cpl_propertylist ** stats_stats,
00060  cpl_frameset     *  target_frames);
00061 
00062 static int hawki_tec_filtchk_save
00063 (cpl_table         ** target_stats,
00064  cpl_parameterlist *  recipe_parlist,
00065  cpl_frameset      *  recipe_frameset,
00066  cpl_propertylist  ** stats_stats,
00067  const char        *  calpro,
00068  const char        *  protype);
00069 
00070 /*-----------------------------------------------------------------------------
00071                             Static variables
00072  -----------------------------------------------------------------------------*/
00073 
00074 static char hawki_tec_filtchk_description[] =
00075 "hawki_tec_filtchk -- Check pairs of flats taken with different filters.\n"
00076 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00077 "raw-file.fits "HAWKI_TEC_FLAT_RAW"\n";
00078 
00079 /*-----------------------------------------------------------------------------
00080                                 Functions code
00081  -----------------------------------------------------------------------------*/
00082 
00083 /*----------------------------------------------------------------------------*/
00091 /*----------------------------------------------------------------------------*/
00092 int cpl_plugin_get_info(cpl_pluginlist * list)
00093 {
00094     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00095     cpl_plugin  *   plugin = &recipe->interface ;
00096 
00097     cpl_plugin_init(plugin,
00098                     CPL_PLUGIN_API,
00099                     HAWKI_BINARY_VERSION,
00100                     CPL_PLUGIN_TYPE_RECIPE,
00101                     "hawki_tec_filtchk",
00102                     "Filter checking recipe",
00103                     hawki_tec_filtchk_description,
00104                     "Cesar Enrique Garcia Dabo",
00105                     PACKAGE_BUGREPORT,  
00106                     hawki_get_license(),
00107                     hawki_tec_filtchk_create,
00108                     hawki_tec_filtchk_exec,
00109                     hawki_tec_filtchk_destroy) ;
00110 
00111     cpl_pluginlist_append(list, plugin) ;
00112     
00113     return 0;
00114 }
00115 
00116 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 static int hawki_tec_filtchk_create(cpl_plugin * plugin)
00127 {
00128     cpl_recipe      * recipe ;
00129     /* cpl_parameter   * p ; */
00130 
00131     /* Get the recipe out of the plugin */
00132     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00133         recipe = (cpl_recipe *)plugin ;
00134     else return -1 ;
00135 
00136     /* Create the parameters list in the cpl_recipe object */
00137     recipe->parameters = cpl_parameterlist_new() ;
00138     if (recipe->parameters == NULL)
00139         return 1;
00140 
00141     /* Fill the parameters list */
00142     /* None.. */
00143 
00144     /* Return */
00145     return 0;
00146 }
00147 
00148 /*----------------------------------------------------------------------------*/
00154 /*----------------------------------------------------------------------------*/
00155 static int hawki_tec_filtchk_exec(cpl_plugin * plugin)
00156 {
00157     cpl_recipe  *   recipe ;
00158 
00159     /* Get the recipe out of the plugin */
00160     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00161         recipe = (cpl_recipe *)plugin ;
00162     else return -1 ;
00163 
00164     /* Issue a banner */
00165     hawki_print_banner();
00166 
00167     return hawki_tec_filtchk(recipe->parameters, recipe->frames) ;
00168 }
00169 
00170 /*----------------------------------------------------------------------------*/
00176 /*----------------------------------------------------------------------------*/
00177 static int hawki_tec_filtchk_destroy(cpl_plugin * plugin)
00178 {
00179     cpl_recipe  *   recipe ;
00180 
00181     /* Get the recipe out of the plugin */
00182     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00183         recipe = (cpl_recipe *)plugin ;
00184     else return -1 ;
00185 
00186     cpl_parameterlist_delete(recipe->parameters) ;
00187     return 0 ;
00188 }
00189 
00190 /*----------------------------------------------------------------------------*/
00197 /*----------------------------------------------------------------------------*/
00198 static int hawki_tec_filtchk(
00199         cpl_parameterlist   *   parlist, 
00200         cpl_frameset        *   framelist)
00201 {
00202     cpl_frameset     *  frames ;
00203     cpl_table        ** target_stats;
00204     cpl_propertylist ** stats_stats;  
00205     int                 idet;
00206     char                calpro[1024];
00207     char                protype[1024];
00208 
00209     /* Identify the RAW and CALIB frames in the input frameset */
00210     if (hawki_dfs_set_groups(framelist)) 
00211     {
00212         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00213         return -1;
00214     }
00215 
00216     /* Retrieve raw frames */
00217     cpl_msg_info(__func__, "Identifying input frames");
00218     frames = hawki_extract_frameset(framelist, HAWKI_TEC_FLAT_RAW) ;
00219     snprintf(calpro, 1024, HAWKI_CALPRO_FILTERPOSCHECK_STATS);
00220     snprintf(protype, 1024, HAWKI_PROTYPE_FILTERPOSCHECK_STATS);
00221     if (frames == NULL)
00222     {
00223         cpl_msg_error(__func__,"Input files should be tagged %s",
00224                       HAWKI_TEC_FLAT_RAW);
00225         return -1;
00226     }
00227     
00228     /* Create the statistics table */
00229     target_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00230     stats_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
00231     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00232     {
00233         target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
00234         stats_stats[idet] = cpl_propertylist_new();
00235     }
00236     hawki_image_stats_initialize(target_stats);
00237 
00238     /* Compute actually the statistics */
00239     hawki_tec_filtchk_frameset_stats(target_stats, stats_stats, frames);
00240     
00241     /* Saving the table product */
00242     hawki_tec_filtchk_save
00243         (target_stats, parlist, framelist, stats_stats, calpro, protype);
00244 
00245     /* Free and return */
00246     cpl_frameset_delete(frames);
00247     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00248     {
00249         cpl_table_delete(target_stats[idet]);
00250         cpl_propertylist_delete(stats_stats[idet]);
00251     }
00252     cpl_free(target_stats); 
00253     cpl_free(stats_stats); 
00254 
00255     /* Return */
00256     if (cpl_error_get_code())
00257     {
00258         cpl_msg_error(__func__,
00259                       "HAWK-I pipeline could not recover from previous errors");
00260         return -1 ;
00261     }
00262     else return 0 ;
00263 }
00264 
00265 /*----------------------------------------------------------------------------*/
00275 /*----------------------------------------------------------------------------*/
00276 static int hawki_tec_filtchk_frameset_stats
00277 (cpl_table       **  target_stats,
00278  cpl_propertylist ** stats_stats,
00279  cpl_frameset     *  target_frames)
00280 {
00281     int iframe;
00282     int nframes;
00283 
00284     /* Loop on the number of frames */
00285     nframes = cpl_frameset_get_size(target_frames);
00286     cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
00287     cpl_msg_indent_more();
00288     for( iframe = 0 ; iframe < nframes ; ++iframe)
00289     {
00290         /* Local storage variables */
00291         cpl_frame     * this_target_frame;
00292 
00293         /* Computing statistics for this frame */
00294         cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
00295         this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
00296         hawki_image_stats_fill_from_frame
00297             (target_stats, this_target_frame, iframe);
00298     }
00299     cpl_msg_indent_less();
00300     
00301     /* Compute stats of the stats */
00302     hawki_image_stats_stats(target_stats, stats_stats);
00303 
00304     /* Print info about the statistics */
00305     hawki_image_stats_print(target_stats);
00306     
00307     return 0;
00308 }
00309 
00310 /*----------------------------------------------------------------------------*/
00320 /*----------------------------------------------------------------------------*/
00321 static int hawki_tec_filtchk_save
00322 (cpl_table         ** target_stats,
00323  cpl_parameterlist *  recipe_parlist,
00324  cpl_frameset      *  recipe_frameset,
00325  cpl_propertylist  ** stats_stats,
00326  const char        *  calpro,
00327  const char        *  protype)
00328 {
00329     const cpl_frame  *  reference_frame;
00330     cpl_propertylist *  referencelist;
00331     cpl_propertylist ** extlists;
00332     int                 idet;
00333     int                 ext_nb;
00334     const char       *  recipe_name = "hawki_tec_filtchk";
00335     
00336     /* Get the reference frame (the first one) */
00337     reference_frame = cpl_frameset_get_first_const(recipe_frameset);
00338     
00339     /* Create the prop lists */
00340     cpl_msg_info(__func__, "Creating the keywords list") ;
00341     referencelist = cpl_propertylist_load_regexp
00342         (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
00343     extlists = 
00344         cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00345     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00346     {
00347         /* Get the extension number */
00348         ext_nb=hawki_get_ext_from_detector
00349             (cpl_frame_get_filename(reference_frame), idet+1);
00350 
00351         /* Propagate the keywords from input frame extensions */
00352         extlists[idet] = cpl_propertylist_load_regexp(
00353                 cpl_frame_get_filename(reference_frame), ext_nb,
00354                 HAWKI_HEADER_EXT_FORWARD, 0);
00355         
00356         /* Add the stats of the stats */
00357         cpl_propertylist_append(extlists[idet],stats_stats[idet]);
00358     }
00359     
00360     /* Write the table with the statistics */
00361     hawki_tables_save(recipe_frameset,
00362                       recipe_parlist,
00363                       recipe_frameset,
00364                       (const cpl_table **)target_stats,
00365                       recipe_name,
00366                       calpro,
00367                       protype,
00368                       (const cpl_propertylist*)referencelist, 
00369                       (const cpl_propertylist**)extlists, 
00370                       "hawki_tec_filtchk_stats.fits");
00371     
00372     /* Free and return */
00373     cpl_propertylist_delete(referencelist) ;
00374     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00375     {
00376         cpl_propertylist_delete(extlists[idet]) ;
00377     }
00378     cpl_free(extlists) ;
00379     return  0;
00380 }