uves_reduce_mflat.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  02111-1307  USA       *
00018  *                                                                              */
00019 
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2008/03/04 15:23:55 $
00023  * $Revision: 1.31 $
00024  * $Name: uves-3_9_0 $
00025  * $Log: uves_reduce_mflat.c,v $
00026  * Revision 1.31  2008/03/04 15:23:55  amodigli
00027  * fixed redchain problem with clean_traps parameter
00028  *
00029  * Revision 1.30  2008/02/15 12:43:49  amodigli
00030  * allow lower/upper chip for parameter process_chip
00031  *
00032  * Revision 1.29  2007/10/05 16:01:45  amodigli
00033  * using proces_chip parameter to process or not a given RED chip
00034  *
00035  * Revision 1.28  2007/09/11 12:55:37  amodigli
00036  * uses uves_frameset_extract to get the SFLAT raw frames
00037  *
00038  * Revision 1.26  2007/08/21 13:08:26  jmlarsen
00039  * Removed irplib_access module, largely deprecated by CPL-4
00040  *
00041  * Revision 1.25  2007/06/28 12:03:32  jmlarsen
00042  * In FLAMES mode, remove any UVES frames from input frameset
00043  *
00044  * Revision 1.24  2007/06/28 09:28:07  jmlarsen
00045  * Insert MASTER_SFLAT into recipe frameset
00046  *
00047  * Revision 1.23  2007/06/22 09:29:02  jmlarsen
00048  * Changed interface of uves_save_image
00049  *
00050  * Revision 1.22  2007/06/06 08:17:33  amodigli
00051  * replace tab with 4 spaces
00052  *
00053  * Revision 1.21  2007/05/22 11:31:35  jmlarsen
00054  * Removed image plotting functionality
00055  *
00056  * Revision 1.20  2007/04/24 16:45:17  amodigli
00057  * changed interface of calls to uves_load_ordertable to match new interface
00058  *
00059  * Revision 1.19  2007/04/24 12:50:29  jmlarsen
00060  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00061  *
00062  * Revision 1.18  2007/04/03 06:29:29  amodigli
00063  * changed interface to uves_load_ordertable
00064  *
00065  * Revision 1.17  2007/02/09 13:40:55  jmlarsen
00066  * Enable calling from uves_mkmaster_sflat
00067  *
00068  * Revision 1.16  2007/02/09 08:59:05  jmlarsen
00069  * Include <float.h>
00070  *
00071  * Revision 1.15  2007/02/09 08:14:16  jmlarsen
00072  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00073  *
00074  * Revision 1.14  2007/01/10 14:17:13  jmlarsen
00075  * Do not threshold master flat to zero
00076  *
00077  * Revision 1.13  2006/12/07 08:23:23  jmlarsen
00078  * uves_load_raw_imagelist: support FLAMES
00079  *
00080  * Revision 1.12  2006/12/01 12:30:15  jmlarsen
00081  * Load FLAMES order table oshift/yshift
00082  *
00083  * Revision 1.11  2006/11/16 14:12:21  jmlarsen
00084  * Changed undefined trace number from 0 to -1, to support zero as an actual 
00085  * trace number
00086  *
00087  * Revision 1.10  2006/11/15 15:02:15  jmlarsen
00088  * Implemented const safe workarounds for CPL functions
00089  *
00090  * Revision 1.8  2006/11/15 14:04:08  jmlarsen
00091  * Removed non-const version of parameterlist_get_first/last/next which is 
00092  * already in CPL, added const-safe wrapper, unwrapper and deallocator functions
00093  *
00094  * Revision 1.7  2006/11/13 12:47:39  jmlarsen
00095  * Don't subtract background for FLAMES reduction
00096  *
00097  * Revision 1.6  2006/11/06 15:19:41  jmlarsen
00098  * Removed unused include directives
00099  *
00100  * Revision 1.5  2006/10/24 14:08:16  jmlarsen
00101  * Added flames flag when loading frames
00102  *
00103  * Revision 1.4  2006/10/09 13:04:58  jmlarsen
00104  * Use macro to define recipe interface functions
00105  *
00106  * Revision 1.3  2006/10/02 08:38:16  jmlarsen
00107  * Added REF_TFLAT computation
00108  *
00109  * Revision 1.2  2006/09/27 15:08:45  jmlarsen
00110  * Fixed doc. bug
00111  *
00112  * Revision 1.1  2006/09/27 13:22:04  jmlarsen
00113  * Factored out flat reduction
00114  *
00115  * Revision 1.4  2006/08/17 13:56:52  jmlarsen
00116  * Reduced max line length
00117  *
00118  * Revision 1.3  2005/12/19 16:17:56  jmlarsen
00119  * Replaced bool -> int
00120  *
00121  */
00122 
00123 #ifdef HAVE_CONFIG_H
00124 #  include <config.h>
00125 #endif
00126 
00127 /*----------------------------------------------------------------------------*/
00132 /*----------------------------------------------------------------------------*/
00137 /*-----------------------------------------------------------------------------
00138                                 Includes
00139  -----------------------------------------------------------------------------*/
00140 #include <uves_reduce_mflat.h>
00141 
00142 #include <uves.h>
00143 #include <uves_backsub.h>
00144 #include <uves_chip.h>
00145 #include <uves_dfs.h>
00146 #include <uves_pfits.h>
00147 #include <uves_parameters.h>
00148 #include <uves_utils.h>
00149 #include <uves_utils_wrappers.h>
00150 #include <uves_qclog.h>
00151 #include <uves_error.h>
00152 #include <uves_msg.h>
00153 
00154 #include <cpl.h>
00155 #include <float.h>
00156 
00157 /*-----------------------------------------------------------------------------
00158                             Functions prototypes
00159  -----------------------------------------------------------------------------*/
00160 
00161 static void uves_mflat_qclog(const cpl_imagelist* raw_images,
00162                      cpl_table* qclog);
00163 
00164 static cpl_error_code uves_msflats(cpl_frameset * set, const cpl_parameterlist *parameters,
00165                    const char *recipe_id,
00166                    const char *starttime);
00167 static void uves_mflat_one(cpl_frameset *frames,
00168                const cpl_parameterlist *parameters, 
00169                bool flames,
00170                const char *recipe_id,
00171                const char *starttime,
00172                const char* prefix);
00173 
00174 static cpl_error_code
00175 uves_mflat_at_ypos(cpl_frameset* set,
00176            const cpl_parameterlist* parameters,
00177            const char *recipe_id,
00178            const char *starttime,
00179                    const cpl_frameset* raw,
00180                    const cpl_frameset* cdb,
00181                    const int ref_x1enc,
00182                    const int ref_x2enc,
00183            const int set_no);
00184 
00185 static void uves_reduce_mflat(cpl_frameset *frames, const cpl_parameterlist *parameters,
00186                   bool flames,
00187                   const char *recipe_id, 
00188                   const char *starttime,
00189                   const char *prefix);
00190 
00191 /*-----------------------------------------------------------------------------
00192                             Implementation
00193  -----------------------------------------------------------------------------*/
00194 const char * const uves_mflat_desc =
00195 "This recipe creates a master flat frame by 1) subtracting the master bias\n"
00196 "frame from each flat field frame, 2) dividing each flat field frame by the\n"
00197 " exposure time for that frame, 3) taking the median of all bias subtracted,\n"
00198 " normalized raw\n flat frames, 4) optionally subtracting the master dark \n"
00199 "frame, and 5) subtracting\n the background to get the bias subtracted, \n"
00200 "optionally dark subtracted, normalized, background subtracted master \n"
00201 "flat-field frame. Symbolically,\n"
00202 " masterflat = median( (flat_i - masterbias)/exptime_i ) - masterdark/exptime\n"
00203 "            - background.\n"
00204 "\n"
00205 "The input flat field frames must have same tag which must match\n"
00206 "(I|D|S|T|SCREEN|)FLAT_(BLUE|RED), for example TFLAT_BLUE or FLAT_RED. Also, a\n"
00207 "master bias (MASTER_BIAS_xxxx) and ordertable (ORDER_TABLE_xxxx) must be\n"
00208 "provided for each chip (xxxx = BLUE, REDL, REDU). A master dark frame\n"
00209 "(MASTER_(P)DARK_xxxx) may optionally be provided. On blue input the recipe\n"
00210 "computes one master flat field frame; on red input the recipe produces a\n"
00211 "master flat field frame for each chip (MASTER_FLAT_xxxx, MASTER_IFLAT_xxxx,\n"
00212 "MASTER_DFLAT_xxxx, MASTER_TFLAT_xxxx or MASTER_SCREEN_FLAT_xxxx).";
00213 
00214 /*----------------------------------------------------------------------------*/
00221 /*----------------------------------------------------------------------------*/
00222 int
00223 uves_mflat_define_parameters_body(cpl_parameterlist *parameters, 
00224                   const char *recipe_id)
00225 {
00226    /*****************
00227     *    General    *
00228     *****************/
00229    if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00230    {
00231       return -1;
00232    }
00233    if (uves_corr_traps_define_parameters(parameters,recipe_id) 
00234        != CPL_ERROR_NONE)
00235    {
00236       return -1;
00237    }
00238 
00239     
00240     /****************************
00241      *  Spline back.sub.        *
00242      ****************************/
00243     
00244     if (uves_propagate_parameters_step(UVES_BACKSUB_ID, parameters, 
00245                        recipe_id, NULL) != 0)
00246     {
00247         return -1;
00248     }
00249 
00250     return (cpl_error_get_code() != CPL_ERROR_NONE);
00251 }
00252 
00253 /*----------------------------------------------------------------------------*/
00291 /*----------------------------------------------------------------------------*/
00292 static cpl_image *
00293 uves_mflat_process_chip(const cpl_imagelist *raw_images, uves_propertylist **raw_headers, 
00294             uves_propertylist *master_flat_header,
00295             const cpl_image *master_bias,
00296             const cpl_image *master_dark, const uves_propertylist *mdark_header, 
00297             const cpl_table *ordertable, const polynomial *order_locations,
00298             bool flames,
00299             const cpl_parameterlist *parameters,
00300             enum uves_chip chip,
00301             const char *recipe_id,
00302             bool DEBUG,
00303             cpl_image **background)
00304 
00305 {
00306     cpl_image *master_flat        = NULL; /* Result */
00307     cpl_image *current_flat       = NULL;
00308     cpl_imagelist *preproc_images = NULL;
00309     int i;
00310 
00311     /* First process each input image and store the results in a new image list */
00312 
00313     preproc_images = cpl_imagelist_new();
00314     for (i = 0; i < cpl_imagelist_get_size(raw_images); i++)
00315     {
00316         double exposure_time = 0.0;
00317 
00318         const uves_propertylist *current_header = NULL;
00319 
00320         current_flat   = cpl_image_duplicate(cpl_imagelist_get_const(raw_images, i));
00321         current_header = raw_headers[i];
00322         
00323         /* Subtract master bias */
00324         if (master_bias != NULL)
00325         {
00326             uves_msg("Subtracting master bias");
00327             check( uves_subtract_bias(current_flat, master_bias), 
00328                "Error subtracting master bias");
00329         }
00330         else
00331         {
00332             uves_msg("Skipping bias subtraction");
00333         }
00334         
00335         /* Normalize to unit exposure time */
00336         check( exposure_time = uves_pfits_get_exptime(current_header), 
00337            "Error reading exposure time");
00338         
00339         uves_msg("Normalizing flat from %f s to unit exposure time", exposure_time);
00340         check( cpl_image_divide_scalar(current_flat, exposure_time),
00341            "Error normalizing flat field");
00342         check( uves_pfits_set_exptime(master_flat_header, 1.0),
00343            "Error writing master frame exposure time");
00344         
00345         /* Append to imagelist */
00346         check( cpl_imagelist_set(preproc_images,     /* Image list */
00347                      current_flat,       /* Image to insert */
00348                      i),                 /* Position (number_of_images=>append) */
00349            "Could not insert image into image list");
00350         
00351         /* Don't deallocate the image. It will be deallocated when
00352            the image list is deallocated */
00353         current_flat = NULL;
00354     }
00355     
00356     /* Take median of all input flats */
00357     uves_msg("Calculating stack median");
00358     check( master_flat = cpl_imagelist_collapse_median_create(preproc_images), 
00359        "Error computing median");
00360     
00361     /* Subtract master dark if present */
00362     if (master_dark != NULL)
00363     {
00364         uves_msg("Subtracting master dark");
00365         check( uves_subtract_dark(master_flat, master_flat_header,
00366                       master_dark, mdark_header), 
00367            "Error subtracting master dark");
00368     }
00369     else
00370     {
00371         uves_msg("Skipping dark subtraction");
00372     }
00373 
00374     if (DEBUG && !flames)
00375     {
00376         check( uves_save_image_local("Pre-background subtracted master flat", "pre",
00377                                      master_flat, chip, -1, -1, master_flat_header, true), 
00378            "Error saving image");
00379     }
00380     
00381     /* Subtract background from master flat */
00382     if (!flames)
00383     {
00384         uves_msg("Subtracting background");
00385         
00386         check( uves_backsub_spline(master_flat, 
00387                        /* Info about chip (wavelength, ...) is 
00388                       stored in any raw header,
00389                       so just pass the first one   */
00390                        raw_headers[0],                
00391                        ordertable, order_locations, 
00392                        parameters, recipe_id,
00393                        chip,
00394                        true,     /* Use flat-field parameters? */
00395                        background),
00396            "Error subtracting background from master flat");
00397     }
00398     else
00399     {
00400         uves_msg("Skipping background subtraction");
00401     }
00402         
00403   cleanup:
00404     uves_free_imagelist(&preproc_images);
00405     uves_free_image(&current_flat);
00406     if (cpl_error_get_code() != CPL_ERROR_NONE)
00407     {
00408         uves_free_image(&master_flat);
00409     }
00410     
00411     return master_flat;
00412 }
00413 
00414 /*----------------------------------------------------------------------------*/
00426 /*----------------------------------------------------------------------------*/
00427 void
00428 uves_mflat_exe_body(cpl_frameset *frames, 
00429                     const cpl_parameterlist *parameters,
00430                     const char *starttime,
00431                     const char *recipe_id)
00432 {
00433     /* Do FLAMES reduction if SFLAT frame is given */
00434     if (cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL) {
00435   
00436         check(uves_msflats(frames, parameters, recipe_id, starttime),
00437               "find same sflats failed");
00438     }
00439     else {
00440         bool flames = false;
00441         check(uves_mflat_one(frames, parameters, flames, recipe_id, 
00442                              starttime, ""),
00443               "Master flat one failed");
00444     }
00445     
00446  cleanup:
00447     return;
00448 }
00449 
00450 /*----------------------------------------------------------------------------*/
00456 /*----------------------------------------------------------------------------*/
00457 static cpl_error_code
00458 uves_msflats(cpl_frameset * set, const cpl_parameterlist *parameters,
00459          const char *recipe_id,
00460          const char *starttime)
00461 {
00462   /* Pseudocode:
00463      extract raw frames from set
00464      extract cdb frames from set
00465      identifies how many different Y position we have 
00466      for each Y pos:
00467          extract from the raw_set the raw frames corresponding to each Y pos 
00468          merge in a new wrk_set the cdb_set 
00469          computes the corresponding master flat 
00470          put the products in the final set 
00471      endfor 
00472   */
00473   cpl_frameset* raw=NULL;
00474   cpl_frameset* cdb=NULL;
00475   cpl_frameset* pro=NULL;
00476   int status=0;
00477   int x1enc=0;
00478   int x2enc=0;
00479 
00480   cpl_table* encoder_tbl=NULL;
00481   int nset=0;
00482   int i=0;
00483 
00484   //Extracts SFLAT raw frames 
00485   check(raw=uves_frameset_extract(set,UVES_SFLAT(false)),
00486                   "Extract %s frames failed",
00487                   UVES_SFLAT(false));
00488 
00489   check(uves_extract_frames_group_type(set,&cdb,CPL_FRAME_GROUP_CALIB),
00490     "Extract cdb frames failed");
00491   check(uves_sflats_get_encoder_steps(raw,&encoder_tbl,&nset),
00492     "Get encoder steps failed");
00493   uves_msg("Check Slit Flat Field Y nominal positions within each set");
00494   for(i=0;i<nset;i++) {
00495 
00496          uves_msg("Slit Flat field set %d: x1enc = %d x2enc = %d",
00497               i+1,
00498               cpl_table_get_int(encoder_tbl,"x1enc",i,&status),
00499               cpl_table_get_int(encoder_tbl,"x2enc",i,&status));
00500 
00501   }
00502 
00503   for(i=0;i<nset;i++) {
00504     x1enc=cpl_table_get_int(encoder_tbl,"x1enc",i,&status);
00505     x2enc=cpl_table_get_int(encoder_tbl,"x2enc",i,&status);
00506 
00507     uves_msg("Processing set %d", i+1);
00508    
00509     check(uves_mflat_at_ypos(set,parameters,recipe_id,starttime,raw,cdb,x1enc,x2enc,i+1),
00510       "Master flat one failed");
00511   }
00512 
00513   cleanup:
00514   uves_free_table(&encoder_tbl);
00515   uves_free_frameset(&raw);
00516   uves_free_frameset(&cdb);
00517   uves_free_frameset(&pro);
00518 
00519     return cpl_error_get_code();
00520 }
00521 
00522 
00523 /*----------------------------------------------------------------------------*/
00529 /*----------------------------------------------------------------------------*/
00530 static cpl_error_code
00531 uves_mflat_at_ypos(cpl_frameset* set,
00532            const cpl_parameterlist* parameters,
00533            const char *recipe_id,
00534            const char *starttime,
00535                    const cpl_frameset* raw,
00536                    const cpl_frameset* cdb,
00537                    const int ref_x1enc,
00538                    const int ref_x2enc,
00539                    const int set_no)
00540 {
00541 
00542     const cpl_frame* frm_tmp=NULL;
00543   char* file=NULL;
00544   uves_propertylist* plist=NULL;
00545   int i=0;
00546   const int threshold = 5;
00547   cpl_frame* frm_dup=NULL;
00548   cpl_frameset* tmp=NULL;
00549   cpl_frameset* pro=NULL;
00550   int x1enc=0;
00551   int x2enc=0;
00552   char prefix[255];
00553   bool flames = true;
00554 
00555   check_nomsg(tmp=cpl_frameset_new());
00556    for(i=0;i<cpl_frameset_get_size(raw);i++)
00557     {
00558     check_nomsg(frm_tmp=cpl_frameset_get_frame_const(raw,i));
00559     check_nomsg(file=cpl_strdup(cpl_frame_get_filename(frm_tmp)));
00560     check_nomsg(plist=uves_propertylist_load(file,0));
00561     check_nomsg(x1enc=uves_pfits_get_slit3_x1encoder(plist));
00562     check_nomsg(x2enc=uves_pfits_get_slit3_x2encoder(plist));
00563     
00564     if( (fabs(x1enc - ref_x1enc) <= threshold) &&
00565         (fabs(x2enc - ref_x2enc) <= threshold) ) {
00566       uves_msg_debug("file=%s x1enc=%d x2enc=%d",file,x1enc,x2enc);
00567       check(frm_dup = cpl_frame_duplicate(frm_tmp),"duplicate");
00568       check(cpl_frameset_insert(tmp,frm_dup),"insert");
00569     }
00570     cpl_free(file);
00571     uves_free_propertylist(&plist);
00572     }
00573    check_nomsg(uves_frameset_merge(tmp,cdb));
00574    sprintf(prefix,"%s%d%s","set",set_no,"_");
00575    check(uves_mflat_one(tmp,parameters, flames, recipe_id, starttime,prefix),"Master flat one failed");
00576    check_nomsg(uves_extract_frames_group_type(tmp,&pro,CPL_FRAME_GROUP_PRODUCT));
00577    check_nomsg(uves_frameset_merge(set,pro));
00578 
00579  cleanup:
00580    uves_free_frameset(&tmp);
00581    uves_free_frameset(&pro);
00582    uves_free_propertylist(&plist);
00583   
00584     return cpl_error_get_code();
00585 }
00586 /*----------------------------------------------------------------------------*/
00599 /*----------------------------------------------------------------------------*/
00600 static void
00601 uves_mflat_one(cpl_frameset *frames,
00602            const cpl_parameterlist *parameters, 
00603            bool flames,
00604            const char *recipe_id,
00605            const char *starttime,
00606                const char* prefix)
00607 {
00608     /* Do flat-fielding */
00609     check_nomsg( uves_reduce_mflat(frames, parameters, 
00610                    flames, recipe_id,
00611                    starttime, prefix) );
00612 
00613   cleanup:
00614     return;
00615 }
00616 
00617 /*----------------------------------------------------------------------------*/
00628 /*----------------------------------------------------------------------------*/
00629 static void
00630 uves_reduce_mflat(cpl_frameset *frames, const cpl_parameterlist *parameters,
00631           bool flames,
00632           const char *recipe_id, 
00633           const char *starttime,
00634           const char *prefix)
00635 {
00636     bool DEBUG;
00637 
00638     /* Input */
00639     cpl_imagelist       *raw_images[2] = {NULL, NULL}; /* An image list for both chips */
00640     uves_propertylist  **raw_headers[2] = {NULL, NULL}; /* Two arrays of pointers */
00641 
00642     /* Master bias */
00643     cpl_image *master_bias               = NULL;
00644     uves_propertylist *master_bias_header = NULL;
00645 
00646     /* Master dark */
00647     cpl_image *master_dark               = NULL;
00648     uves_propertylist *master_dark_header = NULL;
00649 
00650     /* Order table */
00651     cpl_table        *ordertable            = NULL;
00652     uves_propertylist *ordertable_header     = NULL;
00653     polynomial       *order_locations       = NULL;
00654     cpl_table        *traces                = NULL;
00655     
00656     /* Reference master flat */
00657     cpl_image        *ref_flat              = NULL;
00658     uves_propertylist *ref_flat_header       = NULL;
00659 
00660     /* Output */
00661     cpl_table *qclog[]                  = {NULL, NULL};
00662     cpl_image *master_flat              = NULL;
00663     cpl_image *background               = NULL;
00664     uves_propertylist *product_header[]  = {NULL, NULL};
00665     cpl_image *ratio                    = NULL;
00666     
00667     /* Local variables */
00668     char *product_filename = NULL;
00669     char pro_filename[255];
00670     const char *product_tag[2] = {NULL, NULL};
00671     bool blue;
00672     enum uves_chip chip;
00673     const char* PROCESS_CHIP=NULL;
00674 
00675     /* Read recipe parameters */
00676     {
00677     /* General */
00678     check( uves_get_parameter(parameters, NULL, "uves", "debug", CPL_TYPE_BOOL  , &DEBUG ), 
00679            "Could not read parameter");
00680     }
00681     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP), "Could not read parameter");
00682 
00683     uves_string_toupper((char*)PROCESS_CHIP);
00684 
00685     /* Load and check raw flat images and headers, identify arm (blue/red) */
00686     /* On success, 'raw_headers' will be an array with the same size as 'raw_images' */
00687     /* Set product tags to match input tag */
00688 
00689     if (cpl_frameset_find(frames, UVES_FLAT(true )) != NULL ||
00690     cpl_frameset_find(frames, UVES_FLAT(false)) != NULL)
00691     {
00692         check( uves_load_raw_imagelist(frames, 
00693                        flames,
00694                        UVES_FLAT(true), UVES_FLAT(false),
00695                        CPL_TYPE_DOUBLE,
00696                        raw_images, raw_headers, product_header, 
00697                        &blue), "Error loading raw flat frames");
00698         
00699         for (chip = uves_chip_get_first(blue); chip != UVES_CHIP_INVALID; 
00700          chip = uves_chip_get_next(chip))
00701         {
00702             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_FLAT(chip);
00703         }
00704     }
00705     else if (cpl_frameset_find(frames, UVES_DFLAT(true )) != NULL ||
00706          cpl_frameset_find(frames, UVES_DFLAT(false)) != NULL)
00707     {
00708         check( uves_load_raw_imagelist(frames, 
00709                        flames,
00710                        UVES_DFLAT(true), UVES_DFLAT(false),
00711                        CPL_TYPE_DOUBLE,
00712                        raw_images, raw_headers, product_header, 
00713                        &blue), "Error loading raw flat frames");
00714         for (chip = uves_chip_get_first(blue); 
00715          chip != UVES_CHIP_INVALID; 
00716          chip = uves_chip_get_next(chip))
00717         {
00718             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_DFLAT(chip);
00719         }
00720     }
00721     else if  (cpl_frameset_find(frames, UVES_IFLAT(true )) != NULL ||
00722           cpl_frameset_find(frames, UVES_IFLAT(false)) != NULL)
00723     {
00724         check( uves_load_raw_imagelist(frames, 
00725                        flames,
00726                        UVES_IFLAT(true), UVES_IFLAT(false),
00727                        CPL_TYPE_DOUBLE,
00728                        raw_images, raw_headers, product_header, 
00729                        &blue), "Error loading raw flat frames");
00730         for (chip = uves_chip_get_first(blue); 
00731          chip != UVES_CHIP_INVALID; 
00732          chip = uves_chip_get_next(chip))
00733         {
00734             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_IFLAT(chip);
00735         }
00736     }
00737     else if  (cpl_frameset_find(frames, UVES_TFLAT(true )) != NULL ||
00738           cpl_frameset_find(frames, UVES_TFLAT(false)) != NULL)
00739     {
00740         check( uves_load_raw_imagelist(frames, 
00741                        flames,
00742                        UVES_TFLAT(true), UVES_TFLAT(false),
00743                        CPL_TYPE_DOUBLE,
00744                        raw_images, raw_headers, product_header, 
00745                        &blue), "Error loading raw flat frames");
00746         for (chip = uves_chip_get_first(blue); 
00747          chip != UVES_CHIP_INVALID; 
00748          chip = uves_chip_get_next(chip))
00749         {
00750             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_TFLAT(chip);
00751         }
00752     }
00753     else if  (cpl_frameset_find(frames, UVES_SCREEN_FLAT(true )) != NULL ||
00754           cpl_frameset_find(frames, UVES_SCREEN_FLAT(false)) != NULL)
00755     {
00756         check( uves_load_raw_imagelist(frames, 
00757                        flames,
00758                        UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00759                        CPL_TYPE_DOUBLE,
00760                        raw_images, raw_headers, product_header, 
00761                        &blue), "Error loading raw flat frames");
00762         for (chip = uves_chip_get_first(blue); 
00763          chip != UVES_CHIP_INVALID; 
00764          chip = uves_chip_get_next(chip))
00765         {
00766             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SCREEN_FLAT(chip);
00767         }
00768     }
00769     else if  (cpl_frameset_find(frames, UVES_SFLAT(true )) != NULL ||
00770           cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL)
00771     {
00772         check( uves_load_raw_imagelist(frames, 
00773                        flames,
00774                        UVES_SFLAT(true), UVES_SFLAT(false),
00775                        CPL_TYPE_DOUBLE,
00776                        raw_images, raw_headers, product_header, 
00777                        &blue), "Error loading raw flat frames");
00778         for (chip = uves_chip_get_first(blue); 
00779          chip != UVES_CHIP_INVALID; 
00780          chip = uves_chip_get_next(chip))
00781         {
00782             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SFLAT(chip);
00783         }
00784     }
00785     else 
00786     {
00787         assure(false, CPL_ERROR_DATA_NOT_FOUND,
00788            "Missing input flat frame: "
00789            "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s or %s expected",
00790            UVES_FLAT(true) , UVES_FLAT(false),
00791            UVES_DFLAT(true), UVES_DFLAT(false),
00792            UVES_IFLAT(true), UVES_IFLAT(false),
00793            UVES_TFLAT(true), UVES_TFLAT(false),
00794            UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00795            UVES_SFLAT(true), UVES_SFLAT(false));
00796     }
00797 
00798     /* Loop over one or two chips */
00799     for (chip = uves_chip_get_first(blue); 
00800      chip != UVES_CHIP_INVALID; 
00801      chip = uves_chip_get_next(chip))
00802     {
00803 
00804       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00805     chip = uves_chip_get_next(chip);
00806       }
00807 
00808 
00809         const char *ordertable_filename = "";
00810         const char *master_bias_filename = "";
00811         const char *master_dark_filename = "";
00812         const char *chip_name = "";
00813         
00814         int raw_index = uves_chip_get_index(chip);
00815         
00816         uves_msg("Processing %s chip", uves_chip_tostring_upper(chip));
00817 
00818         /* Chip name of first input frame */
00819         check_nomsg( chip_name = uves_pfits_get_chipid(raw_headers[raw_index][0], chip));
00820 
00821         /* Load master bias, set pointer to NULL if not present */
00822         uves_free_image(&master_bias);
00823         uves_free_propertylist(&master_bias_header);
00824         if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00825         {
00826             uves_free_image(&master_bias);
00827             uves_free_propertylist(&master_bias_header);
00828             check( uves_load_mbias(frames,
00829                        chip_name,
00830                        &master_bias_filename, &master_bias,
00831                        &master_bias_header, chip), 
00832                "Error loading master bias");
00833             
00834             uves_msg_low("Using master bias in '%s'", master_bias_filename);
00835         }
00836         else
00837         {
00838             uves_msg_low("No master bias in SOF. Bias subtraction not done");
00839         }
00840     
00841         /* Load master dark, set pointer to NULL if not present */
00842         uves_free_image(&master_dark);
00843         uves_free_propertylist(&master_dark_header);
00844         if (cpl_frameset_find(frames, UVES_MASTER_DARK(chip))  != NULL ||
00845         cpl_frameset_find(frames, UVES_MASTER_PDARK(chip)) != NULL)
00846         {
00847             uves_free_image(&master_dark);
00848             uves_free_propertylist(&master_dark_header);
00849             check( uves_load_mdark(frames, chip_name,
00850                        &master_dark_filename, &master_dark,
00851                        &master_dark_header, chip), 
00852                "Error loading master dark");
00853             
00854             uves_msg_low("Using master dark in '%s'", master_dark_filename);
00855         }
00856         else
00857         {
00858             uves_msg_low("No master dark in SOF. Dark subtraction not done");
00859         }
00860     
00861         /* Load the order table for this chip */
00862         if (flames)
00863         /* FLAMES does not do background subtraction (here)
00864            and therefore does not need an ordertable */
00865         {
00866             if (cpl_frameset_find(frames, UVES_ORDER_TABLE(flames, chip)) != NULL)
00867             {
00868                 uves_msg_warning("Order table (%s) is not used in FLAMES reduction",
00869                          UVES_ORDER_TABLE(flames, chip));
00870             }
00871         }
00872         else
00873         {
00874             uves_free_table       (&ordertable);
00875             uves_free_propertylist(&ordertable_header);
00876             uves_polynomial_delete(&order_locations);
00877             uves_free_table       (&traces);
00878             
00879             check( uves_load_ordertable(frames,
00880                         flames,
00881                         chip_name,
00882                         &ordertable_filename, 
00883                                                 &ordertable,
00884                         &ordertable_header, 
00885                                                 NULL,
00886                                                 &order_locations,
00887                         &traces, NULL, NULL,
00888                                        NULL, NULL, /* fibre_pos,fibre_mask */
00889                         chip,
00890                         false),
00891                "Could not load order table");
00892             uves_msg("Using order table in '%s'", ordertable_filename);
00893         }
00894         
00895         /* Process chip */
00896         uves_free_image(&master_flat);
00897         uves_free_image(&background);
00898         check( master_flat = uves_mflat_process_chip(
00899                raw_images[raw_index], raw_headers[raw_index],
00900                product_header[raw_index],
00901                master_bias,
00902                master_dark, master_dark_header,
00903                ordertable, order_locations,
00904                flames,
00905                parameters,
00906                chip,
00907                recipe_id,
00908                DEBUG,
00909                &background),
00910            "Error processing chip");
00911 
00912         /* Finished. Compute QC parameters and save */
00913         uves_msg("Computing QC parameters");
00914             uves_qclog_delete(&qclog[0]);
00915             qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00916 
00917             check(uves_mflat_qclog(raw_images[raw_index],
00918                    qclog[0]),"error computing qclog");
00919     
00920         uves_msg("Saving products");
00921     
00922         cpl_free(product_filename);
00923         check( product_filename = uves_masterflat_filename(chip), 
00924                "Error getting filename");
00925         strcpy(pro_filename,prefix);
00926         strcat(pro_filename,product_filename);
00927         check( uves_frameset_insert(
00928                    frames,
00929                    master_flat,
00930                    CPL_FRAME_GROUP_PRODUCT,
00931                    CPL_FRAME_TYPE_IMAGE,
00932                    CPL_FRAME_LEVEL_INTERMEDIATE,
00933                    pro_filename,
00934                    product_tag[raw_index],
00935                    raw_headers[raw_index][0],
00936                    product_header[raw_index],
00937                    NULL,
00938                    parameters,
00939                    recipe_id,
00940                    PACKAGE "/" PACKAGE_VERSION, qclog,
00941                    starttime, true, UVES_ALL_STATS),
00942                "Could not add master flat %s %s to frameset", 
00943                product_filename, product_tag[raw_index]);
00944         uves_msg("Master flat %s %s added to frameset", 
00945                  pro_filename, product_tag[raw_index]);
00946 
00947         /* Save background image */
00948         if (!flames)
00949         {
00950             cpl_free(product_filename);
00951             check( product_filename = uves_masterflat_bkg_filename(chip), 
00952                "Error getting filename");
00953             strcpy(pro_filename,prefix);
00954             strcat(pro_filename,product_filename);
00955             
00956             check( uves_frameset_insert(frames,
00957                         background,
00958                         CPL_FRAME_GROUP_PRODUCT,
00959                         CPL_FRAME_TYPE_IMAGE,
00960                         CPL_FRAME_LEVEL_INTERMEDIATE,
00961                         pro_filename,
00962                         UVES_BKG_FLAT(chip),
00963                         raw_headers[raw_index][0],
00964                         product_header[raw_index],
00965                         NULL,
00966                         parameters,
00967                         recipe_id,
00968                         PACKAGE "/" PACKAGE_VERSION, NULL,
00969                         starttime, false, 
00970                         CPL_STATS_MIN | CPL_STATS_MAX),
00971                "Could not add background image '%s' to frameset", 
00972                product_filename);
00973             uves_msg("Master flat background '%s' added to frameset", 
00974                  product_filename);
00975                 }
00976 
00977         /* Compute and save ratio MASTER_TFLAT / REF_TFLAT */
00978         if (strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0)
00979         {
00980             const char *ref_flat_filename;
00981 
00982             uves_free_image(&ref_flat);
00983             uves_free_propertylist(&ref_flat_header);
00984 
00985             check( uves_load_ref_flat(frames, chip_name, &ref_flat_filename, 
00986                           &ref_flat, &ref_flat_header, 
00987                           chip),
00988                "Error loading reference flat field");
00989             
00990             uves_msg("Using reference flat field in '%s'", ref_flat_filename);
00991 
00992             check( ratio = cpl_image_divide_create(master_flat, ref_flat),
00993                "Error computing ratio of master and reference flat");
00994 
00995             cpl_free(product_filename);
00996             check( product_filename = uves_flat_ratio_filename(chip), 
00997                "Error getting filename");
00998             
00999             check( uves_frameset_insert(frames,
01000                         ratio,
01001                         CPL_FRAME_GROUP_PRODUCT,
01002                         CPL_FRAME_TYPE_IMAGE,
01003                         CPL_FRAME_LEVEL_INTERMEDIATE,
01004                         product_filename,
01005                         UVES_RATIO_TFLAT(chip),
01006                         raw_headers[raw_index][0],
01007                         product_header[raw_index],
01008                         NULL,
01009                         parameters,
01010                         recipe_id,
01011                         PACKAGE "/" PACKAGE_VERSION,
01012                         qclog,
01013                         starttime, false, 
01014                         UVES_ALL_STATS),
01015                "Could not add ratio image '%s' to frameset", product_filename);
01016 
01017             uves_msg("Master flat ratio '%s' added to frameset", product_filename);
01018         }
01019 
01020 
01021       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
01022     chip = uves_chip_get_next(chip);
01023       }
01024 
01025     
01026     } /* For each chip */
01027     
01028   cleanup:
01029     /* Input */
01030     if (raw_images[0] != NULL)
01031     {
01032         int i;
01033         for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++) 
01034         {
01035             if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
01036             if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
01037         }
01038         cpl_free(raw_headers[0]); raw_headers[0] = NULL;
01039         cpl_free(raw_headers[1]); raw_headers[1] = NULL;
01040     }
01041 
01042     uves_free_imagelist(&raw_images[0]);
01043     uves_free_imagelist(&raw_images[1]);
01044 
01045     /* Master bias */
01046     uves_free_image(&master_bias);
01047     uves_free_propertylist(&master_bias_header);
01048 
01049     /* Master dark */
01050     uves_free_image(&master_dark);
01051     uves_free_propertylist(&master_dark_header);
01052 
01053     /* Order table */
01054     uves_free_table(&ordertable);
01055     uves_free_propertylist(&ordertable_header);
01056     uves_polynomial_delete(&order_locations);
01057     uves_free_table(&traces);
01058 
01059     /* Reference master flat */
01060     uves_free_image(&ref_flat);
01061     uves_free_propertylist(&ref_flat_header);
01062     
01063     /* Output */
01064     uves_qclog_delete(&qclog[0]);
01065     uves_free_image(&master_flat);
01066     uves_free_image(&background);
01067     uves_free_image(&ratio);
01068     uves_free_propertylist(&product_header[0]);
01069     uves_free_propertylist(&product_header[1]);
01070     cpl_free(product_filename);
01071     
01072     return;
01073 }
01074 
01075 
01076 /*----------------------------------------------------------------------------*/
01082 /*----------------------------------------------------------------------------*/
01083 
01084 static void
01085 uves_mflat_qclog(const cpl_imagelist* raw_images,
01086          cpl_table* qclog)
01087 {
01088   int nraw=0;
01089 
01090   check_nomsg(uves_qclog_add_string(qclog,
01091                         "QC TEST1 ID",
01092                         "Test-on-Master-Flat",
01093                         "Name of QC test",
01094                         "%s"));
01095   check_nomsg(nraw=cpl_imagelist_get_size(raw_images));
01096 
01097   check_nomsg(uves_qclog_add_int(qclog,
01098                         "PRO DATANCOM",
01099                         nraw,
01100                         "Number of frames combined",
01101                         "%d"));
01102  cleanup:
01103   return;
01104 }

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