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: 2013/02/12 10:52:26 $
00023  * $Revision: 1.51 $
00024  */
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 /*----------------------------------------------------------------------------*/
00035 /*----------------------------------------------------------------------------*/
00040 /*-----------------------------------------------------------------------------
00041                                 Includes
00042  -----------------------------------------------------------------------------*/
00043 #include <uves_reduce_mflat.h>
00044 
00045 #include <uves.h>
00046 #include <uves_backsub.h>
00047 #include <uves_chip.h>
00048 #include <uves_dfs.h>
00049 #include <uves_pfits.h>
00050 #include <uves_parameters.h>
00051 #include <uves_utils.h>
00052 #include <uves_utils_wrappers.h>
00053 #include <uves_qclog.h>
00054 #include <uves_error.h>
00055 #include <uves_msg.h>
00056 
00057 #include <cpl.h>
00058 #include <float.h>
00059 #include <string.h>
00060 /*-----------------------------------------------------------------------------
00061                             Functions prototypes
00062  -----------------------------------------------------------------------------*/
00063 
00064 static void uves_mflat_qclog(const cpl_imagelist* raw_images,
00065                      cpl_table* qclog);
00066 
00067 static cpl_error_code uves_msflats(cpl_frameset * set, const cpl_parameterlist *parameters,
00068                    const char *recipe_id,
00069                    const char *starttime);
00070 static void uves_mflat_one(cpl_frameset *frames,
00071                const cpl_parameterlist *parameters, 
00072                bool flames,
00073                const char *recipe_id,
00074                const char *starttime,
00075                const char* prefix);
00076 
00077 static cpl_error_code
00078 uves_mflat_at_ypos(cpl_frameset* set,
00079            const cpl_parameterlist* parameters,
00080            const char *recipe_id,
00081            const char *starttime,
00082                    const cpl_frameset* raw,
00083                    const cpl_frameset* cdb,
00084                    const int ref_x1enc,
00085                    const int ref_x2enc,
00086            const int set_no);
00087 
00088 static void uves_reduce_mflat(cpl_frameset *frames, const cpl_parameterlist *parameters,
00089                   bool flames,
00090                   const char *recipe_id, 
00091                   const char *starttime,
00092                   const char *prefix);
00093 
00094 /*-----------------------------------------------------------------------------
00095                             Implementation
00096  -----------------------------------------------------------------------------*/
00097 const char * const uves_mflat_desc =
00098 "This recipe creates a master flat frame by 1) subtracting the master bias\n"
00099 "frame from each flat field frame, 2) dividing each flat field frame by the\n"
00100 " exposure time for that frame, 3) taking the median of all bias subtracted,\n"
00101 " normalized raw\n flat frames, 4) optionally subtracting the master dark \n"
00102 "frame, and 5) subtracting\n the background to get the bias subtracted, \n"
00103 "optionally dark subtracted, normalized, background subtracted master \n"
00104 "flat-field frame. Symbolically,\n"
00105 " masterflat = median( (flat_i - masterbias)/exptime_i ) - masterdark/exptime\n"
00106 "            - background.\n"
00107 "\n"
00108 "The input flat field frames must have same tag which must match\n"
00109 "(I|D|S|T|SCREEN|)FLAT_(BLUE|RED), for example TFLAT_BLUE or FLAT_RED. Also, a\n"
00110 "master bias (MASTER_BIAS_xxxx) and ordertable (ORDER_TABLE_xxxx) must be\n"
00111 "provided for each chip (xxxx = BLUE, REDL, REDU). A master dark frame\n"
00112 "(MASTER_(P)DARK_xxxx) may optionally be provided. On blue input the recipe\n"
00113 "computes one master flat field frame; on red input the recipe produces a\n"
00114 "master flat field frame for each chip (MASTER_FLAT_xxxx, MASTER_IFLAT_xxxx,\n"
00115 "MASTER_DFLAT_xxxx, MASTER_TFLAT_xxxx or MASTER_SCREEN_FLAT_xxxx).";
00116 
00117 /*----------------------------------------------------------------------------*/
00124 /*----------------------------------------------------------------------------*/
00125 int
00126 uves_mflat_define_parameters_body(cpl_parameterlist *parameters, 
00127                   const char *recipe_id)
00128 {
00129 
00130    cpl_parameter* p=NULL;
00131    /*****************
00132     *    General    *
00133     *****************/
00134    if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00135    {
00136       return -1;
00137    }
00138    if (uves_corr_traps_define_parameters(parameters,recipe_id) 
00139        != CPL_ERROR_NONE)
00140    {
00141       return -1;
00142    }
00143 
00144     /**************************************
00145      *  Master stack generation           *
00146      **************************************/
00147     if (uves_master_flat_define_parameters(parameters,recipe_id) 
00148         != CPL_ERROR_NONE)
00149     {
00150             return -1;
00151     }
00152 
00153     if(strcmp(recipe_id,"flames_cal_mkmaster") ==0) {
00154        check_nomsg(p=cpl_parameterlist_find(parameters,"flames_cal_mkmaster.norm_method"));
00155        cpl_parameter_set_string(p,"exptime");
00156     }
00157   cleanup:
00158 /*
00159     if (uves_master_stack_define_parameters(parameters,recipe_id) 
00160         != CPL_ERROR_NONE)
00161         {
00162             return -1;
00163         }
00164 */
00165     
00166     /****************************
00167      *  Spline back.sub.        *
00168      ****************************/
00169     
00170     if (uves_propagate_parameters_step(UVES_BACKSUB_ID, parameters, 
00171                        recipe_id, NULL) != 0)
00172     {
00173         return -1;
00174     }
00175 
00176 
00177 
00178     return (cpl_error_get_code() != CPL_ERROR_NONE);
00179 }
00180 
00181 /*----------------------------------------------------------------------------*/
00219 /*----------------------------------------------------------------------------*/
00220 static cpl_image *
00221 uves_mflat_process_chip(const cpl_imagelist *raw_images, 
00222                         uves_propertylist **raw_headers, 
00223                         uves_propertylist *master_flat_header,
00224                         const cpl_image *master_bias,
00225                         const cpl_image *master_dark, 
00226                         const uves_propertylist *mdark_header, 
00227                         const cpl_table *ordertable, 
00228                         const polynomial *order_locations,
00229                         bool flames,
00230                         const cpl_parameterlist *parameters,
00231                         enum uves_chip chip,
00232                         const char *recipe_id,
00233                         bool DEBUG,
00234                         cpl_image **background)
00235 
00236 {
00237     cpl_image *master_flat        = NULL; /* Result */
00238     cpl_image *master_flat_tmp        = NULL; /* Result */
00239 
00240     cpl_image *current_flat       = NULL;
00241    
00242     int i;
00243     const char* FLAT_METHOD=NULL;
00244     cpl_vector* exptimes=NULL;
00245     cpl_vector* gain_vals=NULL;
00246     double mdark_exposure=0;
00247     cpl_image* mdark_scaled=NULL;
00248     double fnoise=0;
00249     double gain=0;
00250     cpl_imagelist *raw_images_local=NULL;
00251 
00252     /* First process each input image and store the results in a new image list */
00253    /* Get recipe parameters */
00254   check( uves_get_parameter(parameters, NULL, recipe_id, "norm_method", 
00255              CPL_TYPE_STRING, &FLAT_METHOD),
00256                "Could not read parameter");
00257     uves_string_toupper((char*)FLAT_METHOD);
00258     
00259     raw_images_local=(cpl_imagelist*) raw_images;
00260     exptimes=cpl_vector_new(cpl_imagelist_get_size(raw_images));
00261     gain_vals=cpl_vector_new(cpl_imagelist_get_size(raw_images));
00262     /* to remove compiler warnings */
00263     for (i = 0; i < cpl_imagelist_get_size(raw_images); i++)
00264     {
00265         double exposure_time = 0.0;
00266 
00267         const uves_propertylist *current_header = NULL;
00268 
00269         current_flat   = cpl_image_duplicate(cpl_imagelist_get_const(raw_images, i));
00270         current_header = raw_headers[i];
00271         
00272         /* Subtract master bias */
00273         if (master_bias != NULL)
00274         {
00275             uves_msg("Subtracting master bias");
00276             check( uves_subtract_bias(current_flat, master_bias), 
00277                "Error subtracting master bias");
00278         }
00279         else
00280         {
00281             uves_msg("Skipping bias subtraction");
00282         }
00283         
00284         /* Normalize to unit exposure time */
00285         check( exposure_time = uves_pfits_get_exptime(current_header), 
00286            "Error reading exposure time");
00287         check( gain = uves_pfits_get_gain(current_header,chip), 
00288            "Error reading gain value");
00289         
00290         uves_msg("Normalizing flat from %f s to unit exposure time", exposure_time);
00291         check( cpl_image_divide_scalar(current_flat, exposure_time),
00292            "Error normalizing flat field");
00293         check( uves_pfits_set_exptime(master_flat_header, 1.0),
00294            "Error writing master frame exposure time");
00295         cpl_vector_set(exptimes,i,exposure_time);
00296         cpl_vector_set(gain_vals,i,gain);
00297 
00298         /* Append to imagelist */
00299         check( cpl_imagelist_set(raw_images_local,     /* Image list */
00300                      current_flat,       /* Image to insert */
00301                      i),                 /* Position (number_of_images=>append) */
00302            "Could not insert image into image list");
00303         
00304         /* Don't deallocate the image. It will be deallocated when
00305            the image list is deallocated */
00306         current_flat = NULL;
00307     }
00308 
00309     /* subtract master dark if present */
00310     if (master_dark != NULL)
00311     {
00312         uves_msg("Subtracting master dark");
00313         check_nomsg( mdark_exposure = uves_pfits_get_exptime(mdark_header));
00314         mdark_scaled=cpl_image_duplicate(master_dark);
00315         cpl_image_divide_scalar(mdark_scaled,mdark_exposure); 
00316         check( cpl_imagelist_subtract_image(raw_images_local,mdark_scaled), 
00317            "Error subtracting master dark");
00318         uves_free_image(&mdark_scaled);
00319     }
00320     else
00321     {
00322         uves_msg("Skipping dark subtraction");
00323     }
00324 
00325     
00326     /* Take median of all input flats */
00327     if(strcmp(FLAT_METHOD,"EXPTIME")==0) {
00328        uves_msg("Calculating stack median");
00329        check(master_flat=cpl_imagelist_collapse_median_create(raw_images), 
00330               "Error computing median");
00331     } else {
00332        uves_msg("Calculating stack normalized master");
00333        check( master_flat_tmp = uves_flat_create_normalized_master(raw_images_local,
00334                                    ordertable,
00335                                    order_locations,gain_vals,&fnoise),
00336               "Error computing master flat with normalization");
00337 
00338       check( master_flat = uves_flat_create_normalized_master2(raw_images_local,
00339                                                                ordertable,
00340                                                               order_locations,
00341                                                                master_flat_tmp),
00342               "Error computing master flat with normalization");
00343       uves_free_image(&master_flat_tmp);
00344       uves_propertylist_append_c_double(master_flat_header,UVES_FNOISE,fnoise,
00345                                           "Master flat RMS on frame");
00346 
00347     }
00348     /*clean mem and reset local pointers to null */
00349     raw_images_local=NULL;
00350     uves_free_vector(&exptimes);
00351     uves_free_vector(&gain_vals);
00352 
00353     if (DEBUG && !flames)
00354     {
00355         check( uves_save_image_local("Pre-background subtracted master flat", "pre",
00356                                      master_flat, chip, -1, -1, master_flat_header, true), 
00357            "Error saving image");
00358     }
00359     
00360     /* Subtract background from master flat */
00361     if (!flames)
00362     {
00363         uves_msg("Subtracting background");
00364         
00365         check( uves_backsub_spline(master_flat, 
00366                        /* Info about chip (wavelength, ...) is 
00367                       stored in any raw header,
00368                       so just pass the first one   */
00369                        raw_headers[0],                
00370                        ordertable, order_locations, 
00371                        parameters, recipe_id,
00372                        chip,
00373                        true,     /* Use flat-field parameters? */
00374                        background),
00375            "Error subtracting background from master flat");
00376     }
00377     else
00378     {
00379         uves_msg("Skipping background subtraction");
00380     }
00381         
00382   cleanup:
00383 
00384 
00385     uves_free_image(&current_flat);
00386     if (cpl_error_get_code() != CPL_ERROR_NONE)
00387     {
00388         uves_free_image(&master_flat);
00389     }
00390     
00391     return master_flat;
00392 }
00393 
00394 /*----------------------------------------------------------------------------*/
00406 /*----------------------------------------------------------------------------*/
00407 void
00408 uves_mflat_exe_body(cpl_frameset *frames, 
00409                     const cpl_parameterlist *parameters,
00410                     const char *starttime,
00411                     const char *recipe_id)
00412 {
00413     /* Do FLAMES reduction if SFLAT frame is given */
00414     if (cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL) {
00415   
00416         check(uves_msflats(frames, parameters, recipe_id, starttime),
00417               "find same sflats failed");
00418     }
00419     else {
00420         bool flames = false;
00421         check(uves_mflat_one(frames, parameters, flames, recipe_id, 
00422                              starttime, ""),
00423               "Master flat one failed");
00424     }
00425     
00426  cleanup:
00427     return;
00428 }
00429 
00430 /*----------------------------------------------------------------------------*/
00436 /*----------------------------------------------------------------------------*/
00437 static cpl_error_code
00438 uves_msflats(cpl_frameset * set, const cpl_parameterlist *parameters,
00439          const char *recipe_id,
00440          const char *starttime)
00441 {
00442   /* Pseudocode:
00443      extract raw frames from set
00444      extract cdb frames from set
00445      identifies how many different Y position we have 
00446      for each Y pos:
00447          extract from the raw_set the raw frames corresponding to each Y pos 
00448          merge in a new wrk_set the cdb_set 
00449          computes the corresponding master flat 
00450          put the products in the final set 
00451      endfor 
00452   */
00453   cpl_frameset* raw=NULL;
00454   cpl_frameset* cdb=NULL;
00455   cpl_frameset* pro=NULL;
00456   int status=0;
00457   int x1enc=0;
00458   int x2enc=0;
00459 
00460   cpl_table* encoder_tbl=NULL;
00461   int nset=0;
00462   int i=0;
00463 
00464   //Extracts SFLAT raw frames 
00465   check(raw=uves_frameset_extract(set,UVES_SFLAT(false)),
00466                   "Extract %s frames failed",
00467                   UVES_SFLAT(false));
00468 
00469   check(uves_extract_frames_group_type(set,&cdb,CPL_FRAME_GROUP_CALIB),
00470     "Extract cdb frames failed");
00471   check(uves_sflats_get_encoder_steps(raw,&encoder_tbl,&nset),
00472     "Get encoder steps failed");
00473   uves_msg("Check Slit Flat Field Y nominal positions within each set");
00474   for(i=0;i<nset;i++) {
00475 
00476          uves_msg("Slit Flat field set %d: x1enc = %d x2enc = %d",
00477               i+1,
00478               cpl_table_get_int(encoder_tbl,"x1enc",i,&status),
00479               cpl_table_get_int(encoder_tbl,"x2enc",i,&status));
00480 
00481   }
00482 
00483   for(i=0;i<nset;i++) {
00484     x1enc=cpl_table_get_int(encoder_tbl,"x1enc",i,&status);
00485     x2enc=cpl_table_get_int(encoder_tbl,"x2enc",i,&status);
00486 
00487     uves_msg("Processing set %d", i+1);
00488    
00489     check(uves_mflat_at_ypos(set,parameters,recipe_id,starttime,raw,cdb,x1enc,x2enc,i+1),
00490       "Master flat one failed");
00491   }
00492 
00493   cleanup:
00494   uves_free_table(&encoder_tbl);
00495   uves_free_frameset(&raw);
00496   uves_free_frameset(&cdb);
00497   uves_free_frameset(&pro);
00498 
00499     return cpl_error_get_code();
00500 }
00501 
00502 
00503 /*----------------------------------------------------------------------------*/
00509 /*----------------------------------------------------------------------------*/
00510 static cpl_error_code
00511 uves_mflat_at_ypos(cpl_frameset* set,
00512            const cpl_parameterlist* parameters,
00513            const char *recipe_id,
00514            const char *starttime,
00515                    const cpl_frameset* raw,
00516                    const cpl_frameset* cdb,
00517                    const int ref_x1enc,
00518                    const int ref_x2enc,
00519                    const int set_no)
00520 {
00521 
00522     const cpl_frame* frm_tmp=NULL;
00523   char* file=NULL;
00524   uves_propertylist* plist=NULL;
00525   cpl_size i=0;
00526   const int threshold = 5;
00527   cpl_frame* frm_dup=NULL;
00528   cpl_frameset* tmp=NULL;
00529   cpl_frameset* pro=NULL;
00530   int x1enc=0;
00531   int x2enc=0;
00532   char prefix[255];
00533   bool flames = true;
00534 
00535   check_nomsg(tmp=cpl_frameset_new());
00536    for(i=0;i<cpl_frameset_get_size(raw);i++)
00537     {
00538     check_nomsg(frm_tmp=cpl_frameset_get_frame_const(raw,i));
00539     check_nomsg(file=cpl_strdup(cpl_frame_get_filename(frm_tmp)));
00540     check_nomsg(plist=uves_propertylist_load(file,0));
00541     check_nomsg(x1enc=uves_pfits_get_slit3_x1encoder(plist));
00542     check_nomsg(x2enc=uves_pfits_get_slit3_x2encoder(plist));
00543     
00544     if( (fabs(x1enc - ref_x1enc) <= threshold) &&
00545         (fabs(x2enc - ref_x2enc) <= threshold) ) {
00546       uves_msg_debug("file=%s x1enc=%d x2enc=%d",file,x1enc,x2enc);
00547       check(frm_dup = cpl_frame_duplicate(frm_tmp),"duplicate");
00548       check(cpl_frameset_insert(tmp,frm_dup),"insert");
00549     }
00550     cpl_free(file);
00551     uves_free_propertylist(&plist);
00552     }
00553    check_nomsg(uves_frameset_merge(tmp,cdb));
00554    sprintf(prefix,"%s%d%s","set",set_no,"_");
00555    check(uves_mflat_one(tmp,parameters, flames, recipe_id, starttime,prefix),"Master flat one failed");
00556    check_nomsg(uves_extract_frames_group_type(tmp,&pro,CPL_FRAME_GROUP_PRODUCT));
00557    check_nomsg(uves_frameset_merge(set,pro));
00558 
00559  cleanup:
00560    uves_free_frameset(&tmp);
00561    uves_free_frameset(&pro);
00562    uves_free_propertylist(&plist);
00563   
00564     return cpl_error_get_code();
00565 }
00566 /*----------------------------------------------------------------------------*/
00579 /*----------------------------------------------------------------------------*/
00580 static void
00581 uves_mflat_one(cpl_frameset *frames,
00582            const cpl_parameterlist *parameters, 
00583            bool flames,
00584            const char *recipe_id,
00585            const char *starttime,
00586                const char* prefix)
00587 {
00588     /* Do flat-fielding */
00589     check_nomsg( uves_reduce_mflat(frames, parameters, 
00590                    flames, recipe_id,
00591                    starttime, prefix) );
00592 
00593   cleanup:
00594     return;
00595 }
00596 
00597 /*----------------------------------------------------------------------------*/
00608 /*----------------------------------------------------------------------------*/
00609 static void
00610 uves_reduce_mflat(cpl_frameset *frames, const cpl_parameterlist *parameters,
00611           bool flames,
00612           const char *recipe_id, 
00613           const char *starttime,
00614           const char *prefix)
00615 {
00616     bool DEBUG;
00617 
00618     /* Input */
00619     cpl_imagelist       *raw_images[2] = {NULL, NULL}; /* An image list for both chips */
00620     uves_propertylist  **raw_headers[2] = {NULL, NULL}; /* Two arrays of pointers */
00621 
00622     /* Master bias */
00623     cpl_image *master_bias               = NULL;
00624     uves_propertylist *master_bias_header = NULL;
00625 
00626     /* Master dark */
00627     cpl_image *master_dark               = NULL;
00628     uves_propertylist *master_dark_header = NULL;
00629 
00630     /* Order table */
00631     cpl_table        *ordertable            = NULL;
00632     uves_propertylist *ordertable_header     = NULL;
00633     polynomial       *order_locations       = NULL;
00634     cpl_table        *traces                = NULL;
00635     
00636     /* Reference master flat */
00637     cpl_image        *ref_flat              = NULL;
00638     uves_propertylist *ref_flat_header       = NULL;
00639 
00640     /* Output */
00641     cpl_table *qclog[]                  = {NULL, NULL};
00642     cpl_image *master_flat              = NULL;
00643     cpl_image *background               = NULL;
00644     uves_propertylist *product_header[]  = {NULL, NULL};
00645     cpl_image *ratio                    = NULL;
00646     
00647     /* Local variables */
00648     char *product_filename = NULL;
00649     char pro_filename[255];
00650     const char *product_tag[2] = {NULL, NULL};
00651     bool blue;
00652     enum uves_chip chip;
00653     const char* PROCESS_CHIP=NULL;
00654 
00655 
00656 
00657 
00658     const char *ordertable_filename = "";
00659     const char *master_bias_filename = "";
00660     const char *master_dark_filename = "";
00661     const char *chip_name = "";
00662     int raw_index = 0;
00663     const char *ref_flat_filename;
00664 
00665     /* Read recipe parameters */
00666     {
00667     /* General */
00668     check( uves_get_parameter(parameters, NULL, "uves", "debug", CPL_TYPE_BOOL  , &DEBUG ), 
00669            "Could not read parameter");
00670     }
00671     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP), "Could not read parameter");
00672 
00673     uves_string_toupper((char*)PROCESS_CHIP);
00674 
00675     /* Load and check raw flat images and headers, identify arm (blue/red) */
00676     /* On success, 'raw_headers' will be an array with the same size as 'raw_images' */
00677     /* Set product tags to match input tag */
00678 
00679     if (cpl_frameset_find(frames, UVES_FLAT(true )) != NULL ||
00680     cpl_frameset_find(frames, UVES_FLAT(false)) != NULL)
00681     {
00682         check( uves_load_raw_imagelist(frames, 
00683                        flames,
00684                        UVES_FLAT(true), UVES_FLAT(false),
00685                        CPL_TYPE_DOUBLE,
00686                        raw_images, raw_headers, product_header, 
00687                        &blue), "Error loading raw flat frames");
00688         
00689         for (chip = uves_chip_get_first(blue); chip != UVES_CHIP_INVALID; 
00690          chip = uves_chip_get_next(chip))
00691         {
00692             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_FLAT(chip);
00693         }
00694     }
00695     else if (cpl_frameset_find(frames, UVES_DFLAT(true )) != NULL ||
00696          cpl_frameset_find(frames, UVES_DFLAT(false)) != NULL)
00697     {
00698         check( uves_load_raw_imagelist(frames, 
00699                        flames,
00700                        UVES_DFLAT(true), UVES_DFLAT(false),
00701                        CPL_TYPE_DOUBLE,
00702                        raw_images, raw_headers, product_header, 
00703                        &blue), "Error loading raw flat frames");
00704         for (chip = uves_chip_get_first(blue); 
00705          chip != UVES_CHIP_INVALID; 
00706          chip = uves_chip_get_next(chip))
00707         {
00708             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_DFLAT(chip);
00709         }
00710     }
00711     else if  (cpl_frameset_find(frames, UVES_IFLAT(true )) != NULL ||
00712           cpl_frameset_find(frames, UVES_IFLAT(false)) != NULL)
00713     {
00714         check( uves_load_raw_imagelist(frames, 
00715                        flames,
00716                        UVES_IFLAT(true), UVES_IFLAT(false),
00717                        CPL_TYPE_DOUBLE,
00718                        raw_images, raw_headers, product_header, 
00719                        &blue), "Error loading raw flat frames");
00720         for (chip = uves_chip_get_first(blue); 
00721          chip != UVES_CHIP_INVALID; 
00722          chip = uves_chip_get_next(chip))
00723         {
00724             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_IFLAT(chip);
00725         }
00726     }
00727     else if  (cpl_frameset_find(frames, UVES_TFLAT(true )) != NULL ||
00728           cpl_frameset_find(frames, UVES_TFLAT(false)) != NULL)
00729     {
00730         check( uves_load_raw_imagelist(frames, 
00731                        flames,
00732                        UVES_TFLAT(true), UVES_TFLAT(false),
00733                        CPL_TYPE_DOUBLE,
00734                        raw_images, raw_headers, product_header, 
00735                        &blue), "Error loading raw flat frames");
00736         for (chip = uves_chip_get_first(blue); 
00737          chip != UVES_CHIP_INVALID; 
00738          chip = uves_chip_get_next(chip))
00739         {
00740             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_TFLAT(chip);
00741         }
00742     }
00743     else if  (cpl_frameset_find(frames, UVES_SCREEN_FLAT(true )) != NULL ||
00744           cpl_frameset_find(frames, UVES_SCREEN_FLAT(false)) != NULL)
00745     {
00746         check( uves_load_raw_imagelist(frames, 
00747                        flames,
00748                        UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00749                        CPL_TYPE_DOUBLE,
00750                        raw_images, raw_headers, product_header, 
00751                        &blue), "Error loading raw flat frames");
00752         for (chip = uves_chip_get_first(blue); 
00753          chip != UVES_CHIP_INVALID; 
00754          chip = uves_chip_get_next(chip))
00755         {
00756             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SCREEN_FLAT(chip);
00757         }
00758     }
00759     else if  (cpl_frameset_find(frames, UVES_SFLAT(true )) != NULL ||
00760           cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL)
00761     {
00762         check( uves_load_raw_imagelist(frames, 
00763                        flames,
00764                        UVES_SFLAT(true), UVES_SFLAT(false),
00765                        CPL_TYPE_DOUBLE,
00766                        raw_images, raw_headers, product_header, 
00767                        &blue), "Error loading raw flat frames");
00768         for (chip = uves_chip_get_first(blue); 
00769          chip != UVES_CHIP_INVALID; 
00770          chip = uves_chip_get_next(chip))
00771         {
00772             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SFLAT(chip);
00773         }
00774     }
00775     else 
00776     {
00777         assure(false, CPL_ERROR_DATA_NOT_FOUND,
00778            "Missing input flat frame: "
00779            "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s or %s expected",
00780            UVES_FLAT(true) , UVES_FLAT(false),
00781            UVES_DFLAT(true), UVES_DFLAT(false),
00782            UVES_IFLAT(true), UVES_IFLAT(false),
00783            UVES_TFLAT(true), UVES_TFLAT(false),
00784            UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00785            UVES_SFLAT(true), UVES_SFLAT(false));
00786     }
00787 
00788     /* Loop over one or two chips */
00789     for (chip = uves_chip_get_first(blue); 
00790      chip != UVES_CHIP_INVALID; 
00791      chip = uves_chip_get_next(chip))
00792     {
00793 
00794       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00795     chip = uves_chip_get_next(chip);
00796       }
00797         
00798         raw_index = uves_chip_get_index(chip);
00799         
00800         uves_msg("Processing %s chip", uves_chip_tostring_upper(chip));
00801 
00802         /* Chip name of first input frame */
00803         check_nomsg( chip_name = uves_pfits_get_chipid(raw_headers[raw_index][0], chip));
00804 
00805         /* Load master bias, set pointer to NULL if not present */
00806         uves_free_image(&master_bias);
00807         uves_free_propertylist(&master_bias_header);
00808         if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00809         {
00810             uves_free_image(&master_bias);
00811             uves_free_propertylist(&master_bias_header);
00812             check( uves_load_mbias(frames,
00813                        chip_name,
00814                        &master_bias_filename, &master_bias,
00815                        &master_bias_header, chip), 
00816                "Error loading master bias");
00817             
00818             uves_msg_low("Using master bias in '%s'", master_bias_filename);
00819         }
00820         else
00821         {
00822             uves_msg_low("No master bias in SOF. Bias subtraction not done");
00823         }
00824     
00825         /* Load master dark, set pointer to NULL if not present */
00826         uves_free_image(&master_dark);
00827         uves_free_propertylist(&master_dark_header);
00828         if (cpl_frameset_find(frames, UVES_MASTER_DARK(chip))  != NULL ||
00829         cpl_frameset_find(frames, UVES_MASTER_PDARK(chip)) != NULL)
00830         {
00831             uves_free_image(&master_dark);
00832             uves_free_propertylist(&master_dark_header);
00833             check( uves_load_mdark(frames, chip_name,
00834                        &master_dark_filename, &master_dark,
00835                        &master_dark_header, chip), 
00836                "Error loading master dark");
00837             
00838             uves_msg_low("Using master dark in '%s'", master_dark_filename);
00839         }
00840         else
00841         {
00842             uves_msg_low("No master dark in SOF. Dark subtraction not done");
00843         }
00844     
00845         /* Load the order table for this chip */
00846         if (flames)
00847         /* FLAMES does not do background subtraction (here)
00848            and therefore does not need an ordertable */
00849         {
00850             if (cpl_frameset_find(frames, UVES_ORDER_TABLE(flames, chip)) != NULL)
00851             {
00852                 uves_msg_warning("Order table (%s) is not used in FLAMES reduction",
00853                          UVES_ORDER_TABLE(flames, chip));
00854             }
00855         }
00856         else
00857         {
00858             uves_free_table       (&ordertable);
00859             uves_free_propertylist(&ordertable_header);
00860             uves_polynomial_delete(&order_locations);
00861             uves_free_table       (&traces);
00862             
00863             check( uves_load_ordertable(frames,
00864                         flames,
00865                         chip_name,
00866                         &ordertable_filename, 
00867                                                 &ordertable,
00868                         &ordertable_header, 
00869                                                 NULL,
00870                                                 &order_locations,
00871                         &traces, NULL, NULL,
00872                                        NULL, NULL, /* fibre_pos,fibre_mask */
00873                         chip,
00874                         false),
00875                "Could not load order table");
00876             uves_msg("Using order table in '%s'", ordertable_filename);
00877         }
00878 
00879         /* Compute QC parameters and save */
00880         uves_msg("Computing QC parameters");
00881             uves_qclog_delete(&qclog[0]);
00882             qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00883 
00884             check(uves_mflat_qclog(raw_images[raw_index],
00885                    qclog[0]),"error computing qclog");
00886     
00887 
00888        
00889         /* Process chip */
00890         uves_free_image(&master_flat);
00891         uves_free_image(&background);
00892         check( master_flat = uves_mflat_process_chip(
00893                raw_images[raw_index], raw_headers[raw_index],
00894                product_header[raw_index],
00895                master_bias,
00896                master_dark, master_dark_header,
00897                ordertable, order_locations,
00898                flames,
00899                parameters,
00900                chip,
00901                recipe_id,
00902                DEBUG,
00903                &background),
00904            "Error processing chip");
00905 
00906          /* Finished. Save */ 
00907         uves_msg("Saving products");
00908     
00909         cpl_free(product_filename);
00910         check( product_filename = uves_masterflat_filename(chip), 
00911                "Error getting filename");
00912         strcpy(pro_filename,prefix);
00913         strcat(pro_filename,product_filename);
00914         check( uves_frameset_insert(
00915                    frames,
00916                    master_flat,
00917                    CPL_FRAME_GROUP_PRODUCT,
00918                    CPL_FRAME_TYPE_IMAGE,
00919                    CPL_FRAME_LEVEL_INTERMEDIATE,
00920                    pro_filename,
00921                    product_tag[raw_index],
00922                    raw_headers[raw_index][0],
00923                    product_header[raw_index],
00924                    NULL,
00925                    parameters,
00926                    recipe_id,
00927                    PACKAGE "/" PACKAGE_VERSION, qclog,
00928                    starttime, true, UVES_ALL_STATS),
00929                "Could not add master flat %s %s to frameset", 
00930                product_filename, product_tag[raw_index]);
00931         uves_msg("Master flat %s %s added to frameset", 
00932                  pro_filename, product_tag[raw_index]);
00933 
00934         /* Save background image */
00935         if (!flames)
00936         {
00937             cpl_free(product_filename);
00938             check( product_filename = uves_masterflat_bkg_filename(chip), 
00939                "Error getting filename");
00940             strcpy(pro_filename,prefix);
00941             strcat(pro_filename,product_filename);
00942             
00943             check( uves_frameset_insert(frames,
00944                         background,
00945                         CPL_FRAME_GROUP_PRODUCT,
00946                         CPL_FRAME_TYPE_IMAGE,
00947                         CPL_FRAME_LEVEL_INTERMEDIATE,
00948                         pro_filename,
00949                         UVES_BKG_FLAT(chip),
00950                         raw_headers[raw_index][0],
00951                         product_header[raw_index],
00952                         NULL,
00953                         parameters,
00954                         recipe_id,
00955                         PACKAGE "/" PACKAGE_VERSION, NULL,
00956                         starttime, false, 
00957                         CPL_STATS_MIN | CPL_STATS_MAX),
00958                "Could not add background image '%s' to frameset", 
00959                product_filename);
00960             uves_msg("Master flat background '%s' added to frameset", 
00961                  product_filename);
00962                 }
00963 
00964         /* Compute and save ratio MASTER_TFLAT / REF_TFLAT */
00965         if (strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0)
00966         {
00967 
00968             uves_free_image(&ref_flat);
00969             uves_free_propertylist(&ref_flat_header);
00970 
00971             check( uves_load_ref_flat(frames, chip_name, &ref_flat_filename, 
00972                           &ref_flat, &ref_flat_header, 
00973                           chip),
00974                "Error loading reference flat field");
00975             
00976             uves_msg("Using reference flat field in '%s'", ref_flat_filename);
00977 
00978             check( ratio = cpl_image_divide_create(master_flat, ref_flat),
00979                "Error computing ratio of master and reference flat");
00980 
00981             cpl_free(product_filename);
00982             check( product_filename = uves_flat_ratio_filename(chip), 
00983                "Error getting filename");
00984             
00985             check( uves_frameset_insert(frames,
00986                         ratio,
00987                         CPL_FRAME_GROUP_PRODUCT,
00988                         CPL_FRAME_TYPE_IMAGE,
00989                         CPL_FRAME_LEVEL_INTERMEDIATE,
00990                         product_filename,
00991                         UVES_RATIO_TFLAT(chip),
00992                         raw_headers[raw_index][0],
00993                         product_header[raw_index],
00994                         NULL,
00995                         parameters,
00996                         recipe_id,
00997                         PACKAGE "/" PACKAGE_VERSION,
00998                         qclog,
00999                         starttime, false, 
01000                         UVES_ALL_STATS),
01001                "Could not add ratio image '%s' to frameset", product_filename);
01002 
01003             uves_msg("Master flat ratio '%s' added to frameset", product_filename);
01004         }
01005 
01006 
01007       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
01008     chip = uves_chip_get_next(chip);
01009       }
01010 
01011     
01012     } /* For each chip */
01013     
01014   cleanup:
01015     /* Input */
01016     if (raw_images[0] != NULL)
01017     {
01018         int i;
01019         for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++) 
01020         {
01021             if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
01022             if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
01023         }
01024         cpl_free(raw_headers[0]); raw_headers[0] = NULL;
01025         cpl_free(raw_headers[1]); raw_headers[1] = NULL;
01026     }
01027 
01028     uves_free_imagelist(&raw_images[0]);
01029     uves_free_imagelist(&raw_images[1]);
01030 
01031     /* Master bias */
01032     uves_free_image(&master_bias);
01033     uves_free_propertylist(&master_bias_header);
01034 
01035     /* Master dark */
01036     uves_free_image(&master_dark);
01037     uves_free_propertylist(&master_dark_header);
01038 
01039     /* Order table */
01040     uves_free_table(&ordertable);
01041     uves_free_propertylist(&ordertable_header);
01042     uves_polynomial_delete(&order_locations);
01043     uves_free_table(&traces);
01044 
01045     /* Reference master flat */
01046     uves_free_image(&ref_flat);
01047     uves_free_propertylist(&ref_flat_header);
01048     
01049     /* Output */
01050     uves_qclog_delete(&qclog[0]);
01051     uves_free_image(&master_flat);
01052     uves_free_image(&background);
01053     uves_free_image(&ratio);
01054     uves_free_propertylist(&product_header[0]);
01055     uves_free_propertylist(&product_header[1]);
01056     cpl_free(product_filename);
01057     
01058     return;
01059 }
01060 
01061 
01062 /*----------------------------------------------------------------------------*/
01068 /*----------------------------------------------------------------------------*/
01069 
01070 static void
01071 uves_mflat_qclog(const cpl_imagelist* raw_images,
01072          cpl_table* qclog)
01073 {
01074   int nraw=0;
01075 
01076   check_nomsg(uves_qclog_add_string(qclog,
01077                         "QC TEST1 ID",
01078                         "Test-on-Master-Flat",
01079                         "Name of QC test",
01080                         "%s"));
01081   check_nomsg(nraw=cpl_imagelist_get_size(raw_images));
01082 
01083   check_nomsg(uves_qclog_add_int(qclog,
01084                         "PRO DATANCOM",
01085                         nraw,
01086                         "Number of frames combined",
01087                         "%d"));
01088  cleanup:
01089   return;
01090 }

Generated on 3 Mar 2013 for UVES Pipeline Reference Manual by  doxygen 1.6.1