visir_img_illu.c

00001 /* $Id: visir_img_illu.c,v 1.84 2009/02/27 10:43:29 llundin Exp $
00002  *
00003  * This file is part of the VISIR 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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2009/02/27 10:43:29 $
00024  * $Revision: 1.84 $
00025  * $Name: visir-3_5_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "visir_recipe.h"
00037 
00038 /*-----------------------------------------------------------------------------
00039                                 Defines
00040 -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "visir_img_illu"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Private Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 static cpl_image * visir_img_illu_fit(const cpl_table *, int, int);
00048 static cpl_error_code visir_img_illu_save(cpl_frameset *,
00049                                           const cpl_parameterlist *,
00050                                           const cpl_table *,
00051                                           const cpl_image *);
00052 
00053 VISIR_RECIPE_DEFINE(visir_img_illu,
00054                     VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00055                     VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00056                     VISIR_PARAM_STRIPNON |
00057                     VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
00058                     "Illumination recipe",
00059                     "This recipe compares the illumination of a bright star "
00060                     "at different\n"
00061                     "positions on the detector. It produces a normalised "
00062                     "illumination map.\n"
00063                     "The files listed in the Set Of Frames (sof-file) "
00064                     "must be tagged:\n"
00065                     "VISIR-illumination-file.fits " VISIR_IMG_ILLU_RAW "\n"
00066                     MAN_VISIR_CALIB_BPM_IMG);
00067 
00068 /*----------------------------------------------------------------------------*/
00072 /*----------------------------------------------------------------------------*/
00073 
00074 /*-----------------------------------------------------------------------------
00075                                 Functions code
00076  -----------------------------------------------------------------------------*/
00077 
00078 /*----------------------------------------------------------------------------*/
00085 /*----------------------------------------------------------------------------*/
00086 static int visir_img_illu(cpl_frameset            * framelist,
00087                           const cpl_parameterlist * parlist)
00088 {
00089     irplib_framelist * allframes = NULL;
00090     irplib_framelist * rawframes = NULL;
00091     const char      *   badpix;
00092     const char      *   flat;
00093     cpl_imagelist   *   nodded = NULL;
00094     cpl_table       *   tab = NULL;
00095     cpl_image       *   fitted = NULL;
00096     int                 nx, ny;
00097     int                 nfiles;
00098 
00099 
00100     /* Identify the RAW and CALIB frames in the input frameset */
00101     skip_if (visir_dfs_set_groups(framelist));
00102 
00103     /* Objects observation */
00104     allframes = irplib_framelist_cast(framelist);
00105     skip_if(allframes == NULL);
00106     rawframes = irplib_framelist_extract(allframes, VISIR_IMG_ILLU_RAW);
00107     skip_if (rawframes == NULL);
00108 
00109     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
00110                                                    visir_property_regexp,
00111                                                    CPL_FALSE));
00112 
00113     skip_if(visir_dfs_check_framelist_tag(rawframes));
00114 
00115     /* Bad pixels calibration file */
00116     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00117 
00118     /* Flatfield calibration file */
00119     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00120 
00121     /* Combine the frames */
00122     cpl_msg_info(cpl_func, "Construct the nodded images");
00123     nodded = visir_inputs_combine(RECIPE_STRING, parlist, rawframes, badpix, flat,
00124                                   NULL, CPL_FALSE, 0.0,0);
00125     if (nodded == NULL) {
00126         cpl_msg_error(cpl_func, "Could not combine the input frames");
00127         skip_if(1);
00128     }
00129 
00130     nfiles = cpl_imagelist_get_size(nodded);
00131     nx = cpl_image_get_size_x(cpl_imagelist_get(nodded, 0));
00132     ny = cpl_image_get_size_y(cpl_imagelist_get(nodded, 0));
00133     skip_if (0);
00134 
00135     /* Allocate and initialise arrays */
00136     tab = visir_table_new_xypos(nodded, "FLUX");
00137     skip_if (tab == NULL);
00138 
00139     /* Get the illumination */
00140     cpl_msg_info(cpl_func, "Detect the objects and compute the flux");
00141 
00142     /* Fit the polynomial */
00143     cpl_msg_info(cpl_func, "Fit a 2d polynomial on the flux");
00144     if ((fitted = visir_img_illu_fit(tab, nx, ny)) == NULL) {
00145         cpl_msg_error(cpl_func, "Could not compute the fit: '%s' in %s",
00146                       cpl_error_get_message(), cpl_error_get_where());
00147         skip_if(1);
00148     }
00149     
00150     /* Save the products */
00151     cpl_msg_info(cpl_func, "Saving products");
00152     skip_if (visir_img_illu_save(framelist, parlist, tab, fitted));
00153 
00154     end_skip;
00155 
00156     irplib_framelist_delete(allframes);
00157     irplib_framelist_delete(rawframes);
00158     cpl_imagelist_delete(nodded);
00159     cpl_image_delete(fitted);
00160     cpl_table_delete(tab);
00161 
00162     return cpl_error_get_code();
00163 }
00164 
00165 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 static cpl_image * visir_img_illu_fit(
00175         const cpl_table * tab,
00176         int               nx,
00177         int               ny)
00178 {
00179     const int           nrow = cpl_table_get_nrow(tab);
00180     cpl_polynomial  *   pol;
00181     cpl_bivector    *   surface;
00182     cpl_vector      *   x_pos;
00183     cpl_vector      *   y_pos;
00184     cpl_vector      *   values;
00185     double              norm;
00186     cpl_image       *   fitted;
00187     double              xpos, ypos, flux;
00188     double              mse = -1;
00189     int                 npoints = 0;
00190     int                 i;
00191 
00192 
00193     /* This will catch tab == NULL */
00194     cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00195 
00196 
00197     /* Count the number of valid FWHMs values */
00198     for (i=0 ; i < nrow ; i++)
00199         if (cpl_table_get_double(tab, "X_POS", i, NULL) > 0 &&
00200             cpl_table_get_double(tab, "Y_POS", i, NULL) > 0)
00201             npoints++;
00202 
00203     assert(!cpl_error_get_code());
00204 
00205     /* Need at least six data-points to fit a 2nd degree 2D-polynomial */
00206     cpl_ensure(npoints >= 6, CPL_ERROR_DATA_NOT_FOUND, NULL);
00207 
00208     /* Fill the grid to fit */
00209     surface = cpl_bivector_new(npoints);
00210     x_pos = cpl_bivector_get_x(surface);
00211     y_pos = cpl_bivector_get_y(surface);
00212     values = cpl_vector_new(npoints);
00213     npoints = 0;
00214     for (i=0 ; i < nrow ; i++) {
00215         xpos = cpl_table_get_double(tab, "X_POS", i, NULL);
00216         if (xpos <= 0) continue;
00217         ypos = cpl_table_get_double(tab, "Y_POS", i, NULL);
00218         if (ypos <= 0) continue;
00219 
00220         flux = cpl_table_get_double(tab, "FLUX",  i, NULL);
00221 
00222 
00223         cpl_vector_set(x_pos,  npoints, xpos);
00224         cpl_vector_set(y_pos,  npoints, ypos);
00225         cpl_vector_set(values, npoints, flux);
00226         npoints++;
00227     }
00228     
00229     /* Normalise the grid */
00230     norm = cpl_vector_get(values, npoints/2 );
00231     cpl_vector_divide_scalar(values, norm);
00232 
00233     if (cpl_error_get_code()) {
00234         cpl_bivector_delete(surface);
00235         cpl_vector_delete(values);
00236         return NULL;
00237     }
00238     
00239     /* Apply the fit */
00240     pol = cpl_polynomial_fit_2d_create(surface, values, 2, &mse);
00241     cpl_msg_info(cpl_func, "Mean Squared Error(%d): %g", npoints, mse);
00242 
00243     cpl_bivector_delete(surface);
00244     cpl_vector_delete(values);
00245 
00246     if (pol == NULL) return NULL;
00247     
00248     /* Generate the polynomial image */
00249     fitted = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00250     if (cpl_image_fill_polynomial(fitted, pol, 1.0, 1.0, 1.0, 1.0)) {
00251         cpl_image_delete(fitted);
00252         cpl_polynomial_delete(pol);
00253         return NULL;
00254     }
00255     cpl_polynomial_delete(pol);
00256     
00257     return fitted;
00258 }
00259 
00260 /*----------------------------------------------------------------------------*/
00269 /*----------------------------------------------------------------------------*/
00270 static cpl_error_code visir_img_illu_save(cpl_frameset            * set,
00271                                           const cpl_parameterlist * parlist,
00272                                           const cpl_table         * tab,
00273                                           const cpl_image         * fitted)
00274 {
00275 
00276 
00277     bug_if(0);
00278 
00279     /* SAVE THE TABLE  - and THE IMAGE */
00280     skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
00281                                VISIR_IMG_ILLU_TAB_PROCATG, NULL, NULL,
00282                                visir_pipe_id,
00283                                RECIPE_STRING "_tab" CPL_DFS_FITS));
00284 
00285     if (fitted)
00286         skip_if(irplib_dfs_save_image(set, parlist, set, fitted, CPL_BPP_IEEE_FLOAT,
00287                                   RECIPE_STRING, VISIR_IMG_ILLU_FITTED_PROCATG,
00288                                   NULL, NULL, visir_pipe_id,
00289                                   RECIPE_STRING CPL_DFS_FITS));
00290 
00291     end_skip;
00292 
00293     return cpl_error_get_code();
00294 }

Generated on Mon Feb 6 15:23:49 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8