uves_mbias_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: 2011/11/02 15:43:23 $
00023  * $Revision: 1.62 $
00024  * $Name: uves-5_0_0 $
00025  * $Log: uves_mbias_impl.c,v $
00026  * Revision 1.62  2011/11/02 15:43:23  amodigli
00027  * fixed compiler warning
00028  *
00029  * Revision 1.61  2011/09/09 09:56:21  amodigli
00030  * using irplib_mkmaster() functionality
00031  *
00032  * Revision 1.60  2010/09/24 09:49:18  amodigli
00033  * added info on mean master bias level as asked by Daniel
00034  *
00035  * Revision 1.59  2010/09/24 09:32:03  amodigli
00036  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00037  *
00038  * Revision 1.57  2010/08/30 14:16:08  amodigli
00039  * fixed problem computing QC.RON.OUTi.RAW (DFS09268)
00040  *
00041  * Revision 1.56  2010/06/11 11:40:46  amodigli
00042  * rename method to stack_method
00043  *
00044  * Revision 1.55  2010/06/02 09:20:44  amodigli
00045  * added correction of bias level before computation of median stack-then put back mean of levels on master bias
00046  *
00047  * Revision 1.54  2010/03/31 10:09:59  amodigli
00048  * added description
00049  *
00050  * Revision 1.53  2010/03/22 15:58:02  amodigli
00051  * added params and possibility to compute master as mean stack
00052  *
00053  * Revision 1.52  2009/10/29 17:16:29  amodigli
00054  * added param to specify if red cdd is new/old in call to uves_get_badpix
00055  *
00056  * Revision 1.51  2008/09/29 06:56:48  amodigli
00057  * add #include <string.h>
00058  *
00059  * Revision 1.50  2008/05/01 09:51:03  amodigli
00060  * fixed compiler warnings
00061  *
00062  * Revision 1.49  2008/04/02 14:02:02  amodigli
00063  * fixed compilation warnings
00064  *
00065  * Revision 1.48  2008/03/28 08:53:43  amodigli
00066  * IRPLIB_CONCAT2X-->UVES_CONCAT2X
00067  *
00068  * Revision 1.47  2008/03/04 15:20:59  amodigli
00069  * fixed redchain problem with clean_traps parameter
00070  *
00071  * Revision 1.46  2008/03/03 16:34:03  amodigli
00072  * added parameter to control trap column correction
00073  *
00074  * Revision 1.45  2008/02/15 12:43:49  amodigli
00075  * allow lower/upper chip for parameter process_chip
00076  *
00077  * Revision 1.44  2007/10/05 16:01:44  amodigli
00078  * using proces_chip parameter to process or not a given RED chip
00079  *
00080  * Revision 1.43  2007/08/21 13:08:26  jmlarsen
00081  * Removed irplib_access module, largely deprecated by CPL-4
00082  *
00083  * Revision 1.42  2007/06/11 13:28:26  jmlarsen
00084  * Changed recipe contact address to cpl at eso.org
00085  *
00086  * Revision 1.41  2007/06/08 13:06:16  jmlarsen
00087  * Send bug reports to Andrea
00088  *
00089  * Revision 1.40  2007/06/06 08:17:33  amodigli
00090  * replace tab with 4 spaces
00091  *
00092  * Revision 1.39  2007/05/22 11:31:35  jmlarsen
00093  * Removed image plotting functionality
00094  *
00095  * Revision 1.38  2007/04/24 12:50:29  jmlarsen
00096  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00097  *
00098  * Revision 1.37  2007/02/27 07:42:21  jmlarsen
00099  * Fixed bug when counting non-rejected pixels
00100  *
00101  * Revision 1.36  2007/02/26 10:15:56  jmlarsen
00102  * Be robust against the unlikely case that there are no good pixels to compute QC stats
00103  *
00104  * Revision 1.35  2007/02/23 13:32:43  jmlarsen
00105  * Change QC computation to exactly match MIDAS' STAT/IMAGE
00106  *
00107  * Revision 1.34  2007/02/09 13:37:43  jmlarsen
00108  * Enable calling from uves_cal_mkmaster
00109  *
00110  * Revision 1.33  2007/02/09 08:57:29  jmlarsen
00111  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00112  *
00113  * Revision 1.32  2007/02/09 08:14:16  jmlarsen
00114  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00115  *
00116  * Revision 1.31  2007/01/10 12:37:39  jmlarsen
00117  * Removed obsolete comments
00118  *
00119  * Revision 1.30  2006/12/07 08:23:23  jmlarsen
00120  * uves_load_raw_imagelist: support FLAMES
00121  *
00122  * Revision 1.29  2006/11/15 15:02:14  jmlarsen
00123  * Implemented const safe workarounds for CPL functions
00124  *
00125  * Revision 1.27  2006/11/15 14:04:08  jmlarsen
00126  * Removed non-const version of parameterlist_get_first/last/next which is 
00127  * already in CPL, added const-safe wrapper, unwrapper and deallocator functions
00128  *
00129  * Revision 1.26  2006/11/06 15:19:41  jmlarsen
00130  * Removed unused include directives
00131  *
00132  * Revision 1.25  2006/10/17 12:33:02  jmlarsen
00133  * Added semicolon at UVES_RECIPE_DEFINE invocation
00134  *
00135  * Revision 1.24  2006/10/09 13:01:13  jmlarsen
00136  * Use macro to define recipe interface functions
00137  *
00138  * Revision 1.23  2006/09/19 14:31:10  jmlarsen
00139  * uves_insert_frame(): use bitmap to specify which image statistics keywords must 
00140  * be computed
00141  *
00142  * Revision 1.22  2006/09/19 06:55:52  jmlarsen
00143  * Changed interface of uves_frameset to optionally write image statistics kewwords
00144  *
00145  * Revision 1.21  2006/08/24 11:36:37  jmlarsen
00146  * Write recipe start/stop time to header
00147  *
00148  * Revision 1.20  2006/08/18 13:35:42  jmlarsen
00149  * Fixed/changed QC parameter formats
00150  *
00151  * Revision 1.19  2006/08/17 13:56:53  jmlarsen
00152  * Reduced max line length
00153  *
00154  * Revision 1.18  2006/08/11 14:56:05  amodigli
00155  * removed Doxygen warnings
00156  *
00157  * Revision 1.17  2006/08/10 10:52:11  jmlarsen
00158  * Bugfix in comparison of chip id
00159  *
00160  * Revision 1.16  2006/07/14 12:19:28  jmlarsen
00161  * Support multiple QC tests per product
00162  *
00163  * Revision 1.15  2006/07/03 13:09:24  amodigli
00164  * adjusted description display layout
00165  *
00166  * Revision 1.14  2006/07/03 12:58:34  jmlarsen
00167  * Support flagging instead of interpolating bad pixels
00168  *
00169  * Revision 1.13  2006/06/22 08:58:33  jmlarsen
00170  * Use correct port number for QC
00171  *
00172  * Revision 1.12  2006/06/16 08:25:45  jmlarsen
00173  * Manually propagate ESO.DET. keywords from 1st/2nd input header
00174  *
00175  * Revision 1.11  2006/06/07 09:01:28  amodigli
00176  * added some doc
00177  *
00178  * Revision 1.10  2006/06/06 08:40:50  jmlarsen
00179  * Changed order of messages
00180  *
00181  * Revision 1.9  2006/05/12 15:40:08  jmlarsen
00182  * Fixed mixed code declarations
00183  *
00184  * Revision 1.8  2006/05/09 15:42:00  amodigli
00185  * added QC log
00186  *
00187  * Revision 1.7  2006/05/08 16:47:15  amodigli
00188  * added QC
00189  *
00190  * Revision 1.6  2006/05/08 15:38:46  amodigli
00191  * made changes to have port-id
00192  *
00193  * Revision 1.5  2006/05/05 14:33:22  jmlarsen
00194  * Removed debugging message
00195  *
00196  * Revision 1.4  2006/05/05 13:54:52  jmlarsen
00197  * Removed warnings about unused variables
00198  *
00199  * Revision 1.3  2006/04/20 10:47:39  amodigli
00200  * added qclog
00201  *
00202  * Revision 1.2  2006/04/06 09:48:15  amodigli
00203  * changed uves_frameset_insert interface to have QC log
00204  *
00205  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00206  * Moved recipe implementations to ./uves directory
00207  *
00208  * Revision 1.42  2006/01/19 08:47:24  jmlarsen
00209  * Inserted missing doxygen end tag
00210  *
00211  * Revision 1.41  2005/12/19 16:17:55  jmlarsen
00212  * Replaced bool -> int
00213  *
00214  */
00215 
00216 #ifdef HAVE_CONFIG_H
00217 #  include <config.h>
00218 #endif
00219 
00220 /*----------------------------------------------------------------------------*/
00227 /*----------------------------------------------------------------------------*/
00228 
00229 /*-----------------------------------------------------------------------------
00230                                 Includes
00231  -----------------------------------------------------------------------------*/
00232 #include <uves_mbias_impl.h>
00233 
00234 #include <uves_utils.h>
00235 #include <uves_corrbadpix.h>
00236 #include <uves_parameters.h>
00237 #include <uves.h>
00238 #include <uves_dfs.h>
00239 #include <uves_pfits.h>
00240 #include <uves_qclog.h>
00241 #include <uves_recipe.h>
00242 #include <uves_utils_wrappers.h>
00243 #include <uves_error.h>
00244 #include <irplib_mkmaster.h>
00245 #include <uves_msg.h>
00246 
00247 /* Library */
00248 #include <cpl.h>
00249 #include <float.h>
00250 #include <ctype.h>
00251 #include <string.h>
00252 /*-----------------------------------------------------------------------------
00253                             Functions prototypes
00254  -----------------------------------------------------------------------------*/
00255 
00256 static void uves_mbias_qclog(const cpl_imagelist* raw_imgs,
00257                              uves_propertylist **raw_headers,
00258                  enum uves_chip chip,
00259                  const cpl_image* mbia,
00260                  /* int sx_pix, Size of X bin in pix 
00261                  int sy_pix, Size of Y bin in pix */
00262                             cpl_table* qclog
00263     );
00264 
00265 static void  
00266 uves_mbias_qc_ron_raw(const cpl_image* rbia,
00267                       enum uves_chip chip,
00268                       const int x_cent_s,
00269                       const int x_cent_e,
00270                       const int y_cent_s,
00271                       const int y_cent_e,
00272                       cpl_table* qclog);
00273 
00274 static int
00275 uves_mbias_define_parameters(cpl_parameterlist *parameters);
00276 
00277 /*-----------------------------------------------------------------------------
00278                             Recipe standard code
00279  -----------------------------------------------------------------------------*/
00280 #define cpl_plugin_get_info uves_mbias_get_info
00281 UVES_RECIPE_DEFINE(
00282     UVES_MBIAS_ID, UVES_MBIAS_DOM,
00283     /* Warning: if more parameters are added to this recipe, they 
00284        need to be propagated to uves_cal_mkmaster! */
00285     uves_mbias_define_parameters,
00286     "Jonas M. Larsen", "cpl@eso.org",
00287     "Creates the master bias frame", 
00288     "This recipe creates a master bias frame by computing the median of all input\n"
00289     "bias frames. All input frames must have same tag and size and must be either\n"
00290     "BIAS_BLUE or BIAS_RED.\n"
00291     "On blue input the recipe computes one master bias frame; on red input a \n"
00292     "master bias frame for each chip is produced. The average, standard deviation\n"
00293     "and median of the master bias image(s) are written to the FITS header(s)");
00294 
00295 
00296 /*-----------------------------------------------------------------------------
00297                             Functions code
00298  -----------------------------------------------------------------------------*/
00301 /*----------------------------------------------------------------------------*/
00308 /*----------------------------------------------------------------------------*/
00309 int uves_mbias_define_parameters_body(cpl_parameterlist *parameters,
00310                        const char *recipe_id)
00311 {
00312 
00313     /*****************
00314      *    General    *
00315      *****************/
00316     if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00317         {
00318             return -1;
00319         }
00320 
00321     /**************************************
00322      *  detector's trap correction        *
00323      **************************************/
00324  
00325     if (uves_corr_traps_define_parameters(parameters,recipe_id) 
00326         != CPL_ERROR_NONE)
00327         {
00328             return -1;
00329         }
00330 
00331     /**************************************
00332      *  Master stack generation           *
00333      **************************************/
00334 
00335     if (uves_master_stack_define_parameters(parameters,recipe_id) 
00336         != CPL_ERROR_NONE)
00337         {
00338             return -1;
00339         }
00340 
00341     return (cpl_error_get_code() != CPL_ERROR_NONE);
00342 }
00343 
00344 
00345 
00346 /*----------------------------------------------------------------------------*/
00352 /*----------------------------------------------------------------------------*/
00353 static int
00354 uves_mbias_define_parameters(cpl_parameterlist *parameters)
00355 {
00356     return uves_mbias_define_parameters_body(parameters, make_str(UVES_MBIAS_ID));
00357 }
00358 
00359 
00360 
00361 
00362 
00363 /*----------------------------------------------------------------------------*/
00380 /*----------------------------------------------------------------------------*/
00381 static cpl_image *
00382 uves_mbias_process_chip(const cpl_imagelist *raw_images, 
00383                         uves_propertylist **raw_headers, 
00384                         uves_propertylist *mbias_header,
00385                         int binx, int biny,
00386                         enum uves_chip chip,
00387                         bool CLEAN_TRAPS,
00388                         const char* STACK_METHOD,
00389                         const double STACK_KLOW,
00390                         const double STACK_KHIGH,
00391                         const int STACK_NITER)
00392 {
00393     cpl_image *master_bias        = NULL; /* Result */
00394     double exposure_time = 0;
00395     int badpixels_cleaned;
00396     int i;
00397     int nraw=0;
00398     bool red_ccd_is_new=false;
00399     cpl_vector* bias_levels=NULL;
00400     double bias_mean=0;
00401 
00402     uves_msg("Calculating master bias...");
00403 
00404     check_nomsg(red_ccd_is_new=uves_ccd_is_new(raw_headers[0]));
00405     /* Get the median at each pixel */
00406     if(strcmp(STACK_METHOD,"MEDIAN")==0) {
00407        uves_msg("method median");
00408        master_bias=irplib_mkmaster_median(raw_images,5.,5,1.e-5);
00409     } else {
00410        uves_msg("method mean");
00411        master_bias=irplib_mkmaster_mean(raw_images,5.,5,1.e-5,STACK_KLOW,STACK_KHIGH,STACK_NITER);
00412     }
00413 
00414     /* Set mbias exposure time to average of inputs */
00415     exposure_time = 0;
00416     nraw=cpl_imagelist_get_size(raw_images);
00417     for (i = 0; i < nraw; i++)
00418     {
00419         check( exposure_time += uves_pfits_get_exptime(raw_headers[i]), 
00420            "Error reading exposure time");
00421     }
00422     exposure_time /= nraw;
00423     
00424     check( uves_pfits_set_exptime(mbias_header, exposure_time),
00425        "Error setting master bias exposure time");
00426    if(CLEAN_TRAPS) {
00427       check( badpixels_cleaned = 
00428              uves_correct_badpix_all(master_bias, mbias_header, 
00429                                      chip, binx, biny, false,red_ccd_is_new),
00430              "Error replacing bad pixels");
00431 
00432       uves_msg("%d bad pixels replaced", badpixels_cleaned);
00433    }
00434 
00435   cleanup:
00436     if (cpl_error_get_code() != CPL_ERROR_NONE)
00437     {
00438         uves_free_image(&master_bias);
00439     }
00440     
00441     return master_bias;
00442 }
00443 
00444 /*----------------------------------------------------------------------------*/
00451 /*----------------------------------------------------------------------------*/
00452 static void
00453 UVES_CONCAT2X(UVES_MBIAS_ID,exe)(cpl_frameset *frames, 
00454                    const cpl_parameterlist *parameters,
00455                    const char *starttime)
00456 {
00457     uves_mbias_exe_body(frames, parameters, starttime, make_str(UVES_MBIAS_ID));
00458     return;
00459 }
00460 
00461 /*----------------------------------------------------------------------------*/
00472 /*----------------------------------------------------------------------------*/
00473 void
00474 uves_mbias_exe_body(cpl_frameset *frames, 
00475             const cpl_parameterlist *parameters,
00476             const char *starttime,
00477             const char *recipe_id)
00478 {
00479     /* Input */
00480     cpl_imagelist *raw_images[2]        = {NULL, NULL};
00481     uves_propertylist **raw_headers[2]   = {NULL, NULL};    /* Two arrays of pointers */
00482 
00483     cpl_table* qclog[2] = {NULL, NULL};
00484 
00485     /* Output */
00486     uves_propertylist *product_header[2] = {NULL, NULL};
00487     cpl_image *master_bias              = NULL;
00488     cpl_stats *mbias_stats              = NULL;
00489     
00490     /* Local variables */
00491     char *product_filename = NULL;
00492     bool blue;
00493     enum uves_chip chip;
00494     int binx, biny;
00495     const char* PROCESS_CHIP=NULL;
00496     bool CLEAN_TRAPS;
00497     int raw_index = 0;
00498     int i=0;
00499 
00500     const char* STACK_METHOD=NULL;
00501     double STACK_KLOW=0;
00502     double STACK_KHIGH=0;
00503     int STACK_NITER=0;
00504 
00505     /* Load and check raw bias images and headers, identify arm (blue/red) */
00506     /* On success, 'raw_headers' will be arrays with the same length as 'raw_images' */
00507     check( uves_load_raw_imagelist(frames, 
00508                    false,     /* FLAMES format? (no) */
00509                    UVES_BIAS(true), UVES_BIAS(false),
00510                    CPL_TYPE_DOUBLE,
00511                    raw_images, raw_headers, product_header, 
00512                    &blue), "Error loading raw frames");
00513     
00514     /* Get binning from first header (i.e. BLUE or REDL chip, first raw frame) */
00515     check( binx = uves_pfits_get_binx(raw_headers[0][0]), 
00516        "Could not get raw frame x-binning");
00517     check( biny = uves_pfits_get_biny(raw_headers[0][0]), 
00518        "Could not get raw frame y-binning");
00519     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
00520                "Could not read parameter");
00521     uves_string_toupper((char*)PROCESS_CHIP);
00522 
00523     check( uves_get_parameter(parameters, NULL, recipe_id, "clean_traps", CPL_TYPE_BOOL, &CLEAN_TRAPS),
00524                "Could not read parameter");
00525 
00526     check( uves_get_parameter(parameters, NULL, recipe_id, "stack_method", CPL_TYPE_STRING, &STACK_METHOD),
00527                "Could not read parameter");
00528     uves_string_toupper((char*)STACK_METHOD);
00529 
00530     check( uves_get_parameter(parameters, NULL, recipe_id, "klow", CPL_TYPE_DOUBLE, &STACK_KLOW),
00531                "Could not read parameter");
00532     check( uves_get_parameter(parameters, NULL, recipe_id, "khigh", CPL_TYPE_DOUBLE, &STACK_KHIGH),
00533                "Could not read parameter");
00534     check( uves_get_parameter(parameters, NULL, recipe_id, "niter", CPL_TYPE_INT, &STACK_NITER),
00535                "Could not read parameter");
00536 
00537     /* Loop over one or two chips */
00538     for (chip = uves_chip_get_first(blue); 
00539      chip != UVES_CHIP_INVALID;
00540      chip = uves_chip_get_next(chip))
00541     {
00542        if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00543     chip = uves_chip_get_next(chip);
00544       }
00545         raw_index = uves_chip_get_index(chip);
00546 
00547         uves_msg("Processing %s chip",
00548              uves_chip_tostring_upper(chip));
00549 
00550         uves_msg_debug("Binning = %dx%d", binx, biny);
00551 
00552         /* Process chip */
00553         uves_free_image(&master_bias);
00554         check( master_bias = uves_mbias_process_chip(raw_images[raw_index], 
00555                                                      raw_headers[raw_index],
00556                                                      product_header[raw_index],
00557                                                      binx, biny,
00558                                                      chip,CLEAN_TRAPS,
00559                                                      STACK_METHOD,
00560                                                      STACK_KLOW,
00561                                                      STACK_KHIGH,
00562                                                      STACK_NITER),
00563            "Error processing chip");
00564         
00565         
00566         cpl_free(product_filename);
00567         check( product_filename = uves_masterbias_filename(chip), 
00568            "Error getting filename");
00569 
00570         /* Finished. Calculate QC parameters and save */
00571         uves_msg("Calculating QC parameters");
00572             uves_qclog_delete(&qclog[0]);
00573             qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00574             check(uves_mbias_qclog(raw_images[raw_index],
00575                                    raw_headers[raw_index],
00576                                    chip,
00577                                    master_bias,
00578                    /* binx,biny, */
00579                    qclog[0]),"error computing qclog");
00580 
00581         /* Insert into frame set */
00582         uves_msg("Saving product...");
00583         
00584         check( uves_frameset_insert(frames,
00585                     master_bias,
00586                     CPL_FRAME_GROUP_PRODUCT,
00587                     CPL_FRAME_TYPE_IMAGE,
00588                     CPL_FRAME_LEVEL_INTERMEDIATE,
00589                     product_filename,
00590                     UVES_MASTER_BIAS(chip),
00591                     raw_headers[raw_index][0], /* First frame */
00592                     product_header[raw_index],
00593                     NULL,
00594                     parameters,
00595                     recipe_id,
00596                     PACKAGE "/" PACKAGE_VERSION,qclog,
00597                     starttime, true, 
00598                     UVES_ALL_STATS),
00599            "Could not add master bias %s to frameset", product_filename);
00600             uves_qclog_delete(&qclog[0]);
00601         uves_msg("Master bias '%s' added to frameset", product_filename);
00602 
00603         if(strcmp(PROCESS_CHIP,"REDL") == 0) {
00604     chip = uves_chip_get_next(chip);
00605       }
00606 
00607 
00608         } /* For each chip */
00609 
00610   cleanup:
00611     /* Input */
00612     if (raw_images[0] != NULL)
00613     {
00614 
00615         for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++) 
00616         {
00617             if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
00618             if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
00619         }
00620         cpl_free(raw_headers[0]); raw_headers[0] = NULL;
00621         cpl_free(raw_headers[1]); raw_headers[1] = NULL;
00622     }
00623     uves_free_imagelist(&raw_images[0]);
00624     uves_free_imagelist(&raw_images[1]);
00625     /* Output */
00626 
00627     uves_qclog_delete(&qclog[0]);
00628     uves_free_image(&master_bias);
00629     uves_free_propertylist(&product_header[0]);
00630     uves_free_propertylist(&product_header[1]);
00631     cpl_free(product_filename);
00632     uves_free_stats(&mbias_stats);
00633     
00634     return;
00635 }
00636 
00637 
00638 
00639 static int
00640 count_good(const cpl_image *image)
00641 {
00642     return 
00643         cpl_image_get_size_x(image) * cpl_image_get_size_y(image) - 
00644         cpl_image_count_rejected(image);
00645 }
00646 /*----------------------------------------------------------------------------*/
00653 /*----------------------------------------------------------------------------*/
00654 static void
00655 reject_lo_hi(cpl_image *image, double min, double max)
00656 {
00657   cpl_mask *mask_lo = NULL;
00658   cpl_mask *mask_hi = NULL;
00659 
00660   mask_lo = cpl_mask_threshold_image_create(image, -DBL_MAX, min);
00661   mask_hi = cpl_mask_threshold_image_create(image, max, DBL_MAX);
00662   assure_mem( mask_lo );
00663   assure_mem( mask_hi );
00664 
00665   cpl_mask_or(mask_lo, mask_hi);
00666 
00667   cpl_image_reject_from_mask(image, mask_lo);
00668   
00669   cleanup:
00670   uves_free_mask(&mask_lo);
00671   uves_free_mask(&mask_hi);
00672   return;
00673 }
00674 
00685 static void uves_mbias_qclog(const cpl_imagelist* raw_imgs,
00686                              uves_propertylist **raw_headers,
00687                  enum uves_chip chip,
00688                  const cpl_image* mbia,
00689                  /* int sx_pix, Size of X bin in pix 
00690                  int sy_pix, Size of Y bin in pix */
00691                             cpl_table* qclog
00692                             ) 
00693 {
00694   int nx_pix= 0;  /* No of X pix */
00695   int ny_pix= 0;  /* No of Y pix */
00696 
00697   int sample_x= 100; /* X size of sampling window in pix */
00698   int sample_y= 100; /* Y size of sampling window in pix */
00699   int x_cent_s= 0;   /* X sampling window starting point */
00700   int x_cent_e= 0;   /* X sampling window ending point */
00701   int y_cent_s= 0;   /* Y sampling window starting point */
00702   int y_cent_e= 0;   /* Y sampling window ending point */
00703 
00704 
00705 
00706 
00707 
00708   double upp_threshold= 0.0;
00709   double low_threshold= 0.0;
00710   double extra=0.1;
00711   double qc_ron_master= 0.0;
00712 
00713   double master_median=0.0;
00714   int pn= 0;
00715 
00716 
00717   double min=0.0;
00718   double max=0.0;
00719   double struct_col=0.0;
00720   double struct_row=0.0;
00721 
00722   double time_s=+9999999.0;
00723   double time_e=-9999999.0;
00724   int nraw=0;
00725   double qc_duty_cycle=0.;
00726   double exposure_time=0;
00727   int i=0;
00728   char key_name[80];
00729 
00730   const cpl_image* rbia=NULL;
00731   cpl_image* tima=NULL;
00732   cpl_image* avg_col=NULL;
00733   cpl_image* avg_row=NULL;
00734   
00735   uves_qclog_add_string(qclog,
00736                         "QC TEST1 ID",
00737                         "Test-on-Master-Bias",
00738                         "Name of QC test",
00739                         "%s");
00740 
00741   uves_msg("Computing duty cycle...");
00742 
00743   /* Set mbias exposure time to average of inputs */
00744   exposure_time = 0;
00745   nraw = cpl_imagelist_get_size(raw_imgs);
00746   check_nomsg(uves_qclog_add_int(qclog,
00747                         "PRO DATANCOM",
00748                         nraw,
00749                         "Number of frames combined",
00750                         "%d"));
00751 
00752 
00753   for (i = 0; i < nraw; i++)
00754     {
00755       check( exposure_time = uves_pfits_get_mjdobs(raw_headers[i]),
00756          "Error reading exposure time");
00757       if(exposure_time >= time_e) time_e = exposure_time;
00758       if(exposure_time <= time_s) time_s = exposure_time;
00759     }
00760   if(nraw > 1) {
00761     qc_duty_cycle = (time_e-time_s)/ (nraw-1);
00762   }
00763   else
00764       {
00765       qc_duty_cycle = 0;
00766       }
00767  
00768   check_nomsg(uves_qclog_add_double(qclog,
00769                         "QC DUTYCYCL",
00770                         qc_duty_cycle,
00771                         "Time to store a frame",
00772                         "%.5e"));
00773 
00774   /* The following is not really used in MIDAS so we comment
00775   strcpy(date,uves_pfits_get_tpl_start(plist));
00776   */
00777 
00778 
00779   /* CONVERT FROM MIDAS
00780   nx_pix = m$value({mbia},NPIX(1));
00781   ny_pix = m$value({mbia},NPIX(2));
00782   */
00783 
00784   nx_pix = cpl_image_get_size_x(mbia);
00785   ny_pix = cpl_image_get_size_y(mbia);
00786 
00787 
00788   x_cent_s = (nx_pix - sample_x)/2;
00789   x_cent_e = (nx_pix + sample_x)/2;
00790   y_cent_s = (ny_pix - sample_y)/2;
00791   y_cent_e = (ny_pix + sample_y)/2;
00792  
00793   
00794   check_nomsg(upp_threshold = 
00795           cpl_image_get_median_window(mbia,
00796                       x_cent_s,
00797                       y_cent_s,
00798                       x_cent_e,
00799                       y_cent_e)*(1 + extra));
00800   check_nomsg(low_threshold = 
00801           cpl_image_get_median_window(mbia,
00802                       x_cent_s,
00803                       y_cent_s,
00804                       x_cent_e,
00805                       y_cent_e)*(1 - extra));
00806   
00807   /* convert from MIDAS
00808   pn = {uves_portid({PATHID})};
00809   */
00810   check_nomsg(pn = PORT_ID(chip));
00811   uves_msg_debug("Port number = %d", pn);
00812 
00813   rbia = cpl_imagelist_get_const(raw_imgs,0);
00814   check_nomsg(uves_mbias_qc_ron_raw(rbia, chip,
00815                     x_cent_s,x_cent_e,y_cent_s,y_cent_e,qclog));
00816 
00817 
00818   /* convert from  MIDAS
00819   stat/ima {mbia} + bins=1 exc={low_threshold},{upp_threshold};
00820   */
00821   check_nomsg(tima=cpl_image_duplicate(mbia));
00822 
00823   check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
00824   if (count_good(tima) >= 2)
00825       {
00826           check_nomsg(master_median = cpl_image_get_median(tima));
00827           check_nomsg(qc_ron_master = cpl_image_get_stdev(tima));
00828       }
00829   else
00830       {
00831           master_median = -1;
00832           qc_ron_master = -1;
00833           uves_msg_warning("Only %d good pixels in image. Setting QC parameters to -1",
00834                            count_good(tima));
00835       }
00836   uves_free_image(&tima);
00837 
00838   check_nomsg(uves_qclog_add_double(qclog,
00839                         "PRO DATAMED",
00840                         master_median,
00841                         "Median of pixel values",
00842                         "%7.3f"));
00843 
00844   sprintf(key_name, "QC OUT%d RON MASTER", pn);
00845   check_nomsg(uves_qclog_add_double(qclog,
00846                         key_name,
00847                         qc_ron_master,
00848                         "Read noise frame in ADU",
00849                         "%8.4f"));
00850 
00851   /* ==========================
00852    * Calculates Bias struct
00853    * ==========================
00854    */
00855 
00856  
00857   /*
00858    * in case of RED frame cuts out values greater than 300.
00859    * as the frame can be affected by this local operation we 
00860    * do the calculation on a copy of the original frame
00861    */
00862 
00863   
00864   check_nomsg(tima=cpl_image_duplicate(mbia));
00865   if (chip != UVES_CHIP_BLUE) {
00866     /*
00867     replace/ima {mbia} {tmpfrm} 300,>=300.;
00868     */
00869     check_nomsg(cpl_image_threshold(tima,
00870                                     -DBL_MAX,300,
00871                                     -DBL_MAX,300));
00872   }
00873 
00874 
00875   check_nomsg(avg_col = cpl_image_collapse_create(tima,1));
00876   check_nomsg(cpl_image_divide_scalar(avg_col,cpl_image_get_size_x(tima)));
00877 
00878   /* restricts statistics to +/- 2 ADU around mean */
00879   min = cpl_image_get_mean(avg_col) - 2;
00880   max = cpl_image_get_mean(avg_col) + 2; 
00881 
00882   /* replace with MIDAS
00883   stat/ima avg_col + exc={min},{max};
00884   */
00885   check_nomsg( reject_lo_hi(avg_col, min, max) );
00886   if (count_good(avg_col) >= 2)
00887       {
00888           check_nomsg(struct_col = cpl_image_get_stdev(avg_col));
00889       }
00890   else
00891       {
00892           struct_col = -1;
00893           uves_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
00894                            count_good(avg_col));
00895       }
00896 
00897   sprintf(key_name,"%s%d%s","QC OUT",pn," STRUCTY");
00898   check_nomsg(uves_qclog_add_double(qclog,
00899                         key_name,
00900                         struct_col,
00901                         "structure in Y (bias slope)",
00902                         "%8.4f"));
00903 
00904 
00905 
00906   check_nomsg(avg_row = cpl_image_collapse_create(tima,0));
00907   check_nomsg(cpl_image_divide_scalar(avg_row,cpl_image_get_size_y(tima)));
00908 
00909   /* restricts statistics to +/- 2 ADU around mean */
00910   min = cpl_image_get_mean(avg_row) - 2;
00911   max = cpl_image_get_mean(avg_row) + 2;
00912  
00913   /* replace with MIDAS
00914   stat/ima avg_row + exc={min},{max};
00915   */
00916   check_nomsg( reject_lo_hi(avg_row, min, max) );
00917   if (count_good(avg_row) >= 2)
00918       {
00919           check_nomsg(struct_row = cpl_image_get_stdev(avg_row));
00920       }
00921   else
00922       {
00923           struct_row = -1;
00924           uves_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
00925                            count_good(avg_row));
00926       }
00927 
00928   
00929   sprintf(key_name,"%s%d%s","QC OUT",pn," STRUCTX");
00930   check_nomsg(uves_qclog_add_double(qclog,
00931                         key_name,
00932                         struct_row,
00933                         "structure in X (bias slope)",
00934                         "%8.4f"));
00935 
00936  
00937 
00938  
00939  cleanup:
00940   uves_free_image(&avg_col);
00941   uves_free_image(&avg_row);
00942   uves_free_image(&tima);
00943   
00944   return;
00945 
00946 }
00960 static void
00961 uves_mbias_qc_ron_raw(const cpl_image* rbia,
00962                       enum uves_chip chip,
00963                       const int x_cent_s,
00964                       const int x_cent_e,
00965                       const int y_cent_s,
00966                       const int y_cent_e,
00967                       cpl_table* qclog)
00968 {
00969 
00970   double qc_ron_raw=0.0;
00971   double upp_threshold=0.0;
00972   double low_threshold=0.0;
00973   double extra=0.1;
00974   char key_name[80];
00975   int pn=0;
00976   cpl_image* tima=NULL;
00977 
00978   /* replace with MIDAS
00979   date   = "{{mbia},ESO.TPL.START}";
00980   store/frame infrm {incat} 1;
00981   */
00982   check_nomsg(upp_threshold = 
00983           cpl_image_get_median_window(rbia,
00984                       x_cent_s,
00985                       y_cent_s,
00986                       x_cent_e,
00987                       y_cent_e)*(1 + extra));
00988   
00989   check_nomsg(low_threshold = 
00990           cpl_image_get_median_window(rbia,
00991                       x_cent_s,
00992                       y_cent_s,
00993                       x_cent_e,
00994                       y_cent_e)*(1 - extra));
00995   
00996 
00997 
00998   /* replace from MIDAS
00999   stat/ima {rbia} + bins=1 exc={low_treshold},{upp_treshold};
01000   */
01001   check_nomsg(tima=cpl_image_duplicate(rbia));
01002 
01003   check_nomsg( reject_lo_hi(tima, low_threshold, upp_threshold) );
01004   if (count_good(tima) >= 2)
01005       {
01006           check_nomsg(qc_ron_raw = cpl_image_get_stdev(tima));
01007       }
01008   else
01009       {
01010           qc_ron_raw = -1;
01011           uves_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
01012                            count_good(tima));
01013       }
01014 
01015 
01016   /* replace from MIDAS
01017   pn = {uves_portid({PATHID})};
01018   */
01019   check_nomsg(pn = PORT_ID(chip));
01020 
01021 
01022   sprintf(key_name,"%s%d%s","QC OUT",pn," RON RAW");
01023   check_nomsg(uves_qclog_add_double(qclog,
01024                         key_name,
01025                         qc_ron_raw,
01026                         "Read noise frame in ADU",
01027                         "%8.4f"));
01028 
01029  cleanup:
01030   uves_free_image(&tima);
01031   return;
01032 }
01033 

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1