isaac_img_illum.c

00001 /* $Id: isaac_img_illum.c,v 1.36 2010/03/02 13:26:12 llundin Exp $
00002  *
00003  * This file is part of the ISAAC 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: 2010/03/02 13:26:12 $
00024  * $Revision: 1.36 $
00025  * $Name: HEAD $
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_plugin.h"
00040 #include "irplib_utils.h"
00041 #include "irplib_calib.h"
00042 #include "irplib_strehl.h"
00043 
00044 #include "isaac_utils.h"
00045 #include "isaac_pfits.h"
00046 #include "isaac_dfs.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                                 Define
00050  -----------------------------------------------------------------------------*/
00051 
00052 #define RECIPE_STRING "isaac_img_illum"
00053 
00054 /*-----------------------------------------------------------------------------
00055                             Functions prototypes
00056  -----------------------------------------------------------------------------*/
00057 
00058 static cpl_error_code isaac_img_illum_fill_parameterlist(cpl_parameterlist *);
00059 static cpl_image * isaac_img_illum_reduce(const cpl_frameset *, const char *,
00060                                           const char *, const char *,
00061                                           cpl_table **);
00062 static cpl_bivector * isaac_img_illum_find_pos(const cpl_imagelist *,
00063                                                const cpl_bivector *);
00064 static cpl_vector * isaac_img_illum_phot(const cpl_imagelist *,
00065                                          const cpl_bivector *, int *);
00066 static cpl_error_code isaac_img_illum_save(cpl_frameset *,
00067                                            const cpl_parameterlist *,
00068                                            const cpl_image *, const cpl_table *);
00069 
00070 CPL_RECIPE_DEFINE(isaac_img_illum, ISAAC_BINARY_VERSION,
00071                   isaac_img_illum_fill_parameterlist(recipe->parameters),
00072                   "Lars Lundin", PACKAGE_BUGREPORT, "2008", 
00073                   "ISAAC imaging illumination frame recipe",
00074                   RECIPE_STRING " -- ISAAC imaging illumination frame recipe.\n"
00075                   "The files listed in the Set Of Frames (sof-file) " 
00076                   "must be tagged:\n"
00077                   "raw-file.fits "  ISAAC_IMG_ILLUM_RAW" or\n"
00078                   "flat-file.fits " ISAAC_CALIB_FLAT" or\n"
00079                   "bpm-file.fits "  ISAAC_CALIB_BPM" or\n"
00080                   "dark-file.fits " ISAAC_CALIB_DARK"\n");
00081 
00082 /*-----------------------------------------------------------------------------
00083                             Static variables
00084  -----------------------------------------------------------------------------*/
00085 
00086 static struct {
00087     /* Inputs */
00088     int         s_hx;
00089     int         s_hy;
00090     double      star_r;
00091     double      bg_r1;
00092     double      bg_r2;
00093     int         pos_x;
00094     int         pos_y;
00095 
00096     /* Outputs */
00097     int         poly_deg;
00098     double      fit_error;
00099     double      norm;
00100     double      illum1;
00101     double      illumx;
00102     double      illumy;
00103     double      illumxy;
00104     double      illumxx;
00105     double      illumyy;
00106 } isaac_img_illum_config;
00107 
00108 /*-----------------------------------------------------------------------------
00109                                 Functions code
00110  -----------------------------------------------------------------------------*/
00111 
00112 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 static
00122 cpl_error_code isaac_img_illum_fill_parameterlist(cpl_parameterlist * self)
00123 {
00124     const char * context = PACKAGE "." RECIPE_STRING;
00125     cpl_error_code err;
00126 
00127     cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
00128 
00129     /* Fill the parameters list */
00130 
00131     /* --star_r */
00132     err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00133                                           "star_r", 10.0, NULL, context,
00134                                           "The star radius");
00135     cpl_ensure_code(!err, err);
00136 
00137     /* --bg_r1 */
00138     err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00139                                           "bg_r1", 12.0, NULL, context,
00140                                           "The internal background radius");
00141     cpl_ensure_code(!err, err);
00142 
00143     /* --bg_r2 */
00144     err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00145                                           "bg_r2", 30.0, NULL, context,
00146                                           "The external background radius");
00147     cpl_ensure_code(!err, err);
00148 
00149     /* --s_hx */
00150     err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00151                                        "s_hx", 50, NULL, context,
00152                                        "Half size of the seach box in x");
00153     cpl_ensure_code(!err, err);
00154 
00155     /* --s_hy */
00156     err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00157                                        "s_hy", 50, NULL, context,
00158                                        "Half size of the seach box in y");
00159     cpl_ensure_code(!err, err);
00160 
00161     /* --pos_x */
00162     err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00163                                        "pos_x", -1, NULL, context,
00164                                        "The star position in x");
00165     cpl_ensure_code(!err, err);
00166 
00167     /* --pos_y */
00168     err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00169                                        "pos_y", -1, NULL, context,
00170                                        "The star position in y");
00171     cpl_ensure_code(!err, err);
00172 
00173     return CPL_ERROR_NONE;
00174 }
00175 
00176 /*----------------------------------------------------------------------------*/
00184 /*----------------------------------------------------------------------------*/
00185 static int isaac_img_illum(cpl_frameset            * framelist,
00186                            const cpl_parameterlist * parlist)
00187 {
00188     cpl_frameset * rawframes = NULL;
00189     cpl_image    * ima       = NULL;
00190     cpl_table    * flux      = NULL;
00191     const char   * bpm;
00192     const char   * flat;
00193     const char   * dark;
00194 
00195 
00196     /* Retrieve input parameters */
00197     isaac_img_illum_config.s_hx
00198         = irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "s_hx");
00199 
00200     isaac_img_illum_config.s_hy
00201         = irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "s_hy");
00202 
00203     isaac_img_illum_config.star_r
00204         = irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00205                                           "star_r");
00206     isaac_img_illum_config.bg_r1
00207         = irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00208                                           "bg_r1");
00209     isaac_img_illum_config.bg_r2
00210         = irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00211                                           "bg_r2");
00212     isaac_img_illum_config.pos_x
00213         = irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
00214                                        "pos_x");
00215     isaac_img_illum_config.pos_y
00216         = irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
00217                                        "pos_y");
00218 
00219     /* Identify the RAW and CALIB frames in the input frameset */
00220     skip_if (isaac_dfs_set_groups(framelist));
00221 
00222     /* Retrieve calibration data */
00223     flat = isaac_extract_filename(framelist, ISAAC_CALIB_FLAT);
00224     dark = isaac_extract_filename(framelist, ISAAC_CALIB_DARK);
00225     bpm  = isaac_extract_filename(framelist, ISAAC_CALIB_BPM);
00226 
00227     /* Retrieve raw frames */
00228     rawframes = isaac_extract_frameset(framelist, ISAAC_IMG_ILLUM_RAW);
00229     error_if(rawframes == NULL, CPL_ERROR_DATA_NOT_FOUND,
00230              "Cannot find any raw frame in the input");
00231     
00232     /* Apply the data reduction */
00233     cpl_msg_info(cpl_func, "Apply the reduction");
00234     ima = isaac_img_illum_reduce(rawframes, flat, dark, bpm, &flux);
00235     error_if (ima == NULL, cpl_error_get_code(), "Cannot reduce the data");
00236 
00237     /* Save the products */
00238     cpl_msg_info(cpl_func, "Saving the products");
00239     skip_if (isaac_img_illum_save(framelist, parlist, ima, flux));
00240 
00241     end_skip;
00242 
00243     cpl_image_delete(ima);
00244     cpl_table_delete(flux);
00245     cpl_frameset_delete(rawframes);
00246 
00247     return cpl_error_get_code();
00248 }
00249 
00250 /*----------------------------------------------------------------------------*/
00260 /*----------------------------------------------------------------------------*/
00261 static cpl_image * isaac_img_illum_reduce(const cpl_frameset * raw,
00262                                           const char         * flat,
00263                                           const char         * dark,
00264                                           const char         * bpm,
00265                                           cpl_table         ** fl)
00266 {
00267     cpl_bivector    *   offsets;
00268     cpl_imagelist   *   in;
00269     cpl_bivector    *   positions;
00270     cpl_matrix      *   purged_pos;
00271     double          *   positions_x,
00272                     *   positions_y,
00273                     *   purged_pos_x,
00274                     *   purged_pos_y;
00275     cpl_vector      *   flux;
00276     cpl_vector      *   purged_flux;
00277     cpl_polynomial  *   poly;
00278     cpl_image       *   poly_ima;
00279     int                 nval = 0; /* uninit warning */
00280     int                 nx, ny;
00281     int                 nflux;
00282     int                 power[2];
00283     int                 i, j;
00284     
00285     /* Initialise */
00286     *fl = NULL;
00287 
00288     /* Get the offsets from the raw frames */
00289     cpl_msg_info(cpl_func, "Get the offsets from the files headers");
00290     cpl_msg_indent_more();
00291     if ((offsets = isaac_get_offsets(raw)) == NULL) {
00292         cpl_msg_error(cpl_func, "Cannot get the offsets");
00293         cpl_msg_indent_less();
00294         return NULL;
00295     }
00296     cpl_msg_indent_less();
00297 
00298     /* Load the input data */
00299     cpl_msg_info(cpl_func, "Load the input data");
00300     cpl_msg_indent_more();
00301     if ((in = cpl_imagelist_load_frameset(raw, CPL_TYPE_FLOAT, 1, 0)) == NULL) {
00302         cpl_msg_error(cpl_func, "Cannot load input data");
00303         cpl_bivector_delete(offsets);
00304         cpl_msg_indent_less();
00305         return NULL;
00306     }
00307     cpl_msg_indent_less();
00308 
00309     /* Apply the calibrations */
00310     if (flat || dark || bpm) {
00311         cpl_msg_info(cpl_func, "Apply the calibrations");
00312         cpl_msg_indent_more();
00313         if (irplib_flat_dark_bpm_calib(in, flat, dark, bpm) == -1) {
00314             cpl_msg_error(cpl_func, "Cannot calibrate the data");
00315             cpl_imagelist_delete(in);
00316             cpl_bivector_delete(offsets);
00317             cpl_msg_indent_less();
00318             return NULL;
00319         }
00320         cpl_msg_indent_less();
00321     }
00322 
00323     /* Find the positions */
00324     cpl_msg_info(cpl_func, "Find the positions");
00325     cpl_msg_indent_more();
00326     if ((positions=isaac_img_illum_find_pos(in, offsets)) == NULL) {
00327         cpl_msg_error(cpl_func, "Cannot find the positions");
00328         cpl_imagelist_delete(in);
00329         cpl_bivector_delete(offsets);
00330         cpl_msg_indent_less();
00331         return NULL;
00332     }
00333     cpl_bivector_delete(offsets);
00334     cpl_msg_indent_less();
00335 
00336     /* Compute the photometry */
00337     cpl_msg_info(cpl_func, "Compute the photometry");
00338     cpl_msg_indent_more();
00339     if ((flux=isaac_img_illum_phot(in, positions, &nval)) == NULL) {
00340         cpl_msg_error(cpl_func, "Cannot find the positions");
00341         cpl_imagelist_delete(in);
00342         cpl_bivector_delete(positions);
00343         cpl_msg_indent_less();
00344         return NULL;
00345     }
00346     nflux = cpl_vector_get_size(flux);
00347     nx = cpl_image_get_size_x(cpl_imagelist_get(in, 0));
00348     ny = cpl_image_get_size_y(cpl_imagelist_get(in, 0));
00349     cpl_imagelist_delete(in);
00350     cpl_msg_indent_less();
00351     if (nval < 1) {
00352         cpl_msg_error(cpl_func, "No flux computed");
00353         cpl_bivector_delete(positions);
00354         cpl_vector_delete(flux);
00355         return NULL;
00356     }
00357     
00358     /* Purge positions */
00359     purged_pos = cpl_matrix_new(2, nval);
00360     purged_pos_x = cpl_matrix_get_data(purged_pos);
00361     purged_pos_y = purged_pos_x + nval;
00362     positions_x = cpl_bivector_get_x_data(positions);
00363     positions_y = cpl_bivector_get_y_data(positions);
00364     purged_flux = cpl_vector_new(nval);
00365     j = 0;
00366     for (i=0; i < nflux; i++) {
00367         if (fabs(cpl_vector_get(flux, i)) > 0) {
00368             purged_pos_x[j] = positions_x[i];
00369             purged_pos_y[j] = positions_y[i];
00370             cpl_vector_set(purged_flux, j, cpl_vector_get(flux, i));
00371             j++;
00372         }
00373     }
00374 
00375     if (j != nval) {
00376         cpl_msg_warning(cpl_func, "Fixing size inconsistency: %d <=> %d",
00377                         j, nval);
00378         cpl_matrix_set_size(purged_pos, 2, j);
00379         cpl_vector_set_size(purged_flux, j);
00380     }
00381 
00382     /* Create the flux table */
00383     *fl = cpl_table_new(cpl_vector_get_size(flux));
00384     cpl_table_new_column(*fl, "POSX", CPL_TYPE_DOUBLE);
00385     cpl_table_new_column(*fl, "POSY", CPL_TYPE_DOUBLE);
00386     cpl_table_new_column(*fl, "FLUX", CPL_TYPE_DOUBLE);
00387     for (i=0; i<cpl_vector_get_size(flux); i++) {
00388         cpl_table_set_double(*fl, "POSX", i, positions_x[i]);
00389         cpl_table_set_double(*fl, "POSY", i, positions_y[i]);
00390         cpl_table_set_double(*fl, "FLUX", i, cpl_vector_get(flux, i));
00391     }
00392     cpl_bivector_delete(positions);
00393     cpl_vector_delete(flux);
00394     
00395     /* Degree of the polynomial */
00396     if (nval >= 6)
00397         isaac_img_illum_config.poly_deg = 2;
00398     else if (nval >= 3) isaac_img_illum_config.poly_deg = 1;
00399     else isaac_img_illum_config.poly_deg = 0;
00400 
00401     cpl_msg_info(cpl_func, "Fitting a %d-degree 2D-polynomial using %d of %d "
00402                  "fluxes", isaac_img_illum_config.poly_deg, nval, nflux);
00403 
00404     /* Fit the polynomial */
00405     poly = cpl_polynomial_new(2);
00406     if (cpl_polynomial_fit(poly, purged_pos, NULL, purged_flux, NULL, CPL_FALSE,
00407                            NULL, &(isaac_img_illum_config.poly_deg))) {
00408         cpl_msg_error(cpl_func, "Cannot fit the polynomial");
00409         cpl_matrix_delete(purged_pos);
00410         cpl_vector_delete(purged_flux);
00411         cpl_polynomial_delete(poly);
00412         cpl_table_delete(*fl);
00413         *fl = NULL;
00414         return NULL;
00415     }
00416 
00417     cpl_vector_fill_polynomial_fit_residual(purged_flux, purged_flux, NULL, poly,
00418                                             purged_pos, NULL);
00419 
00420     cpl_matrix_delete(purged_pos);
00421 
00422     isaac_img_illum_config.fit_error
00423         = cpl_vector_product(purged_flux, purged_flux)
00424         / cpl_vector_get_size(purged_flux);
00425 
00426     if (cpl_msg_get_level() <= CPL_MSG_DEBUG)
00427         cpl_vector_dump(purged_flux, stdout);
00428 
00429     cpl_vector_delete(purged_flux);
00430    
00431     /* Fill the coeffs */
00432     power[0] = 0; power[1] = 0;
00433     isaac_img_illum_config.illum1 = cpl_polynomial_get_coeff(poly, power);
00434     if (isaac_img_illum_config.poly_deg >= 1) {
00435         power[0] = 1; power[1] = 0;
00436         isaac_img_illum_config.illumx = cpl_polynomial_get_coeff(poly, power);
00437         power[0] = 0; power[1] = 1;
00438         isaac_img_illum_config.illumy = cpl_polynomial_get_coeff(poly, power);
00439     }
00440     if (isaac_img_illum_config.poly_deg >= 2) {
00441         power[0] = 1; power[1] = 1;
00442         isaac_img_illum_config.illumxy = cpl_polynomial_get_coeff(poly, power);
00443         power[0] = 2; power[1] = 0;
00444         isaac_img_illum_config.illumxx = cpl_polynomial_get_coeff(poly, power);
00445         power[0] = 0; power[1] = 2;
00446         isaac_img_illum_config.illumyy = cpl_polynomial_get_coeff(poly, power);
00447     }
00448     cpl_msg_info(cpl_func, "P(X,Y)=a0+a1*X+a2*Y+a3*X*Y+a4*X^2+a5*Y^2");
00449     cpl_msg_info(cpl_func, "a0 = %g", isaac_img_illum_config.illum1);
00450     cpl_msg_info(cpl_func, "a1 = %g", isaac_img_illum_config.illumx);
00451     cpl_msg_info(cpl_func, "a2 = %g", isaac_img_illum_config.illumy);
00452     cpl_msg_info(cpl_func, "a3 = %g", isaac_img_illum_config.illumxy);
00453     cpl_msg_info(cpl_func, "a4 = %g", isaac_img_illum_config.illumxx);
00454     cpl_msg_info(cpl_func, "a5 = %g", isaac_img_illum_config.illumyy);
00455     cpl_msg_info(cpl_func, "error = %g", isaac_img_illum_config.fit_error);
00456     
00457     /* Create the polynomial image */
00458     poly_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00459     cpl_image_fill_polynomial(poly_ima, poly, 1.0, 1.0, 1.0, 1.0);
00460     cpl_polynomial_delete(poly);
00461 
00462     /* Normalise the image */
00463     isaac_img_illum_config.norm = cpl_image_get_mean(poly_ima);
00464     cpl_image_divide_scalar(poly_ima, isaac_img_illum_config.norm);
00465 
00466     return poly_ima;
00467 }
00468 
00469 /*----------------------------------------------------------------------------*/
00477 /*----------------------------------------------------------------------------*/
00478 static cpl_vector * isaac_img_illum_phot(const cpl_imagelist * ilist,
00479                                          const cpl_bivector  * positions,
00480                                          int                 * nvalid)
00481 {
00482     const int      ni          = cpl_imagelist_get_size(ilist);
00483     /* Create the flux vector */
00484     cpl_vector   * self        = cpl_vector_new(ni);
00485     const double * positions_x = cpl_bivector_get_x_data_const(positions);
00486     const double * positions_y = cpl_bivector_get_y_data_const(positions);
00487     int            nok         = 0;
00488     int            i;
00489 
00490 
00491     bug_if(ilist     == NULL);
00492     bug_if(positions == NULL);
00493     bug_if(nvalid    == NULL);
00494 
00495     /* Loop on the frames */
00496     for (i=0; i < ni; i++) {
00497         const cpl_image * cur_ima = cpl_imagelist_get_const(ilist, i);
00498         const double bg
00499             = irplib_strehl_ring_background(cur_ima, positions_x[i],
00500                                             positions_y[i],
00501                                             isaac_img_illum_config.bg_r1,
00502                                             isaac_img_illum_config.bg_r2,
00503                                             IRPLIB_BG_METHOD_MEDIAN);
00504         const double fl
00505             = irplib_strehl_disk_flux(cur_ima, positions_x[i], positions_y[i],
00506                                       isaac_img_illum_config.star_r, bg);
00507         cpl_vector_set(self, i, fl); 
00508         if (fabs(fl)>1e-3) nok++; 
00509         cpl_msg_info(cpl_func, "Image %2d has flux at (%g,%g): %g", i+1,
00510                      positions_x[i], positions_y[i], fl);
00511     }
00512 
00513     *nvalid = nok;
00514 
00515     end_skip;
00516 
00517     if (cpl_error_get_code()) {
00518         cpl_vector_delete(self);
00519         self = NULL;
00520     }
00521 
00522     return self;
00523 }
00524  
00525 /*----------------------------------------------------------------------------*/
00532 /*----------------------------------------------------------------------------*/
00533 static cpl_bivector * isaac_img_illum_find_pos(const cpl_imagelist * ilist,
00534                                                const cpl_bivector  * offsets)
00535 {
00536     const int      ni = cpl_imagelist_get_size(ilist);
00537     /* Create the positions bivector */
00538     cpl_bivector * self = cpl_bivector_new(ni);
00539     double       * self_x = cpl_bivector_get_x_data(self);
00540     double       * self_y = cpl_bivector_get_y_data(self);
00541     const double * offsets_x = cpl_bivector_get_x_data_const(offsets);
00542     const double * offsets_y = cpl_bivector_get_y_data_const(offsets);
00543     cpl_image    * tmp_ima   = NULL;
00544     cpl_image    * filtered_ima = NULL;
00545     cpl_mask     * kernel = cpl_mask_new(3, 3);
00546     int            i;
00547 
00548     bug_if(ilist   == NULL);
00549     bug_if(offsets == NULL);
00550     
00551     /* Create the kernel */
00552     bug_if(cpl_mask_not(kernel));
00553     
00554     /* Loop on all the images */
00555     for (i=0; i < ni; i++) {
00556         const cpl_image * cur_ima = cpl_imagelist_get_const(ilist, i);
00557         const int         nx = cpl_image_get_size_x(cur_ima);
00558         const int         ny = cpl_image_get_size_y(cur_ima);
00559         int               llx, lly, urx, ury, posx, posy;
00560 
00561         /* Define zone */
00562         if (isaac_img_illum_config.pos_x > 0 &&
00563             isaac_img_illum_config.pos_y > 0) {
00564             llx = isaac_img_illum_config.pos_x + offsets_x[i] - 
00565                 isaac_img_illum_config.s_hx;
00566             urx = isaac_img_illum_config.pos_x + offsets_x[i] + 
00567                 isaac_img_illum_config.s_hx;
00568             lly = isaac_img_illum_config.pos_y + offsets_y[i] - 
00569                 isaac_img_illum_config.s_hy;
00570             ury = isaac_img_illum_config.pos_y + offsets_y[i] + 
00571                 isaac_img_illum_config.s_hy;
00572         } else {
00573             llx = nx/2 + offsets_x[i] - isaac_img_illum_config.s_hx;
00574             urx = nx/2 + offsets_x[i] + isaac_img_illum_config.s_hx;
00575             lly = ny/2 + offsets_y[i] - isaac_img_illum_config.s_hy;
00576             ury = ny/2 + offsets_y[i] + isaac_img_illum_config.s_hy;
00577         }
00578 
00579         /* Extract */
00580         cpl_image_delete(tmp_ima);
00581         tmp_ima = cpl_image_extract(cur_ima, llx, lly, urx, ury);
00582         any_if ("Bad star zone. llx=%d, urx=%d, nx=%d. "
00583                 "lly=%d, ury=%d, ny=%d", llx, urx, nx, lly, ury, ny);
00584 
00585         cpl_image_delete(filtered_ima);
00586         filtered_ima = cpl_image_new(urx-llx+1, ury-lly+1,
00587                                      cpl_image_get_type(tmp_ima));
00588         cpl_image_filter_mask(filtered_ima, tmp_ima, kernel, CPL_FILTER_MEDIAN,
00589                               CPL_BORDER_FILTER);
00590 
00591         /* Find the max */
00592         bug_if(cpl_image_get_maxpos(filtered_ima, &posx, &posy));
00593         self_x[i] = llx + posx;
00594         self_y[i] = lly + posy;
00595         cpl_msg_info(cpl_func, "image %2d has star at: %g, %g", i+1,
00596                      self_x[i], self_y[i]);
00597     }
00598 
00599     end_skip;
00600 
00601     cpl_image_delete(tmp_ima);
00602     cpl_image_delete(filtered_ima);
00603     cpl_mask_delete(kernel);
00604 
00605     if (cpl_error_get_code()) {
00606         cpl_bivector_delete(self);
00607         self = NULL;
00608     }
00609 
00610     return self;
00611 }
00612 
00613 /*----------------------------------------------------------------------------*/
00622 /*----------------------------------------------------------------------------*/
00623 static cpl_error_code isaac_img_illum_save(cpl_frameset            * set,
00624                                            const cpl_parameterlist * parlist,
00625                                            const cpl_image         * ima,
00626                                            const cpl_table         * flux)
00627 {
00628     /* Get FITS header from reference file */
00629     const char * filename     = cpl_frame_get_filename
00630         (irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW));
00631     cpl_propertylist * plist  = NULL;
00632     cpl_propertylist * qclist = cpl_propertylist_new();
00633     const char       * sval;
00634 
00635     skip_if(0);
00636 
00637     plist = cpl_propertylist_load(filename, 0);
00638     skip_if(plist == NULL);
00639 
00640     sval = isaac_pfits_get_filter(plist);
00641     if (cpl_error_get_code()) cpl_error_reset();
00642     else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
00643 
00644     cpl_propertylist_append_double(qclist, "ESO QC ILLUM1",
00645             isaac_img_illum_config.illum1);
00646     cpl_propertylist_append_double(qclist, "ESO QC ILLUMX", 
00647             isaac_img_illum_config.illumx);
00648     cpl_propertylist_append_double(qclist, "ESO QC ILLUMY", 
00649             isaac_img_illum_config.illumy);
00650     cpl_propertylist_append_double(qclist, "ESO QC ILLUMXY",
00651             isaac_img_illum_config.illumxy);
00652     cpl_propertylist_append_double(qclist, "ESO QC ILLUMXX", 
00653             isaac_img_illum_config.illumxx);
00654     cpl_propertylist_append_double(qclist, "ESO QC ILLUMYY",
00655             isaac_img_illum_config.illumyy);
00656     cpl_propertylist_append_double(qclist, "ESO QC FITERROR",
00657             isaac_img_illum_config.fit_error);
00658     cpl_propertylist_append_double(qclist, "ESO QC FITNORM",
00659             isaac_img_illum_config.norm);
00660 
00661     bug_if(0);
00662 
00663     /* Get the keywords for the paf file */
00664     (void)cpl_propertylist_erase_regexp(plist,
00665                                         "^(ARCFILE|MJD-OBS|ESO TPL ID|DATE-OBS"
00666                                         "|ESO DET DIT|ESO DET NDIT"
00667                                         "|ESO DET NCORRS|ESO DET MODE NAME"
00668                                         "|ESO OCS SELECT-ARM)$", 1);
00669     
00670     /* Write the image */
00671     skip_if(irplib_dfs_save_image(set, parlist, set, ima, CPL_BPP_IEEE_FLOAT,
00672                                   RECIPE_STRING, ISAAC_IMG_ILLUM_RES,
00673                                   qclist, NULL, PACKAGE "/" PACKAGE_VERSION,
00674                                   RECIPE_STRING CPL_DFS_FITS));
00675 
00676     if (flux != NULL) {
00677         /* Write the table */
00678         skip_if(irplib_dfs_save_table(set, parlist, set, flux, NULL,
00679                                       RECIPE_STRING, ISAAC_IMG_ILLUM_TAB,
00680                                       qclist, NULL, PACKAGE "/" PACKAGE_VERSION,
00681                                       RECIPE_STRING "_flux" CPL_DFS_FITS));
00682     }
00683     
00684     /* Copy the QC in paflist */
00685     bug_if(cpl_propertylist_append(plist, qclist));
00686     cpl_propertylist_empty(qclist);
00687 
00688     /* PRO.CATG */
00689     cpl_propertylist_update_string(plist, CPL_DFS_PRO_CATG,
00690                                    ISAAC_IMG_ILLUM_RES);
00691 
00692     /* Save the PAF file */
00693     skip_if(cpl_dfs_save_paf("ISAAC", RECIPE_STRING, plist,
00694                              RECIPE_STRING CPL_DFS_PAF));
00695 
00696     end_skip;
00697 
00698     cpl_propertylist_delete(plist);
00699     cpl_propertylist_delete(qclist);
00700 
00701     return cpl_error_get_code();
00702 }

Generated on Wed Mar 9 15:43:10 2011 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8