fors_img_sky_flat_impl.c

00001 /* $Id: fors_img_sky_flat_impl.c,v 1.21 2008/08/04 12:05:54 cizzo Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2006 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: cizzo $
00023  * $Date: 2008/08/04 12:05:54 $
00024  * $Revision: 1.21 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_img_sky_flat_impl.h>
00033 
00034 #include <fors_stack.h>
00035 #include <fors_dfs.h>
00036 #include <fors_qc.h>
00037 #include <fors_utils.h>
00038 
00039 #include <cpl.h>
00040 
00047 const char *const fors_img_sky_flat_name = "fors_img_sky_flat";
00048 const char *const fors_img_sky_flat_description_short = "Compute master img_sky_flat frame";
00049 const char *const fors_img_sky_flat_author = "Jonas M. Larsen";
00050 const char *const fors_img_sky_flat_email = PACKAGE_BUGREPORT;
00051 const char *const fors_img_sky_flat_description = 
00052 "Input files:\n"
00053 "  DO category:               Type:       Explanation:             Number:\n"
00054 "  SKY_FLAT_IMG               Raw         Jittered sky flat fields    1+\n"
00055 "  MASTER_BIAS                FITS image  Master bias                 1\n"
00056 "\n"
00057 "Output files:\n"
00058 "  DO category:               Data type:  Explanation:\n"
00059 "  MASTER_SKY_FLAT_IMG        FITS image  Master sky flat field\n";
00065 void fors_img_sky_flat_define_parameters(cpl_parameterlist *parameters)
00066 {
00067     const char *context = cpl_sprintf("fors.%s", fors_img_sky_flat_name);
00068     
00069     fors_stack_define_parameters(parameters, context, "median");
00070 
00071     cpl_free((void *)context);
00072 
00073     return;
00074 }
00075 
00076 #undef cleanup
00077 #define cleanup \
00078 do { \
00079     cpl_frameset_delete(sflat_frames); \
00080     cpl_frameset_delete(master_bias_frame); \
00081     fors_stack_method_delete(&sm); \
00082     cpl_free((void *)context); \
00083     fors_image_delete_const(&master_bias); \
00084     fors_image_delete(&master_sky_flat); \
00085     fors_image_list_delete(&sflats, fors_image_delete); \
00086     cpl_propertylist_delete(qc); \
00087     fors_setting_delete(&setting); \
00088 } while (0)
00089 
00098 void fors_img_sky_flat(cpl_frameset *frames, const cpl_parameterlist *parameters)
00099 {
00100     /* Raw */
00101     cpl_frameset *sflat_frames      = NULL;
00102     fors_image_list *sflats         = NULL;
00103 
00104     /* Calibration */
00105     cpl_frameset *master_bias_frame = NULL;
00106     const fors_image *master_bias   = NULL; 
00107 
00108     /* Products */
00109     fors_image *master_sky_flat = NULL;
00110     cpl_propertylist *qc = cpl_propertylist_new();
00111     double saturation;
00112 
00113     /* Parameters */
00114     stack_method *sm = NULL;
00115 
00116     /* Other */
00117     const char *context = cpl_sprintf("fors.%s", fors_img_sky_flat_name);
00118     fors_setting *setting = NULL;
00119 
00120     /* Get parameters */
00121     sm = fors_stack_method_new(parameters, context);
00122     assure( !cpl_error_get_code(), return, "Could not get stacking method");
00123     
00124     /* Find raw */
00125     sflat_frames = fors_frameset_extract(frames, SKY_FLAT_IMG);
00126     assure( cpl_frameset_get_size(sflat_frames) > 0, return, 
00127             "No %s provided", SKY_FLAT_IMG);
00128 
00129     /* Find calibration */
00130     master_bias_frame = fors_frameset_extract(frames, MASTER_BIAS);
00131     assure( cpl_frameset_get_size(master_bias_frame) == 1, return, 
00132             "One %s required. %d found", 
00133             MASTER_BIAS, cpl_frameset_get_size(master_bias_frame));
00134 
00135     /* Get setting */
00136     setting = fors_setting_new(cpl_frameset_get_first_const(sflat_frames));
00137     assure( !cpl_error_get_code(), return, "Could not get instrument setting" );
00138     
00139     master_bias = fors_image_load(cpl_frameset_get_first(master_bias_frame), 
00140                                   NULL, setting, NULL);
00141     assure( !cpl_error_get_code(), return, 
00142             "Could not load master bias");
00143 
00144     /* Load raw frames, subtract bias */
00145     sflats = fors_image_load_list(sflat_frames, master_bias, setting, &saturation);
00146     assure( !cpl_error_get_code(), return, "Could not load sky flat images");
00147 
00148     /* Normalize to median = 1.
00149      *
00150      * (The input sky flats generally have different normalization because of
00151      *  the time dependent sky level. We must bring the input flats
00152      *  to the same normalization before stacking.)
00153      */
00154     {
00155         fors_image *image;
00156         
00157         for (image = fors_image_list_first(sflats);
00158              image != NULL;
00159              image = fors_image_list_next(sflats)) {
00160 
00161             fors_image_divide_scalar(image, fors_image_get_median(image, NULL), -1.0);
00162             
00163             assure( !cpl_error_get_code(), return, "Raw sky flat normalization failed");
00164         }
00165     }
00166 
00167 
00168     /* Stack */
00169     master_sky_flat = fors_stack(sflats, sm);
00170     assure( !cpl_error_get_code(), return, "Sky flat stacking failed");
00171 
00172     /* QC */
00173     fors_qc_start_group(qc, fors_qc_dic_version, setting->instrument);
00174     
00175     fors_qc_write_group_heading(cpl_frameset_get_first(sflat_frames),
00176                                 MASTER_SKY_FLAT_IMG,
00177                                 setting->instrument);
00178     assure( !cpl_error_get_code(), return, "Could not write %s QC parameters", 
00179             MASTER_SKY_FLAT_IMG);
00180 
00181     fors_qc_write_qc_double(qc,
00182                             saturation,
00183                             "QC.OVEREXPO",
00184                             "%",
00185                             "Percentage of overexposed pixels",
00186                             setting->instrument);
00187     fors_qc_end_group();
00188 
00189     /* Save product */
00190     fors_dfs_save_image(frames, master_sky_flat, MASTER_SKY_FLAT_IMG,
00191                         qc, parameters, fors_img_sky_flat_name, 
00192                         cpl_frameset_get_first(sflat_frames));
00193     assure( !cpl_error_get_code(), return, "Saving %s failed",
00194             MASTER_SKY_FLAT_IMG);
00195     
00196     cleanup;
00197     return;
00198 }
00199 
00200 

Generated on Wed Sep 10 07:31:51 2008 for FORS Pipeline Reference Manual by  doxygen 1.4.6