uves_mdark_impl.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA          *
00018  */
00019  
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2008/03/28 08:53:50 $
00023  * $Revision: 1.39 $
00024  * $Name: uves-3_9_0 $
00025  * $Log: uves_mdark_impl.c,v $
00026  * Revision 1.39  2008/03/28 08:53:50  amodigli
00027  * IRPLIB_CONCAT2X-->UVES_CONCAT2X
00028  *
00029  * Revision 1.38  2008/02/15 12:43:49  amodigli
00030  * allow lower/upper chip for parameter process_chip
00031  *
00032  * Revision 1.37  2007/10/05 16:01:45  amodigli
00033  * using proces_chip parameter to process or not a given RED chip
00034  *
00035  * Revision 1.36  2007/08/31 06:27:02  amodigli
00036  * include uves_globals.h
00037  *
00038  * Revision 1.35  2007/08/24 08:28:36  amodigli
00039  * clearer parm doc
00040  *
00041  * Revision 1.34  2007/08/24 06:55:36  amodigli
00042  * fixed parameter io for qc
00043  *
00044  * Revision 1.33  2007/08/21 13:08:26  jmlarsen
00045  * Removed irplib_access module, largely deprecated by CPL-4
00046  *
00047  * Revision 1.32  2007/08/17 10:07:02  amodigli
00048  * added QC params asked by DFS04195
00049  *
00050  * Revision 1.31  2007/06/11 13:28:26  jmlarsen
00051  * Changed recipe contact address to cpl at eso.org
00052  *
00053  * Revision 1.30  2007/06/08 13:06:16  jmlarsen
00054  * Send bug reports to Andrea
00055  *
00056  * Revision 1.29  2007/06/06 08:17:33  amodigli
00057  * replace tab with 4 spaces
00058  *
00059  * Revision 1.28  2007/05/16 09:50:40  jmlarsen
00060  * Do not threshold to zero after bias subtraction
00061  *
00062  * Revision 1.27  2007/04/24 12:50:29  jmlarsen
00063  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00064  *
00065  * Revision 1.26  2007/02/09 13:37:47  jmlarsen
00066  * Enable calling from uves_cal_mkmaster
00067  *
00068  * Revision 1.25  2007/02/09 08:57:44  jmlarsen
00069  * Include <float.h>
00070  *
00071  * Revision 1.24  2007/02/09 08:14:16  jmlarsen
00072  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00073  *
00074  * Revision 1.23  2006/12/07 08:23:23  jmlarsen
00075  * uves_load_raw_imagelist: support FLAMES
00076  *
00077  * Revision 1.22  2006/11/15 15:02:14  jmlarsen
00078  * Implemented const safe workarounds for CPL functions
00079  *
00080  * Revision 1.20  2006/11/15 14:04:08  jmlarsen
00081  * Removed non-const version of parameterlist_get_first/last/next which is
00082  * already in CPL, added const-safe wrapper, unwrapper and deallocator functions
00083  *
00084  * Revision 1.19  2006/11/06 15:19:41  jmlarsen
00085  * Removed unused include directives
00086  *
00087  * Revision 1.18  2006/10/17 12:33:02  jmlarsen
00088  * Added semicolon at UVES_RECIPE_DEFINE invocation
00089  *
00090  * Revision 1.17  2006/10/09 13:01:13  jmlarsen
00091  * Use macro to define recipe interface functions
00092  *
00093  * Revision 1.16  2006/09/19 14:31:17  jmlarsen
00094  * uves_insert_frame(): use bitmap to specify which image statistics keywords must be computed
00095  *
00096  * Revision 1.15  2006/09/19 06:55:55  jmlarsen
00097  * Changed interface of uves_frameset to optionally write image statistics kewwords
00098  *
00099  * Revision 1.14  2006/08/24 11:36:37  jmlarsen
00100  * Write recipe start/stop time to header
00101  *
00102  * Revision 1.13  2006/08/17 13:56:53  jmlarsen
00103  * Reduced max line length
00104  *
00105  * Revision 1.12  2006/08/11 14:56:05  amodigli
00106  * removed Doxygen warnings
00107  *
00108  * Revision 1.11  2006/07/14 12:19:28  jmlarsen
00109  * Support multiple QC tests per product
00110  *
00111  * Revision 1.10  2006/07/03 13:02:18  jmlarsen
00112  * Threshold to zero after bias subtraction
00113  *
00114  * Revision 1.9  2006/06/28 13:29:06  amodigli
00115  * removed TEST ID from QC log
00116  *
00117  * Revision 1.8  2006/06/16 08:25:45  jmlarsen
00118  * Manually propagate ESO.DET. keywords from 1st/2nd input header
00119  *
00120  * Revision 1.7  2006/06/13 11:57:02  jmlarsen
00121  * Check that calibration frames are from the same chip ID
00122  *
00123  * Revision 1.6  2006/06/01 14:43:17  jmlarsen
00124  * Added missing documentation
00125  *
00126  * Revision 1.5  2006/05/09 07:42:18  amodigli
00127  * added QC-LOG
00128  *
00129  * Revision 1.4  2006/04/06 12:57:22  jmlarsen
00130  * Added support for PDARK, MASTER_PDARK frames
00131  *
00132  * Revision 1.3  2006/04/06 09:48:15  amodigli
00133  * changed uves_frameset_insert interface to have QC log
00134  *
00135  * Revision 1.2  2006/04/06 08:37:33  jmlarsen
00136  * Removed memory leak
00137  *
00138  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00139  * Moved recipe implementations to ./uves directory
00140  *
00141  * Revision 1.27  2006/01/19 08:47:24  jmlarsen
00142  * Insertedv missing doxygen end tag
00143  *
00144  * Revision 1.26  2005/12/19 16:17:55  jmlarsen
00145  * Replaced bool -> int
00146  *
00147  */
00148 
00149 #ifdef HAVE_CONFIG_H
00150 #  include <config.h>
00151 #endif
00152 
00153 /*----------------------------------------------------------------------------*/
00160 /*----------------------------------------------------------------------------*/
00161 
00162 /*-----------------------------------------------------------------------------
00163                                 Includes
00164  -----------------------------------------------------------------------------*/
00165 #include <uves_mdark_impl.h>
00166 
00167 #include <uves_parameters.h>
00168 #include <uves_utils.h>
00169 #include <uves.h>
00170 #include <uves_dfs.h>
00171 #include <uves_pfits.h>
00172 #include <uves_qclog.h>
00173 #include <uves_recipe.h>
00174 #include <uves_utils_wrappers.h>
00175 #include <uves_error.h>
00176 #include <uves_globals.h>
00177 
00178 #include <cpl.h>
00179 #include <float.h>
00180 
00181 /*-----------------------------------------------------------------------------
00182                             Functions prototypes
00183  -----------------------------------------------------------------------------*/
00184 static int
00185 uves_qcdark_define_parameters_body(cpl_parameterlist *parameters, 
00186                    const char *recipe_id);
00187 static int
00188 uves_mdark_define_parameters(cpl_parameterlist *parameters);
00189 
00190 static void uves_mdark_region_qc(cpl_image* img, 
00191                                  const cpl_parameterlist* p,
00192                                  const cpl_imagelist* raw_images,
00193                                  cpl_table* qclog);
00194 static cpl_image *
00195 uves_mdark_process_chip(const cpl_imagelist *raw_images, 
00196             uves_propertylist **raw_headers,
00197             const cpl_image *master_bias,
00198             uves_propertylist *mdark_header, 
00199             const cpl_parameterlist *parameters,
00200                         cpl_table* qclog, const int do_qc);
00201 
00202 
00203 /*-----------------------------------------------------------------------------
00204                             Recipe standard code
00205  -----------------------------------------------------------------------------*/
00206 #define cpl_plugin_get_info uves_mdark_get_info
00207 UVES_RECIPE_DEFINE(
00208     UVES_MDARK_ID, UVES_MDARK_DOM, 
00209     /* Warning: if more parameters are added to this recipe, they 
00210        need to be propagated to uves_cal_mkmaster! */
00211     uves_mdark_define_parameters,
00212     "Jonas M. Larsen", "cpl@eso.org",
00213     "Creates the master dark frame",
00214     "This recipe creates a master dark frame by taking the median of all\n"
00215     "input frames which should have identical exposure times. Symbolically,\n"
00216     "   masterdark = median( dark_i ) - masterbias\n"
00217     "\n"
00218     "The input dark frames must have same tag and size and must be either\n"
00219     "(P)DARK_BLUE or (P)DARK_RED. Also, a master bias (MASTER_BIAS_xxxx) must\n"
00220     "be provided for each chip (xxxx = BLUE, REDL, REDU).\n"
00221     "\n"
00222     "On blue input the recipe computes one master dark frame; on red input the\n"
00223     "recipe produces a master dark frame for each chip (MASTER_(P)DARK_xxxx).\n");
00224 
00226 /*-----------------------------------------------------------------------------
00227                             Functions code
00228  -----------------------------------------------------------------------------*/
00229 /*----------------------------------------------------------------------------*/
00235 /*----------------------------------------------------------------------------*/
00236 static int
00237 uves_mdark_define_parameters(cpl_parameterlist *parameters)
00238 {
00239     return uves_qcdark_define_parameters_body(parameters, 
00240                          make_str(UVES_MDARK_ID));
00241 }
00242 
00243 /*----------------------------------------------------------------------------*/
00250 /*----------------------------------------------------------------------------*/
00251 static int
00252 uves_qcdark_define_parameters_body(cpl_parameterlist *parameters, 
00253                   const char *recipe_id)
00254 {
00255     /*****************
00256      *    General    *
00257      *****************/
00258     if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00259     {
00260         return -1;
00261     }
00262 
00263     
00264     /****************************
00265      *  Spline back.sub.        *
00266      ****************************/
00267     
00268     if (uves_propagate_parameters_step(UVES_QCDARK_ID, parameters, 
00269                        recipe_id, NULL) != 0)
00270     {
00271         return -1;
00272     }
00273 
00274     return (cpl_error_get_code() != CPL_ERROR_NONE);
00275 }
00276 
00277 
00278 /*----------------------------------------------------------------------------*/
00286 /*----------------------------------------------------------------------------*/
00287 cpl_parameterlist *
00288 uves_qcdark_define_parameters(void)
00289 {
00290   
00291     const char *name = "";
00292     char *full_name = NULL;
00293     cpl_parameter *p = NULL;
00294     cpl_parameterlist *parameters = NULL;
00295     
00296     parameters = cpl_parameterlist_new();
00297     
00298     {
00299    
00300       name = "reg.num_x";
00301       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00302        uves_msg("parameter=%s",full_name);
00303       uves_parameter_new_value(p, full_name,
00304                    CPL_TYPE_INT,
00305                    "Number of regions on X direction "
00306                                "(where mean/med/rms are computed). ",
00307                    UVES_QCDARK_ID,
00308                    4);
00309 
00310       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name );
00311       cpl_parameterlist_append(parameters, p);
00312       cpl_free(full_name);
00313 
00314 
00315 
00316       name = "reg.num_y";
00317       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00318       uves_parameter_new_value(p, full_name,
00319                    CPL_TYPE_INT,
00320                    "Number of regions on Y direction"
00321                                "(where mean/med/rms are computed). ",
00322                    UVES_QCDARK_ID,
00323                    4);
00324 
00325       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00326       cpl_parameterlist_append(parameters, p);
00327       cpl_free(full_name);
00328 
00329 
00330 
00331       name = "reg.box_sx";
00332       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00333       uves_parameter_new_value(p, full_name,
00334                    CPL_TYPE_INT,
00335                    "Region X size",
00336                    UVES_QCDARK_ID,
00337                    100);
00338       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00339       cpl_parameterlist_append(parameters, p);
00340       cpl_free(full_name);
00341 
00342 
00343       name = "reg.box_sy";
00344       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00345       uves_parameter_new_value(p, full_name,
00346                    CPL_TYPE_INT,
00347                    "Region Y size",
00348                    UVES_QCDARK_ID,
00349                    100);
00350       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00351       cpl_parameterlist_append(parameters, p);
00352       cpl_free(full_name);
00353 
00354       name = "reg.border_x";
00355       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00356       uves_parameter_new_value(p, full_name,
00357                    CPL_TYPE_INT,
00358                    "X distance from the left hand side "
00359                                "detector's border and the left hand side "
00360                                "regin's bottom corner",
00361                    UVES_QCDARK_ID,
00362                    100);
00363       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00364       cpl_parameterlist_append(parameters, p);
00365       cpl_free(full_name);
00366 
00367 
00368       name = "reg.border_y";
00369       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00370       uves_parameter_new_value(p, full_name,
00371                    CPL_TYPE_INT,
00372                    "Y distance from the left hand side "
00373                                "detector's border and the left hand side "
00374                                "regin's bottom corner",
00375                    UVES_QCDARK_ID,
00376                    100);
00377       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00378       cpl_parameterlist_append(parameters, p);
00379       cpl_free(full_name);
00380 
00381 
00382 
00383       name = "reg.when";
00384       full_name = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00385       uves_parameter_new_enum(p, full_name,
00386                    CPL_TYPE_INT,
00387                    "When QC analysis is performed. "
00388                                "0: on each raw frame or "
00389                                "1: on the master frame",
00390                    UVES_QCDARK_ID,
00391                   0,2,0,1);
00392       cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
00393       cpl_parameterlist_append(parameters, p);
00394       cpl_free(full_name);
00395 
00396 
00397     }
00398     
00399     if (cpl_error_get_code() != CPL_ERROR_NONE)
00400     {
00401         cpl_msg_error(__func__, "Creation of mdark parameters failed: '%s'", 
00402               cpl_error_get_where());
00403         cpl_parameterlist_delete(parameters);
00404         return NULL;
00405     }
00406     else
00407     {
00408         return parameters;
00409     }
00410 }
00411 
00412 /*----------------------------------------------------------------------------*/
00432 /*----------------------------------------------------------------------------*/
00433 static cpl_image *
00434 uves_mdark_process_chip(const cpl_imagelist *raw_images, 
00435                         uves_propertylist **raw_headers, 
00436                         const cpl_image *master_bias,
00437                 uves_propertylist *mdark_header, 
00438             const cpl_parameterlist *parameters,
00439             cpl_table* qclog,const int do_qc)
00440 {
00441     cpl_image *master_dark        = NULL; /* Result */
00442     cpl_image *current_dark       = NULL;
00443     cpl_imagelist *preproc_images = NULL;
00444     double min_exptime = 0;
00445     double max_exptime = 0;
00446     int i;
00447 
00448     /* First process each input image and store the results in a new image list */
00449 
00450     preproc_images = cpl_imagelist_new();
00451     for (i = 0; i < cpl_imagelist_get_size(raw_images); i++)
00452     {
00453         double exposure_time = 0.0;
00454         const uves_propertylist *current_header;
00455         
00456         current_dark = cpl_image_duplicate(cpl_imagelist_get_const(raw_images, i));
00457         current_header = raw_headers[i];
00458                 
00459         /* Subtract master bias */
00460         if (master_bias != NULL)
00461         {
00462             uves_msg("Subtracting master bias");
00463             check( uves_subtract_bias(current_dark, master_bias), 
00464                "Error subtracting master bias");
00465 
00466                     if (false) {
00467             uves_msg_debug("Thresholding to non-negative values");
00468             check( cpl_image_threshold(current_dark, 
00469                            0, DBL_MAX,     /* Interval */
00470                            0, DBL_MAX),    /* New values */
00471                "Error thresholding image");
00472                     }
00473         }
00474         else
00475         {
00476             uves_msg("Skipping bias subtraction");
00477         }
00478         
00479         check( exposure_time = uves_pfits_get_exptime(current_header), 
00480            "Error reading exposure time");
00481 
00482         if(do_qc == 0) {
00483       uves_msg("Calculating QC parameters on raw dark frame %d",i);
00484           uves_mdark_region_qc(current_dark,parameters,raw_images,qclog);
00485     }
00486 
00487         /* Initialize/update min/max exposure time*/
00488         if (i == 0 || exposure_time < min_exptime)
00489         {
00490             min_exptime = exposure_time;
00491         }
00492         if (i == 0 || exposure_time > max_exptime)
00493         {
00494             max_exptime = exposure_time;
00495         }
00496 
00497         /* Do not normalize to unit exposure time */
00498 /*        If this is uncommented, then remember to also calculate the
00499         correct master dark exposure time below.
00500             uves_msg("Normalizing from %f s to unit exposure time", exposure_time);
00501         check( cpl_image_divide_scalar(current_dark, exposure_time), 
00502         "Error normalizing dark frame");   */        
00503 
00504         /* Append to imagelist */
00505         check( cpl_imagelist_set(preproc_images,
00506                      current_dark,  
00507                      i),            /* Position (number_of_images=>append) */
00508            "Could not insert image into image list");
00509         
00510         /* Don't deallocate the image. It will be deallocated when
00511            the image list is deallocated */
00512         current_dark = NULL;
00513     }
00514 
00515     /* Check exposure times */
00516     uves_msg("Exposure times range from %e s to %e s (%e %% variation)", 
00517             min_exptime,
00518             max_exptime,
00519             100 * (max_exptime - min_exptime) / min_exptime);
00520 
00521     if ((max_exptime - min_exptime) / min_exptime > .001)
00522     {
00523         uves_msg_warning("Exposure times differ by %e %%", 
00524                  100 * (max_exptime - min_exptime) / min_exptime);
00525     }
00526     
00527     /* Get median stack of input darks */
00528     uves_msg("Calculating stack median");
00529     check( master_dark = cpl_imagelist_collapse_median_create(preproc_images), 
00530        "Error computing median");
00531 
00532     check( uves_pfits_set_exptime(mdark_header, (max_exptime + min_exptime)/2),
00533        "Error setting master dark exposure time");
00534     
00535   cleanup:
00536     uves_free_image(&current_dark);
00537     uves_free_imagelist(&preproc_images);
00538     if (cpl_error_get_code() != CPL_ERROR_NONE)
00539     {
00540         uves_free_image(&master_dark);
00541     }
00542     
00543     return master_dark;
00544 }
00545 
00546 
00547 /*----------------------------------------------------------------------------*/
00554 /*----------------------------------------------------------------------------*/
00555 static void
00556 UVES_CONCAT2X(UVES_MDARK_ID,exe)(cpl_frameset *frames, 
00557                    const cpl_parameterlist *parameters,
00558                    const char *starttime)
00559 {
00560     uves_mdark_exe_body(frames, parameters, starttime, make_str(UVES_MDARK_ID));
00561     return;
00562 }
00563 
00564 /*----------------------------------------------------------------------------*/
00576 /*----------------------------------------------------------------------------*/
00577 void
00578 uves_mdark_exe_body(cpl_frameset *frames, 
00579             const cpl_parameterlist *parameters,
00580             const char *starttime,
00581             const char *recipe_id)
00582 {
00583     /* Function id */
00584     /*
00585      * Variables that will contain the values of the recipe parameters 
00586      */
00587     /* None */
00588 
00589     /* CPL objects */
00590     /* Input */
00591     cpl_imagelist *raw_images[2]       = {NULL, NULL};
00592     uves_propertylist  **raw_headers[2] = {NULL, NULL};    /* Two arrays of pointers */
00593 
00594     /* Master bias */
00595     cpl_image *master_bias               = NULL;
00596     uves_propertylist *master_bias_header = NULL;
00597     
00598     /* Output */
00599     cpl_table* qclog[2] = {NULL, NULL};
00600     cpl_image *master_dark              = NULL;
00601     uves_propertylist *product_header[2] = {NULL, NULL};
00602     
00603     /* Local variables */
00604     char *product_filename = NULL;
00605     const char *product_tag[2] = {NULL, NULL};
00606     bool blue;
00607     enum uves_chip chip;
00608 
00609     const char* pname=NULL;
00610    
00611 
00612     int pr_when=0;
00613     const char* PROCESS_CHIP=NULL;
00614 
00615 
00616     /* Load and check raw dark images and headers, identify arm (blue/red) */
00617     /* On success, 'raw_headers' will be an array with the same size as 'raw_images' */
00618     if (cpl_frameset_find(frames, UVES_DARK(true )) != NULL ||
00619     cpl_frameset_find(frames, UVES_DARK(false)) != NULL)
00620     {
00621         check( uves_load_raw_imagelist(frames, 
00622                        false,    /* FLAMES format? */
00623                        UVES_DARK(true), UVES_DARK(false),
00624                        CPL_TYPE_DOUBLE,
00625                        raw_images, raw_headers, product_header, 
00626                        &blue), "Error loading raw dark frames");
00627         
00628         for (chip = uves_chip_get_first(blue); 
00629          chip != UVES_CHIP_INVALID; 
00630          chip = uves_chip_get_next(chip))
00631         {
00632             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_DARK(chip);
00633         }
00634     }
00635     else if (cpl_frameset_find(frames, UVES_PDARK(true )) != NULL ||
00636          cpl_frameset_find(frames, UVES_PDARK(false)) != NULL)
00637     {
00638         check( uves_load_raw_imagelist(frames, 
00639                        false,  /* FLAMES format? */
00640                        UVES_PDARK(true), UVES_PDARK(false),
00641                        CPL_TYPE_DOUBLE,
00642                        raw_images, raw_headers, product_header, 
00643                        &blue), "Error loading raw dark frames");
00644         
00645         for (chip = uves_chip_get_first(blue);
00646          chip != UVES_CHIP_INVALID;
00647          chip = uves_chip_get_next(chip))
00648         {
00649             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_PDARK(chip);
00650         }
00651     }
00652     else
00653     {
00654         assure(false, CPL_ERROR_DATA_NOT_FOUND,
00655            "Missing input dark frame: %s, %s, %s or %s expected",
00656            UVES_DARK(true) , UVES_DARK(false),
00657            UVES_PDARK(true), UVES_PDARK(false));
00658     }
00659 
00660 
00661   
00662     pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, "reg.when");
00663     check(uves_get_parameter(parameters,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_when), 
00664      "Could not read parameter");
00665     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
00666                "Could not read parameter");
00667     uves_string_toupper((char*)PROCESS_CHIP);
00668 
00669     /* Loop over one or two chips */
00670     for (chip = uves_chip_get_first(blue); 
00671          chip != UVES_CHIP_INVALID; 
00672          chip = uves_chip_get_next(chip))
00673         {
00674 
00675       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00676     chip = uves_chip_get_next(chip);
00677       }
00678 
00679         const char *master_bias_filename = "";
00680         const char *chip_name = "";
00681         
00682         int raw_index = uves_chip_get_index(chip);
00683         
00684         uves_msg("Processing %s chip",
00685              uves_chip_tostring_upper(chip));
00686 
00687         /* Get chip name of first input frame */
00688         check_nomsg( chip_name = uves_pfits_get_chipid(raw_headers[raw_index][0], chip));
00689 
00690         /* Load master bias, set pointer to NULL if not present */
00691         uves_free_image(&master_bias);
00692         uves_free_propertylist(&master_bias_header);
00693         if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00694         {
00695             check( uves_load_mbias(frames, chip_name,
00696                        &master_bias_filename, &master_bias, 
00697                        &master_bias_header, chip), 
00698                "Error loading master bias");
00699             
00700             uves_msg_low("Using master bias in '%s'", master_bias_filename);
00701         }
00702         else
00703         {
00704             uves_msg_low("No master bias in SOF. Bias subtraction not done");
00705         }
00706         
00707         /* Process chip */
00708 
00709 
00710 
00711         uves_free_image(&master_dark);
00712     uves_qclog_delete(&qclog[0]);
00713     qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00714         check( master_dark = uves_mdark_process_chip(raw_images[raw_index], 
00715                              raw_headers[raw_index], 
00716                              master_bias,
00717                              product_header[raw_index],
00718                              parameters,
00719                                                      qclog[0],pr_when),
00720            "Error processing chip");
00721 
00722 
00723         /* Finished. Save */
00724                 /* Finished. Calculate QC parameters and save */
00725         if(pr_when==1) {
00726       uves_msg("Calculating QC parameters on master dark frame");
00727           uves_mdark_region_qc(master_dark,parameters,
00728                    raw_images[raw_index],qclog[0]);
00729 
00730     }
00731 
00732         /* Insert into frame set */
00733         uves_msg("Saving product");
00734         
00735         cpl_free(product_filename);
00736         check( product_filename = uves_masterdark_filename(chip), "Error getting filename");
00737 
00738 
00739         check( uves_frameset_insert(frames,
00740                     master_dark,
00741                     CPL_FRAME_GROUP_PRODUCT,
00742                     CPL_FRAME_TYPE_IMAGE,
00743                     CPL_FRAME_LEVEL_INTERMEDIATE,
00744                     product_filename,
00745                     product_tag[raw_index],
00746                     raw_headers[raw_index][0],
00747                     product_header[raw_index],
00748                     NULL,
00749                     parameters,
00750                     recipe_id,
00751                     PACKAGE "/" PACKAGE_VERSION,qclog,
00752                     starttime, true, 
00753                     UVES_ALL_STATS), 
00754            "Could not add master dark %s to frameset", product_filename);
00755         uves_msg("Master dark %s added to frameset", product_filename);
00756         uves_qclog_delete(&qclog[0]);
00757 
00758       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
00759     chip = uves_chip_get_next(chip);
00760       }
00761         
00762 
00763     }/* For each chip */
00764 
00765   cleanup:
00766     /* Input */
00767     if (raw_images[0] != NULL)
00768     {
00769         int i;
00770         for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++) 
00771         {
00772             if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
00773             if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
00774         }
00775 
00776         cpl_free(raw_headers[0]); raw_headers[0] = NULL;
00777         cpl_free(raw_headers[1]); raw_headers[1] = NULL;
00778     }
00779 
00780     uves_free_imagelist(&raw_images[0]);
00781     uves_free_imagelist(&raw_images[1]);
00782 
00783     /* Master bias */
00784     uves_free_image(&master_bias);
00785     uves_free_propertylist(&master_bias_header);
00786 
00787     /* Output */
00788     uves_qclog_delete(&qclog[0]);
00789     uves_free_image(&master_dark);
00790     uves_free_propertylist(&product_header[0]);
00791     uves_free_propertylist(&product_header[1]);
00792     cpl_free(product_filename);
00793 
00794     return;
00795 }
00796 
00797 
00798 static void 
00799 uves_mdark_region_qc(cpl_image* img, 
00800                      const cpl_parameterlist* p,
00801                      const cpl_imagelist* raw_images,
00802                      cpl_table* qclog)
00803 {
00804 
00805   int pr_num_x=4;
00806   int pr_num_y=4;
00807   int pr_box_sx=100;
00808   int pr_box_sy=100;
00809   int pr_border_x=100;
00810   int pr_border_y=100;
00811 
00812 
00813   int i=0;
00814   int j=0;
00815   int llx=0;
00816   int lly=0;
00817   int urx=0;
00818   int ury=0;
00819   int space_x=0;
00820   int space_y=0;
00821   int sx=0;
00822   int sy=0;
00823   int nraw=0;
00824   int raw=0;
00825   char *name = "";
00826   char *pname = NULL;
00827   char qc_key[MAX_NAME_SIZE];
00828   char qc_com[MAX_NAME_SIZE];
00829   double qc_avg=0;
00830   double qc_med=0;
00831   double qc_rms=0;
00832   double qc_min=0;
00833   double qc_max=0;
00834   cpl_table* qc_sto=NULL;
00835   const char* recipe_id=make_str(UVES_MDARK_ID);
00836 
00837 
00838   name = "reg.num_x";
00839   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00840 
00841   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_num_x), 
00842     "Could not read parameter");
00843 
00844   name = "reg.num_y";
00845   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00846   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_num_y), 
00847      "Could not read parameter");
00848 
00849   name = "reg.box_sx";
00850   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00851   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_box_sx),
00852      "Could not read parameter");
00853 
00854   name = "reg.box_sy";
00855   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00856   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_box_sy),
00857      "Could not read parameter");
00858 
00859   name = "reg.border_x";
00860   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00861   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_border_x),
00862      "Could not read parameter");
00863 
00864   name = "reg.border_y";
00865   pname = uves_sprintf("%s.%s", UVES_QCDARK_ID, name);
00866   check(uves_get_parameter(p,NULL,recipe_id,pname,CPL_TYPE_INT,&pr_border_y),
00867      "Could not read parameter");
00868 
00869 
00870   check_nomsg(nraw=cpl_imagelist_get_size(raw_images));
00871   check_nomsg(uves_qclog_add_int(qclog,
00872                         "PRO DATANCOM",
00873                         nraw,
00874                         "Number of frames combined",
00875                         "%d"));
00876 
00877   sx=cpl_image_get_size_x(img);
00878   sy=cpl_image_get_size_y(img);
00879   space_x=(int)((sx-2*pr_border_x)/pr_num_x);
00880   space_y=(int)((sy-2*pr_border_y)/pr_num_y);
00881   qc_sto=cpl_table_new(pr_num_x*pr_num_y);
00882   cpl_table_new_column(qc_sto,"MIN",CPL_TYPE_DOUBLE);
00883   cpl_table_new_column(qc_sto,"MAX",CPL_TYPE_DOUBLE);
00884   cpl_table_new_column(qc_sto,"AVG",CPL_TYPE_DOUBLE);
00885   cpl_table_new_column(qc_sto,"MED",CPL_TYPE_DOUBLE);
00886   cpl_table_new_column(qc_sto,"RMS",CPL_TYPE_DOUBLE);
00887 
00888   for(i=0;i<pr_num_x;i++) {
00889     llx=pr_border_x+i*space_x;
00890     urx=llx+pr_box_sx;
00891     for(j=0;j<pr_num_y;j++) {
00892       lly=pr_border_y+j*space_y;
00893       ury=lly+pr_box_sy;
00894       raw=i*pr_num_y+j;
00895       qc_min=cpl_image_get_min_window(img,llx,lly,urx,ury);
00896       qc_max=cpl_image_get_min_window(img,llx,lly,urx,ury);
00897       qc_avg=cpl_image_get_mean_window(img,llx,lly,urx,ury);
00898       qc_med=cpl_image_get_median_window(img,llx,lly,urx,ury);
00899       qc_rms=cpl_image_get_stdev_window(img,llx,lly,urx,ury);
00900       uves_msg_debug("QC on area [%d,%d:%d,%d]. Min %g Max %g Avg %g Med %g Rms %g",
00901            llx,lly,urx,ury,qc_min,qc_max,qc_avg,qc_med,qc_rms);
00902 
00903       sprintf(qc_key,"%s%d%d%s","QC REG",i,j," MIN");
00904       sprintf(qc_com,"%s%d%d","Min of region [%d,%d]",i,j);
00905       check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
00906       check_nomsg(cpl_table_set_double(qc_sto,"MIN",raw,qc_min));
00907 
00908       sprintf(qc_key,"%s%d%d%s","QC REG",i,j," MAX");
00909       sprintf(qc_com,"%s%d%d","Max of region [%d,%d]",i,j);
00910       check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
00911       check_nomsg(cpl_table_set_double(qc_sto,"MAX",raw,qc_max));
00912 
00913       sprintf(qc_key,"%s%d%d%s","QC REG",i,j," AVG");
00914       sprintf(qc_com,"%s%d%d","Mean of region [%d,%d]",i,j);
00915       check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
00916       check_nomsg(cpl_table_set_double(qc_sto,"AVG",raw,qc_avg));
00917 
00918       sprintf(qc_key,"%s%d%d%s","QC REG",i,j," MED");
00919       sprintf(qc_com,"%s%d%d","Median of region [%d,%d]",i,j);
00920       check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
00921       check_nomsg(cpl_table_set_double(qc_sto,"MED",raw,qc_med));
00922 
00923       sprintf(qc_key,"%s%d%d%s","QC REG",i,j," RMS");
00924       sprintf(qc_com,"%s%d%d","Rms of region [%d,%d]",i,j);
00925       check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
00926       check_nomsg(cpl_table_set_double(qc_sto,"RMS",raw,qc_rms));
00927 
00928 
00929     }
00930   }
00931 
00932 
00933   check_nomsg(qc_min=cpl_table_get_column_min(qc_sto,"MIN"));
00934   check_nomsg(qc_max=cpl_table_get_column_max(qc_sto,"MIN"));
00935   check_nomsg(qc_avg=cpl_table_get_column_mean(qc_sto,"MIN"));
00936   check_nomsg(qc_med=cpl_table_get_column_median(qc_sto,"MIN"));
00937   check_nomsg(qc_rms=cpl_table_get_column_stdev(qc_sto,"MIN"));
00938 
00939   sprintf(qc_key,"%s","QC REG MIN MIN");
00940   sprintf(qc_com,"%s","Min of all Mins");
00941   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
00942 
00943   sprintf(qc_key,"%s","QC REG MIN MAX");
00944   sprintf(qc_com,"%s","Max of all Mins");
00945   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
00946 
00947   sprintf(qc_key,"%s","QC REG MIN AVG");
00948   sprintf(qc_com,"%s","Mean of all Mins");
00949   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
00950 
00951   sprintf(qc_key,"%s","QC REG MIN MED");
00952   sprintf(qc_com,"%s","Median of all Mins");
00953   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
00954 
00955   sprintf(qc_key,"%s","QC REG MIN RMS");
00956   sprintf(qc_com,"%s","Rms of all Mins");
00957   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
00958 
00959 
00960 
00961   check_nomsg(qc_min=cpl_table_get_column_min(qc_sto,"MAX"));
00962   check_nomsg(qc_max=cpl_table_get_column_max(qc_sto,"MAX"));
00963   check_nomsg(qc_avg=cpl_table_get_column_mean(qc_sto,"MAX"));
00964   check_nomsg(qc_med=cpl_table_get_column_median(qc_sto,"MAX"));
00965   check_nomsg(qc_rms=cpl_table_get_column_stdev(qc_sto,"MAX"));
00966 
00967   sprintf(qc_key,"%s","QC REG MAX MIN");
00968   sprintf(qc_com,"%s","Min of all Maxs");
00969   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
00970 
00971   sprintf(qc_key,"%s","QC REG MAX MAX");
00972   sprintf(qc_com,"%s","Max of all Maxs");
00973   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
00974 
00975   sprintf(qc_key,"%s","QC REG MAX AVG");
00976   sprintf(qc_com,"%s","Mean of all Maxs");
00977   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
00978 
00979   sprintf(qc_key,"%s","QC REG MAX MED");
00980   sprintf(qc_com,"%s","Median of all Maxs");
00981   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
00982 
00983   sprintf(qc_key,"%s","QC REG MAX RMS");
00984   sprintf(qc_com,"%s","Rms of all Maxs");
00985   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
00986 
00987 
00988   check_nomsg(qc_min=cpl_table_get_column_min(qc_sto,"AVG"));
00989   check_nomsg(qc_max=cpl_table_get_column_max(qc_sto,"AVG"));
00990   check_nomsg(qc_avg=cpl_table_get_column_mean(qc_sto,"AVG"));
00991   check_nomsg(qc_med=cpl_table_get_column_median(qc_sto,"AVG"));
00992   check_nomsg(qc_rms=cpl_table_get_column_stdev(qc_sto,"AVG"));
00993 
00994   sprintf(qc_key,"%s","QC REG AVG MIN");
00995   sprintf(qc_com,"%s","Min of all Means");
00996   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
00997 
00998   sprintf(qc_key,"%s","QC REG AVG MAX");
00999   sprintf(qc_com,"%s","Max of all Means");
01000   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
01001 
01002   sprintf(qc_key,"%s","QC REG AVG AVG");
01003   sprintf(qc_com,"%s","Mean of all Means");
01004   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
01005 
01006   sprintf(qc_key,"%s","QC REG AVG MED");
01007   sprintf(qc_com,"%s","Median of all Means");
01008   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
01009 
01010   sprintf(qc_key,"%s","QC REG AVG RMS");
01011   sprintf(qc_com,"%s","Rms of all Means");
01012   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
01013 
01014 
01015   check_nomsg(qc_min=cpl_table_get_column_min(qc_sto,"MED"));
01016   check_nomsg(qc_max=cpl_table_get_column_max(qc_sto,"MED"));
01017   check_nomsg(qc_avg=cpl_table_get_column_mean(qc_sto,"MED"));
01018   check_nomsg(qc_med=cpl_table_get_column_median(qc_sto,"MED"));
01019   check_nomsg(qc_rms=cpl_table_get_column_stdev(qc_sto,"MED"));
01020 
01021   sprintf(qc_key,"%s","QC REG MED MIN");
01022   sprintf(qc_com,"%s","Min of all Medians");
01023   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
01024 
01025   sprintf(qc_key,"%s","QC REG MED MAX");
01026   sprintf(qc_com,"%s","Max of all Medians");
01027   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
01028 
01029   sprintf(qc_key,"%s","QC REG MED AVG");
01030   sprintf(qc_com,"%s","Mean of all Medians");
01031   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
01032 
01033   sprintf(qc_key,"%s","QC REG MED MED");
01034   sprintf(qc_com,"%s","Median of all Medians");
01035   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
01036 
01037   sprintf(qc_key,"%s","QC REG MED RMS");
01038   sprintf(qc_com,"%s","Rms of all Medians");
01039   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
01040 
01041 
01042   check_nomsg(qc_min=cpl_table_get_column_min(qc_sto,"RMS"));
01043   check_nomsg(qc_max=cpl_table_get_column_max(qc_sto,"RMS"));
01044   check_nomsg(qc_avg=cpl_table_get_column_mean(qc_sto,"RMS"));
01045   check_nomsg(qc_med=cpl_table_get_column_median(qc_sto,"RMS"));
01046   check_nomsg(qc_rms=cpl_table_get_column_stdev(qc_sto,"RMS"));
01047 
01048 
01049   sprintf(qc_key,"%s","QC REG RMS MIN");
01050   sprintf(qc_com,"%s","Min of all Rms");
01051   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_min,qc_com,"%g"));
01052 
01053   sprintf(qc_key,"%s","QC REG RMS MAX");
01054   sprintf(qc_com,"%s","Max of all Rms");
01055   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_max,qc_com,"%g"));
01056 
01057   sprintf(qc_key,"%s","QC REG RMS AVG");
01058   sprintf(qc_com,"%s","Mean of all Rms");
01059   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_avg,qc_com,"%g"));
01060 
01061   sprintf(qc_key,"%s","QC REG RMS MED");
01062   sprintf(qc_com,"%s","Median of all Rms");
01063   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_med,qc_com,"%g"));
01064 
01065   sprintf(qc_key,"%s","QC REG RMS RMS");
01066   sprintf(qc_com,"%s","Rms of all Rms");
01067   check_nomsg(uves_qclog_add_double(qclog,qc_key,qc_rms,qc_com,"%g"));
01068 
01069 
01070 
01071 
01072  cleanup:
01073   uves_free_table(&qc_sto);
01074 
01075   return;
01076 
01077 }
01078 
01079 

Generated on Fri Apr 18 14:11:42 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1