isaac_img_twflat.c

00001 /* $Id: isaac_img_twflat.c,v 1.43 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.43 $
00025  * $Name: HEAD $
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     int             *   labels;
00261     int                 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, nlabels);
00342         cpl_msg_indent_more();
00343         flat_one = cpl_frameset_extract(flatframes, labels, i);
00344         dark_one = NULL;
00345         if (darkframes) dark_one = cpl_frameset_extract(darkframes, labels, i);
00346         twflat = isaac_img_twflat_reduce(flat_one, dark_one);
00347         cpl_msg_indent_less();
00348 
00349         /* Save the products */
00350         cpl_msg_info(cpl_func, "Save the products");
00351         cpl_msg_indent_more();
00352         if (twflat == NULL) {
00353             cpl_msg_warning(cpl_func, "Cannot reduce set nb %d", i+1);
00354         } else {
00355             /* Create the Bad pixels map */
00356             if (isaac_img_twflat_config.bpm_flag) {
00357                 if ((bpm = cpl_mask_threshold_image_create(
00358                                 cpl_imagelist_get(twflat, 0),
00359                                 isaac_img_twflat_config.low_thresh,
00360                                 isaac_img_twflat_config.high_thresh)) == NULL) {
00361                     cpl_msg_warning(cpl_func, "Cannot create bad pixels map");
00362                 } else {
00363                     cpl_mask_not(bpm);
00364                     isaac_img_twflat_config.nb_badpix = cpl_mask_count(bpm);
00365                     bpm_im = cpl_image_new_from_mask(bpm);
00366                     cpl_mask_delete(bpm);
00367                 }
00368             }
00369             isaac_img_twflat_save(twflat, bpm_im, i+1, flat_one, parlist, 
00370                     framelist);
00371             if (bpm_im) cpl_image_delete(bpm_im);
00372             cpl_imagelist_delete(twflat);
00373            if (!cpl_error_get_code()) did_reduce = CPL_TRUE;
00374         }
00375         cpl_msg_indent_less();
00376         cpl_frameset_delete(flat_one);
00377         if (dark_one) cpl_frameset_delete(dark_one);
00378     }
00379     
00380     /* Free and return */
00381     cpl_frameset_delete(flatframes);
00382     if (darkframes) cpl_frameset_delete(darkframes);
00383     cpl_free(labels); 
00384 
00385     cpl_ensure_code(did_reduce, CPL_ERROR_ILLEGAL_INPUT);
00386 
00387     return cpl_error_set_where(cpl_func); /* Propagate error, if any */
00388 }
00389 
00390 /*----------------------------------------------------------------------------*/
00401 /*----------------------------------------------------------------------------*/
00402 static cpl_imagelist * isaac_img_twflat_reduce(
00403         cpl_frameset    *   flatframes,
00404         cpl_frameset    *   darkframes)
00405 {
00406     cpl_frame           *   frame;
00407     cpl_propertylist    *   plist;
00408     const char          *   sval;
00409     cpl_image           *   flat_image;
00410     cpl_imagelist       *   in,
00411                         *   dark,
00412                         *   results;
00413     cpl_stats           *   stats_ima;
00414     cpl_vector          *   medians;
00415     double                  gradient;
00416     double                  norm;
00417     int                     nima;
00418     cpl_vector          *   reve_ll;
00419     cpl_vector          *   reve_lr;
00420     cpl_vector          *   reve_ul;
00421     cpl_vector          *   reve_ur;
00422     double                  ll, lr, ul, ur;
00423     int                     i;
00424 
00425     /* Test entries */
00426     if (flatframes == NULL) return NULL;
00427 
00428     /* Initialise */
00429     nima = cpl_frameset_get_size(flatframes);
00430     
00431     /* Check the error status */
00432     if (cpl_error_get_code()) return NULL;
00433 
00434     /* Print out the filter and the read-out mode */
00435     frame = cpl_frameset_get_frame(flatframes, 0);
00436     plist=cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
00437     sval = isaac_pfits_get_filter(plist);
00438     if (cpl_error_get_code()) {
00439         cpl_msg_error(cpl_func, "Cannot get the filter or ROM or TPL.ID");
00440         cpl_propertylist_delete(plist);
00441         return NULL;
00442     }
00443     cpl_msg_info(cpl_func, "Filter:        [%s]", sval);
00444     cpl_propertylist_delete(plist);
00445 
00446     /* Load input image set */
00447     cpl_msg_info(cpl_func, "---> Loading input set");
00448     if ((in = cpl_imagelist_load_frameset(flatframes, CPL_TYPE_FLOAT, 1,
00449                     0)) == NULL) {
00450         cpl_msg_error(cpl_func, "Cannot load the image set");
00451         return NULL;
00452     }
00453     
00454     /* Compute some stats on input images */
00455     cpl_msg_info(cpl_func, "---> Computing stats");
00456     cpl_msg_info(cpl_func, "image      min        max        med     rms");
00457     cpl_msg_info(cpl_func, "---------------------------------------------");
00458     medians = cpl_vector_new(nima);
00459     for (i=0; i<nima; i++) {
00460         stats_ima = cpl_stats_new_from_image(cpl_imagelist_get(in, i), 
00461                 CPL_STATS_ALL);
00462         if (stats_ima == NULL) {
00463             cpl_msg_error(cpl_func, "Cannot compute stats on image %d", i+1);
00464             cpl_imagelist_delete(in);
00465             cpl_vector_delete(medians);
00466             return NULL;
00467         }
00468         cpl_vector_set(medians, i, cpl_stats_get_median(stats_ima));
00469         cpl_msg_info(cpl_func, "%02d   %10.2f %10.2f %10.2f %10.2f",
00470                 i+1,
00471                 cpl_stats_get_min(stats_ima),
00472                 cpl_stats_get_max(stats_ima),
00473                 cpl_stats_get_median(stats_ima),
00474                 cpl_stats_get_stdev(stats_ima));
00475         if (cpl_stats_get_median(stats_ima) < 1e-6) {
00476             cpl_msg_error(cpl_func, "image %d has negative flux: aborting", 
00477                     i+1);
00478             cpl_imagelist_delete(in);
00479             cpl_vector_delete(medians);
00480             cpl_stats_delete(stats_ima);
00481             return NULL;
00482         }
00483         cpl_stats_delete(stats_ima);
00484     }
00485     cpl_msg_info(cpl_func, "---------------------------------------------");
00486 
00487     /* Compute min max stdev and mean of the medians */
00488     isaac_img_twflat_config.med_min   = cpl_vector_get_min(medians);
00489     isaac_img_twflat_config.med_max   = cpl_vector_get_max(medians);
00490     isaac_img_twflat_config.med_avg   = cpl_vector_get_mean(medians);
00491     isaac_img_twflat_config.med_stdev = cpl_vector_get_stdev(medians);
00492     cpl_vector_delete(medians);
00493  
00494     /* Apply dark correction to all planes if requested */
00495     if (darkframes) {
00496         cpl_msg_info(cpl_func, "---> Subtracting dark");
00497         /* Load dark cube */
00498         if ((dark = cpl_imagelist_load_frameset(darkframes, CPL_TYPE_FLOAT, 1,
00499                         0)) == NULL) {
00500             cpl_msg_error(cpl_func, "Cannot load the darks set");
00501             cpl_imagelist_delete(in);
00502             return NULL;
00503         }
00504         /* Dark correction */
00505         if (cpl_imagelist_get_size(dark) == 1) {
00506             cpl_imagelist_subtract_image(in, cpl_imagelist_get(dark, 0));
00507         } else {
00508             cpl_imagelist_subtract(in, dark);
00509         }
00510         cpl_imagelist_delete(dark);
00511 
00512         /* With the darks provided, the proportional method is used */
00513         cpl_msg_info(cpl_func, "switching to proportional fit");
00514         isaac_img_twflat_config.prop_flag = 1;
00515     }
00516     
00517     /* See if flux gradient is large enough for a correct fit */
00518     if (!isaac_img_twflat_config.prop_flag) {
00519         gradient=fabs(isaac_img_twflat_config.med_max/
00520                 isaac_img_twflat_config.med_min);
00521         if (gradient < 4.0) {
00522             cpl_msg_warning(cpl_func, "low flux gradient: %g", gradient);
00523             cpl_msg_warning(cpl_func,"a proportional fit may give better ");
00524             cpl_msg_warning(cpl_func,"results (requires a master dark frame)");
00525         }
00526     }
00527 
00528     /* Monitor the odd/even column effect */
00529     cpl_msg_info(cpl_func, "---> Monitor odd/even column effect");
00530     reve_ll = cpl_vector_new(nima);
00531     reve_lr = cpl_vector_new(nima);
00532     reve_ul = cpl_vector_new(nima);
00533     reve_ur = cpl_vector_new(nima);
00534     for (i=0; i<nima; i++) {
00535         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 1, &ll);
00536         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 2, &lr);
00537         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 3, &ul);
00538         irplib_oddeven_monitor(cpl_imagelist_get(in, i), 4, &ur);
00539         cpl_vector_set(reve_ll, i, ll);
00540         cpl_vector_set(reve_lr, i, lr);
00541         cpl_vector_set(reve_ul, i, ul);
00542         cpl_vector_set(reve_ur, i, ur);
00543     }
00544     isaac_img_twflat_config.oddeven_ll_max = cpl_vector_get_max(reve_ll);
00545     isaac_img_twflat_config.oddeven_lr_max = cpl_vector_get_max(reve_lr);
00546     isaac_img_twflat_config.oddeven_ul_max = cpl_vector_get_max(reve_ul);
00547     isaac_img_twflat_config.oddeven_ur_max = cpl_vector_get_max(reve_ur);
00548     isaac_img_twflat_config.oddeven_ll_stdev = cpl_vector_get_stdev(reve_ll);
00549     isaac_img_twflat_config.oddeven_lr_stdev = cpl_vector_get_stdev(reve_lr);
00550     isaac_img_twflat_config.oddeven_ul_stdev = cpl_vector_get_stdev(reve_ul);
00551     isaac_img_twflat_config.oddeven_ur_stdev = cpl_vector_get_stdev(reve_ur);
00552     isaac_img_twflat_config.oddeven_ll_mean = cpl_vector_get_mean(reve_ll);
00553     isaac_img_twflat_config.oddeven_lr_mean = cpl_vector_get_mean(reve_lr);
00554     isaac_img_twflat_config.oddeven_ul_mean = cpl_vector_get_mean(reve_ul);
00555     isaac_img_twflat_config.oddeven_ur_mean = cpl_vector_get_mean(reve_ur);
00556     cpl_vector_delete(reve_ll);
00557     cpl_vector_delete(reve_lr);
00558     cpl_vector_delete(reve_ul);
00559     cpl_vector_delete(reve_ur);
00560 
00561     /* Fit slopes, get results */
00562     cpl_msg_info(cpl_func, "---> Fitting slopes");
00563     if (isaac_img_twflat_config.prop_flag) {
00564         results = irplib_flat_fit_set(in, 0);
00565         if (results == NULL) {
00566             cpl_msg_error(cpl_func, "creating twilight flat-field: aborting");
00567             cpl_imagelist_delete(in);
00568             return NULL;
00569         }
00570     } else {
00571         results = irplib_flat_fit_set(in, 1);
00572         if (results == NULL) {
00573             cpl_msg_error(cpl_func, "creating twilight flat-field: aborting");
00574             cpl_imagelist_delete(in);
00575             return NULL;
00576         }
00577     }
00578     cpl_imagelist_delete(in);
00579 
00580     /* Normalize gain */
00581     flat_image = cpl_imagelist_get(results, 0);
00582     norm = cpl_image_get_mean(flat_image);
00583     cpl_image_divide_scalar(flat_image, norm);
00584     if (cpl_error_get_code()) {
00585         cpl_imagelist_delete(results);
00586         return NULL;
00587     }
00588 
00589     return results;
00590 }
00591 
00592 /*----------------------------------------------------------------------------*/
00602 /*----------------------------------------------------------------------------*/
00603 static int isaac_img_twflat_save(
00604         cpl_imagelist       *   flat,
00605         cpl_image           *   bpm,
00606         int                     set_nb,
00607         cpl_frameset        *   set,
00608         cpl_parameterlist   *   parlist,
00609         cpl_frameset        *   set_tot)
00610 {
00611     cpl_propertylist    *   plist;
00612     cpl_propertylist    *   paflist;
00613     cpl_propertylist    *   qclist;
00614     const cpl_frame           *   ref_frame;
00615     char                *   filename;
00616     const char          *   sval;
00617     int                     nima, file_id;
00618     int                     nraw;
00619 
00620     /* Initialise */
00621     nima = cpl_imagelist_get_size(flat);
00622     if (nima != 2 && nima != 3) return -1;
00623 
00624     /* Get the QC params in qclist */
00625     qclist = cpl_propertylist_new();
00626    
00627     /* Get the reference frame */
00628     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
00629     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00630                     0)) == NULL) {
00631         cpl_msg_error(cpl_func, "getting header from reference frame");
00632         cpl_propertylist_delete(qclist);
00633         return -1;
00634     }
00635     /* Test the status */
00636     if (cpl_error_get_code()) {
00637         cpl_propertylist_delete(qclist);
00638         cpl_propertylist_delete(plist);
00639         return -1;
00640     }
00641     /* Add FILTER */
00642     sval = isaac_pfits_get_filter(plist);
00643     if (cpl_error_get_code()) cpl_error_reset();
00644     else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
00645     /* Add OBJECTIVE */
00646     sval = isaac_pfits_get_objective(plist);
00647     if (cpl_error_get_code()) cpl_error_reset();
00648     else cpl_propertylist_append_string(qclist, "ESO QC OBJECTIVE", sval);
00649     cpl_propertylist_delete(plist);
00650     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDMIN",
00651             isaac_img_twflat_config.med_min);
00652     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDMAX",
00653             isaac_img_twflat_config.med_max);
00654     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDAVG",
00655             isaac_img_twflat_config.med_avg);
00656     cpl_propertylist_append_double(qclist, "ESO QC TWFLAT MEDSTDEV",
00657             isaac_img_twflat_config.med_stdev);
00658     if (isaac_img_twflat_config.nb_badpix >=0 ) {
00659         cpl_propertylist_append_int(qclist, "ESO QC TWFLAT NBADPIX",
00660                 isaac_img_twflat_config.nb_badpix);
00661     }
00662     cpl_propertylist_append_int(qclist, "ESO QC TWFLAT NFRAMES",
00663             cpl_frameset_get_size(set));
00664     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLMAX",
00665             isaac_img_twflat_config.oddeven_ll_max);
00666     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLMEAN",
00667             isaac_img_twflat_config.oddeven_ll_mean);
00668     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LLSTDEV",
00669             isaac_img_twflat_config.oddeven_ll_stdev);
00670     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRMAX",
00671             isaac_img_twflat_config.oddeven_lr_max);
00672     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRMEAN",
00673             isaac_img_twflat_config.oddeven_lr_mean);
00674     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN LRSTDEV",
00675             isaac_img_twflat_config.oddeven_lr_stdev);
00676     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULMAX",
00677             isaac_img_twflat_config.oddeven_ul_max);
00678     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULMEAN",
00679             isaac_img_twflat_config.oddeven_ul_mean);
00680     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN ULSTDEV",
00681             isaac_img_twflat_config.oddeven_ul_stdev);
00682     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URMAX",
00683             isaac_img_twflat_config.oddeven_ur_max);
00684     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URMEAN",
00685             isaac_img_twflat_config.oddeven_ur_mean);
00686     cpl_propertylist_append_double(qclist, "ESO QC ODDEVEN URSTDEV",
00687             isaac_img_twflat_config.oddeven_ur_stdev);
00688 
00689     /* Write the flat image */
00690     filename = cpl_sprintf("isaac_img_twflat_set%02d.fits", set_nb);
00691     irplib_dfs_save_image(set_tot,
00692             parlist,
00693             set,
00694             cpl_imagelist_get(flat, 0),
00695             CPL_BPP_IEEE_FLOAT,
00696             "isaac_img_twflat",
00697             ISAAC_IMG_TWFLAT_RES,
00698             qclist,
00699             NULL,
00700             PACKAGE "/" PACKAGE_VERSION,
00701             filename);
00702     cpl_free(filename);
00703 
00704     /* Write the bpm image */
00705     if (bpm) {
00706         filename = cpl_sprintf("isaac_img_twflat_set%02d_bpm.fits", set_nb);
00707         irplib_dfs_save_image(set_tot,
00708                 parlist,
00709                 set,
00710                 bpm,
00711                 CPL_BPP_IEEE_FLOAT,
00712                 "isaac_img_twflat",
00713                 ISAAC_IMG_TWFLAT_BPM,
00714                 qclist,
00715                 NULL,
00716                 PACKAGE "/" PACKAGE_VERSION,
00717                 filename);
00718         cpl_free(filename);
00719     }
00720     
00721     /* Write the intercept image */
00722     if (isaac_img_twflat_config.intercept_flag && nima == 3) {
00723         filename = cpl_sprintf("isaac_img_twflat_set%02d_inter.fits",set_nb);
00724         irplib_dfs_save_image(set_tot,
00725                 parlist,
00726                 set,
00727                 cpl_imagelist_get(flat, 1),
00728                 CPL_BPP_IEEE_FLOAT,
00729                 "isaac_img_twflat",
00730                 ISAAC_IMG_TWFLAT_INTERC,
00731                 qclist,
00732                 NULL,
00733                 PACKAGE "/" PACKAGE_VERSION,
00734                 filename);
00735         cpl_free(filename);
00736     }
00737     
00738     /* Write the error map image */
00739     if (isaac_img_twflat_config.errmap_flag) {
00740         if (nima == 2)  file_id = 1;
00741         else            file_id = 2;
00742         filename=cpl_sprintf("isaac_img_twflat_set%02d_errmap.fits", set_nb);
00743         irplib_dfs_save_image(set_tot,
00744                 parlist,
00745                 set,
00746                 cpl_imagelist_get(flat, file_id),
00747                 CPL_BPP_IEEE_FLOAT,
00748                 "isaac_img_twflat",
00749                 ISAAC_IMG_TWFLAT_ERR,
00750                 qclist,
00751                 NULL,
00752                 PACKAGE "/" PACKAGE_VERSION,
00753                 filename);
00754         cpl_free(filename);
00755     }
00756 
00757     /* Get FITS header from reference file */
00758     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00759                     0)) == NULL) {
00760         cpl_msg_error(cpl_func, "getting header from reference frame");
00761         cpl_propertylist_delete(qclist);
00762         return -1;
00763     }
00764 
00765     /* Get the keywords for the paf file */
00766     paflist = cpl_propertylist_new();
00767     cpl_propertylist_copy_property_regexp(paflist, plist,
00768         "^(DATE-OBS|ESO DET CHIP NAME|ARCFILE|ESO TPL ID|ESO DET MODE NAME|"
00769         "ESO DET NCORRS NAME|ESO DET RSPEED|ESO DET DIT)$", 0);
00770     cpl_propertylist_delete(plist);
00771 
00772 
00773     /* Add PRO.DATANCOM keyword (DFS05421) */
00774     nraw = 0;
00775     while (ref_frame != NULL) {
00776         if (cpl_frame_get_group(ref_frame) == CPL_FRAME_GROUP_RAW) ++nraw;
00777         ref_frame = cpl_frameset_get_next(set);
00778     }
00779 
00780     if (!cpl_propertylist_has(paflist, PRO_DATANCOM)) {
00781         cpl_propertylist_update_int(paflist, PRO_DATANCOM, nraw);
00782         cpl_propertylist_set_comment(paflist, PRO_DATANCOM,
00783                                      "Number of combined frames");
00784     }
00785     /* The above piece of code regarding DFS05421 has been partially copied
00786        from cpl_dfs.c as a workaround for that request until PAF files are
00787        completely deprecated (which is planned for the long term). */
00788 
00789     /* Copy the QC in paflist */
00790     cpl_propertylist_copy_property_regexp(paflist, qclist, "", 0);
00791     cpl_propertylist_delete(qclist);
00792 
00793     /* PRO.CATG */
00794     cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
00795                                    ISAAC_IMG_TWFLAT_RES);
00796 
00797     /* Save the PAF file */
00798     filename = cpl_sprintf("isaac_img_twflat_set%02d.paf", set_nb);
00799     cpl_dfs_save_paf("ISAAC",
00800             "isaac_img_twflat",
00801             paflist,
00802             filename);
00803     cpl_free(filename);
00804     cpl_propertylist_delete(paflist);
00805     return  0;
00806 }
00807 
00808 /*----------------------------------------------------------------------------*/
00815 /*----------------------------------------------------------------------------*/
00816 static int isaac_img_twflat_compare(
00817         const cpl_frame *   frame1, 
00818         const cpl_frame *   frame2) 
00819 {
00820     int                     comparison;
00821     cpl_propertylist    *   plist1;
00822     cpl_propertylist    *   plist2;
00823     const char          *   sval1,
00824                         *   sval2;
00825 
00826     /* Test entries */
00827     if (frame1==NULL || frame2==NULL) return -1;
00828 
00829     /* Get property lists */
00830     if ((plist1=cpl_propertylist_load(cpl_frame_get_filename(frame1),
00831                     0)) == NULL) {
00832         cpl_msg_error(cpl_func, "getting header from reference frame");
00833         return -1;
00834     }
00835     if ((plist2=cpl_propertylist_load(cpl_frame_get_filename(frame2),
00836                     0)) == NULL) {
00837         cpl_msg_error(cpl_func, "getting header from reference frame");
00838         cpl_propertylist_delete(plist1);
00839         return -1;
00840     }
00841 
00842     /* Test status */
00843     if (cpl_error_get_code()) {
00844         cpl_propertylist_delete(plist1);
00845         cpl_propertylist_delete(plist2);
00846         return -1;
00847     }
00848 
00849     comparison = 1;
00850 
00851     /* Compare filters */
00852     sval1 = isaac_pfits_get_filter(plist1);
00853     sval2 = isaac_pfits_get_filter(plist2);
00854     if (cpl_error_get_code()) {
00855         cpl_msg_error(cpl_func, "cannot get the filter");
00856         cpl_propertylist_delete(plist1);
00857         cpl_propertylist_delete(plist2);
00858         return -1;
00859     }
00860     if (strcmp(sval1, sval2)) comparison = 0;
00861 
00862     cpl_propertylist_delete(plist1);
00863     cpl_propertylist_delete(plist2);
00864     return comparison;
00865 }
00866 
00867 

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