isaac_img_twflat.c

00001 /* $Id: isaac_img_twflat.c,v 1.44 2012/01/25 12:51:30 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: 2012/01/25 12:51:30 $
00024  * $Revision: 1.44 $
00025  * $Name: isaac-6_1_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "irplib_flat.h"
00042 
00043 #include "isaac_utils.h"
00044 #include "irplib_oddeven.h"
00045 #include "isaac_pfits.h"
00046 #include "isaac_dfs.h"
00047 
00048 /* FIXME: To be removed when CPL4.2 is available */
00049 #define PRO_DATANCOM                "ESO PRO DATANCOM"
00050 
00051 /*-----------------------------------------------------------------------------
00052                             Functions prototypes
00053  -----------------------------------------------------------------------------*/
00054 
00055 static int isaac_img_twflat_create(cpl_plugin *);
00056 static int isaac_img_twflat_exec(cpl_plugin *);
00057 static int isaac_img_twflat_destroy(cpl_plugin *);
00058 static int isaac_img_twflat(cpl_parameterlist *, cpl_frameset *);
00059 static cpl_imagelist * isaac_img_twflat_reduce(cpl_frameset *, cpl_frameset *);
00060 static int isaac_img_twflat_save(cpl_imagelist *, cpl_image *, int, 
00061         cpl_frameset *, cpl_parameterlist *, cpl_frameset *);
00062 static int isaac_img_twflat_compare(const cpl_frame *, const cpl_frame *); 
00063 
00064 /*-----------------------------------------------------------------------------
00065                             Static variables
00066  -----------------------------------------------------------------------------*/
00067 
00068 static struct {
00069     double      low_thresh;
00070     double      high_thresh;
00071     int         nb_badpix;
00072     double      med_stdev;
00073     double      med_avg;
00074     double      med_min;
00075     double      med_max;
00076     int         prop_flag;
00077     int         bpm_flag;
00078     int         errmap_flag;
00079     int         intercept_flag;
00080     double      oddeven_ll_max;
00081     double      oddeven_lr_max;
00082     double      oddeven_ul_max;
00083     double      oddeven_ur_max;
00084     double      oddeven_ll_stdev;
00085     double      oddeven_lr_stdev;
00086     double      oddeven_ul_stdev;
00087     double      oddeven_ur_stdev;
00088     double      oddeven_ll_mean;
00089     double      oddeven_lr_mean;
00090     double      oddeven_ul_mean;
00091     double      oddeven_ur_mean;
00092 } isaac_img_twflat_config;
00093 
00094 static char isaac_img_twflat_description[] = 
00095 "isaac_img_twflat -- ISAAC imaging flat-field creation from twillight images.\n"
00096 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00097 "raw-file.fits "ISAAC_IMG_TWFLAT_RAW" or\n"
00098 "raw-file.fits "ISAAC_IMG_TWFLAT_POL_RAW" or\n"
00099 "dark-raw-file.fits "ISAAC_CALIB_DARK"\n";
00100 
00101 /*-----------------------------------------------------------------------------
00102                                 Functions code
00103  -----------------------------------------------------------------------------*/
00104 
00105 /*----------------------------------------------------------------------------*/
00113 /*----------------------------------------------------------------------------*/
00114 int cpl_plugin_get_info(cpl_pluginlist * list)
00115 {
00116     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00117     cpl_plugin  *   plugin = &recipe->interface;
00118 
00119     cpl_plugin_init(plugin,
00120                     CPL_PLUGIN_API,
00121                     ISAAC_BINARY_VERSION,
00122                     CPL_PLUGIN_TYPE_RECIPE,
00123                     "isaac_img_twflat",
00124                     "Twillight flat recipe",
00125                     isaac_img_twflat_description,
00126                     "Lars Lundin",
00127                     PACKAGE_BUGREPORT,
00128                     isaac_get_license(),
00129                     isaac_img_twflat_create,
00130                     isaac_img_twflat_exec,
00131                     isaac_img_twflat_destroy);
00132 
00133     cpl_pluginlist_append(list, plugin);
00134     
00135     return 0;
00136 }
00137 
00138 /*----------------------------------------------------------------------------*/
00147 /*----------------------------------------------------------------------------*/
00148 static int isaac_img_twflat_create(cpl_plugin * plugin)
00149 {
00150     cpl_recipe      * recipe;
00151     cpl_parameter   * p;
00152 
00153     /* Get the recipe out of the plugin */
00154     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00155         recipe = (cpl_recipe *)plugin;
00156     else return -1;
00157 
00158     /* Create the parameters list in the cpl_recipe object */
00159     recipe->parameters = cpl_parameterlist_new();
00160 
00161     /* Fill the parameters list */
00162     /* --t */
00163     p = cpl_parameter_new_value("isaac.isaac_img_twflat.thresholds",
00164                                 CPL_TYPE_STRING,
00165                                 "Low and high thresholds for the BPM",
00166                                 "isaac.isaac_img_twflat",
00167                                 "0.5,2.0");
00168     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "t");
00169     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00170     cpl_parameterlist_append(recipe->parameters, p);
00171     /* --prop */
00172     p = cpl_parameter_new_value("isaac.isaac_img_twflat.proport", CPL_TYPE_BOOL,
00173             "flag to activate the proportional fit", "isaac.isaac_img_twflat",
00174             FALSE);
00175     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "prop");
00176     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00177     cpl_parameterlist_append(recipe->parameters, p);
00178     
00179     /* --bpm */
00180     p = cpl_parameter_new_value("isaac.isaac_img_twflat.bpm", CPL_TYPE_BOOL,
00181             "flag to create the bad pixels map", "isaac.isaac_img_twflat",
00182             TRUE);
00183     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bpm");
00184     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00185     cpl_parameterlist_append(recipe->parameters, p);
00186     
00187     /* --errmap */
00188     p = cpl_parameter_new_value("isaac.isaac_img_twflat.errmap", CPL_TYPE_BOOL,
00189             "flag to create the error map", "isaac.isaac_img_twflat",
00190             FALSE);
00191     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "errmap");
00192     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00193     cpl_parameterlist_append(recipe->parameters, p);
00194     
00195     /* --intercept */
00196     p = cpl_parameter_new_value("isaac.isaac_img_twflat.intercept", 
00197             CPL_TYPE_BOOL, "flag to create the intercept image", 
00198             "isaac.isaac_img_twflat", FALSE);
00199     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "intercept");
00200     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00201     cpl_parameterlist_append(recipe->parameters, p);
00202     
00203     /* Return */
00204     return 0;
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00213 /*----------------------------------------------------------------------------*/
00214 static int isaac_img_twflat_exec(cpl_plugin * plugin)
00215 {
00216     cpl_recipe  *   recipe;
00217 
00218     /* Get the recipe out of the plugin */
00219     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00220         recipe = (cpl_recipe *)plugin;
00221     else return -1;
00222 
00223     return isaac_img_twflat(recipe->parameters, recipe->frames);
00224 }
00225 
00226 /*----------------------------------------------------------------------------*/
00232 /*----------------------------------------------------------------------------*/
00233 static int isaac_img_twflat_destroy(cpl_plugin * plugin)
00234 {
00235     cpl_recipe  *   recipe;
00236 
00237     /* Get the recipe out of the plugin */
00238     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00239         recipe = (cpl_recipe *)plugin;
00240     else return -1;
00241 
00242     cpl_parameterlist_delete(recipe->parameters);
00243     return 0;
00244 }
00245 
00246 /*----------------------------------------------------------------------------*/
00253 /*----------------------------------------------------------------------------*/
00254 static int isaac_img_twflat(
00255         cpl_parameterlist   *   parlist, 
00256         cpl_frameset        *   framelist)
00257 {
00258     const char      *   sval;
00259     cpl_parameter   *   par;
00260     cpl_size        *   labels;
00261     cpl_size            nlabels;
00262     cpl_frameset    *   flatframes;
00263     cpl_frameset    *   darkframes;
00264     cpl_frameset    *   flat_one;
00265     cpl_frameset    *   dark_one;
00266     int                 ndarks;
00267     cpl_imagelist   *   twflat;
00268     cpl_mask        *   bpm;
00269     cpl_image       *   bpm_im;
00270     cpl_boolean         did_reduce = CPL_FALSE;
00271     int                 i;
00272     
00273     /* Initialise */
00274     par = NULL;
00275     bpm_im = NULL;
00276     isaac_img_twflat_config.nb_badpix = -1;
00277 
00278     /* Retrieve input parameters */
00279     /* --t */
00280     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_twflat.thresholds");
00281     sval = cpl_parameter_get_string(par);
00282     if (sscanf(sval, "%lg,%lg",
00283                     &isaac_img_twflat_config.low_thresh,
00284                     &isaac_img_twflat_config.high_thresh)!=2) {
00285         return -1;
00286     }
00287     /* --prop */
00288     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_twflat.proport");
00289     isaac_img_twflat_config.prop_flag = cpl_parameter_get_bool(par);
00290     /* --bpm */
00291     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_twflat.bpm");
00292     isaac_img_twflat_config.bpm_flag = cpl_parameter_get_bool(par);
00293     /* --errmap */
00294     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_twflat.errmap");
00295     isaac_img_twflat_config.errmap_flag = cpl_parameter_get_bool(par);
00296     /* --intercept */
00297     par = cpl_parameterlist_find(parlist, "isaac.isaac_img_twflat.intercept");
00298     isaac_img_twflat_config.intercept_flag = cpl_parameter_get_bool(par);
00299    
00300     /* Identify the RAW and CALIB frames in the input frameset */
00301     if (isaac_dfs_set_groups(framelist)) {
00302         cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
00303         return -1;
00304     }
00305 
00306     /* Retrieve raw frames */
00307     if ((flatframes = isaac_extract_frameset(framelist,
00308                     ISAAC_IMG_TWFLAT_RAW)) != NULL) {
00309     } else if ((flatframes = isaac_extract_frameset(framelist,
00310                     ISAAC_IMG_TWFLAT_POL_RAW)) != NULL) {
00311     } else {
00312         cpl_msg_error(cpl_func, "Cannot find flat frames in the input list");
00313         return -1;
00314     }
00315     darkframes = isaac_extract_frameset(framelist, ISAAC_CALIB_DARK);
00316 
00317     /* If dark frames are provided - check their number  */
00318     if (darkframes != NULL) {
00319         ndarks = cpl_frameset_get_size(darkframes);
00320         if ((ndarks != 1) && (cpl_frameset_get_size(flatframes) != ndarks)) {
00321             cpl_msg_error(cpl_func, "Invalid number of dark frames submitted");
00322             cpl_frameset_delete(flatframes);
00323             cpl_frameset_delete(darkframes);
00324             return -1;
00325         }
00326     }
00327     
00328     /* Labelise all input frames */
00329     labels = cpl_frameset_labelise(flatframes, isaac_img_twflat_compare, 
00330                 &nlabels);
00331     if (labels == NULL) {
00332         cpl_msg_error(cpl_func, "Cannot labelise input frames");
00333         cpl_frameset_delete(flatframes);
00334         if (darkframes) cpl_frameset_delete(darkframes);
00335         return -1; 
00336     }
00337    
00338     /* Extract settings and reduce each of them */
00339     for (i=0; i<nlabels; i++) {
00340         /* Reduce data set nb i */
00341         cpl_msg_info(cpl_func, "Reduce data set no %d out of %d", i+1,
00342                      (int)nlabels);
00343         cpl_msg_indent_more();
00344         flat_one = cpl_frameset_extract(flatframes, labels, i);
00345         dark_one = NULL;
00346         if (darkframes) dark_one = cpl_frameset_extract(darkframes, labels, i);
00347         twflat = isaac_img_twflat_reduce(flat_one, dark_one);
00348         cpl_msg_indent_less();
00349 
00350         /* Save the products */
00351         cpl_msg_info(cpl_func, "Save the products");
00352         cpl_msg_indent_more();
00353         if (twflat == NULL) {
00354             cpl_msg_warning(cpl_func, "Cannot reduce set nb %d", i+1);
00355         } else {
00356             /* Create the Bad pixels map */
00357             if (isaac_img_twflat_config.bpm_flag) {
00358                 if ((bpm = cpl_mask_threshold_image_create(
00359                                 cpl_imagelist_get(twflat, 0),
00360                                 isaac_img_twflat_config.low_thresh,
00361                                 isaac_img_twflat_config.high_thresh)) == NULL) {
00362                     cpl_msg_warning(cpl_func, "Cannot create bad pixels map");
00363                 } else {
00364                     cpl_mask_not(bpm);
00365                     isaac_img_twflat_config.nb_badpix = cpl_mask_count(bpm);
00366                     bpm_im = cpl_image_new_from_mask(bpm);
00367                     cpl_mask_delete(bpm);
00368                 }
00369             }
00370             isaac_img_twflat_save(twflat, bpm_im, i+1, flat_one, parlist, 
00371                     framelist);
00372             if (bpm_im) cpl_image_delete(bpm_im);
00373             cpl_imagelist_delete(twflat);
00374            if (!cpl_error_get_code()) did_reduce = CPL_TRUE;
00375         }
00376         cpl_msg_indent_less();
00377         cpl_frameset_delete(flat_one);
00378         if (dark_one) cpl_frameset_delete(dark_one);
00379     }
00380     
00381     /* Free and return */
00382     cpl_frameset_delete(flatframes);
00383     if (darkframes) cpl_frameset_delete(darkframes);
00384     cpl_free(labels); 
00385 
00386     cpl_ensure_code(did_reduce, CPL_ERROR_ILLEGAL_INPUT);
00387 
00388     return cpl_error_set_where(cpl_func); /* Propagate error, if any */
00389 }
00390 
00391 /*----------------------------------------------------------------------------*/
00402 /*----------------------------------------------------------------------------*/
00403 static cpl_imagelist * isaac_img_twflat_reduce(
00404         cpl_frameset    *   flatframes,
00405         cpl_frameset    *   darkframes)
00406 {
00407     cpl_frame           *   frame;
00408     cpl_propertylist    *   plist;
00409     const char          *   sval;
00410     cpl_image           *   flat_image;
00411     cpl_imagelist       *   in,
00412                         *   dark,
00413                         *   results;
00414     cpl_stats           *   stats_ima;
00415     cpl_vector          *   medians;
00416     double                  gradient;
00417     double                  norm;
00418     int                     nima;
00419     cpl_vector          *   reve_ll;
00420     cpl_vector          *   reve_lr;
00421     cpl_vector          *   reve_ul;
00422     cpl_vector          *   reve_ur;
00423     double                  ll, lr, ul, ur;
00424     int                     i;
00425 
00426     /* Test entries */
00427     if (flatframes == NULL) return NULL;
00428 
00429     /* Initialise */
00430     nima = cpl_frameset_get_size(flatframes);
00431     
00432     /* Check the error status */
00433     if (cpl_error_get_code()) return NULL;
00434 
00435     /* Print out the filter and the read-out mode */
00436     frame = cpl_frameset_get_frame(flatframes, 0);
00437     plist=cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
00438     sval = isaac_pfits_get_filter(plist);
00439     if (cpl_error_get_code()) {
00440         cpl_msg_error(cpl_func, "Cannot get the filter or ROM or TPL.ID");
00441         cpl_propertylist_delete(plist);
00442         return NULL;
00443     }
00444     cpl_msg_info(cpl_func, "Filter:        [%s]", sval);
00445     cpl_propertylist_delete(plist);
00446 
00447     /* Load input image set */
00448     cpl_msg_info(cpl_func, "---> Loading input set");
00449     if ((in = cpl_imagelist_load_frameset(flatframes, CPL_TYPE_FLOAT, 1,
00450                     0)) == NULL) {
00451         cpl_msg_error(cpl_func, "Cannot load the image set");
00452         return NULL;
00453     }
00454     
00455     /* Compute some stats on input images */
00456     cpl_msg_info(cpl_func, "---> Computing stats");
00457     cpl_msg_info(cpl_func, "image      min        max        med     rms");
00458     cpl_msg_info(cpl_func, "---------------------------------------------");
00459     medians = cpl_vector_new(nima);
00460     for (i=0; i<nima; i++) {
00461         stats_ima = cpl_stats_new_from_image(cpl_imagelist_get(in, i), 
00462                 CPL_STATS_ALL);
00463         if (stats_ima == NULL) {
00464             cpl_msg_error(cpl_func, "Cannot compute stats on image %d", i+1);
00465             cpl_imagelist_delete(in);
00466             cpl_vector_delete(medians);
00467             return NULL;
00468         }
00469         cpl_vector_set(medians, i, cpl_stats_get_median(stats_ima));
00470         cpl_msg_info(cpl_func, "%02d   %10.2f %10.2f %10.2f %10.2f",
00471                 i+1,
00472                 cpl_stats_get_min(stats_ima),
00473                 cpl_stats_get_max(stats_ima),
00474                 cpl_stats_get_median(stats_ima),
00475                 cpl_stats_get_stdev(stats_ima));
00476         if (cpl_stats_get_median(stats_ima) < 1e-6) {
00477             cpl_msg_error(cpl_func, "image %d has negative flux: aborting", 
00478                     i+1);
00479             cpl_imagelist_delete(in);
00480             cpl_vector_delete(medians);
00481             cpl_stats_delete(stats_ima);
00482             return NULL;
00483         }
00484         cpl_stats_delete(stats_ima);
00485     }
00486     cpl_msg_info(cpl_func, "---------------------------------------------");
00487 
00488     /* Compute min max stdev and mean of the medians */
00489     isaac_img_twflat_config.med_min   = cpl_vector_get_min(medians);
00490     isaac_img_twflat_config.med_max   = cpl_vector_get_max(medians);
00491     isaac_img_twflat_config.med_avg   = cpl_vector_get_mean(medians);
00492     isaac_img_twflat_config.med_stdev = cpl_vector_get_stdev(medians);
00493     cpl_vector_delete(medians);
00494  
00495     /* Apply dark correction to all planes if requested */
00496     if (darkframes) {
00497         cpl_msg_info(cpl_func, "---> Subtracting dark");
00498         /* Load dark cube */
00499         if ((dark = cpl_imagelist_load_frameset(darkframes, CPL_TYPE_FLOAT, 1,
00500                         0)) == NULL) {
00501             cpl_msg_error(cpl_func, "Cannot load the darks set");
00502             cpl_imagelist_delete(in);
00503             return NULL;
00504         }
00505         /* Dark correction */
00506         if (cpl_imagelist_get_size(dark) == 1) {
00507             cpl_imagelist_subtract_image(in, cpl_imagelist_get(dark, 0));
00508         } else {
00509             cpl_imagelist_subtract(in, dark);
00510         }
00511         cpl_imagelist_delete(dark);
00512 
00513         /* With the darks provided, the proportional method is used */
00514         cpl_msg_info(cpl_func, "switching to proportional fit");
00515         isaac_img_twflat_config.prop_flag = 1;
00516     }
00517     
00518     /* See if flux gradient is large enough for a correct fit */
00519     if (!isaac_img_twflat_config.prop_flag) {
00520         gradient=fabs(isaac_img_twflat_config.med_max/
00521                 isaac_img_twflat_config.med_min);
00522         if (gradient < 4.0) {
00523             cpl_msg_warning(cpl_func, "low flux gradient: %g", gradient);
00524             cpl_msg_warning(cpl_func,"a proportional fit may give better ");
00525             cpl_msg_warning(cpl_func,"results (requires a master dark frame)");
00526         }
00527     }
00528 
00529     /* Monitor the odd/even column effect */
00530     cpl_msg_info(cpl_func, "---> Monitor odd/even column effect");
00531     reve_ll = cpl_vector_new(nima);
00532     reve_lr = cpl_vector_new(nima);
00533     reve_ul = cpl_vector_new(nima);
00534     reve_ur = cpl_vector_new(nima);
00535     for (i=0; i<nima; i++) {
00536         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 1, &ll);
00537         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 2, &lr);
00538         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 3, &ul);
00539         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 4, &ur);
00540         cpl_vector_set(reve_ll, i, ll);
00541         cpl_vector_set(reve_lr, i, lr);
00542         cpl_vector_set(reve_ul, i, ul);
00543         cpl_vector_set(reve_ur, i, ur);
00544     }
00545     isaac_img_twflat_config.oddeven_ll_max = cpl_vector_get_max(reve_ll);
00546     isaac_img_twflat_config.oddeven_lr_max = cpl_vector_get_max(reve_lr);
00547     isaac_img_twflat_config.oddeven_ul_max = cpl_vector_get_max(reve_ul);
00548     isaac_img_twflat_config.oddeven_ur_max = cpl_vector_get_max(reve_ur);
00549     isaac_img_twflat_config.oddeven_ll_stdev = cpl_vector_get_stdev(reve_ll);
00550     isaac_img_twflat_config.oddeven_lr_stdev = cpl_vector_get_stdev(reve_lr);
00551     isaac_img_twflat_config.oddeven_ul_stdev = cpl_vector_get_stdev(reve_ul);
00552     isaac_img_twflat_config.oddeven_ur_stdev = cpl_vector_get_stdev(reve_ur);
00553     isaac_img_twflat_config.oddeven_ll_mean = cpl_vector_get_mean(reve_ll);
00554     isaac_img_twflat_config.oddeven_lr_mean = cpl_vector_get_mean(reve_lr);
00555     isaac_img_twflat_config.oddeven_ul_mean = cpl_vector_get_mean(reve_ul);
00556     isaac_img_twflat_config.oddeven_ur_mean = cpl_vector_get_mean(reve_ur);
00557     cpl_vector_delete(reve_ll);
00558     cpl_vector_delete(reve_lr);
00559     cpl_vector_delete(reve_ul);
00560     cpl_vector_delete(reve_ur);
00561 
00562     /* Fit slopes, get results */
00563     cpl_msg_info(cpl_func, "---> Fitting slopes");
00564     if (isaac_img_twflat_config.prop_flag) {
00565         results = irplib_flat_fit_set(in, 0);
00566         if (results == NULL) {
00567             cpl_msg_error(cpl_func, "creating twilight flat-field: aborting");
00568             cpl_imagelist_delete(in);
00569             return NULL;
00570         }
00571     } else {
00572         results = irplib_flat_fit_set(in, 1);
00573         if (results == NULL) {
00574             cpl_msg_error(cpl_func, "creating twilight flat-field: aborting");
00575             cpl_imagelist_delete(in);
00576             return NULL;
00577         }
00578     }
00579     cpl_imagelist_delete(in);
00580 
00581     /* Normalize gain */
00582     flat_image = cpl_imagelist_get(results, 0);
00583     norm = cpl_image_get_mean(flat_image);
00584     cpl_image_divide_scalar(flat_image, norm);
00585     if (cpl_error_get_code()) {
00586         cpl_imagelist_delete(results);
00587         return NULL;
00588     }
00589 
00590     return results;
00591 }
00592 
00593 /*----------------------------------------------------------------------------*/
00603 /*----------------------------------------------------------------------------*/
00604 static int isaac_img_twflat_save(
00605         cpl_imagelist       *   flat,
00606         cpl_image           *   bpm,
00607         int                     set_nb,
00608         cpl_frameset        *   set,
00609         cpl_parameterlist   *   parlist,
00610         cpl_frameset        *   set_tot)
00611 {
00612     cpl_propertylist    *   plist;
00613     cpl_propertylist    *   paflist;
00614     cpl_propertylist    *   qclist;
00615     const cpl_frame           *   ref_frame;
00616     char                *   filename;
00617     const char          *   sval;
00618     int                     nima, file_id;
00619     int                     nraw;
00620 
00621     /* Initialise */
00622     nima = cpl_imagelist_get_size(flat);
00623     if (nima != 2 && nima != 3) return -1;
00624 
00625     /* Get the QC params in qclist */
00626     qclist = cpl_propertylist_new();
00627    
00628     /* Get the reference frame */
00629     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
00630     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00631                     0)) == NULL) {
00632         cpl_msg_error(cpl_func, "getting header from reference frame");
00633         cpl_propertylist_delete(qclist);
00634         return -1;
00635     }
00636     /* Test the status */
00637     if (cpl_error_get_code()) {
00638         cpl_propertylist_delete(qclist);
00639         cpl_propertylist_delete(plist);
00640         return -1;
00641     }
00642     /* Add FILTER */
00643     sval = isaac_pfits_get_filter(plist);
00644     if (cpl_error_get_code()) cpl_error_reset();
00645     else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
00646     /* Add OBJECTIVE */
00647     sval = isaac_pfits_get_objective(plist);
00648     if (cpl_error_get_code()) cpl_error_reset();
00649     else cpl_propertylist_append_string(qclist, "ESO QC OBJECTIVE", sval);
00650     cpl_propertylist_delete(plist);
00651     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDMIN",
00652             isaac_img_twflat_config.med_min);
00653     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDMAX",
00654             isaac_img_twflat_config.med_max);
00655     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDAVG",
00656             isaac_img_twflat_config.med_avg);
00657     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDSTDEV",
00658             isaac_img_twflat_config.med_stdev);
00659     if (isaac_img_twflat_config.nb_badpix >=0 ) {
00660         cpl_propertylist_append_int(qclist, "ESO QC TWFLAT NBADPIX",
00661                 isaac_img_twflat_config.nb_badpix);
00662     }
00663     cpl_propertylist_append_int(qclist, "ESO QC TWFLAT NFRAMES",
00664             cpl_frameset_get_size(set));
00665     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLMAX",
00666             isaac_img_twflat_config.oddeven_ll_max);
00667     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLMEAN",
00668             isaac_img_twflat_config.oddeven_ll_mean);
00669     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLSTDEV",
00670             isaac_img_twflat_config.oddeven_ll_stdev);
00671     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRMAX",
00672             isaac_img_twflat_config.oddeven_lr_max);
00673     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRMEAN",
00674             isaac_img_twflat_config.oddeven_lr_mean);
00675     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRSTDEV",
00676             isaac_img_twflat_config.oddeven_lr_stdev);
00677     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULMAX",
00678             isaac_img_twflat_config.oddeven_ul_max);
00679     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULMEAN",
00680             isaac_img_twflat_config.oddeven_ul_mean);
00681     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULSTDEV",
00682             isaac_img_twflat_config.oddeven_ul_stdev);
00683     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URMAX",
00684             isaac_img_twflat_config.oddeven_ur_max);
00685     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URMEAN",
00686             isaac_img_twflat_config.oddeven_ur_mean);
00687     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URSTDEV",
00688             isaac_img_twflat_config.oddeven_ur_stdev);
00689 
00690     /* Write the flat image */
00691     filename = cpl_sprintf("isaac_img_twflat_set%02d.fits", set_nb);
00692     irplib_dfs_save_image(set_tot,
00693             parlist,
00694             set,
00695             cpl_imagelist_get(flat, 0),
00696             CPL_BPP_IEEE_FLOAT,
00697             "isaac_img_twflat",
00698             ISAAC_IMG_TWFLAT_RES,
00699             qclist,
00700             NULL,
00701             PACKAGE "/" PACKAGE_VERSION,
00702             filename);
00703     cpl_free(filename);
00704 
00705     /* Write the bpm image */
00706     if (bpm) {
00707         filename = cpl_sprintf("isaac_img_twflat_set%02d_bpm.fits", set_nb);
00708         irplib_dfs_save_image(set_tot,
00709                 parlist,
00710                 set,
00711                 bpm,
00712                 CPL_BPP_IEEE_FLOAT,
00713                 "isaac_img_twflat",
00714                 ISAAC_IMG_TWFLAT_BPM,
00715                 qclist,
00716                 NULL,
00717                 PACKAGE "/" PACKAGE_VERSION,
00718                 filename);
00719         cpl_free(filename);
00720     }
00721     
00722     /* Write the intercept image */
00723     if (isaac_img_twflat_config.intercept_flag && nima == 3) {
00724         filename = cpl_sprintf("isaac_img_twflat_set%02d_inter.fits",set_nb);
00725         irplib_dfs_save_image(set_tot,
00726                 parlist,
00727                 set,
00728                 cpl_imagelist_get(flat, 1),
00729                 CPL_BPP_IEEE_FLOAT,
00730                 "isaac_img_twflat",
00731                 ISAAC_IMG_TWFLAT_INTERC,
00732                 qclist,
00733                 NULL,
00734                 PACKAGE "/" PACKAGE_VERSION,
00735                 filename);
00736         cpl_free(filename);
00737     }
00738     
00739     /* Write the error map image */
00740     if (isaac_img_twflat_config.errmap_flag) {
00741         if (nima == 2)  file_id = 1;
00742         else            file_id = 2;
00743         filename=cpl_sprintf("isaac_img_twflat_set%02d_errmap.fits", set_nb);
00744         irplib_dfs_save_image(set_tot,
00745                 parlist,
00746                 set,
00747                 cpl_imagelist_get(flat, file_id),
00748                 CPL_BPP_IEEE_FLOAT,
00749                 "isaac_img_twflat",
00750                 ISAAC_IMG_TWFLAT_ERR,
00751                 qclist,
00752                 NULL,
00753                 PACKAGE "/" PACKAGE_VERSION,
00754                 filename);
00755         cpl_free(filename);
00756     }
00757 
00758     /* Get FITS header from reference file */
00759     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00760                     0)) == NULL) {
00761         cpl_msg_error(cpl_func, "getting header from reference frame");
00762         cpl_propertylist_delete(qclist);
00763         return -1;
00764     }
00765 
00766     /* Get the keywords for the paf file */
00767     paflist = cpl_propertylist_new();
00768     cpl_propertylist_copy_property_regexp(paflist, plist,
00769         "^(DATE-OBS|ESO DET CHIP NAME|ARCFILE|ESO TPL ID|ESO DET MODE NAME|"
00770         "ESO DET NCORRS NAME|ESO DET RSPEED|ESO DET DIT)$", 0);
00771     cpl_propertylist_delete(plist);
00772 
00773 
00774     /* Add PRO.DATANCOM keyword (DFS05421) */
00775     nraw = 0;
00776     while (ref_frame != NULL) {
00777         if (cpl_frame_get_group(ref_frame) == CPL_FRAME_GROUP_RAW) ++nraw;
00778         ref_frame = cpl_frameset_get_next(set);
00779     }
00780 
00781     if (!cpl_propertylist_has(paflist, PRO_DATANCOM)) {
00782         cpl_propertylist_update_int(paflist, PRO_DATANCOM, nraw);
00783         cpl_propertylist_set_comment(paflist, PRO_DATANCOM,
00784                                      "Number of combined frames");
00785     }
00786     /* The above piece of code regarding DFS05421 has been partially copied
00787        from cpl_dfs.c as a workaround for that request until PAF files are
00788        completely deprecated (which is planned for the long term). */
00789 
00790     /* Copy the QC in paflist */
00791     cpl_propertylist_copy_property_regexp(paflist, qclist, "", 0);
00792     cpl_propertylist_delete(qclist);
00793 
00794     /* PRO.CATG */
00795     cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
00796                                    ISAAC_IMG_TWFLAT_RES);
00797 
00798     /* Save the PAF file */
00799     filename = cpl_sprintf("isaac_img_twflat_set%02d.paf", set_nb);
00800     cpl_dfs_save_paf("ISAAC",
00801             "isaac_img_twflat",
00802             paflist,
00803             filename);
00804     cpl_free(filename);
00805     cpl_propertylist_delete(paflist);
00806     return  0;
00807 }
00808 
00809 /*----------------------------------------------------------------------------*/
00816 /*----------------------------------------------------------------------------*/
00817 static int isaac_img_twflat_compare(
00818         const cpl_frame *   frame1, 
00819         const cpl_frame *   frame2) 
00820 {
00821     int                     comparison;
00822     cpl_propertylist    *   plist1;
00823     cpl_propertylist    *   plist2;
00824     const char          *   sval1,
00825                         *   sval2;
00826 
00827     /* Test entries */
00828     if (frame1==NULL || frame2==NULL) return -1;
00829 
00830     /* Get property lists */
00831     if ((plist1=cpl_propertylist_load(cpl_frame_get_filename(frame1),
00832                     0)) == NULL) {
00833         cpl_msg_error(cpl_func, "getting header from reference frame");
00834         return -1;
00835     }
00836     if ((plist2=cpl_propertylist_load(cpl_frame_get_filename(frame2),
00837                     0)) == NULL) {
00838         cpl_msg_error(cpl_func, "getting header from reference frame");
00839         cpl_propertylist_delete(plist1);
00840         return -1;
00841     }
00842 
00843     /* Test status */
00844     if (cpl_error_get_code()) {
00845         cpl_propertylist_delete(plist1);
00846         cpl_propertylist_delete(plist2);
00847         return -1;
00848     }
00849 
00850     comparison = 1;
00851 
00852     /* Compare filters */
00853     sval1 = isaac_pfits_get_filter(plist1);
00854     sval2 = isaac_pfits_get_filter(plist2);
00855     if (cpl_error_get_code()) {
00856         cpl_msg_error(cpl_func, "cannot get the filter");
00857         cpl_propertylist_delete(plist1);
00858         cpl_propertylist_delete(plist2);
00859         return -1;
00860     }
00861     if (strcmp(sval1, sval2)) comparison = 0;
00862 
00863     cpl_propertylist_delete(plist1);
00864     cpl_propertylist_delete(plist2);
00865     return comparison;
00866 }
00867 
00868 

Generated on Mon Feb 6 14:44:02 2012 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8