sofi_img_domeflat.c

00001 /* $Id: sofi_img_domeflat.c,v 1.16 2012/01/12 12:50:31 llundin Exp $
00002  *
00003  * This file is part of the SOFI 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: llundin $
00023  * $Date: 2012/01/12 12:50:31 $
00024  * $Revision: 1.16 $
00025  * $Name: sofi-1_5_5 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_utils.h"
00040 
00041 #include "sofi_utils.h"
00042 #include "sofi_pfits.h"
00043 #include "sofi_dfs.h"
00044 
00045 /*-----------------------------------------------------------------------------
00046                             Functions prototypes
00047  -----------------------------------------------------------------------------*/
00048 
00049 static int sofi_img_domeflat_create(cpl_plugin *);
00050 static int sofi_img_domeflat_exec(cpl_plugin *);
00051 static int sofi_img_domeflat_destroy(cpl_plugin *);
00052 static int sofi_img_domeflat(cpl_parameterlist *, cpl_frameset *);
00053 static cpl_image * sofi_img_domeflat_compute(cpl_frameset *);
00054 static cpl_image * sofi_img_domeflat_bias(cpl_image *, cpl_image *,
00055         int, int, int, int);
00056 static int sofi_img_domeflat_save(cpl_image *, cpl_parameterlist *, 
00057         cpl_frameset *);
00058 
00059 /*-----------------------------------------------------------------------------
00060                             Static variables
00061  -----------------------------------------------------------------------------*/
00062 
00063 static char sofi_img_domeflat_description[] = 
00064 "sofi_img_domeflat -- SOFI imaging flat-field creation.\n"
00065 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00066 "raw-file.fits "SOFI_IMG_DOMEFLAT_RAW"\n";
00067 
00068 /*-----------------------------------------------------------------------------
00069                                 Functions code
00070  -----------------------------------------------------------------------------*/
00071 
00072 /*----------------------------------------------------------------------------*/
00080 /*----------------------------------------------------------------------------*/
00081 int cpl_plugin_get_info(cpl_pluginlist * list)
00082 {
00083     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00084     cpl_plugin  *   plugin = &recipe->interface;
00085 
00086     cpl_plugin_init(plugin,
00087                     CPL_PLUGIN_API,
00088                     SOFI_BINARY_VERSION,
00089                     CPL_PLUGIN_TYPE_RECIPE,
00090                     "sofi_img_domeflat",
00091                     "Dome flat recipe",
00092                     sofi_img_domeflat_description,
00093                     "Yves Jung",
00094                     "yjung@eso.org",
00095                     sofi_get_license(),
00096                     sofi_img_domeflat_create,
00097                     sofi_img_domeflat_exec,
00098                     sofi_img_domeflat_destroy);
00099 
00100     cpl_pluginlist_append(list, plugin);
00101     
00102     return 0;
00103 }
00104 
00105 /*----------------------------------------------------------------------------*/
00114 /*----------------------------------------------------------------------------*/
00115 static int sofi_img_domeflat_create(cpl_plugin * plugin)
00116 {
00117     cpl_recipe      * recipe;
00118 
00119     /* Get the recipe out of the plugin */
00120     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00121         recipe = (cpl_recipe *)plugin;
00122     else return -1;
00123 
00124     /* Create the parameters list in the cpl_recipe object */
00125     recipe->parameters = cpl_parameterlist_new();
00126 
00127     /* Fill the parameters list */
00128     
00129     /* Return */
00130     return 0;
00131 }
00132 
00133 /*----------------------------------------------------------------------------*/
00139 /*----------------------------------------------------------------------------*/
00140 static int sofi_img_domeflat_exec(cpl_plugin * plugin)
00141 {
00142     cpl_recipe  *   recipe;
00143 
00144     /* Get the recipe out of the plugin */
00145     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00146         recipe = (cpl_recipe *)plugin;
00147     else return -1;
00148 
00149     return sofi_img_domeflat(recipe->parameters, recipe->frames);
00150 }
00151 
00152 /*----------------------------------------------------------------------------*/
00158 /*----------------------------------------------------------------------------*/
00159 static int sofi_img_domeflat_destroy(cpl_plugin * plugin)
00160 {
00161     cpl_recipe  *   recipe;
00162 
00163     /* Get the recipe out of the plugin */
00164     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00165         recipe = (cpl_recipe *)plugin;
00166     else return -1;
00167 
00168     cpl_parameterlist_delete(recipe->parameters);
00169     return 0;
00170 }
00171 
00172 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 static int sofi_img_domeflat(
00181         cpl_parameterlist   *   parlist, 
00182         cpl_frameset        *   framelist)
00183 {
00184     cpl_frameset    *   flatframes;
00185     cpl_image       *   flat;
00186     
00187     /* Identify the RAW and CALIB frames in the input frameset */
00188     if (sofi_dfs_set_groups(framelist)) {
00189         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00190         return -1;
00191     }
00192 
00193     /* Retrieve raw frames */
00194     if ((flatframes = sofi_extract_frameset(framelist,
00195                     SOFI_IMG_DOMEFLAT_RAW)) == NULL) {
00196         cpl_msg_error(cpl_func, "Cannot find flat frames in the input list");
00197         return -1;
00198     }
00199 
00200     /* Compute the flat field */
00201     cpl_msg_info(cpl_func, "Compute dome flat");
00202     if ((flat = sofi_img_domeflat_compute(flatframes)) == NULL) {
00203         cpl_msg_error(cpl_func, "Cannot compute dome flat");
00204         cpl_frameset_delete(flatframes);
00205         return -1;
00206     }
00207     cpl_frameset_delete(flatframes);
00208     
00209     /* Save the flat field */
00210     cpl_msg_info(cpl_func, "Save the product");
00211     if (sofi_img_domeflat_save(flat, parlist, framelist) == -1) {
00212         cpl_msg_error(cpl_func, "Cannot save the product");
00213         cpl_image_delete(flat);
00214         return -1;
00215     }
00216     cpl_image_delete(flat);
00217 
00218     /* Free and return */
00219     return 0;
00220 }
00221 
00222 /*----------------------------------------------------------------------------*/
00228 /*----------------------------------------------------------------------------*/
00229 static cpl_image * sofi_img_domeflat_compute(cpl_frameset * fset)
00230 {
00231     cpl_imagelist       *   ilist;
00232     int                     x1, x2, x3, x4;
00233     cpl_image           *   on;
00234     cpl_image           *   on_ma;
00235     cpl_image           *   off;
00236     cpl_image           *   off_ma;
00237     cpl_image           *   bias;
00238     
00239     /* Test entries */
00240     if (fset == NULL) return NULL;
00241     if (cpl_frameset_get_size(fset) != 8) return NULL;
00242     
00243     /* Initialise */
00244     x1 = 500;
00245     x2 = 600;
00246     x3 = 50;
00247     x4 = 150;
00248     
00249     /* Load the input frames */
00250     cpl_msg_info(cpl_func, "Load the frames");
00251     if ((ilist = cpl_imagelist_load_frameset(fset, CPL_TYPE_FLOAT, 0,
00252                     0)) == NULL) {
00253         cpl_msg_error(cpl_func, "Cannot load the frames");
00254         return NULL;
00255     }
00256 
00257     /* Get the on images */
00258     on =    cpl_image_add_create(   cpl_imagelist_get(ilist, 3),
00259                                     cpl_imagelist_get(ilist, 4));
00260     cpl_image_divide_scalar(on, 2);
00261     on_ma = cpl_image_add_create(   cpl_imagelist_get(ilist, 2),
00262                                     cpl_imagelist_get(ilist, 5));
00263     cpl_image_divide_scalar(on_ma, 2);
00264     
00265     /* Get the on bias */
00266     if ((bias = sofi_img_domeflat_bias(on, on_ma, x1, x2, x3, x4)) == NULL) {
00267         cpl_msg_error(cpl_func, "Cannot compute the bias of the on frames");
00268         cpl_image_delete(on);
00269         cpl_image_delete(on_ma);
00270         cpl_imagelist_delete(ilist);
00271         return NULL;
00272     }
00273     cpl_image_delete(on_ma);
00274 
00275     /* Correct the on bias */
00276     cpl_image_subtract(on, bias);
00277     cpl_image_delete(bias);
00278 
00279     /* Get the off images */
00280     off =   cpl_image_add_create(   cpl_imagelist_get(ilist, 0),
00281                                     cpl_imagelist_get(ilist, 7));
00282     cpl_image_divide_scalar(off, 2);
00283     off_ma =cpl_image_add_create(   cpl_imagelist_get(ilist, 1),
00284                                     cpl_imagelist_get(ilist, 6));
00285     cpl_image_divide_scalar(off_ma, 2);
00286     cpl_imagelist_delete(ilist);
00287  
00288     /* Get the off bias */
00289     if ((bias = sofi_img_domeflat_bias(off, off_ma, x1, x2, x3, 
00290                     x4)) == NULL) {
00291         cpl_msg_error(cpl_func, "Cannot compute the bias of the off frames");
00292         cpl_image_delete(on);
00293         cpl_image_delete(off);
00294         cpl_image_delete(off_ma);
00295         return NULL;
00296     }
00297     cpl_image_delete(off_ma);
00298 
00299     /* Correct the on bias */
00300     cpl_image_subtract(off, bias);
00301     cpl_image_delete(bias);
00302 
00303     /* Compute the flat */
00304     cpl_image_subtract(on, off);
00305     cpl_image_delete(off);
00306 
00307     /* Normalise the flat */
00308     cpl_image_normalise(on, CPL_NORM_MEAN);
00309     
00310     /* Return */
00311     return on;
00312 }
00313 
00314 /*----------------------------------------------------------------------------*/
00325 /*----------------------------------------------------------------------------*/
00326 static cpl_image * sofi_img_domeflat_bias(
00327         cpl_image           *   in,
00328         cpl_image           *   in_mask,
00329         int                     x1,
00330         int                     x2,
00331         int                     x3,
00332         int                     x4)
00333 {
00334     int                     nx, ny;
00335     cpl_image           *   a_1d;
00336     cpl_image           *   b_1d;
00337     cpl_image           *   c_1d;
00338     cpl_image           *   a_2d;
00339     float               *   pa_1d;
00340     float               *   pa_2d;
00341     int                     i, j;
00342 
00343     /* Test entries */
00344     if (in == NULL) return NULL;
00345     if (in_mask == NULL) return NULL;
00346 
00347     /* Initialise */
00348     nx = cpl_image_get_size_x(in);
00349     ny = cpl_image_get_size_y(in);
00350 
00351     /* Compute the bias */
00352     a_1d = cpl_image_collapse_window_create(in, x1, 1, x2, ny, 1);
00353     cpl_image_divide_scalar(a_1d, x2-x1+1);
00354     c_1d = cpl_image_collapse_window_create(in_mask, x1, 1, x2, ny, 1);
00355     cpl_image_divide_scalar(c_1d, x2-x1+1);
00356     b_1d = cpl_image_collapse_window_create(in_mask, x3, 1, x4, ny, 1);
00357     cpl_image_divide_scalar(b_1d, x4-x3+1);
00358     cpl_image_subtract(a_1d, c_1d);
00359     cpl_image_delete(c_1d);
00360     cpl_image_add(a_1d, b_1d);
00361     cpl_image_delete(b_1d);
00362     
00363     /* Expand a_1d to a_2d */
00364     a_2d = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00365     pa_2d = cpl_image_get_data_float(a_2d);
00366     pa_1d = cpl_image_get_data_float(a_1d);
00367     for (j=0 ; j<ny ; j++) {
00368         for (i=0 ; i<nx ; i++) {
00369             pa_2d[i+j*nx] = pa_1d[j];
00370         }
00371     }
00372     cpl_image_delete(a_1d);
00373 
00374     return a_2d;
00375 }
00376 
00377 /*----------------------------------------------------------------------------*/
00385 /*----------------------------------------------------------------------------*/
00386 static int sofi_img_domeflat_save(
00387         cpl_image           *   flat,
00388         cpl_parameterlist   *   parlist,
00389         cpl_frameset        *   set)
00390 {
00391     cpl_propertylist    *   plist;
00392     cpl_propertylist    *   paflist;
00393     cpl_propertylist    *   qclist;
00394     const cpl_frame     *   ref_frame;
00395 
00396 
00397     /* Get the QC params in qclist */
00398     qclist = cpl_propertylist_new();
00399 
00400     /* Write the average image */
00401     irplib_dfs_save_image(set,
00402             parlist,
00403             set,
00404             flat,
00405             CPL_BPP_IEEE_FLOAT,
00406             "sofi_img_domeflat",
00407             SOFI_IMG_DOMEFLAT_RES,
00408             qclist,
00409             NULL,
00410             PACKAGE "/" PACKAGE_VERSION,
00411             "sofi_img_domeflat.fits");
00412     
00413     /* Get the reference frame */
00414     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
00415 
00416     /* Get FITS header from reference file */
00417     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00418                     0)) == NULL) {
00419         cpl_msg_error(cpl_func, "getting header from reference frame");
00420         cpl_propertylist_delete(qclist);
00421         return -1;
00422     }
00423 
00424     /* Get the keywords for the paf file */
00425     paflist = cpl_propertylist_new();
00426     cpl_propertylist_copy_property_regexp(paflist, plist,
00427         "^(DATE-OBS|ESO DET CHIP NAME|ARCFILE|ESO TPL ID|ESO DET MODE NAME|"
00428         "ESO DET NCORRS NAME|ESO DET RSPEED|ESO DET DIT)$", 0);
00429     cpl_propertylist_delete(plist);
00430 
00431     /* Copy the QC in paflist */
00432     /* cpl_propertylist_copy_property_regexp(paflist, qclist, "", 0) ; */
00433     cpl_propertylist_delete(qclist);
00434 
00435     /* Save the PAF file */
00436     cpl_dfs_save_paf("SOFI",
00437             "sofi_img_domeflat",
00438             paflist,
00439             "sofi_img_domeflat.paf");
00440     cpl_propertylist_delete(paflist);
00441     return  0;
00442 }
00443 

Generated on Thu Mar 7 16:21:06 2013 for SOFI Pipeline Reference Manual by  doxygen 1.5.8