HAWKI Pipeline Reference Manual 1.8.12
hawki_tec_filtchk.c
00001 /* $Id: hawki_tec_filtchk.c,v 1.16 2013/03/11 11:02:59 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: 2013/03/11 11:02:59 $
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 <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 #ifdef __cplusplus
00053 extern "C"
00054 #endif
00055 int cpl_plugin_get_info(cpl_pluginlist * list);
00056 
00057 static int hawki_tec_filtchk_create(cpl_plugin *) ;
00058 static int hawki_tec_filtchk_exec(cpl_plugin *) ;
00059 static int hawki_tec_filtchk_destroy(cpl_plugin *) ;
00060 static int hawki_tec_filtchk(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 static int hawki_tec_filtchk_frameset_stats
00063 (cpl_table       **  target_stats,
00064  cpl_propertylist ** stats_stats,
00065  cpl_frameset     *  target_frames);
00066 
00067 static int hawki_tec_filtchk_save
00068 (cpl_table         ** target_stats,
00069  cpl_parameterlist *  recipe_parlist,
00070  cpl_frameset      *  recipe_frameset,
00071  cpl_propertylist  ** stats_stats,
00072  const char        *  calpro,
00073  const char        *  protype);
00074 
00075 /*-----------------------------------------------------------------------------
00076                             Static variables
00077  -----------------------------------------------------------------------------*/
00078 
00079 static char hawki_tec_filtchk_description[] =
00080 "hawki_tec_filtchk -- Check pairs of flats taken with different filters.\n"
00081 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00082 "raw-file.fits "HAWKI_TEC_FLAT_RAW"\n";
00083 
00084 /*-----------------------------------------------------------------------------
00085                                 Functions code
00086  -----------------------------------------------------------------------------*/
00087 
00088 /*----------------------------------------------------------------------------*/
00096 /*----------------------------------------------------------------------------*/
00097 int cpl_plugin_get_info(cpl_pluginlist * list)
00098 {
00099     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00100     cpl_plugin  *   plugin = &recipe->interface ;
00101 
00102     cpl_plugin_init(plugin,
00103                     CPL_PLUGIN_API,
00104                     HAWKI_BINARY_VERSION,
00105                     CPL_PLUGIN_TYPE_RECIPE,
00106                     "hawki_tec_filtchk",
00107                     "Filter checking recipe",
00108                     hawki_tec_filtchk_description,
00109                     "Cesar Enrique Garcia Dabo",
00110                     PACKAGE_BUGREPORT,  
00111                     hawki_get_license(),
00112                     hawki_tec_filtchk_create,
00113                     hawki_tec_filtchk_exec,
00114                     hawki_tec_filtchk_destroy) ;
00115 
00116     cpl_pluginlist_append(list, plugin) ;
00117     
00118     return 0;
00119 }
00120 
00121 /*----------------------------------------------------------------------------*/
00130 /*----------------------------------------------------------------------------*/
00131 static int hawki_tec_filtchk_create(cpl_plugin * plugin)
00132 {
00133     cpl_recipe      * recipe ;
00134     /* cpl_parameter   * p ; */
00135 
00136     /* Get the recipe out of the plugin */
00137     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00138         recipe = (cpl_recipe *)plugin ;
00139     else return -1 ;
00140 
00141     /* Create the parameters list in the cpl_recipe object */
00142     recipe->parameters = cpl_parameterlist_new() ;
00143     if (recipe->parameters == NULL)
00144         return 1;
00145 
00146     /* Fill the parameters list */
00147     /* None.. */
00148 
00149     /* Return */
00150     return 0;
00151 }
00152 
00153 /*----------------------------------------------------------------------------*/
00159 /*----------------------------------------------------------------------------*/
00160 static int hawki_tec_filtchk_exec(cpl_plugin * plugin)
00161 {
00162     cpl_recipe  *   recipe ;
00163 
00164     /* Get the recipe out of the plugin */
00165     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00166         recipe = (cpl_recipe *)plugin ;
00167     else return -1 ;
00168 
00169     /* Issue a banner */
00170     hawki_print_banner();
00171 
00172     return hawki_tec_filtchk(recipe->parameters, recipe->frames) ;
00173 }
00174 
00175 /*----------------------------------------------------------------------------*/
00181 /*----------------------------------------------------------------------------*/
00182 static int hawki_tec_filtchk_destroy(cpl_plugin * plugin)
00183 {
00184     cpl_recipe  *   recipe ;
00185 
00186     /* Get the recipe out of the plugin */
00187     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00188         recipe = (cpl_recipe *)plugin ;
00189     else return -1 ;
00190 
00191     cpl_parameterlist_delete(recipe->parameters) ;
00192     return 0 ;
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00202 /*----------------------------------------------------------------------------*/
00203 static int hawki_tec_filtchk(
00204         cpl_parameterlist   *   parlist, 
00205         cpl_frameset        *   framelist)
00206 {
00207     cpl_frameset     *  frames ;
00208     cpl_table        ** target_stats;
00209     cpl_propertylist ** stats_stats;  
00210     int                 idet;
00211     char                calpro[1024];
00212     char                protype[1024];
00213 
00214     /* Identify the RAW and CALIB frames in the input frameset */
00215     if (hawki_dfs_set_groups(framelist)) 
00216     {
00217         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00218         return -1;
00219     }
00220 
00221     /* Retrieve raw frames */
00222     cpl_msg_info(__func__, "Identifying input frames");
00223     frames = hawki_extract_frameset(framelist, HAWKI_TEC_FLAT_RAW) ;
00224     snprintf(calpro, 1024, HAWKI_CALPRO_FILTERPOSCHECK_STATS);
00225     snprintf(protype, 1024, HAWKI_PROTYPE_FILTERPOSCHECK_STATS);
00226     if (frames == NULL)
00227     {
00228         cpl_msg_error(__func__,"Input files should be tagged %s",
00229                       HAWKI_TEC_FLAT_RAW);
00230         return -1;
00231     }
00232     
00233     /* Create the statistics table */
00234     target_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00235     stats_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
00236     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00237     {
00238         target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
00239         stats_stats[idet] = cpl_propertylist_new();
00240     }
00241     hawki_image_stats_initialize(target_stats);
00242 
00243     /* Compute actually the statistics */
00244     hawki_tec_filtchk_frameset_stats(target_stats, stats_stats, frames);
00245     
00246     /* Saving the table product */
00247     hawki_tec_filtchk_save
00248         (target_stats, parlist, framelist, stats_stats, calpro, protype);
00249 
00250     /* Free and return */
00251     cpl_frameset_delete(frames);
00252     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00253     {
00254         cpl_table_delete(target_stats[idet]);
00255         cpl_propertylist_delete(stats_stats[idet]);
00256     }
00257     cpl_free(target_stats); 
00258     cpl_free(stats_stats); 
00259 
00260     /* Return */
00261     if (cpl_error_get_code())
00262     {
00263         cpl_msg_error(__func__,
00264                       "HAWK-I pipeline could not recover from previous errors");
00265         return -1 ;
00266     }
00267     else return 0 ;
00268 }
00269 
00270 /*----------------------------------------------------------------------------*/
00280 /*----------------------------------------------------------------------------*/
00281 static int hawki_tec_filtchk_frameset_stats
00282 (cpl_table       **  target_stats,
00283  cpl_propertylist ** stats_stats,
00284  cpl_frameset     *  target_frames)
00285 {
00286     int iframe;
00287     int nframes;
00288 
00289     /* Loop on the number of frames */
00290     nframes = cpl_frameset_get_size(target_frames);
00291     cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
00292     cpl_msg_indent_more();
00293     for( iframe = 0 ; iframe < nframes ; ++iframe)
00294     {
00295         /* Local storage variables */
00296         cpl_frame     * this_target_frame;
00297 
00298         /* Computing statistics for this frame */
00299         cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
00300         this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
00301         hawki_image_stats_fill_from_frame
00302             (target_stats, this_target_frame, iframe);
00303     }
00304     cpl_msg_indent_less();
00305     
00306     /* Compute stats of the stats */
00307     hawki_image_stats_stats(target_stats, stats_stats);
00308 
00309     /* Print info about the statistics */
00310     hawki_image_stats_print(target_stats);
00311     
00312     return 0;
00313 }
00314 
00315 /*----------------------------------------------------------------------------*/
00325 /*----------------------------------------------------------------------------*/
00326 static int hawki_tec_filtchk_save
00327 (cpl_table         ** target_stats,
00328  cpl_parameterlist *  recipe_parlist,
00329  cpl_frameset      *  recipe_frameset,
00330  cpl_propertylist  ** stats_stats,
00331  const char        *  calpro,
00332  const char        *  protype)
00333 {
00334     const cpl_frame  *  reference_frame;
00335     cpl_propertylist *  referencelist;
00336     cpl_propertylist ** extlists;
00337     int                 idet;
00338     int                 ext_nb;
00339     const char       *  recipe_name = "hawki_tec_filtchk";
00340     
00341     /* Get the reference frame (the first one) */
00342     reference_frame = cpl_frameset_get_first_const(recipe_frameset);
00343     
00344     /* Create the prop lists */
00345     cpl_msg_info(__func__, "Creating the keywords list") ;
00346     referencelist = cpl_propertylist_load_regexp
00347         (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
00348     extlists = 
00349         cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00350     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00351     {
00352         /* Get the extension number */
00353         ext_nb=hawki_get_ext_from_detector
00354             (cpl_frame_get_filename(reference_frame), idet+1);
00355 
00356         /* Propagate the keywords from input frame extensions */
00357         extlists[idet] = cpl_propertylist_load_regexp(
00358                 cpl_frame_get_filename(reference_frame), ext_nb,
00359                 HAWKI_HEADER_EXT_FORWARD, 0);
00360         
00361         /* Add the stats of the stats */
00362         cpl_propertylist_append(extlists[idet],stats_stats[idet]);
00363     }
00364     
00365     /* Write the table with the statistics */
00366     hawki_tables_save(recipe_frameset,
00367                       recipe_parlist,
00368                       recipe_frameset,
00369                       (const cpl_table **)target_stats,
00370                       recipe_name,
00371                       calpro,
00372                       protype,
00373                       (const cpl_propertylist*)referencelist, 
00374                       (const cpl_propertylist**)extlists, 
00375                       "hawki_tec_filtchk_stats.fits");
00376     
00377     /* Free and return */
00378     cpl_propertylist_delete(referencelist) ;
00379     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00380     {
00381         cpl_propertylist_delete(extlists[idet]) ;
00382     }
00383     cpl_free(extlists) ;
00384     return  0;
00385 }