HAWKI Pipeline Reference Manual 1.8.6
hawki_step_stats.c
00001 /* $Id: hawki_step_stats.c,v 1.12 2011/10/24 10:42:03 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:42:03 $
00024  * $Revision: 1.12 $
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_step_stats_create(cpl_plugin *) ;
00053 static int hawki_step_stats_exec(cpl_plugin *) ;
00054 static int hawki_step_stats_destroy(cpl_plugin *) ;
00055 static int hawki_step_stats(cpl_parameterlist *, cpl_frameset *) ;
00056 
00057 static int hawki_step_stats_frameset_stats
00058 (cpl_table        ** target_stats,
00059  cpl_propertylist ** stats_stats,
00060  cpl_frameset     *  target_frames);
00061 
00062 static int hawki_step_stats_save
00063 (cpl_table         ** target_stats,
00064  cpl_parameterlist *  recipe_parlist,
00065  cpl_frameset      *  recipe_frameset,
00066  cpl_frameset      *  used_frameset,
00067  cpl_propertylist  ** stats_stats,
00068  const char        *  calpro,
00069  const char        *  protype);
00070 
00071 /*-----------------------------------------------------------------------------
00072                             Static variables
00073  -----------------------------------------------------------------------------*/
00074 
00075 static char hawki_step_stats_description[] =
00076 "hawki_step_stats -- hawki statistics utility (mean, stdev, ...).\n"
00077 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00078 "raw-jitter.fits "HAWKI_IMG_JITTER_RAW" or\n"
00079 "bkg.fits "HAWKI_CALPRO_BKGIMAGE" or\n"
00080 "raw-flat.fits "HAWKI_CAL_FLAT_RAW" or\n"
00081 "raw-dark.fits "HAWKI_CAL_DARK_RAW" or\n"
00082 "raw-zpoint.fits "HAWKI_CAL_ZPOINT_RAW" \n"
00083 "The recipe creates as an output:\n"
00084 "hawki_step_stats.fits ("HAWKI_CALPRO_JITTER_STATS"): Statistics of raw jitter images, or\n"
00085 "hawki_step_stats.fits ("HAWKI_CALPRO_JITTER_BKG_STATS"): Statistics of background images, or\n"
00086 "hawki_step_stats.fits ("HAWKI_CALPRO_FLAT_STATS"): Statistics of raw flats, or\n"
00087 "hawki_step_stats.fits ("HAWKI_CALPRO_DARK_STATS"): Statistics of raw darks, or\n"
00088 "hawki_step_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS"): Statistics of raw standard star images.\n"
00089 "Return code:\n"
00090 "esorex exits with an error code of 0 if the recipe completes successfully\n"
00091 "or 1 otherwise";
00092 
00093 
00094 /*-----------------------------------------------------------------------------
00095                                 Functions code
00096  -----------------------------------------------------------------------------*/
00097 
00098 /*----------------------------------------------------------------------------*/
00106 /*----------------------------------------------------------------------------*/
00107 int cpl_plugin_get_info(cpl_pluginlist * list)
00108 {
00109     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00110     cpl_plugin  *   plugin = &recipe->interface ;
00111 
00112     cpl_plugin_init(plugin,
00113                     CPL_PLUGIN_API,
00114                     HAWKI_BINARY_VERSION,
00115                     CPL_PLUGIN_TYPE_RECIPE,
00116                     "hawki_step_stats",
00117                     "Standard statistics utility",
00118                     hawki_step_stats_description,
00119                     "Cesar Enrique Garcia Dabo",
00120                     PACKAGE_BUGREPORT,  
00121                     hawki_get_license(),
00122                     hawki_step_stats_create,
00123                     hawki_step_stats_exec,
00124                     hawki_step_stats_destroy) ;
00125 
00126     cpl_pluginlist_append(list, plugin) ;
00127     
00128     return 0;
00129 }
00130 
00131 /*----------------------------------------------------------------------------*/
00140 /*----------------------------------------------------------------------------*/
00141 static int hawki_step_stats_create(cpl_plugin * plugin)
00142 {
00143     cpl_recipe      * recipe ;
00144     /* cpl_parameter   * p ; */
00145 
00146     /* Get the recipe out of the plugin */
00147     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00148         recipe = (cpl_recipe *)plugin ;
00149     else return -1 ;
00150 
00151     /* Create the parameters list in the cpl_recipe object */
00152     recipe->parameters = cpl_parameterlist_new() ;
00153     if (recipe->parameters == NULL)
00154         return 1;
00155 
00156     /* Fill the parameters list */
00157     /* None.. */
00158 
00159     /* Return */
00160     return 0;
00161 }
00162 
00163 /*----------------------------------------------------------------------------*/
00169 /*----------------------------------------------------------------------------*/
00170 static int hawki_step_stats_exec(cpl_plugin * plugin)
00171 {
00172     cpl_recipe  *   recipe ;
00173 
00174     /* Get the recipe out of the plugin */
00175     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00176         recipe = (cpl_recipe *)plugin ;
00177     else return -1 ;
00178 
00179     /* Issue a banner */
00180     hawki_print_banner();
00181 
00182     return hawki_step_stats(recipe->parameters, recipe->frames) ;
00183 }
00184 
00185 /*----------------------------------------------------------------------------*/
00191 /*----------------------------------------------------------------------------*/
00192 static int hawki_step_stats_destroy(cpl_plugin * plugin)
00193 {
00194     cpl_recipe  *   recipe ;
00195 
00196     /* Get the recipe out of the plugin */
00197     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00198         recipe = (cpl_recipe *)plugin ;
00199     else return -1 ;
00200 
00201     cpl_parameterlist_delete(recipe->parameters) ;
00202     return 0 ;
00203 }
00204 
00205 /*----------------------------------------------------------------------------*/
00212 /*----------------------------------------------------------------------------*/
00213 static int hawki_step_stats(
00214         cpl_parameterlist   *   parlist, 
00215         cpl_frameset        *   framelist)
00216 {
00217     cpl_frameset     *  frames ;
00218     cpl_table        ** target_stats;
00219     cpl_propertylist ** stats_stats;  
00220     int                 idet;
00221     char                calpro[1024];
00222     char                protype[1024];
00223 
00224     /* Identify the RAW and CALIB frames in the input frameset */
00225     if (hawki_dfs_set_groups(framelist)) 
00226     {
00227         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00228         return -1;
00229     }
00230 
00231     /* Retrieve raw frames */
00232     cpl_msg_info(__func__, "Identifying input frames");
00233     frames = hawki_extract_frameset(framelist, HAWKI_IMG_JITTER_RAW) ;
00234     snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_STATS);
00235     snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_STATS);
00236     if (frames == NULL)
00237     {
00238         frames = hawki_extract_frameset(framelist, HAWKI_CALPRO_BKGIMAGE);
00239         snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_BKG_STATS);
00240         snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_BKG_STATS);
00241     }
00242     if (frames == NULL)
00243     {
00244         frames = hawki_extract_frameset(framelist, HAWKI_CAL_DARK_RAW);
00245         snprintf(calpro, 1024, HAWKI_CALPRO_DARK_STATS);
00246         snprintf(protype, 1024, HAWKI_PROTYPE_DARK_STATS);
00247     }
00248     if (frames == NULL)
00249     {
00250         frames = hawki_extract_frameset(framelist, HAWKI_CAL_FLAT_RAW);
00251         snprintf(calpro, 1024, HAWKI_CALPRO_FLAT_STATS);
00252         snprintf(protype, 1024, HAWKI_PROTYPE_FLAT_STATS);
00253     }
00254     if (frames == NULL)
00255     {
00256         frames = hawki_extract_frameset(framelist, HAWKI_CAL_ZPOINT_RAW);
00257         snprintf(calpro, 1024, HAWKI_CALPRO_ZPOINT_STATS);
00258         snprintf(protype, 1024, HAWKI_PROTYPE_ZPOINT_STATS);
00259     }
00260     if (frames == NULL)
00261     {
00262         cpl_msg_error(__func__,"Tag of input frames not supported");
00263         cpl_msg_error(__func__,"Supported: %s %s %s %s %s",
00264                 HAWKI_IMG_JITTER_RAW, HAWKI_CALPRO_BKGIMAGE,
00265                 HAWKI_CAL_DARK_RAW, HAWKI_CAL_FLAT_RAW, HAWKI_CAL_ZPOINT_RAW);
00266         return -1;
00267     }
00268     
00269     /* Create the statistics table and the "stats of the stats"*/
00270     target_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00271     stats_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
00272     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00273     {
00274         target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
00275         stats_stats[idet] = cpl_propertylist_new();
00276     }
00277     hawki_image_stats_initialize(target_stats);
00278 
00279     /* Compute actually the statistics */
00280     hawki_step_stats_frameset_stats(target_stats, stats_stats, frames);
00281 
00282     /* Saving the table product */
00283     if(hawki_step_stats_save
00284         (target_stats, parlist, framelist, frames, stats_stats, calpro, protype) !=0)
00285         cpl_msg_warning(__func__,"Some data could not be saved. "
00286                         "Check permisions or disk space\n");
00287 
00288     /* Free and return */
00289     cpl_frameset_delete(frames);
00290     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00291     {
00292         cpl_table_delete(target_stats[idet]);
00293         cpl_propertylist_delete(stats_stats[idet]);
00294     }
00295     cpl_free(target_stats); 
00296     cpl_free(stats_stats); 
00297 
00298     /* Return */
00299     if (cpl_error_get_code())
00300     {
00301         cpl_msg_error(__func__,
00302                       "HAWK-I pipeline could not recover from previous errors");
00303         return -1 ;
00304     }
00305     else return 0 ;
00306 }
00307 
00308 /*----------------------------------------------------------------------------*/
00318 /*----------------------------------------------------------------------------*/
00319 static int hawki_step_stats_frameset_stats
00320 (cpl_table        ** target_stats,
00321  cpl_propertylist ** stats_stats,
00322  cpl_frameset     *  target_frames)
00323 {
00324     int iframe;
00325     int nframes;
00326 
00327     /* Loop on the number of frames */
00328     nframes = cpl_frameset_get_size(target_frames);
00329     cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
00330     cpl_msg_indent_more();
00331     for( iframe = 0 ; iframe < nframes ; ++iframe)
00332     {
00333         /* Local storage variables */
00334         cpl_frame     * this_target_frame;
00335 
00336         /* Computing statistics for this frame */
00337         cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
00338         this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
00339         hawki_image_stats_fill_from_frame
00340             (target_stats, this_target_frame, iframe);
00341     }
00342     cpl_msg_indent_less();
00343     
00344     /* Compute stats of the stats */
00345     hawki_image_stats_stats(target_stats, stats_stats);
00346 
00347     /* Print info about the statistics */
00348     hawki_image_stats_print(target_stats);
00349     
00350     return 0;
00351 }
00352 
00353 /*----------------------------------------------------------------------------*/
00363 /*----------------------------------------------------------------------------*/
00364 static int hawki_step_stats_save
00365 (cpl_table         ** target_stats,
00366  cpl_parameterlist *  recipe_parlist,
00367  cpl_frameset      *  recipe_frameset,
00368  cpl_frameset      *  used_frameset,
00369  cpl_propertylist  ** stats_stats,
00370  const char        *  calpro,
00371  const char        *  protype)
00372 {
00373     const cpl_frame  *  reference_frame;
00374     cpl_propertylist *  referencelist;
00375     cpl_propertylist ** extlists;
00376     int                 idet;
00377     int                 ext_nb;
00378     const char       *  recipe_name = "hawki_step_stats";
00379     cpl_errorstate      error_prevstate = cpl_errorstate_get();
00380     
00381     
00382     /* Get the reference frame (the first one) */
00383     reference_frame = cpl_frameset_get_first_const(used_frameset);
00384     
00385     /* Create the prop lists */
00386     cpl_msg_info(__func__, "Creating the keywords list") ;
00387     referencelist = cpl_propertylist_load_regexp
00388         (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
00389     extlists = 
00390         cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00391     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00392     {
00393         /* Get the extension number */
00394         ext_nb=hawki_get_ext_from_detector
00395             (cpl_frame_get_filename(reference_frame), idet+1);
00396 
00397         /* Propagate the keywords from input frame extensions */
00398         extlists[idet] = cpl_propertylist_load_regexp(
00399                 cpl_frame_get_filename(reference_frame), ext_nb,
00400                 HAWKI_HEADER_EXT_FORWARD, 0);
00401         
00402         /* Add the stats of the stats */
00403         cpl_propertylist_append(extlists[idet],stats_stats[idet]);
00404     }
00405     
00406     /* Write the table with the statistics */
00407     hawki_tables_save(recipe_frameset,
00408                       recipe_parlist,
00409                       used_frameset,
00410                       (const cpl_table **)target_stats,
00411                       recipe_name,
00412                       calpro,
00413                       protype,
00414                       (const cpl_propertylist*)referencelist, 
00415                       (const cpl_propertylist**)extlists, 
00416                       "hawki_step_stats.fits");
00417 
00418     /* Free and return */
00419     cpl_propertylist_delete(referencelist) ;
00420     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00421     {
00422         cpl_propertylist_delete(extlists[idet]) ;
00423     }
00424     cpl_free(extlists) ;
00425 
00426     if(!cpl_errorstate_is_equal(error_prevstate))
00427     {
00428         cpl_errorstate_set(CPL_ERROR_NONE);
00429         return -1;
00430     }
00431     return  0;
00432 }