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

Generated on Thu Nov 15 14:32:28 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1