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