isaac_img_illum.c

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