hawki_step_stats.c

00001 /* $Id: hawki_step_stats.c,v 1.10 2010/06/04 15:54:40 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/06/04 15:54:40 $
00024  * $Revision: 1.10 $
00025  * $Name: hawki-1_7_2 $
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     if (cpl_error_get_code()) return -1 ;
00297     else return 0 ;
00298 }
00299 
00300 /*----------------------------------------------------------------------------*/
00310 /*----------------------------------------------------------------------------*/
00311 static int hawki_step_stats_frameset_stats
00312 (cpl_table        ** target_stats,
00313  cpl_propertylist ** stats_stats,
00314  cpl_frameset     *  target_frames)
00315 {
00316     int iframe;
00317     int nframes;
00318 
00319     /* Loop on the number of frames */
00320     nframes = cpl_frameset_get_size(target_frames);
00321     cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
00322     cpl_msg_indent_more();
00323     for( iframe = 0 ; iframe < nframes ; ++iframe)
00324     {
00325         /* Local storage variables */
00326         cpl_frame     * this_target_frame;
00327 
00328         /* Computing statistics for this frame */
00329         cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
00330         this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
00331         hawki_image_stats_fill_from_frame
00332             (target_stats, this_target_frame, iframe);
00333     }
00334     cpl_msg_indent_less();
00335     
00336     /* Compute stats of the stats */
00337     hawki_image_stats_stats(target_stats, stats_stats);
00338 
00339     /* Print info about the statistics */
00340     hawki_image_stats_print(target_stats);
00341     
00342     return 0;
00343 }
00344 
00345 /*----------------------------------------------------------------------------*/
00355 /*----------------------------------------------------------------------------*/
00356 static int hawki_step_stats_save
00357 (cpl_table         ** target_stats,
00358  cpl_parameterlist *  recipe_parlist,
00359  cpl_frameset      *  recipe_frameset,
00360  cpl_frameset      *  used_frameset,
00361  cpl_propertylist  ** stats_stats,
00362  const char        *  calpro,
00363  const char        *  protype)
00364 {
00365     const cpl_frame  *  reference_frame;
00366     cpl_propertylist *  referencelist;
00367     cpl_propertylist ** extlists;
00368     int                 idet;
00369     int                 ext_nb;
00370     const char       *  recipe_name = "hawki_step_stats";
00371     cpl_errorstate      error_prevstate = cpl_errorstate_get();
00372     
00373     
00374     /* Get the reference frame (the first one) */
00375     reference_frame = cpl_frameset_get_first_const(used_frameset);
00376     
00377     /* Create the prop lists */
00378     cpl_msg_info(__func__, "Creating the keywords list") ;
00379     referencelist = cpl_propertylist_load_regexp
00380         (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
00381     extlists = 
00382         cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
00383     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00384     {
00385         /* Get the extension number */
00386         ext_nb=hawki_get_ext_from_detector
00387             (cpl_frame_get_filename(reference_frame), idet+1);
00388 
00389         /* Propagate the keywords from input frame extensions */
00390         extlists[idet] = cpl_propertylist_load_regexp(
00391                 cpl_frame_get_filename(reference_frame), ext_nb,
00392                 HAWKI_HEADER_EXT_FORWARD, 0);
00393         
00394         /* Add the stats of the stats */
00395         cpl_propertylist_append(extlists[idet],stats_stats[idet]);
00396     }
00397     
00398     /* Write the table with the statistics */
00399     hawki_tables_save(recipe_frameset,
00400                       recipe_parlist,
00401                       used_frameset,
00402                       (const cpl_table **)target_stats,
00403                       recipe_name,
00404                       calpro,
00405                       protype,
00406                       (const cpl_propertylist*)referencelist, 
00407                       (const cpl_propertylist**)extlists, 
00408                       "hawki_step_stats.fits");
00409 
00410     /* Free and return */
00411     cpl_propertylist_delete(referencelist) ;
00412     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) 
00413     {
00414         cpl_propertylist_delete(extlists[idet]) ;
00415     }
00416     cpl_free(extlists) ;
00417 
00418     if(!cpl_errorstate_is_equal(error_prevstate))
00419     {
00420         cpl_errorstate_set(CPL_ERROR_NONE);
00421         return -1;
00422     }
00423     return  0;
00424 }

Generated on 10 Jun 2010 for HAWKI Pipeline Reference Manual by  doxygen 1.6.1