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: 2010/12/16 16:57:40 $
00023  * $Revision: 1.49 $
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                                                                exptimes),
00343               "Error computing master flat with normalization");
00344       uves_free_image(&master_flat_tmp);
00345       uves_propertylist_append_c_double(master_flat_header,UVES_FNOISE,fnoise,
00346                                           "Master flat RMS on frame");
00347 
00348     }
00349     /*clean mem and reset local pointers to null */
00350     raw_images_local=NULL;
00351     uves_free_vector(&exptimes);
00352     uves_free_vector(&gain_vals);
00353 
00354     if (DEBUG && !flames)
00355     {
00356         check( uves_save_image_local("Pre-background subtracted master flat", "pre",
00357                                      master_flat, chip, -1, -1, master_flat_header, true), 
00358            "Error saving image");
00359     }
00360     
00361     /* Subtract background from master flat */
00362     if (!flames)
00363     {
00364         uves_msg("Subtracting background");
00365         
00366         check( uves_backsub_spline(master_flat, 
00367                        /* Info about chip (wavelength, ...) is 
00368                       stored in any raw header,
00369                       so just pass the first one   */
00370                        raw_headers[0],                
00371                        ordertable, order_locations, 
00372                        parameters, recipe_id,
00373                        chip,
00374                        true,     /* Use flat-field parameters? */
00375                        background),
00376            "Error subtracting background from master flat");
00377     }
00378     else
00379     {
00380         uves_msg("Skipping background subtraction");
00381     }
00382         
00383   cleanup:
00384 
00385 
00386     uves_free_image(&current_flat);
00387     if (cpl_error_get_code() != CPL_ERROR_NONE)
00388     {
00389         uves_free_image(&master_flat);
00390     }
00391     
00392     return master_flat;
00393 }
00394 
00395 /*----------------------------------------------------------------------------*/
00407 /*----------------------------------------------------------------------------*/
00408 void
00409 uves_mflat_exe_body(cpl_frameset *frames, 
00410                     const cpl_parameterlist *parameters,
00411                     const char *starttime,
00412                     const char *recipe_id)
00413 {
00414     /* Do FLAMES reduction if SFLAT frame is given */
00415     if (cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL) {
00416   
00417         check(uves_msflats(frames, parameters, recipe_id, starttime),
00418               "find same sflats failed");
00419     }
00420     else {
00421         bool flames = false;
00422         check(uves_mflat_one(frames, parameters, flames, recipe_id, 
00423                              starttime, ""),
00424               "Master flat one failed");
00425     }
00426     
00427  cleanup:
00428     return;
00429 }
00430 
00431 /*----------------------------------------------------------------------------*/
00437 /*----------------------------------------------------------------------------*/
00438 static cpl_error_code
00439 uves_msflats(cpl_frameset * set, const cpl_parameterlist *parameters,
00440          const char *recipe_id,
00441          const char *starttime)
00442 {
00443   /* Pseudocode:
00444      extract raw frames from set
00445      extract cdb frames from set
00446      identifies how many different Y position we have 
00447      for each Y pos:
00448          extract from the raw_set the raw frames corresponding to each Y pos 
00449          merge in a new wrk_set the cdb_set 
00450          computes the corresponding master flat 
00451          put the products in the final set 
00452      endfor 
00453   */
00454   cpl_frameset* raw=NULL;
00455   cpl_frameset* cdb=NULL;
00456   cpl_frameset* pro=NULL;
00457   int status=0;
00458   int x1enc=0;
00459   int x2enc=0;
00460 
00461   cpl_table* encoder_tbl=NULL;
00462   int nset=0;
00463   int i=0;
00464 
00465   //Extracts SFLAT raw frames 
00466   check(raw=uves_frameset_extract(set,UVES_SFLAT(false)),
00467                   "Extract %s frames failed",
00468                   UVES_SFLAT(false));
00469 
00470   check(uves_extract_frames_group_type(set,&cdb,CPL_FRAME_GROUP_CALIB),
00471     "Extract cdb frames failed");
00472   check(uves_sflats_get_encoder_steps(raw,&encoder_tbl,&nset),
00473     "Get encoder steps failed");
00474   uves_msg("Check Slit Flat Field Y nominal positions within each set");
00475   for(i=0;i<nset;i++) {
00476 
00477          uves_msg("Slit Flat field set %d: x1enc = %d x2enc = %d",
00478               i+1,
00479               cpl_table_get_int(encoder_tbl,"x1enc",i,&status),
00480               cpl_table_get_int(encoder_tbl,"x2enc",i,&status));
00481 
00482   }
00483 
00484   for(i=0;i<nset;i++) {
00485     x1enc=cpl_table_get_int(encoder_tbl,"x1enc",i,&status);
00486     x2enc=cpl_table_get_int(encoder_tbl,"x2enc",i,&status);
00487 
00488     uves_msg("Processing set %d", i+1);
00489    
00490     check(uves_mflat_at_ypos(set,parameters,recipe_id,starttime,raw,cdb,x1enc,x2enc,i+1),
00491       "Master flat one failed");
00492   }
00493 
00494   cleanup:
00495   uves_free_table(&encoder_tbl);
00496   uves_free_frameset(&raw);
00497   uves_free_frameset(&cdb);
00498   uves_free_frameset(&pro);
00499 
00500     return cpl_error_get_code();
00501 }
00502 
00503 
00504 /*----------------------------------------------------------------------------*/
00510 /*----------------------------------------------------------------------------*/
00511 static cpl_error_code
00512 uves_mflat_at_ypos(cpl_frameset* set,
00513            const cpl_parameterlist* parameters,
00514            const char *recipe_id,
00515            const char *starttime,
00516                    const cpl_frameset* raw,
00517                    const cpl_frameset* cdb,
00518                    const int ref_x1enc,
00519                    const int ref_x2enc,
00520                    const int set_no)
00521 {
00522 
00523     const cpl_frame* frm_tmp=NULL;
00524   char* file=NULL;
00525   uves_propertylist* plist=NULL;
00526   int i=0;
00527   const int threshold = 5;
00528   cpl_frame* frm_dup=NULL;
00529   cpl_frameset* tmp=NULL;
00530   cpl_frameset* pro=NULL;
00531   int x1enc=0;
00532   int x2enc=0;
00533   char prefix[255];
00534   bool flames = true;
00535 
00536   check_nomsg(tmp=cpl_frameset_new());
00537    for(i=0;i<cpl_frameset_get_size(raw);i++)
00538     {
00539     check_nomsg(frm_tmp=cpl_frameset_get_frame_const(raw,i));
00540     check_nomsg(file=cpl_strdup(cpl_frame_get_filename(frm_tmp)));
00541     check_nomsg(plist=uves_propertylist_load(file,0));
00542     check_nomsg(x1enc=uves_pfits_get_slit3_x1encoder(plist));
00543     check_nomsg(x2enc=uves_pfits_get_slit3_x2encoder(plist));
00544     
00545     if( (fabs(x1enc - ref_x1enc) <= threshold) &&
00546         (fabs(x2enc - ref_x2enc) <= threshold) ) {
00547       uves_msg_debug("file=%s x1enc=%d x2enc=%d",file,x1enc,x2enc);
00548       check(frm_dup = cpl_frame_duplicate(frm_tmp),"duplicate");
00549       check(cpl_frameset_insert(tmp,frm_dup),"insert");
00550     }
00551     cpl_free(file);
00552     uves_free_propertylist(&plist);
00553     }
00554    check_nomsg(uves_frameset_merge(tmp,cdb));
00555    sprintf(prefix,"%s%d%s","set",set_no,"_");
00556    check(uves_mflat_one(tmp,parameters, flames, recipe_id, starttime,prefix),"Master flat one failed");
00557    check_nomsg(uves_extract_frames_group_type(tmp,&pro,CPL_FRAME_GROUP_PRODUCT));
00558    check_nomsg(uves_frameset_merge(set,pro));
00559 
00560  cleanup:
00561    uves_free_frameset(&tmp);
00562    uves_free_frameset(&pro);
00563    uves_free_propertylist(&plist);
00564   
00565     return cpl_error_get_code();
00566 }
00567 /*----------------------------------------------------------------------------*/
00580 /*----------------------------------------------------------------------------*/
00581 static void
00582 uves_mflat_one(cpl_frameset *frames,
00583            const cpl_parameterlist *parameters, 
00584            bool flames,
00585            const char *recipe_id,
00586            const char *starttime,
00587                const char* prefix)
00588 {
00589     /* Do flat-fielding */
00590     check_nomsg( uves_reduce_mflat(frames, parameters, 
00591                    flames, recipe_id,
00592                    starttime, prefix) );
00593 
00594   cleanup:
00595     return;
00596 }
00597 
00598 /*----------------------------------------------------------------------------*/
00609 /*----------------------------------------------------------------------------*/
00610 static void
00611 uves_reduce_mflat(cpl_frameset *frames, const cpl_parameterlist *parameters,
00612           bool flames,
00613           const char *recipe_id, 
00614           const char *starttime,
00615           const char *prefix)
00616 {
00617     bool DEBUG;
00618 
00619     /* Input */
00620     cpl_imagelist       *raw_images[2] = {NULL, NULL}; /* An image list for both chips */
00621     uves_propertylist  **raw_headers[2] = {NULL, NULL}; /* Two arrays of pointers */
00622 
00623     /* Master bias */
00624     cpl_image *master_bias               = NULL;
00625     uves_propertylist *master_bias_header = NULL;
00626 
00627     /* Master dark */
00628     cpl_image *master_dark               = NULL;
00629     uves_propertylist *master_dark_header = NULL;
00630 
00631     /* Order table */
00632     cpl_table        *ordertable            = NULL;
00633     uves_propertylist *ordertable_header     = NULL;
00634     polynomial       *order_locations       = NULL;
00635     cpl_table        *traces                = NULL;
00636     
00637     /* Reference master flat */
00638     cpl_image        *ref_flat              = NULL;
00639     uves_propertylist *ref_flat_header       = NULL;
00640 
00641     /* Output */
00642     cpl_table *qclog[]                  = {NULL, NULL};
00643     cpl_image *master_flat              = NULL;
00644     cpl_image *background               = NULL;
00645     uves_propertylist *product_header[]  = {NULL, NULL};
00646     cpl_image *ratio                    = NULL;
00647     
00648     /* Local variables */
00649     char *product_filename = NULL;
00650     char pro_filename[255];
00651     const char *product_tag[2] = {NULL, NULL};
00652     bool blue;
00653     enum uves_chip chip;
00654     const char* PROCESS_CHIP=NULL;
00655 
00656 
00657 
00658 
00659     const char *ordertable_filename = "";
00660     const char *master_bias_filename = "";
00661     const char *master_dark_filename = "";
00662     const char *chip_name = "";
00663     int raw_index = 0;
00664     const char *ref_flat_filename;
00665 
00666     /* Read recipe parameters */
00667     {
00668     /* General */
00669     check( uves_get_parameter(parameters, NULL, "uves", "debug", CPL_TYPE_BOOL  , &DEBUG ), 
00670            "Could not read parameter");
00671     }
00672     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP), "Could not read parameter");
00673 
00674     uves_string_toupper((char*)PROCESS_CHIP);
00675 
00676     /* Load and check raw flat images and headers, identify arm (blue/red) */
00677     /* On success, 'raw_headers' will be an array with the same size as 'raw_images' */
00678     /* Set product tags to match input tag */
00679 
00680     if (cpl_frameset_find(frames, UVES_FLAT(true )) != NULL ||
00681     cpl_frameset_find(frames, UVES_FLAT(false)) != NULL)
00682     {
00683         check( uves_load_raw_imagelist(frames, 
00684                        flames,
00685                        UVES_FLAT(true), UVES_FLAT(false),
00686                        CPL_TYPE_DOUBLE,
00687                        raw_images, raw_headers, product_header, 
00688                        &blue), "Error loading raw flat frames");
00689         
00690         for (chip = uves_chip_get_first(blue); chip != UVES_CHIP_INVALID; 
00691          chip = uves_chip_get_next(chip))
00692         {
00693             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_FLAT(chip);
00694         }
00695     }
00696     else if (cpl_frameset_find(frames, UVES_DFLAT(true )) != NULL ||
00697          cpl_frameset_find(frames, UVES_DFLAT(false)) != NULL)
00698     {
00699         check( uves_load_raw_imagelist(frames, 
00700                        flames,
00701                        UVES_DFLAT(true), UVES_DFLAT(false),
00702                        CPL_TYPE_DOUBLE,
00703                        raw_images, raw_headers, product_header, 
00704                        &blue), "Error loading raw flat frames");
00705         for (chip = uves_chip_get_first(blue); 
00706          chip != UVES_CHIP_INVALID; 
00707          chip = uves_chip_get_next(chip))
00708         {
00709             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_DFLAT(chip);
00710         }
00711     }
00712     else if  (cpl_frameset_find(frames, UVES_IFLAT(true )) != NULL ||
00713           cpl_frameset_find(frames, UVES_IFLAT(false)) != NULL)
00714     {
00715         check( uves_load_raw_imagelist(frames, 
00716                        flames,
00717                        UVES_IFLAT(true), UVES_IFLAT(false),
00718                        CPL_TYPE_DOUBLE,
00719                        raw_images, raw_headers, product_header, 
00720                        &blue), "Error loading raw flat frames");
00721         for (chip = uves_chip_get_first(blue); 
00722          chip != UVES_CHIP_INVALID; 
00723          chip = uves_chip_get_next(chip))
00724         {
00725             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_IFLAT(chip);
00726         }
00727     }
00728     else if  (cpl_frameset_find(frames, UVES_TFLAT(true )) != NULL ||
00729           cpl_frameset_find(frames, UVES_TFLAT(false)) != NULL)
00730     {
00731         check( uves_load_raw_imagelist(frames, 
00732                        flames,
00733                        UVES_TFLAT(true), UVES_TFLAT(false),
00734                        CPL_TYPE_DOUBLE,
00735                        raw_images, raw_headers, product_header, 
00736                        &blue), "Error loading raw flat frames");
00737         for (chip = uves_chip_get_first(blue); 
00738          chip != UVES_CHIP_INVALID; 
00739          chip = uves_chip_get_next(chip))
00740         {
00741             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_TFLAT(chip);
00742         }
00743     }
00744     else if  (cpl_frameset_find(frames, UVES_SCREEN_FLAT(true )) != NULL ||
00745           cpl_frameset_find(frames, UVES_SCREEN_FLAT(false)) != NULL)
00746     {
00747         check( uves_load_raw_imagelist(frames, 
00748                        flames,
00749                        UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00750                        CPL_TYPE_DOUBLE,
00751                        raw_images, raw_headers, product_header, 
00752                        &blue), "Error loading raw flat frames");
00753         for (chip = uves_chip_get_first(blue); 
00754          chip != UVES_CHIP_INVALID; 
00755          chip = uves_chip_get_next(chip))
00756         {
00757             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SCREEN_FLAT(chip);
00758         }
00759     }
00760     else if  (cpl_frameset_find(frames, UVES_SFLAT(true )) != NULL ||
00761           cpl_frameset_find(frames, UVES_SFLAT(false)) != NULL)
00762     {
00763         check( uves_load_raw_imagelist(frames, 
00764                        flames,
00765                        UVES_SFLAT(true), UVES_SFLAT(false),
00766                        CPL_TYPE_DOUBLE,
00767                        raw_images, raw_headers, product_header, 
00768                        &blue), "Error loading raw flat frames");
00769         for (chip = uves_chip_get_first(blue); 
00770          chip != UVES_CHIP_INVALID; 
00771          chip = uves_chip_get_next(chip))
00772         {
00773             product_tag[uves_chip_get_index(chip)] = UVES_MASTER_SFLAT(chip);
00774         }
00775     }
00776     else 
00777     {
00778         assure(false, CPL_ERROR_DATA_NOT_FOUND,
00779            "Missing input flat frame: "
00780            "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s or %s expected",
00781            UVES_FLAT(true) , UVES_FLAT(false),
00782            UVES_DFLAT(true), UVES_DFLAT(false),
00783            UVES_IFLAT(true), UVES_IFLAT(false),
00784            UVES_TFLAT(true), UVES_TFLAT(false),
00785            UVES_SCREEN_FLAT(true), UVES_SCREEN_FLAT(false),
00786            UVES_SFLAT(true), UVES_SFLAT(false));
00787     }
00788 
00789     /* Loop over one or two chips */
00790     for (chip = uves_chip_get_first(blue); 
00791      chip != UVES_CHIP_INVALID; 
00792      chip = uves_chip_get_next(chip))
00793     {
00794 
00795       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00796     chip = uves_chip_get_next(chip);
00797       }
00798         
00799         raw_index = uves_chip_get_index(chip);
00800         
00801         uves_msg("Processing %s chip", uves_chip_tostring_upper(chip));
00802 
00803         /* Chip name of first input frame */
00804         check_nomsg( chip_name = uves_pfits_get_chipid(raw_headers[raw_index][0], chip));
00805 
00806         /* Load master bias, set pointer to NULL if not present */
00807         uves_free_image(&master_bias);
00808         uves_free_propertylist(&master_bias_header);
00809         if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00810         {
00811             uves_free_image(&master_bias);
00812             uves_free_propertylist(&master_bias_header);
00813             check( uves_load_mbias(frames,
00814                        chip_name,
00815                        &master_bias_filename, &master_bias,
00816                        &master_bias_header, chip), 
00817                "Error loading master bias");
00818             
00819             uves_msg_low("Using master bias in '%s'", master_bias_filename);
00820         }
00821         else
00822         {
00823             uves_msg_low("No master bias in SOF. Bias subtraction not done");
00824         }
00825     
00826         /* Load master dark, set pointer to NULL if not present */
00827         uves_free_image(&master_dark);
00828         uves_free_propertylist(&master_dark_header);
00829         if (cpl_frameset_find(frames, UVES_MASTER_DARK(chip))  != NULL ||
00830         cpl_frameset_find(frames, UVES_MASTER_PDARK(chip)) != NULL)
00831         {
00832             uves_free_image(&master_dark);
00833             uves_free_propertylist(&master_dark_header);
00834             check( uves_load_mdark(frames, chip_name,
00835                        &master_dark_filename, &master_dark,
00836                        &master_dark_header, chip), 
00837                "Error loading master dark");
00838             
00839             uves_msg_low("Using master dark in '%s'", master_dark_filename);
00840         }
00841         else
00842         {
00843             uves_msg_low("No master dark in SOF. Dark subtraction not done");
00844         }
00845     
00846         /* Load the order table for this chip */
00847         if (flames)
00848         /* FLAMES does not do background subtraction (here)
00849            and therefore does not need an ordertable */
00850         {
00851             if (cpl_frameset_find(frames, UVES_ORDER_TABLE(flames, chip)) != NULL)
00852             {
00853                 uves_msg_warning("Order table (%s) is not used in FLAMES reduction",
00854                          UVES_ORDER_TABLE(flames, chip));
00855             }
00856         }
00857         else
00858         {
00859             uves_free_table       (&ordertable);
00860             uves_free_propertylist(&ordertable_header);
00861             uves_polynomial_delete(&order_locations);
00862             uves_free_table       (&traces);
00863             
00864             check( uves_load_ordertable(frames,
00865                         flames,
00866                         chip_name,
00867                         &ordertable_filename, 
00868                                                 &ordertable,
00869                         &ordertable_header, 
00870                                                 NULL,
00871                                                 &order_locations,
00872                         &traces, NULL, NULL,
00873                                        NULL, NULL, /* fibre_pos,fibre_mask */
00874                         chip,
00875                         false),
00876                "Could not load order table");
00877             uves_msg("Using order table in '%s'", ordertable_filename);
00878         }
00879 
00880         /* Compute QC parameters and save */
00881         uves_msg("Computing QC parameters");
00882             uves_qclog_delete(&qclog[0]);
00883             qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00884 
00885             check(uves_mflat_qclog(raw_images[raw_index],
00886                    qclog[0]),"error computing qclog");
00887     
00888 
00889        
00890         /* Process chip */
00891         uves_free_image(&master_flat);
00892         uves_free_image(&background);
00893         check( master_flat = uves_mflat_process_chip(
00894                raw_images[raw_index], raw_headers[raw_index],
00895                product_header[raw_index],
00896                master_bias,
00897                master_dark, master_dark_header,
00898                ordertable, order_locations,
00899                flames,
00900                parameters,
00901                chip,
00902                recipe_id,
00903                DEBUG,
00904                &background),
00905            "Error processing chip");
00906 
00907          /* Finished. Save */ 
00908         uves_msg("Saving products");
00909     
00910         cpl_free(product_filename);
00911         check( product_filename = uves_masterflat_filename(chip), 
00912                "Error getting filename");
00913         strcpy(pro_filename,prefix);
00914         strcat(pro_filename,product_filename);
00915         check( uves_frameset_insert(
00916                    frames,
00917                    master_flat,
00918                    CPL_FRAME_GROUP_PRODUCT,
00919                    CPL_FRAME_TYPE_IMAGE,
00920                    CPL_FRAME_LEVEL_INTERMEDIATE,
00921                    pro_filename,
00922                    product_tag[raw_index],
00923                    raw_headers[raw_index][0],
00924                    product_header[raw_index],
00925                    NULL,
00926                    parameters,
00927                    recipe_id,
00928                    PACKAGE "/" PACKAGE_VERSION, qclog,
00929                    starttime, true, UVES_ALL_STATS),
00930                "Could not add master flat %s %s to frameset", 
00931                product_filename, product_tag[raw_index]);
00932         uves_msg("Master flat %s %s added to frameset", 
00933                  pro_filename, product_tag[raw_index]);
00934 
00935         /* Save background image */
00936         if (!flames)
00937         {
00938             cpl_free(product_filename);
00939             check( product_filename = uves_masterflat_bkg_filename(chip), 
00940                "Error getting filename");
00941             strcpy(pro_filename,prefix);
00942             strcat(pro_filename,product_filename);
00943             
00944             check( uves_frameset_insert(frames,
00945                         background,
00946                         CPL_FRAME_GROUP_PRODUCT,
00947                         CPL_FRAME_TYPE_IMAGE,
00948                         CPL_FRAME_LEVEL_INTERMEDIATE,
00949                         pro_filename,
00950                         UVES_BKG_FLAT(chip),
00951                         raw_headers[raw_index][0],
00952                         product_header[raw_index],
00953                         NULL,
00954                         parameters,
00955                         recipe_id,
00956                         PACKAGE "/" PACKAGE_VERSION, NULL,
00957                         starttime, false, 
00958                         CPL_STATS_MIN | CPL_STATS_MAX),
00959                "Could not add background image '%s' to frameset", 
00960                product_filename);
00961             uves_msg("Master flat background '%s' added to frameset", 
00962                  product_filename);
00963                 }
00964 
00965         /* Compute and save ratio MASTER_TFLAT / REF_TFLAT */
00966         if (strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0)
00967         {
00968 
00969             uves_free_image(&ref_flat);
00970             uves_free_propertylist(&ref_flat_header);
00971 
00972             check( uves_load_ref_flat(frames, chip_name, &ref_flat_filename, 
00973                           &ref_flat, &ref_flat_header, 
00974                           chip),
00975                "Error loading reference flat field");
00976             
00977             uves_msg("Using reference flat field in '%s'", ref_flat_filename);
00978 
00979             check( ratio = cpl_image_divide_create(master_flat, ref_flat),
00980                "Error computing ratio of master and reference flat");
00981 
00982             cpl_free(product_filename);
00983             check( product_filename = uves_flat_ratio_filename(chip), 
00984                "Error getting filename");
00985             
00986             check( uves_frameset_insert(frames,
00987                         ratio,
00988                         CPL_FRAME_GROUP_PRODUCT,
00989                         CPL_FRAME_TYPE_IMAGE,
00990                         CPL_FRAME_LEVEL_INTERMEDIATE,
00991                         product_filename,
00992                         UVES_RATIO_TFLAT(chip),
00993                         raw_headers[raw_index][0],
00994                         product_header[raw_index],
00995                         NULL,
00996                         parameters,
00997                         recipe_id,
00998                         PACKAGE "/" PACKAGE_VERSION,
00999                         qclog,
01000                         starttime, false, 
01001                         UVES_ALL_STATS),
01002                "Could not add ratio image '%s' to frameset", product_filename);
01003 
01004             uves_msg("Master flat ratio '%s' added to frameset", product_filename);
01005         }
01006 
01007 
01008       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
01009     chip = uves_chip_get_next(chip);
01010       }
01011 
01012     
01013     } /* For each chip */
01014     
01015   cleanup:
01016     /* Input */
01017     if (raw_images[0] != NULL)
01018     {
01019         int i;
01020         for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++) 
01021         {
01022             if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
01023             if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
01024         }
01025         cpl_free(raw_headers[0]); raw_headers[0] = NULL;
01026         cpl_free(raw_headers[1]); raw_headers[1] = NULL;
01027     }
01028 
01029     uves_free_imagelist(&raw_images[0]);
01030     uves_free_imagelist(&raw_images[1]);
01031 
01032     /* Master bias */
01033     uves_free_image(&master_bias);
01034     uves_free_propertylist(&master_bias_header);
01035 
01036     /* Master dark */
01037     uves_free_image(&master_dark);
01038     uves_free_propertylist(&master_dark_header);
01039 
01040     /* Order table */
01041     uves_free_table(&ordertable);
01042     uves_free_propertylist(&ordertable_header);
01043     uves_polynomial_delete(&order_locations);
01044     uves_free_table(&traces);
01045 
01046     /* Reference master flat */
01047     uves_free_image(&ref_flat);
01048     uves_free_propertylist(&ref_flat_header);
01049     
01050     /* Output */
01051     uves_qclog_delete(&qclog[0]);
01052     uves_free_image(&master_flat);
01053     uves_free_image(&background);
01054     uves_free_image(&ratio);
01055     uves_free_propertylist(&product_header[0]);
01056     uves_free_propertylist(&product_header[1]);
01057     cpl_free(product_filename);
01058     
01059     return;
01060 }
01061 
01062 
01063 /*----------------------------------------------------------------------------*/
01069 /*----------------------------------------------------------------------------*/
01070 
01071 static void
01072 uves_mflat_qclog(const cpl_imagelist* raw_images,
01073          cpl_table* qclog)
01074 {
01075   int nraw=0;
01076 
01077   check_nomsg(uves_qclog_add_string(qclog,
01078                         "QC TEST1 ID",
01079                         "Test-on-Master-Flat",
01080                         "Name of QC test",
01081                         "%s"));
01082   check_nomsg(nraw=cpl_imagelist_get_size(raw_images));
01083 
01084   check_nomsg(uves_qclog_add_int(qclog,
01085                         "PRO DATANCOM",
01086                         nraw,
01087                         "Number of frames combined",
01088                         "%d"));
01089  cleanup:
01090   return;
01091 }

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