hawki_step_stats.c

00001 /* $Id: hawki_step_stats.c,v 1.11 2010/09/28 14:10:45 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: 2010/09/28 14:10:45 $
00024  * $Revision: 1.11 $
00025  * $Name: hawki-1_8_0 $
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 
00154     /* Fill the parameters list */
00155     /* None.. */
00156 
00157     /* Return */
00158     return 0;
00159 }
00160 
00161 /*----------------------------------------------------------------------------*/
00167 /*----------------------------------------------------------------------------*/
00168 static int hawki_step_stats_exec(cpl_plugin * plugin)
00169 {
00170     cpl_recipe  *   recipe ;
00171 
00172     /* Get the recipe out of the plugin */
00173     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00174         recipe = (cpl_recipe *)plugin ;
00175     else return -1 ;
00176 
00177     /* Issue a banner */
00178     hawki_print_banner();
00179 
00180     return hawki_step_stats(recipe->parameters, recipe->frames) ;
00181 }
00182 
00183 /*----------------------------------------------------------------------------*/
00189 /*----------------------------------------------------------------------------*/
00190 static int hawki_step_stats_destroy(cpl_plugin * plugin)
00191 {
00192     cpl_recipe  *   recipe ;
00193 
00194     /* Get the recipe out of the plugin */
00195     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00196         recipe = (cpl_recipe *)plugin ;
00197     else return -1 ;
00198 
00199     cpl_parameterlist_delete(recipe->parameters) ;
00200     return 0 ;
00201 }
00202 
00203 /*----------------------------------------------------------------------------*/
00210 /*----------------------------------------------------------------------------*/
00211 static int hawki_step_stats(
00212         cpl_parameterlist   *   parlist, 
00213         cpl_frameset        *   framelist)
00214 {
00215     cpl_frameset     *  frames ;
00216     cpl_table        ** target_stats;
00217     cpl_propertylist ** stats_stats;  
00218     int                 idet;
00219     char                calpro[1024];
00220     char                protype[1024];
00221 
00222     /* Identify the RAW and CALIB frames in the input frameset */
00223     if (hawki_dfs_set_groups(framelist)) 
00224     {
00225         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00226         return -1;
00227     }
00228 
00229     /* Retrieve raw frames */
00230     cpl_msg_info(__func__, "Identifying input frames");
00231     frames = hawki_extract_frameset(framelist, HAWKI_IMG_JITTER_RAW) ;
00232     snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_STATS);
00233     snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_STATS);
00234     if (frames == NULL)
00235     {
00236         frames = hawki_extract_frameset(framelist, HAWKI_CALPRO_BKGIMAGE);
00237         snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_BKG_STATS);
00238         snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_BKG_STATS);
00239     }
00240     if (frames == NULL)
00241     {
00242         frames = hawki_extract_frameset(framelist, HAWKI_CAL_DARK_RAW);
00243         snprintf(calpro, 1024, HAWKI_CALPRO_DARK_STATS);
00244         snprintf(protype, 1024, HAWKI_PROTYPE_DARK_STATS);
00245     }
00246     if (frames == NULL)
00247     {
00248         frames = hawki_extract_frameset(framelist, HAWKI_CAL_FLAT_RAW);
00249         snprintf(calpro, 1024, HAWKI_CALPRO_FLAT_STATS);
00250         snprintf(protype, 1024, HAWKI_PROTYPE_FLAT_STATS);
00251     }
00252     if (frames == NULL)
00253     {
00254         frames = hawki_extract_frameset(framelist, HAWKI_CAL_ZPOINT_RAW);
00255         snprintf(calpro, 1024, HAWKI_CALPRO_ZPOINT_STATS);
00256         snprintf(protype, 1024, HAWKI_PROTYPE_ZPOINT_STATS);
00257     }
00258     if (frames == NULL)
00259     {
00260         cpl_msg_error(__func__,"Tag of input frames not supported");
00261         cpl_msg_error(__func__,"Supported: %s %s %s %s %s",
00262                 HAWKI_IMG_JITTER_RAW, HAWKI_CALPRO_BKGIMAGE,
00263                 HAWKI_CAL_DARK_RAW, HAWKI_CAL_FLAT_RAW, HAWKI_CAL_ZPOINT_RAW);
00264         return -1;
00265     }
00266     
00267     /* Create the statistics table and the "stats of the stats"*/
00268     target_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
00269     stats_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
00270     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00271     {
00272         target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
00273         stats_stats[idet] = cpl_propertylist_new();
00274     }
00275     hawki_image_stats_initialize(target_stats);
00276 
00277     /* Compute actually the statistics */
00278     hawki_step_stats_frameset_stats(target_stats, stats_stats, frames);
00279 
00280     /* Saving the table product */
00281     if(hawki_step_stats_save
00282         (target_stats, parlist, framelist, frames, stats_stats, calpro, protype) !=0)
00283         cpl_msg_warning(__func__,"Some data could not be saved. "
00284                         "Check permisions or disk space\n");
00285 
00286     /* Free and return */
00287     cpl_frameset_delete(frames);
00288     for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00289     {
00290         cpl_table_delete(target_stats[idet]);
00291         cpl_propertylist_delete(stats_stats[idet]);
00292     }
00293     cpl_free(target_stats); 
00294     cpl_free(stats_stats); 
00295 
00296     /* Return */
00297     if (cpl_error_get_code())
00298     {
00299         cpl_msg_error(__func__,
00300                       "HAWK-I pipeline could not recover from previous errors");
00301         return -1 ;
00302     }
00303     else return 0 ;
00304 }
00305 
00306 /*----------------------------------------------------------------------------*/
00316 /*----------------------------------------------------------------------------*/
00317 static int hawki_step_stats_frameset_stats
00318 (cpl_table        ** target_stats,
00319  cpl_propertylist ** stats_stats,
00320  cpl_frameset     *  target_frames)
00321 {
00322     int iframe;
00323     int nframes;
00324 
00325     /* Loop on the number of frames */
00326     nframes = cpl_frameset_get_size(target_frames);
00327     cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
00328     cpl_msg_indent_more();
00329     for( iframe = 0 ; iframe < nframes ; ++iframe)
00330     {
00331         /* Local storage variables */
00332         cpl_frame     * this_target_frame;
00333 
00334         /* Computing statistics for this frame */
00335         cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
00336         this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
00337         hawki_image_stats_fill_from_frame
00338             (target_stats, this_target_frame, iframe);
00339     }
00340     cpl_msg_indent_less();
00341     
00342     /* Compute stats of the stats */
00343     hawki_image_stats_stats(target_stats, stats_stats);
00344 
00345     /* Print info about the statistics */
00346     hawki_image_stats_print(target_stats);
00347     
00348     return 0;
00349 }
00350 
00351 /*----------------------------------------------------------------------------*/
00361 /*----------------------------------------------------------------------------*/
00362 static int hawki_step_stats_save
00363 (cpl_table         ** target_stats,
00364  cpl_parameterlist *  recipe_parlist,
00365  cpl_frameset      *  recipe_frameset,
00366  cpl_frameset      *  used_frameset,
00367  cpl_propertylist  ** stats_stats,
00368  const char        *  calpro,
00369  const char        *  protype)
00370 {
00371     const cpl_frame  *  reference_frame;
00372     cpl_propertylist *  referencelist;
00373     cpl_propertylist ** extlists;
00374     int                 idet;
00375     int                 ext_nb;
00376     const char       *  recipe_name = "hawki_step_stats";
00377     cpl_errorstate      error_prevstate = cpl_errorstate_get();
00378     
00379     
00380     /* Get the reference frame (the first one) */
00381     reference_frame = cpl_frameset_get_first_const(used_frameset);
00382     
00383     /* Create the prop lists */
00384     cpl_msg_info(__func__, "Creating the keywords list") ;
00385     referencelist = cpl_propertylist_load_regexp
00386         (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
00387     extlists = 
00388         cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00389     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00390     {
00391         /* Get the extension number */
00392         ext_nb=hawki_get_ext_from_detector
00393             (cpl_frame_get_filename(reference_frame), idet+1);
00394 
00395         /* Propagate the keywords from input frame extensions */
00396         extlists[idet] = cpl_propertylist_load_regexp(
00397                 cpl_frame_get_filename(reference_frame), ext_nb,
00398                 HAWKI_HEADER_EXT_FORWARD, 0);
00399         
00400         /* Add the stats of the stats */
00401         cpl_propertylist_append(extlists[idet],stats_stats[idet]);
00402     }
00403     
00404     /* Write the table with the statistics */
00405     hawki_tables_save(recipe_frameset,
00406                       recipe_parlist,
00407                       used_frameset,
00408                       (const cpl_table **)target_stats,
00409                       recipe_name,
00410                       calpro,
00411                       protype,
00412                       (const cpl_propertylist*)referencelist, 
00413                       (const cpl_propertylist**)extlists, 
00414                       "hawki_step_stats.fits");
00415 
00416     /* Free and return */
00417     cpl_propertylist_delete(referencelist) ;
00418     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00419     {
00420         cpl_propertylist_delete(extlists[idet]) ;
00421     }
00422     cpl_free(extlists) ;
00423 
00424     if(!cpl_errorstate_is_equal(error_prevstate))
00425     {
00426         cpl_errorstate_set(CPL_ERROR_NONE);
00427         return -1;
00428     }
00429     return  0;
00430 }

Generated on Thu Feb 17 17:13:08 2011 for HAWKI Pipeline Reference Manual by  doxygen 1.4.7